03 Debugger
03 Debugger
1.1 Introduction
1.1.1 Scope
Debugger is a graphical user interface for the Erlang interpreter, which can be used for debugging and testing of Erlang
programs. For example, breakpoints can be set, code can be single-stepped and variable values can be displayed and
changed.
The Erlang interpreter can also be accessed through the interface module int(3).
Warning:
Debugger might at some point start tracing on the processes that execute the interpreted code. This means that a
conflict occurs if tracing by other means is started on any of these processes.
1.1.2 Prerequisites
It is assumed that the reader is familiar with the Erlang programming language.
Modules to be debugged must include debug information, for example, erlc +debug_info MODULE.erl.
1.2 Debugger
1.2.1 Getting Started
To use Debugger, the basic steps are as follows:
Step 1. Start Debugger by calling debugger:start().
The Monitor window is displayed with information about all debugged processes, interpreted modules, and selected
options. Initially there are normally no debugged processes. First, it must be specified which modules that are to be
debugged (also called interpreted). Proceed as follows:
Step 2. Select Module > Interpret... in the Monitor window.
The Interpret Modules window is displayed.
Step 3. Select the appropriate modules from the Interpret Dialog window.
Note:
Only modules compiled with option debug_info set can be interpreted. Non-interpretable modules are
displayed within parenthesis in the Interpret Modules window.
Step 4. In the Monitor window, select Module > the module to be interpreted > View.
The contents of the source file is displayed in the View Module window.
Step 5. Set the breakpoints, if any.
Step 6. Start the program to be debugged. This is done the normal way from the Erlang shell.
All processes executing code in interpreted modules are displayed in the Monitor window.
Step 7. To attach to one of these processes, double-click it, or select the process and then choose Process > Attach.
Attaching to a process opens an Attach Process window for this process.
Step 8. From the Attach Process window, you can control the process execution, inspect variable values, set
breakpoints, and so on.
Note:
When a process reaches a breakpoint, only that process is stopped. Other processes are not affected.
Breakpoints are created and deleted using the Break menu of either the Monitor window, View Module window, or
Attach Process window.
Executable Lines
To have an effect, a breakpoint must be set at an executable line, which is a line of code containing an executable
expression such as a matching or a function call. A blank line or a line containing a comment, function head, or pattern
in a case statement or receive statement is not executable.
In the following example, lines 2, 4, 6, 8, and 11 are executable lines:
1: is_loaded(Module,Compiled) ->
2: case get_file(Module,Compiled) of
3: {ok,File} ->
4: case code:which(Module) of
5: ?TAG ->
6: {loaded,File};
7: _ ->
8: unloaded
9: end;
10: false ->
11: false
12: end.
Line Breakpoints
A line breakpoint is created at a certain line in a module.
Right-click the Module entry to open a popup menu from which the appropriate module can be selected.
A line breakpoint can also be created (and deleted) by double-clicking the line when the module is displayed in the
View Module window or Attach Process window.
Conditional Breakpoints
A conditional breakpoint is created at a certain line in the module, but a process reaching the breakpoint stops only
if a specified condition is true.
The condition is specified by the user as a module name CModule and a function name CFunction. When a process
reaches the breakpoint, CModule:CFunction(Bindings) is evaluated. If and only if this function call returns
true, the process stops. If the function call returns false, the breakpoint is silently ignored.
Bindings is a list of variable bindings. To retrieve the value of Variable (given as an atom), use function
int:get_binding(Variable,Bindings). The function returns unbound or {value,Value}.
Right-click the Module entry to open a popup menu from which the appropriate module can be selected.
Example:
A conditional breakpoint calling c_test:c_break/1 is added at line 6 in module fact. Each time the breakpoint
is reached, the function is called. When N is equal to 3, the function returns true and the process stops.
Extract from fact.erl:
5. fac(0) -> 1;
6. fac(N) when N > 0, is_integer(N) -> N * fac(N-1).
Definition of c_test:c_break/1:
-module(c_test).
-export([c_break/1]).
c_break(Bindings) ->
case int:get_binding('N', Bindings) of
{value, 3} ->
true;
_ ->
false
end.
Function Breakpoints
A function breakpoint is a set of line breakpoints, one at the first line of each clause in the specified function.
To open a popup menu from which the appropriate module can be selected, right-click the Module entry.
To bring up all functions of the module in the listbox, click the OK button (or press the Return or Tab key) when
a module name has been specified,.
For details about the stack trace, see section Errors and Error Handling in the Erlang Reference Manual.
Debugger emulates the stack trace by keeping track of recently called interpreted functions. (The real stack trace cannot
be used, as it shows which functions of Debugger have been called, rather than which interpreted functions.)
This information can be used to traverse the chain of function calls, using the Up and Down buttons in the Attach
Process window.
By default, Debugger only saves information about recursive function calls, that is, function calls that have not yet
returned a value (option Stack On, No Tail).
Sometimes, however, it can be useful to save all calls, even tail-recursive calls. This is done with option Stack On,
Tail. Notice that this option consumes more memory and slows down execution of interpreted functions when there
are many tail-recursive calls.
To turn off the Debugger stack trace facility, select option Stack Off.
Note:
If an error occurs, the stack trace becomes empty in this case.
For information about how to change the stack trace option, see section Monitor Window.
The Auto Attach boxes, Stack Trace label, Back Trace Size label, and Strings box display some options set. For
details about these options, see section Options Menu.
Process Grid
Pid
The process identifier.
Initial Call
The first call to an interpreted function by this process. (Module:Function/Arity)
Name
The registered name, if any. If a registered name is not displayed, it can be that Debugger received information
about the process before the name was registered. Try selecting Edit > Refresh.
Status
The current status, one of the following:
idle
The interpreted function call has returned a value, and the process is no longer executing interpreted code.
running
The process is running.
waiting
The process is waiting in a receive statement.
break
The process is stopped at a breakpoint.
exit
The process has terminated.
no_conn
There is no connection to the node where the process is located.
Information
More information, if any. If the process is stopped at a breakpoint, the field contains information about the location
{Module,Line}. If the process has terminated, the field contains the exit reason.
File Menu
Load Settings...
Tries to load and restore Debugger settings from a file previously saved using Save Settings... (see below). Any
errors are silently ignored.
Notice that settings saved by Erlang/OTP R16B01 or later cannot be read by Erlang/OTP R16B or earlier.
Save Settings...
Saves Debugger settings to a file. The settings include the set of interpreted files, breakpoints, and the selected
options. The settings can be restored in a later Debugger session using Load Settings... (see above). Any errors
are silently ignored.
Exit
Stops Debugger.
Edit Menu
Refresh
Updates information about debugged processes. Information about all terminated processes are removed from
the window. All Attach Process windows for terminated processes are closed.
Kill All
Terminates all processes listed in the window using exit(Pid,kill).
Module Menu
Interpret...
Opens the Interpret Modules window, where new modules to be interpreted can be specified.
Delete All
Stops interpreting all modules. Processes executing in interpreted modules terminate.
For each interpreted module, a corresponding entry is added to the Module menu, with the following submenu:
Delete
Stops interpreting the selected module. Processes executing in this module terminate.
View
Opens a View Module window, displaying the contents of the selected module.
Process Menu
The following menu items apply to the currently selected process, provided it is stopped at a breakpoint (for details,
see section Attach Process window):
Step
Next
Continue
Finish
The following menu items apply to the currently selected process:
Attach
Attaches to the process and open an Attach Process window.
Kill
Terminates the process using exit(Pid,kill).
Break Menu
The items in this menu are used to create and delete breakpoints. For details, see section Breakpoints.
Line Break...
Sets a line breakpoint.
Conditional Break...
Sets a conditional breakpoint.
Function Break...
Sets a function breakpoint.
Enable All
Enables all breakpoints.
Disable All
Disables all breakpoints.
Delete All
Removes all breakpoints.
For each breakpoint, a corresponding entry is added to the Break menu, from which it is possible to disable, enable,
or delete the breakpoint, and to change its trigger action.
Options Menu
Trace Window
Sets the areas to be visible in an Attach Process window. Does not affect existing Attach Process windows.
Auto Attach
Sets the events a debugged process is to be attached to automatically. Affects existing debugged processes.
• First Call - The first time a process calls a function in an interpreted module.
• On Exit - At process termination.
• On Break - When a process reaches a breakpoint.
Stack Trace
Sets the stack trace option, see section Stack Trace. Does not affect existing debugged processes.
Windows Menu
Contains a menu item for each open Debugger window. Selecting one of the items raises the corresponding window.
Help Menu
Help
Shows the Debugger documentation. This function requires a web browser.
An example of how to compile code with debug information from the Erlang shell:
To browse the file hierarchy and interpret the appropriate modules, either select a module name and click Choose (or
press carriage return), or double-click the module name. Interpreted modules have the type erl src.
To interpret all displayed modules in the chosen directory, click All.
To close the window, click Done.
Note:
When Debugger is started in global mode (which is the default, see debugger:start/0), modules added (or deleted)
for interpretation are added (or deleted) on all known Erlang nodes.
File Menu
Close
Closes this window and detach from the process.
Edit Menu
Go to line...
Goes to a specified line number.
Search...
Searches for a specified string.
Process Menu
Step
Executes the current code line, stepping into any (interpreted) function calls.
Next
Executes the current code line and stop at the next line.
Continue
Continues the execution.
Finish
Continues the execution until the current function returns.
Skip
Skips the current code line and stop at the next line. If used on the last line in a function body, the function
returns skipped.
Time Out
Simulates a time-out when executing a receive...after statement.
Stop
Stops the execution of a running process, that is, make the process stop at a breakpoint. The command takes effect
(visibly) the next time the process receives a message.
Where
Verifies that the current location of the execution is visible in the code area.
Kill
Terminates the process using exit(Pid,kill).
Messages
Inspects the message queue of the process. The queue is displayed in the Evaluator area.
Back Trace
Displays the back trace of the process, a summary of the current function calls on the stack, in the Trace area.
Requires that the Trace area is visible and that the Stack Trace option is Stack On, Tail or Stack On, No Tail.
Up
Inspects the previous function call on the stack, showing the location and variable bindings.
Down
Inspects the next function call on the stack, showing the location and variable bindings.
Options Menu
Trace Window
Sets which areas are to be visible. Does not affect other Attach Process windows.
Stack Trace
Same as in the Monitor window, but only affects the debugged process the window is attached to.
Strings
Same as in the Monitor window, but only affects the debugged process the window is attached to.
Back Trace Size...
Sets how many call frames are to be fetched when inspecting the call stack. Does not affect other Attach Process
windows.
The source code is indented and each line is prefixed with its line number.
Clicking a line highlights it and selects it to be the target of the breakpoint functions available from the Break menu.
To set a line breakpoint on a line, double-click it. To remove the breakpoint, double-click the line with an existing
breakpoint.
Breakpoints are marked with a stop symbol.
1.2.8 Performance
Execution of interpreted code is naturally slower than for regularly compiled modules. Using Debugger also increases
the number of processes in the system, as for each debugged process another process (the meta process) is created.
It is also worth to keep in mind that programs with timers can behave differently when debugged. This is especially true
when stopping the execution of a process (for example, at a breakpoint). Time-outs can then occur in other processes
that continue execution as normal.
debugger:start(local | global)
2 Reference Manual
debugger
Erlang module
Exports
start()
start(File)
start(Mode)
start(Mode, File)
Types:
Mode = local | global
File = string()
Starts Debugger.
If a filename is specified as argument, Debugger tries to load its settings from this file. For details about settings, see
the User's Guide.
If local is specified as argument, Debugger interprets code only at the current node. If global is specified as
argument, Debugger interprets code at all known nodes, this is the default.
i
Erlang module
The i module provides short forms for some of the functions used by the graphical Debugger and some of the functions
in module int, the Erlang interpreter.
This module also provides facilities for displaying status information about interpreted processes and break points.
It is possible to attach to interpreted processes by giving the corresponding process identity only. By default, an
attachment window is displayed. Processes at other Erlang nodes can be attached manually or automatically.
By preference, these functions can be included in module shell_default. By default, they are included in that
module.
Exports
ii(AbsModules) -> ok
ii(AbsModule) -> {module, Module} | error
ini(AbsModules) -> ok
ini(AbsModule) -> {module, Module} | error
Types:
AbsModules = [AbsModule]
AbsModule = Module | File
Module = atom()
File = string()
Interprets the specified module(s). ii/1 interprets the module(s) only at the current node, see int:i/1. ini/1 interprets
the module(s) at all known nodes, see int:ni/1.
iq(AbsModule) -> ok
inq(AbsModule) -> ok
Types:
AbsModule = Module | File
Module = atom()
File = string()
Stops interpreting the specified module. iq/1 stops interpreting the module only at the current node. inq/1 stops
interpreting the module at all known nodes.
il() -> ok
Makes a printout of all interpreted modules. Modules are printed together with the full path name of the corresponding
source code file.
ip() -> ok
Prints the current status of all interpreted processes.
ic() -> ok
Clears information about processes executing interpreted code by removing all information about terminated processes.
ir() -> ok
Deletes all breakpoints.
ir(Module) -> ok
Types:
Module = atom()
Deletes all breakpoints in Module.
ipb() -> ok
Prints all existing breakpoints.
ipb(Module) -> ok
Types:
Module = atom()
Prints all existing breakpoints in Module.
help() -> ok
Prints help text.
See Also
int(3)
int
Erlang module
The Erlang interpreter provides mechanisms for breakpoints and stepwise execution of code. It is primarily intended
to be used by Debugger, see the User's Guide and debugger(3).
The following can be done from the shell:
• Specify the modules to be interpreted.
• Specify breakpoints.
• Monitor the current status of all processes executing code in interpreted modules, also processes at other Erlang
nodes.
By attaching to a process executing interpreted code, it is possible to examine variable bindings and order stepwise
execution. This is done by sending and receiving information to/from the process through a third process, called
the meta process. You can implement your own attached process. See int.erl for available functions and
dbg_wx_trace.erl for possible messages.
The interpreter depends on the Kernel, STDLIB, and GS applications. This means that modules belonging to any of
these applications are not allowed to be interpreted, as it could lead to a deadlock or emulator crash. This also applies
to modules belonging to the Debugger application.
Breakpoints
Breakpoints are specified on a line basis. When a process executing code in an interpreted module reaches a breakpoint,
it stops. This means that a breakpoint must be set at an executable line, that is, a code line containing an executable
expression.
A breakpoint has the following:
• A status, which is active or inactive. An inactive breakpoint is ignored.
• A trigger action. When a breakpoint is reached, the trigger action specifies if the breakpoint is to continue as
active (enable), or to become inactive (disable), or to be removed (delete).
• Optionally an associated condition. A condition is a tuple {Module,Name}. When the breakpoint is reached,
Module:Name(Bindings) is called. If it evaluates to true, execution stops. If it evaluates to false, the
breakpoint is ignored. Bindings contains the current variable bindings. To retrieve the value for a specified
variable, use get_binding.
By default, a breakpoint is active, has trigger action enable, and has no associated condition. For details about
breakpoints, see the User's Guide.
Exports
File = string()
Interprets the specified module(s). i/1 interprets the module only at the current node. ni/1 interprets the module
at all known nodes.
A module can be specified by its module name (atom) or filename.
If specified by its module name, the object code Module.beam is searched for in the current path. The source code
Module.erl is searched for first in the same directory as the object code, then in an src directory next to it.
If specified by its filename, the filename can include a path and the .erl extension can be omitted. The object code
Module.beam is searched for first in the same directory as the source code, then in an ebin directory next to it,
and then in the current path.
Note:
The interpreter requires both the source code and the object code. The object code must include debug
information, that is, only modules compiled with option debug_info set can be interpreted.
The functions returns {module,Module} if the module was interpreted, otherwise error is returned.
The argument can also be a list of modules or filenames, in which case the function tries to interpret each module as
specified earlier. The function then always returns ok, but prints some information to stdout if a module cannot
be interpreted.
n(AbsModule) -> ok
nn(AbsModule) -> ok
Types:
AbsModule = Module | File | [Module | File]
Module = atom()
File = string()
Stops interpreting the specified module. n/1 stops interpreting the module only at the current node. nn/1 stops
interpreting the module at all known nodes.
As for i/1 and ni/1, a module can be specified by its module name or filename.
no_break() -> ok
no_break(Module) -> ok
Deletes all breakpoints, or all breakpoints in Module.
Retrieves the binding of Var. This function is intended to be used by the conditional function of a breakpoint.
clear() -> ok
Clears information about processes executing interpreted code by removing all information about terminated processes.