Debugging

io:format

The io:format function prints with a format string. The tilde p prints any term readably, and the tilde n ends the line.

The second argument is the list of values to print, in order. The call itself answers ok, and the printed lines arrive before that answer.

erl

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

erlang:display

The erlang:display function writes one term without a format string. It prints to the group leader even when the shell is busy, and it answers true, not ok.

Use it inside a crashy loop where a format call might itself get tangled.

erl

1> erlang:display({point, 1, 2}).
{point,1,2}
true

Trace calls

The erlang:trace function reports what another process does, without editing its code. First arm a function with erlang:trace_pattern, then attach a tracer to the process with erlang:trace.

Every matching call arrives at the tracer as a tuple tagged trace. This module traces one call to lists:reverse/1 and prints the event; save it as tracedemo.erl.

-module(tracedemo). -export([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.

erlc tracedemo.erl
erl

1> tracedemo:run().
call {lists,reverse,[[1,2,3]]}
ok

Both setup calls answer 1, the number of functions or processes they matched. This shell session checks that answer and turns the pattern off again.

erl

1> erlang:trace_pattern({lists, reverse, 1}, true, [global]).
1
2> erlang:trace_pattern({lists, reverse, 1}, false, [global]).
1

The dbg tracer

The dbg module is the friendly wrapper around erlang:trace: it starts a tracer, chooses the processes, and sets the patterns for you.

The calls below are the usual three. They are shown as comments because the tracer echoes a process id and a process count that differ on every node.

% in the shell, with the child pid in Pid % dbg:tracer(). % dbg:p(Pid, c). % dbg:tpl(lists, reverse, x).

The tracer prints one line for every matching call, in the same shape as the trace event above. Remember to turn it off with dbg:stop/0 when the hunt is over.

Shell habits

The shell keeps a numbered history. The b function prints every binding, v(N) returns the value of command N, and f forgets all bindings, which is the fastest reset when a name gets stuck.

For deep problems, reproduce the failure in a fresh shell first: half of all bugs hide in stale bindings.

erl

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

Next: Encodings and Unicode

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. Why does erlang:display keep working when the shell is busy?

    Answer

    It writes straight to the group leader

    It bypasses the formatting machinery and writes to the group leader, so a busy shell does not block it.

  5. Which call arms a function for tracing?

    Answer

    erlang:trace_pattern/3

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

  6. What does erlang:trace(Pid, true, [call, {tracer, Parent}]) answer when it succeeds?

    Answer

    1

    The setup calls answer the number of matches, which is 1 here.

  7. What does a call event look like at the tracer?

    Answer

    {trace, Pid, call, MFA}

    A trace message is a tuple tagged trace, with the traced pid, the event kind, and the MFA.

  8. Why does the lesson show the dbg calls as comments?

    Answer

    The tracer echoes a pid and a count that differ on every node

    The output carries a process id and a match count, so a fixed transcript would not replay on another node.

  9. What does b() print?

    Answer

    Every current binding

    b() lists the bindings of the current shell, one per line, then answers ok.

  10. What does v(1) do?

    Answer

    Returns the value of command 1

    v(N) returns the value of the Nth command without running it again, so v(1) answers "Ada" here.

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.

Article author: Arthur Isaev