When you do not understand how a certain query is answered by prolog or you when you are debugging a program, you might want to ask prolog to print an execution trace. Below we illustrate this using the program for computing fibonacci numbers from session 2.

First load fibonacci.pl. This contains the program for computing fibonacci numbers (to do so, use 'consult' as explained in session 1).

Trace

To trace all calls to the fib/2 predicate, type trace(fib).

?- trace(fib).
%         fib/2: [call, redo, exit, fail]

Yes
[debug] ?- fib(5,F).
 T Call: (7) fib(5, _G416)
 T Call: (8) fib(4, _L188)
 T Call: (9) fib(3, _L209)
 T Call: (10) fib(2, _L230)
 T Exit: (10) fib(2, 1)
 T Call: (10) fib(1, _L231)
 T Exit: (10) fib(1, 1)
 T Exit: (9) fib(3, 2)
 T Call: (9) fib(2, _L210)
 T Exit: (9) fib(2, 1)
 T Exit: (8) fib(4, 3)
 T Call: (8) fib(3, _L189)
 T Call: (9) fib(2, _L302)
 T Exit: (9) fib(2, 1)
 T Call: (9) fib(1, _L303)
 T Exit: (9) fib(1, 1)
 T Exit: (8) fib(3, 2)
 T Exit: (7) fib(5, 5)

F = 5 

Yes
To stop the tracing, type nodebug:
[debug]  ?- nodebug.

Yes
You will notice that you do not get the trace anymore:
?- fib(5,F).

F = 5 

Yes

Graphical Trace

You can also use the graphical version of the tracer. An extra window will then pop up showing the trace. Depending on the version of SWI you are using, the text-trace appears or does not appear together with the GUI trace (below we show what appears on your screen when you do also get the text-trace).
?- guitracer.
% The graphical front-end will be used for subsequent tracing

Yes
?- trace.

Yes
[trace]  ?- fib(5,F).
 T Call: (6) fib(5, _G278)
 T Call: (7) fib(4, _G361)
 T Call: (8) fib(3, _G367)
 T Call: (9) fib(2, _G373)
 T Exit: (9) fib(2, 1)
 T Call: (9) fib(1, _G373)
 T Exit: (9) fib(1, 1)
 T Exit: (8) fib(3, 2)
 T Call: (8) fib(2, _G376)
 T Exit: (8) fib(2, 1)
 T Exit: (7) fib(4, 3)
 T Call: (7) fib(3, _G379)
 T Call: (8) fib(2, _G385)
 T Exit: (8) fib(2, 1)
 T Call: (8) fib(1, _G385)
 T Exit: (8) fib(1, 1)
 T Exit: (7) fib(3, 2)
 T Exit: (6) fib(5, 5)

F = 5 ;
 T Redo: (8) fib(1, _G385)
 T Fail: (8) fib(1, _G385)
 T Redo: (8) fib(2, _G385)
 T Fail: (8) fib(2, _G385)
 T Fail: (7) fib(3, _G379)
 T Redo: (8) fib(2, _G376)
 T Fail: (8) fib(2, _G376)
 T Redo: (9) fib(1, _G373)
 T Fail: (9) fib(1, _G373)
 T Redo: (9) fib(2, _G373)
 T Fail: (9) fib(2, _G373)
 T Fail: (8) fib(3, _G367)
 T Fail: (7) fib(4, _G361)
 T Fail: (6) fib(5, _G278)

No
To stop the tracing, again type nodebug:
[debug]  ?- nodebug.

Yes
Note that the next time you start tracing again, the graphical tracer will again be used (because you have never said not to use the graphical tracer anymore).
If you do not want the graphical tracer to be used anymore, type noguitracer.
?- noguitracer.

% Subsequent tracing uses the commandline tracer

Yes