Prolog provides a debugging facility for examining the exact steps that are taken during an attempt to evaluate a goal. Once debugging is enabled the user is notified of four kinds of events:
- CALL - the event type generated when an attempt is first made to evaluate a goal.
- REDO - the event type generated when an attempt is made to re-evaluate a goal.
- EXIT - the event type generated when an attempt to evaluate a goal succeeds.
- FAIL - the event type generated when all attempts to evaluate a goal have failed.
The Prolog commands available to control debugging are: trace
, notrace
, spy
, nospy
, nodebug
and debugging
.
Examples
p2(1).
p2(2).
p2(3).
p(a).
p(b) :- p2(3).
p(c).
p(x, y).
Enable exhaustive tracing.
?- trace.
yes
Run queries - note debug information for both p and p2.
?- p(X).
[1] CALL p(X)
[1] EXIT p(a)
X = a
yes;
[1] REDO p(a)
[2] CALL p2(3)
[2] EXIT p2(3)
[1] EXIT p(b)
X = b
yes;
[1] REDO p(b)
[1] EXIT p(c)
X = c
yes
?- p(d).
[1] CALL p(d)
[1] FAIL p(d)
no
Disable exhaustive tracing.
?- notrace.
yes
Re-run same queries - note no debug information.
?- p(X).
X = a
yes;
X = b
yes;
X = c
yes
?- p(d).
no
Add spypoint
?- debugging.
yes
?- spy(p).
yes
?- debugging.
p/1
p/2
yes
Run query - note debug information for p only.
?- p(X).
[1] CALL p(X)
[1] EXIT p(a)
X = a
yes;
[1] REDO p(a)
[1] EXIT p(b)
X = b
yes;
[1] REDO p(b)
[1] EXIT p(c)
X = c
yes
?- p(d).
[1] CALL p(d)
[1] FAIL p(d)
no
Remove spypoint.
?- nospy(p).
yes
?- debugging.
yes
Add spypoints then remove all of them at once with 'nodebug'.
?- spy(p).
yes
?- spy(p2).
yes
?- debugging.
p/1
p/2
p2/1
yes
?- nodebug.
yes
?- debugging.
yes
If you provide "spy" with an atom it will apply spypoints to all predicates with that name. To be more specific you can provide a structure in order to only apply a spypoint for the predicate with both the specified name (functor) and number of arguments (arity).
?- spy('/'(p,1)).
yes
?- debugging.
p/1
yes
?- nodebug.
yes
Spy points can only be applied to atoms and structures
?- spy(1).
Expected an atom or a structure but got a INTEGER with value: 1