The Logtalk distribution includes a command-line debugger tool implemented as a Logtalk application. It can be loaded by typing:

| ?- logtalk_load(debugger(loader)).

It can also be loaded automatically at startup time by using a settings file. This tool implements debugging features similar to those found on most Prolog systems. There are some differences, however, between the usual implementation of Prolog debuggers and the current implementation of the Logtalk debugger that you should be aware. First, unlike most Prolog debuggers, the Logtalk debugger is not a built-in feature but a regular Logtalk application using documented debugging hook predicates. This translates to a different, although similar, set of debugging features when compared with some of the more sophisticated Prolog debuggers. Second, debugging is only possible for entities compiled in debug mode. When compiling an entity in debug mode, Logtalk decorates clauses with source information to allow tracing of the goal execution. Third, the implementation of spy points allows the user to specify the execution context for entering the debugger. This feature is a consequence of the encapsulation of predicates inside objects.

Compiling source files in debug mode

Compilation of source files in debug mode is controlled by the debug compiler flag. The default value for this flag, usually off, is defined in the adapter files. Its default value may be changed globally at runtime by calling:

| ?- set_logtalk_flag(debug, on).

Implicitly, this goal also turns off the optimize flag. In alternative, if we want to compile only some source files in debug mode, we may instead write:

| ?- logtalk_load([file1, file2, ...], [debug(on)]).

The logtalk_make/1 built-in predicate can also be used to recompile all loaded files (that were compiled without using explicit values for the debug and optimize compiler flags in a logtalk_load/2 call or in a loader file file, if used) in debug mode:

| ?- logtalk_make(debug).

With most backend Prolog compilers, the {+d} top-level shortcut can also be used. After debugging, the files can be recompiled in normal or optimized mode using, respectively, the {+n} or {+o} top-level shortcuts.


The clean compiler flag should be turned on whenever the debug flag is turned on at runtime. This is necessary because debug code would not be generated for files previously compiled in normal or optimized mode if there are no changes to the source files.

After loading the debugger, we may check (or enumerate by backtracking), all loaded entities compiled in debug mode as follows:

| ?- debugger::debugging(Entity).

To compile only a specific entity in debug mode, use the set_logtalk_flag/2 directive inside the entity. To compile all entities in a source file in debug mode, use the set_logtalk_flag/2 directive at the beginning of the file.

Procedure box model

Logtalk uses a procedure box model similar to those found on most Prolog systems. The traditional Prolog procedure box model defines four ports (call, exit, redo, and fail) for describing control flow when calling a predicate:

predicate call
success of a predicate call
backtracking into a predicate
failure of a predicate call

Logtalk, as found on some recent Prolog systems, adds a port for dealing with exceptions thrown when calling a predicate:

predicate call throws an exception

In addition to the ports described above, Logtalk adds two more ports, fact and rule, which show the result of the unification of a goal with, respectively, a fact and a rule head:

unification success between a goal and a fact
unification success between a goal and a rule head

Following Prolog tradition, the user may define for which ports the debugger should pause for user interaction by specifying a list of leashed ports. Unleashed ports are just printed with no pause for user interaction. For example:

| ?- debugger::leash([call, exit, fail]).

Alternatively, the user may use an atom abbreviation for a pre-defined set of ports. For example:

| ?- debugger::leash(loose).

The abbreviations defined in Logtalk are similar to those defined on some Prolog compilers:

[fact, rule, call]
[fact, rule, call, redo]
[fact, rule, call, redo, fail, exception]
[fact, rule, call, exit, redo, fail, exception]

By default, the debugger pauses at every port for user interaction.

Activating the debugger

The debuggerp::trace/0 and debuggerp::debug/0 predicates implicitly select the debugger tool as the active debug handler. If you have additional debug handlers loaded (e.g. the ports_profiler tool), those would no longer be active (there can be only one active debug handler at any given time). The debuggerp::nodebug/0 predicate implicitly deselects the debugger tool as the active debug handler.

Defining spy points

Logtalk spy points can be defined by simply stating which predicates should be spied (as in most Prolog debuggers), by stating which predicate clauses to spy given their source file line numbers, or by specifying the execution context for activating a spy point. In the case of line number spy points (also known as breakpoints), the line number must correspond to the first line of an entity clause. To simplify the definition of line number spy points, these are specified using the entity identifier instead of the file name (as all entities share a single namespace, an entity can only be defined in a single file).

Defining line number and predicate spy points

Line number and predicate spy points are specified using the debugger spy/1 predicate. The argument can be a breakpoint (expressed as a Entity-Line pair), a predicate indicator (Name/Arity), a non-terminal indicator (Name//Arity), or a list of spy points. For example:

| ?- debugger::spy(person-42).

Spy points set.

| ?- debugger::spy(foo/2).

Spy points set.

| ?- debugger::spy([foo/4, bar//1, agent-99]).

Spy points set.

Note that setting a line number spy point will remove any existing log point for the same location.

Line numbers and predicate spy points can be removed by using the debugger nospy/1 predicate. The argument can be a spy point, a list of spy points, or a non-instantiated variable in which case all spy points will be removed. For example:

| ?- debugger::nospy(_).

All matching predicate spy points removed.

In breakpoints, the line number must for the first line of a clause that we want to spy. But note that only some Prolog backends provide accurate source file term line numbers. Check the debugger tool documentation for details.

Defining context spy points

A context spy point is a tuple describing a message execution context and a goal:

(Sender, This, Self, Goal)

The debugger is evoked whenever the spy point goal and the specified execution context subsumes the goal currently being executed and its execution context. The user may establish any number of context spy points as necessary. For example, in order to call the debugger whenever a predicate defined on an object named foo is called we may define the following spy point:

| ?- debugger::spy(_, foo, _, _).

Spy point set.

For example, we can spy all calls to a foo/2 predicate with a bar atom in the second argument by setting the condition:

| ?- debugger::spy(_, _, _, foo(_, bar)).

Spy point set.

The debugger nospy/4 predicate may be used to remove all matching spy points. For example, the call:

| ?- debugger::nospy(_, _, foo, _).

All matching context spy points removed.

will remove all context spy points where the value of self matches the atom foo.

Removing all spy points

We may remove all line number, predicate, and context spy points by using the debugger nospyall/0 predicate:

| ?- debugger::nospyall.

All line number spy points removed.
All predicate spy points removed.
All context spy points removed.

There’s also a reset/0 predicate that can be used to reset the debugger to its default settings.

Defining log points

Logtalk log points are similar to line number spy points and thus the line number must correspond to the first line of an entity clause. When the debugger reaches a log point, it prints the corresponding unification port data followed, optionally, by a log message and continues without halting execution for taking a port command. The log message must be a valid atom when quoted. The debugger prints a @ character at the beginning of the line for easy recognition of log points output. Log points are defined using the log/3 predicate. For example:

| ?- debugger::log(agent, 99, 'At the secret headquarter!').
     Log point added.

Predicates logging/3 and nolog/3 can be used to, respectively, query and remove log points. There’s also a nologall/0 predicate that removes all log points.

Note that setting a log point will remove any existing line number spy point for the same location.

Tracing program execution

Logtalk allows tracing of execution for all objects compiled in debug mode. To start the debugger in trace mode, write:

| ?- debugger::trace.


Next, type the query to be debugged. For examples, using the family example in the Logtalk distribution compiled for debugging:

| ?- addams::sister(Sister, Sibling).
     Call: (1) sister(_1082,_1104) ?
     Rule: (1) sister(_1082,_1104) ?
     Call: (2) ::female(_1082) ?
     Call: (3) female(_1082) ?
     Fact: (3) female(morticia) ?
    *Exit: (3) female(morticia) ?
    *Exit: (2) ::female(morticia) ?

While tracing, the debugger will pause for user input at each leashed port, printing an informative message. Each trace line starts with the port, followed by the goal invocation number, followed by the goal. The invocation numbers are unique and allows us to correlate the ports used for a goal. In the output above, you can see for example that the goal ::female(_1082) succeeds with the answer ::female(morticia). The debugger also provides determinism information by prefixing the exit port with a * character when a call succeeds with choice-points pending, thus indicating that there might be alternative solutions for the goal.

Note that, when tracing, spy points will be ignored. Before the port number, when a spy point is set for the current clause or goal, the debugger will print a # character for line number spy points, a + character for predicate spy points, and a * character for context spy points. For example:

| ?- debugger::spy(female/2).


| ?- addams::sister(Sister, Sibling).
     Call: (1) sister(_1078,_1100) ?
     Rule: (1) sister(_1078,_1100) ?
     Call: (2) ::female(_1078) ?
  +  Call: (3) female(_1078) ?

To stop tracing (but still allowing the debugger to stop at defined spy points), write:

| ?- debugger::notrace.


Debugging using spy points

Tracing a program execution may generate large amounts of debugging data. Debugging using spy points allows the user to concentrate in specific points of the code. To start a debugging session using spy points, write:

| ?- debugger::debug.


For example, assuming the spy point we set in the previous section on the female/1 predicate:

| ?- addams::sister(Sister, Sibling).
  +  Call: (3) female(_1078) ?

To stop the debugger, write:

| ?- debugger::nodebug.


Note that stopping the debugger does not remove any defined spy points.

Debugging commands

The debugger pauses at leashed ports when tracing or when finding a spy point for user interaction. The commands available are as follows:

c — creep

go on; you may use the spacebar, return, or enter keys in alternative

l — leap

continues execution until the next spy point is found

s — skip

skips debugging for the current goal; valid at call, redo, and unification ports

S - Skip

similar to skip but displaying all intermediate ports unleashed

q — quasi-skip

skips debugging until returning to the current goal or reaching a spy point; valid at call and redo ports

r — retry

retries the current goal but side-effects are not undone; valid at the fail port

j — jump

reads invocation number and continues execution until a port is reached for that number

z — zap

reads either a port name and continues execution until that port is reached or a negated port name and continues execution until a port other than the negated port is reached

i — ignore

ignores goal, assumes that it succeeded; valid at call and redo ports

f — fail

forces backtracking; may also be used to convert an exception into a failure

n — nodebug

turns off debugging

N — notrace

turns off tracing

@ — command; ! can be used in alternative

reads and executes a query

b — break

suspends execution and starts new interpreter; type end_of_file to terminate

a — abort

returns to top level interpreter

Q — quit

quits Logtalk

p — print

writes current goal using the print/1 predicate if available

d — display

writes current goal without using operator notation

w — write

writes current goal quoting atoms if necessary

$ — dollar

outputs the compiled form of the current goal (for low-level debugging)

x — context

prints execution context

. — file

prints file, entity, predicate, and line number information at an unification port

e — exception

prints exception term thrown by the current goal

E — raise exception

reads and throws an exception term

= — debugging

prints debugging information

< — write depth

sets the write term depth (set to 0 to reset)

* — add

adds a context spy point for the current goal

/ — remove

removes a context spy point for the current goal

+ — add

adds a predicate spy point for the current goal

- — remove

removes a predicate spy point for the current goal

# — add

adds a line number spy point for the current clause

| — remove

removes a line number spy point for the current clause

h — condensed help

prints list of command options

? — extended help

prints list of command options

Customizing term writing

Debugging complex applications often requires customizing term writing. The available options are limiting the writing depth of large compound terms and defining the traditional portray/1 to define how a term should be printed when using the p command at a leashed port.

Term write depth

The terms written by the debugger can be quite large depending on the application being debugged. As described in the previous section, the debugger accepts the < command to set the maximum write term depth for compound terms. This commmand requires that the used backend Prolog compiler supports the non-standard but common max_depth/1 option for the write_term/3 predicate. When the compound term being written is deeply nested, the sub-terms are only written up to the specified depth with the omitted sub-terms replaced usually by .... For example:

| ?- write_term([0,1,2,3,4,5,6,7,8,9], [max_depth(5)]).


The default maximum depth depends on the backend. To print compound terms without a depth limit, set it explicitly to zero if necessary.

Custom term writing

The implicit use of the traditional print/1 predicate (using the p command) and the portray/1 user-defined hook predicate requires backend Prolog compiler support for these predicates. See the documentation of the backend you intend to use for details. As an example, assuming the following portray/1 definition:

portray(e(V1,V2)) :-
    format('~q ---> ~q~n', [V1,V2]).

Calling the print/1 predicate with e.g. a e(x1,x7) compound term argument will output:

| ?- print(e(x1,x7)).

x1 ---> x7

Context-switching calls

Logtalk provides a control construct, (<<)/2, which allows the execution of a query within the context of an object. Common debugging uses include checking an object local predicates (e.g. predicates representing internal dynamic state) and sending a message from within an object. This control construct may also be used to write unit tests.

Consider the following toy example:

:- object(broken).

    :- public(a/1).

    a(A) :- b(A, B), c(B).
    b(1, 2). b(2, 4). b(3, 6).

:- end_object.

Something is wrong when we try the object public predicate, a/1:

| ?- broken::a(A).


For helping diagnosing the problem, instead of compiling the object in debug mode and doing a trace of the query to check the clauses for the non-public predicates, we can instead simply type:

| ?- broken << c(C).

C = 3

The (<<)/2 control construct works by switching the execution context to the object in the first argument and then compiling and executing the second argument within that context:

| ?- broken << (self(Self), sender(Sender), this(This)).

Self = broken
Sender = broken
This = broken


As exemplified above, the (<<)/2 control construct allows you to call an object local and private predicates. However, it is important to stress that we are not bypassing or defeating an object predicate scope directives. The calls take place within the context of the specified object, not within the context of the object making the (<<)/2 call. Thus, the (<<)/2 control construct implements a form of execution-context switching.

The availability of the (<<)/2 control construct is controlled by the context_switching_calls compiler flag (its default value is defined in the adapter files of the backend Prolog compilers).

Debugging messages

Calls to the logtalk::print_message/3 predicate where the message kind is either debug or debug(Group) are only printed, by default, when the debug flag is turned on. Moreover, these calls are suppressed by the compiler when the optimize flag is turned on. Note that actual printing of debug messages does not require compiling the code in debug mode, only turning on the debug flag.


To avoid having to define message_tokens//2 grammar rules for translating each and every debug message, Logtalk provides default tokenization for seven meta-messages that cover the most common cases:


By default, the message is printed as passed to the write/1 predicate followed by a newline.


By default, the message is printed as Key: Value followed by a newline. The key is printed as passed to the write/1 predicate while the value is printed as passed to the writeq/1 predicate.


By default, the message is printed as passed to the format/2 predicate.


By default, the list items are printed indented one per line. The items are preceded by a dash and can be @Message, Key-Value, or Format+Arguments messages. If that is not the case, the item is printed as passed to the writeq/1 predicate.


By default, the title is printed followed by a newline and the indented list items, one per line. The items are printed as in the List meta message.


By default, call user-defined printing Goal in the context of user. The use of a lambda expression allows passing the message stream and prefix. Printing the prefix is delegated to the goal.


By default, call user-defined printing Goal in the context of user. The use of a lambda expression allows passing the message stream.

Some simple examples of using these meta-messages:

| ?- logtalk::print_message(debug, core, @'Phase 1 completed').

| ?- logtalk::print_message(debug, core, [Stream]>>write(Stream,foo)).

| ?- set_logtalk_flag(debug, on).

| ?- logtalk::print_message(debug, core, [Stream]>>write(Stream,foo)).

| ?- logtalk::print_message(debug, core, @'Phase 1 completed').
>>> Phase 1 completed

| ?- logtalk::print_message(debug, core, answer-42).
>>> answer: 42

| ?- logtalk::print_message(debug, core, 'Position: <~d,~d>'+[42,23]).
>>> Position: <42,23>

| ?- logtalk::print_message(debug, core, [arthur,ford,marvin]).
>>> - arthur
>>> - ford
>>> - marvin

| ?- logtalk::print_message(debug, core, names::[arthur,ford,marvin]).
>>> names:
>>> - arthur
>>> - ford
>>> - marvin

The >>> prefix is the default message prefix for debug messages. It can be redefined using the logtalk::message_prefix_stream/4 hook predicate. For example:

:- multifile(logtalk::message_prefix_stream/4).
:- dynamic(logtalk::message_prefix_stream/4).

logtalk::message_prefix_stream(debug, core, '(dbg) ', user_error).

Selective printing of debug messages

By default, all debug messages are either printed or skipped, depending on the debug and optimize flags. When the code is not compiled in optimal mode, the debug_messages tool allows selectively enabling of debug messages per component and per debug group. For example, to enable all debug and debug(Group) messages for the parser component:

% upon loading the tool, all messages are disabled by default:
| ?- logtalk_load(debug_messages(loader)).

% enable both debug and debug(_) messages:
| ?- debug_messages::enable(parser).

To enable only debug(tokenization) messages for the parser component:

% first disable any and all enabled messages:
| ?- debug_messages::disable(parser).

% enable only debug(tokenization) messages:
| ?- debug_messages::enable(parser, tokenization).

See the tool documentation for more details.

Using the term-expansion mechanism for debugging

Debugging messages only output information by default. These messages can, however, be intercepted to perform other actions. An alternative is to use instead the term-expansion mechanism for conditional compilation of debugging goals. For example, the hook_objects library provides a print_goal_hook object that simplifies printing entity goals before or after calling them by simply prefixing them with an operator. See the library and hook object documentation for details. You can also define your own specialized hook objects for custom debugging tasks.

Ports profiling

The Logtalk distribution includes a ports_profiler tool based on the same procedure box model described above. This tool is specially useful for debugging performance issues (e.g. due to lack of determinism or unexpected backtracking). See the tool documentation for details.

Debug and trace events

The debugging API defines two multifile predicates, logtalk::trace_event/2 and logtalk::debug_handler/3 for handiling trace and debug events. It also provides a logtalk::debug_handler/1 multifile predicate that allows an object (or a category) to declare itself as a debug handler provider. The Logtalk debugger and ports_profiler tools are regular applications thar are implemented using this API, which can also be used to implement alternative or new debugging related tools. See the API documentation for details and the source code of the debugger and ports_profiler tools for usage examples.

To define a new debug handler provider, add (to an object or category) clauses for the debug_handler/1 and debug_handler/3 predicates. For example:

% declare my_debug_handler as a debug handler provider
:- multifile(logtalk::debug_handler/1).

% handle debug events
:- multifile(logtalk::debug_handler/3).
logtalk::debug_handler(my_debug_handler, Event, ExCtx) :-
    debug_handler(Event, ExCtx).

debug_handler(fact(Entity,Fact,Clause,File,Line), ExCtx) :-
debug_handler(rule(Entity,Head,Clause,File,Line), ExCtx) :-
debug_handler(top_goal(Goal, TGoal), ExCtx) :-
debug_handler(goal(Goal, TGoal), ExCtx) :-

Your debug handler provider should also either automatically call the logtalk::activate_debug_handler/1 and logtalk::deactivate_debug_handler/0 predicate or provide public predicates to simplify calling these predicates. For example:

:- public(start/0).
start :-

:- public(stop/0).
stop :-

If you only need to define a trace event handler, then simply define clauses for the logtalk::trace_event/2 multifile predicate:

:- multifile(logtalk::trace_event/2).
:- dynamic(logtalk::trace_event/2).

% the Logtalk runtime calls all defined logtalk::trace_event/2 hooks using
% a failure-driven loop; thus we don't have to worry about handling all
% events or failing after handling an event to give other hooks a chance
logtalk::trace_event(fact(Entity, Fact, N, _, _), _) :-
logtalk::trace_event(rule(Entity, Head, N, _, _), _) :-