View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2019, CWI, Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(prolog_trace,
   36          [ trace/1,                            % :Spec
   37            trace/2,                            % :Spec, +Ports
   38            tracing/2                           % :Spec, -Ports
   39          ]).   40:- use_module(library(apply)).

Print access to predicates

This library prints accesses to specified predicates by wrapping the predicate.

See also
- library(debug) for adding conditional print statements to a program. */
   51:- meta_predicate
   52    trace(:),
   53    trace(:, +),
   54    tracing(:, -).   55
   56:- dynamic tracing_mask/2.   57:- volatile tracing_mask/2.
 trace(:Pred) is det
 trace(:Pred, +PortSpec) is det
Print passes through ports of specified predicates. Pred is a, possible partial, specification of a predicate as it is also used be spy/1 and similar predicates. Where a full predicate specification is of the shape Module:Name/Arity (or `//Arity for non-terminals), both the module and arity may be omitted in which case Pred refers to all matching predicates. PortSpec is either a single port (call, exit, fail or redo), preceded with + or - or a list of these. The predicate modifies the current trace specification and then installs a suitable wrapper for the predicate using wrap_predicate/4. For example:

` ?- trace(append). % append/2: [all] % append/3: [all] % append/1: [all] true.

?- append([a,b], [c], L). T Call: lists:append([a, b], [c], _10478) T Call: lists:append([b], [c], _11316) T Call: lists:append([], [c], _11894) T Exit: lists:append([], [c], [c]) T Exit: lists:append([b], [c], [b, c]) T Exit: lists:append([a, b], [c], [a, b, c]) L = [a, b, c].

?- trace(append, -all). % append/2: Not tracing % append/3: Not tracing % append/1: Not tracing

Compatibility
- This library replaces prior built-in functionality. Unlike the built-in version, ports are printed regardless of the debug flag. The built-in version printed the call-stack depth. That is currently not provided by this replacement.
   99trace(Pred) :-
  100    trace(Pred, +all).
  101
  102trace(Pred, Spec) :-
  103    '$find_predicate'(Pred, Preds),
  104    Preds \== [],
  105    maplist(set_trace(Spec), Preds).
  106
  107set_trace(Spec, Pred) :-
  108    (   tracing_mask(Pred, Spec0)
  109    ->  true
  110    ;   Spec0 = 0
  111    ),
  112    modify(Spec, Spec0, Spec1),
  113    retractall(tracing_mask(Pred, _)),
  114    asserta(tracing_mask(Pred, Spec1)),
  115    mask_ports(Spec1, Ports),
  116    pi_head(Pred, Head),
  117    (   Spec1 == 0
  118    ->  unwrap_predicate(Head, trace),
  119        print_message(informational, trace(Head, Ports))
  120    ;   wrapper(Spec1, Head, Wrapped, Wrapper),
  121        wrap_predicate(Head, trace, Wrapped, Wrapper),
  122        print_message(informational, trace(Head, Ports))
  123    ).
  124
  125modify(Var, _, _) :-
  126    var(Var),
  127    !,
  128    instantiation_error(Var).
  129modify([], Spec, Spec) :-
  130    !.
  131modify([H|T], Spec0, Spec) :-
  132    !,
  133    modify(H, Spec0, Spec1),
  134    modify(T, Spec1, Spec).
  135modify(+Port, Spec0, Spec) :-
  136    !,
  137    port_mask(Port, Mask),
  138    Spec is Spec0 \/ Mask.
  139modify(-Port, Spec0, Spec) :-
  140    !,
  141    port_mask(Port, Mask),
  142    Spec is Spec0 /\ \Mask.
  143modify(Port, Spec0, Spec) :-
  144    port_mask(Port, Mask),
  145    Spec is Spec0 \/ Mask.
  146
  147port_mask(all,  0x0f).
  148port_mask(call, 0x01).
  149port_mask(exit, 0x02).
  150port_mask(redo, 0x04).
  151port_mask(fail, 0x08).
  152
  153mask_ports(0, []) :-
  154    !.
  155mask_ports(Pattern, [H|T]) :-
  156    is_masked(Pattern, H, Pattern1),
  157    mask_ports(Pattern1, T).
  158
  159pi_head(M:PI, M:Head) :-
  160    !,
  161    pi_head(PI, Head).
  162pi_head(Name/Arity, Head) :-
  163    functor(Head, Name, Arity).
  164pi_head(Name//Arity0, Head) :-
  165    Arity is Arity0+1,
  166    functor(Head, Name, Arity).
  167
  168wrapper(0, _, Wrapped, Wrapped) :-
  169    !.
  170wrapper(Pattern, Head, Wrapped, Call) :-
  171    is_masked(Pattern, call, Pattern1),
  172    !,
  173    wrapper(Pattern1, Head, Wrapped, Call0),
  174    Call = (   print_message(debug, frame(Head, trace(call))),
  175               Call0
  176           ).
  177wrapper(Pattern, Head, Wrapped, Call) :-
  178    is_masked(Pattern, exit, Pattern1),
  179    !,
  180    wrapper(Pattern1, Head, Wrapped, Call0),
  181    Call = (   Call0,
  182               print_message(debug, frame(Head, trace(exit)))
  183           ).
  184wrapper(Pattern, Head, Wrapped, Call) :-
  185    is_masked(Pattern, redo, Pattern1),
  186    !,
  187    wrapper(Pattern1, Head, Wrapped, Call0),
  188    Call = (   call_cleanup(Call0, Det = true),
  189               (   Det == true
  190               ->  true
  191               ;   true
  192               ;   print_message(debug, frame(Head, trace(redo))),
  193                   fail
  194               )
  195           ).
  196wrapper(Pattern, Head, Wrapped, Call) :-
  197    is_masked(Pattern, fail, Pattern1),
  198    !,
  199    wrapper(Pattern1, Head, Wrapped, Call0),
  200    Call = call((   call_cleanup(Call0, Det = true),
  201                    (   Det == true
  202                    ->  !
  203                    ;   true
  204                    )
  205                ;   print_message(debug, frame(Head, trace(fail))),
  206                    fail
  207                )).
  208
  209is_masked(Pattern0, Port, Pattern) :-
  210    port_mask(Port, Mask),
  211    Pattern0 /\ Mask =:= Mask,
  212    !,
  213    Pattern is Pattern0 /\ \Mask.
 tracing(:Spec, -Ports)
True if Spec is traced using Ports
  219tracing(Spec, Ports) :-
  220    tracing_mask(Spec, Mask),
  221    mask_ports(Mask, Ports)