Debugging
| io:format | |
| erlang:display | |
| The dbg tracer | |
| Shell habits |
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.
1> io:format("name: ~p, age: ~p~n", ["Ada", 36]).
name: "Ada", age: 36
ok
erlang:display
The erlang:display function writes one term and answers the atom ok. It prints to the group leader even when the shell is busy.
Use it inside a crashy loop where io:format might itself get tangled.
1> erlang:display({point, 1, 2}).
{point,1,2}
ok
The dbg tracer
The dbg module traces calls without editing code. Start the tracer, set a pattern, and watch matching calls arrive.
This example traces every call to a module named mymod.
1> dbg:tracer().
ok,<0.91.0>
2> dbg:p(all, c).
ok,[{matched,nonode@nohost,_}]}
3> dbg:tpl(mymod, x, [return_trace]).
Shell habits
The shell keeps a numbered history. The b function prints every binding, and f forgets them all, 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.
1> b().
Name = "Ada"
2> f().
ok
Next: Encodings and Unicode
Article author: Arthur Isaev