SWI-Prolog has a standard 4-port tracer [#!Clocksin:81!#] with an
optional fifth port. This fifth port, called unify allows the
user to inspect the result after unification of the head. The ports are
called call, exit, redo, fail and unify.
The tracer is started by the trace/0 command, when a spy point is
reached and the system is in debugging mode (see spy/1 and debug/1) or
when an error is detected at run time. Note that in the interactive
toplevel goal trace/0 means ``trace the next query''. The tracer
shows the port, displaying the port name, the current depth of the
recursion and the goal. The goal is printed using the Prolog predicate
print/1 (default), write/1 or display/1. An example using all five
ports is shown in figure
.
Figure:
Example trace
![\begin{figure}\begin{boxed}
\begin{code}
Yes
2 ?- visible(+all), leash(-exit).
\...
...( 4) min(3, 2, 2)
Exit: ( 3) min([3, 2], 2)
\end{code}\end{boxed}
\end{figure}](/file/21543/10-Tools.zip/plbin.zip/pl/man/intro.doc/img2.png) |
On leashed ports (set with the predicate leash/1, default are call, exit, redo and fail) the user is prompted for
an action. All actions are single character commands which are executed
WITHOUT waiting for a return (Unix `cbreak' mode), unless the
command line option -tty
is active. Tracer options:
- + Spy (default: all)
-
Set a spy point (see spy/1) on the current predicate.
- - No spy (default: all)
-
Remove the spy point (see nospy/1) from the current predicate.
- / Find (default: all)
-
Search for a port. After the `/', the user can enter a line
to specify the port to search for. This line consists of a set of
letters indicating the port type, followed by an optional term,
that should unify with the goal run by the port. If no term is
specified it is taken as a variable, searching for any port of the
specified type. If an atom is given, any goal whose functor has a
name equal to that atom matches. Examples:
/f |
Search for any fail port |
/fe solve |
Search for a fail or exit port of any goal with
name solve |
/c solve(a, _) |
Search for a call to solve/2 whose first argument
is a variable or the atom a |
/a member(_, _) |
Search for any port on member/2. This is equivalent
to setting a spy point on member/2. |
- . Repeat find (default: all)
-
Repeat the last find command (see `/')
- A Alternatives (default: all)
-
Show all goals that have alternatives.
- C Context (default: all)
-
Toggle `Show Context'. If on the context module of the goal is
displayed between square brackets (see section
).
Default is off.
- L Listing (default: all)
-
List the current predicate with listing/1.
- a Abort (default: all)
-
Abort Prolog execution (see abort/0).
- b Break (default: all)
-
Enter a Prolog break environment (see break/0).
- c Creep (default: all)
-
Continue execution, stop at next port. (Also return, space).
- d Display (default: all)
-
Write goals using the Prolog predicate display/1.
- e Exit (default: all)
-
Terminate Prolog (see halt/0).
- f Fail (default: call, redo, exit)
-
Force failure of the current goal
- g Goals (default: all)
-
Show the list of parent goals (the execution stack). Note that due to tail
recursion optimization a number of parent goals might not exist any more.
- h Help (default: all)
-
Show available options (also `?').
- i Ignore (default: call, redo, fail)
-
Ignore the current goal, pretending it succeeded.
- l Leap (default: all)
-
Continue execution, stop at next spy point.
- n No debug (default: all)
-
Continue execution in `no debug' mode.
- p Print (default: all)
-
Write goals using the Prolog predicate print/1 (default).
- r Retry (default: redo, exit, fail)
-
Undo all actions (except for database and i/o actions) back to the call
port of the current goal and resume execution at the call port.
- s Skip (default: call, redo)
-
Continue execution, stop at the next port of this goal (thus skipping
all calls to children of this goal).
- u Up (default: all)
-
Continue execution, stop at the next port of the parent goal (thus
skipping this goal and all calls to children of this goal). This option
is useful to stop tracing a failure driven loop.
- w Write (default: all)
-
Write goals using the Prolog predicate write/1.
The ideal 4 port model as described in many Prolog books
[#!Clocksin:81!#] is not visible in many Prolog implementations because
code optimisation removes part of the choice- and exit points.
Backtrack points are not shown if either the goal succeeded
deterministically or its alternatives were removed using the cut. When
running in debug mode (debug/0) choice points are only destroyed when
removed by the cut. In debug mode tail recursion optimisation is
switched off.