Skip to content
Jan Wielemaker edited this page Oct 24, 2025 · 7 revisions

The SWI-Prolog graphical debugger

The SWI-Prolog debugger
The debugger showing that in_slot/6 succeeded with a choice point


The graphical debugger can be started using gtrace/0, which otherwise acts the same as trace/0 that invokes the command line debugger. E.g.

?- gtrace, my_goal.

The debugger shows a window with three sub windows:

  • An instance of PceEmacs showing the source location
  • The Bindings window shows the variable bindings for the current clause. Using a context menu, individual bindings may be copied or examined in a dedicated window. See below.
  • The Call stack window shows the call stack as well as the open choice points. The frames may be clicked, causing the source view and bindings view to show the execution context.

The debugger may be controlled using the buttons shown below the menu as well as using the keyboard. Most of the key bindings are the same as for the command line debugger.

Hierarchical refinement

As Prolog allows for retrying goals, the typical way to debug a program is to enter a clause and use __S__kip to skip the goals in the body one-by-one. If a goal behaves unexpectedly, use __R__etry to go back to the start, __C__reep to get inside the clause(s) of the misbehaving goal and use the same procedure to find the culprit.

When the culprit it found, the clause may be edited in two ways: you may select the edit mode and edit in-place or use the Emacs command Ctrl-X 52 to open a new PceEmacs editor at the current location. Edit the clause and use Ctrl-C Ctrl-M to recompile the modified sources. Now use __R__etry and verify the correctness of the modified predicate.

Note this scenario only works if the predicate to debug has no side effects.

Trapping the debugger

Besides using ?- gtrace, my_goal., the debugger may be trapped at specific locations in several ways

  • The predicate gspy/1 sets a spy point on a predicate. This implies that any call to this predicate traps the debugger. Spy points are only honoured if the system runs in debug mode (see debug/0).
  • SWI-Prolog provides break points. These may be set from PceEmacs using Ctrl-C b or the Prolog/Break at menu. The debugger is trapped when execution reaches the indicated location in the source. Break points may be selected and the Prolog/Set breakpoint condition menu can be used to set additional constraints on the break point.
  • gtrap/1 can be used to make the debugger stop on certain exceptions. There is also a GUI front end to define the debugger's behaviour on exceptions that is accessible from Prolog/Edit exceptions in PceEmacs and Edit/Exceptions from the GUI debugger.
  • Finally, you may edit the source and add a call to gtrace/0 anywhere you want. You can also use (Cond -> gtrace ; true) to trap the debugger if Cond succeeds.

Showing details for bindings

The top-left sub-window labelled Bindings shows the the variable bindings for the current clause. Using a context menu, individual bindings may be copied or examined in a dedicated window. This window is shown below. If uses print_term/2 to pretty print the term.

image
Show details on a binding


By default, selecting a new binding updates this window. Using the top-left pin, the window may be pinned and a new binding create a new window, allowing you to compare terms. The top menus allow selecting various write options

Clone this wiki locally