Debugging

A bug has two questions: what is the value here, and which call led to it. The first is answered by printing, the second by tracing, and both work in a running Erlang node without a debugger and without a restart. This lesson starts with the two print functions, io:format/2 and erlang:display/1, then traces a call in another process with erlang:trace/3, shows the mistake that makes a tracer silent, and ends with the shell bindings that reset a session.

io:format

io:format/2 prints with a format string; ~p prints any term in its usual syntax and ~n ends the line. The second argument is the list of values. The call answers ok, and the printed text arrives before that answer:

erl

1> io:format("name: ~p, age: ~p~n", ["Ada", 36]). name: "Ada", age: 36 ok

A reader from a language whose print returns the string is surprised twice: the value of the call is ok, not the text, and a value printed with ~p keeps its type. A string prints with quotes because it is a list of character codes; ~s would print the characters alone. The output lesson lists the other directives.

erlang:display

erlang:display/1 writes one term to the group leader with no format string and answers true. It skips the formatting machinery, so it is the call that still writes when a format call would tangle with a busy shell or with the formatter itself, and it cannot fail on a bad format string because it has none. It ends the term with a newline of its own, so the format that follows needs one `~n` fewer than a reader expects, and a second `~n` prints an empty line:

display.erl

-module(display). -export([main/0]). main() -> Result = erlang:display({point, 1, 2}), io:format("~nreturned ~p~n", [Result]).

erlc display.erl

erl -noshell -eval 'display:main(), halt().'

{point,1,2} returned true

Trace a call

erlang:trace_pattern/3 marks a function as interesting, and erlang:trace/3 attaches a tracer to a process. Every matching call then arrives at the tracer as a message tagged trace. The two setup calls answer the number of functions and processes they matched, which is 1 here. This module traces one call to lists:reverse/1 in a child process:

tracedemo.erl

-module(tracedemo). -export([main/0, run/0]). run() -> Parent = self(), Pid = spawn(fun() -> receive go -> lists:reverse([1, 2, 3]), Parent ! done end end), 1 = erlang:trace_pattern({lists, reverse, 1}, true, [global]), 1 = erlang:trace(Pid, true, [call, {tracer, Parent}]), Pid ! go, print_events(). print_events() -> receive {trace, _Pid, call, MFA} -> io:format("call ~p~n", [MFA]), print_events(); done -> 1 = erlang:trace_pattern({lists, reverse, 1}, false, [global]), ok after 1000 -> ok end. main() -> ok = run(), io:format("done~n").

erlc tracedemo.erl

erl -noshell -eval 'tracedemo:main(), halt().'

call {lists,reverse,[[1,2,3]]} done

The event is {trace, Pid, call, {Module, Function, Arguments}}. The pattern is global, so the trace covers the child even though the parent armed it; the parent is also the tracer, which is why it receives the message. The last clause turns the pattern off again, because a global pattern stays armed for every process on the node and would flood a long-running system with events.

The pattern that was never set

The mistake is to arm the process and forget the pattern. The tracer is attached, the child runs, and no event arrives, because erlang:trace/3 says which process to watch while erlang:trace_pattern/3 says which call to report. This module sets only the first:

no_pattern.erl

-module(no_pattern). -export([main/0]). main() -> Parent = self(), Pid = spawn(fun() -> receive go -> lists:reverse([1, 2, 3]), Parent ! done end end), 1 = erlang:trace(Pid, true, [call, {tracer, Parent}]), Pid ! go, wait(). wait() -> receive {trace, _Pid, call, MFA} -> io:format("call ~p~n", [MFA]); done -> io:format("no trace event~n") after 500 -> io:format("timeout~n") end.

erlc no_pattern.erl

erl -noshell -eval 'no_pattern:main(), halt().'

no trace event

The child finished and sent done before any trace message existed. A tracer that stays silent is almost always missing its pattern, or has it armed for another module or arity: the pair {Module, Function, Arity} must match exactly.

The dbg tracer

dbg is the friendly wrapper around the same two calls. In a shell, dbg:tracer() starts the tracer, dbg:p(Pid, c) watches calls of a process, and dbg:tpl(lists, reverse, x) sets the pattern. The calls are not shown as a transcript because their output carries a process id and a match count that differ on every node; the course types them at the prompt and reads the live values. dbg:stop() turns the tracer off when the hunt ends.

For a live system the same information arrives through dbg:trace_port/2, which writes the events to a file instead of the shell, so a long run can keep them and read them later. The observability part of the course covers the logging side of a running node.

Shell habits

The shell numbers every command. b() prints the current bindings, v(N) returns the value of command N without running it again, and f() forgets every binding. The last one is the fastest reset when a name is stuck from an earlier experiment:

erl

1> Name = "Ada". "Ada" 2> b(). Name = "Ada" ok 3> v(1). "Ada" 4> f(). ok

v(1) answers the value of the first command, the string, and does not repeat its side effects. After f() the name is unbound, so a fresh matching expression works again. A failure that disappears in a fresh shell was a stale binding, which is worth knowing before reading the code.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does io:format("~p~n", [Term]) answer?

    Answer

    ok

    io:format prints and answers ok; the printed term goes to the group leader, not to the caller.

  2. Which directive prints a term readably?

    Answer

    ~p

    ~p prints the term in the usual Erlang syntax, and ~n ends the line.

  3. What does erlang:display(Term) answer?

    Answer

    true

    display writes the term with no formatting and answers true, which is the easy fact to get wrong.

  4. Which call marks a function for tracing?

    Answer

    erlang:trace_pattern/3

    trace_pattern marks the function; trace then attaches a tracer to a process.

  5. What does this module print? -module(q1). -export([main/0]). main() -> Result = io:format("value ~p~n", [42]), io:format("~p~n", [Result]).

    Answer

    value 42 / ok

    The first call prints its line and answers ok, which the second call prints.

  6. What does this module print? -module(q2). -export([main/0]). main() -> Result = erlang:display({point, 1, 2}), io:format("~p~n", [Result]).

    Answer

    {point,1,2} / true

    display writes the term and answers true, which the format call then prints.

  7. What does this module print? -module(q3). -export([main/0]). main() -> io:format("~p~n", [is_list("Ada")]), io:format("~s~n", ["Ada"]).

    Answer

    true / Ada

    A string is a list of character codes, and ~s prints the characters without the quotes.

  8. Why does no_pattern:main/0 print no trace event?

    Answer

    Only erlang:trace/3 ran, without erlang:trace_pattern/3

    trace selects the process and trace_pattern selects the call; without the pattern no event is produced.

  9. What does this module print? -module(q4). -export([main/0]). main() -> Text = io_lib:format("value ~p", [42]), io:format("~s~n", [Text]).

    Answer

    value 42

    io_lib:format/2 returns the text as data, and the ~s directive prints it without quotes.

  10. What does f() do?

    Answer

    Forgets every shell binding

    f() clears the bindings, which is the fastest reset after a stale name.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Автор статьи: Arthur Isaev