erlang trace tutorial

Thomas Lindgren <>
Mon Oct 25 11:07:52 CEST 2004

--- Reto Kramer <> wrote:

> Can anyone point me to a hands-on tutorial out there
> that would 
> introduce me to Erlang's tracing facilities?

First have a look at 'dbg' and 'seq_trace' to see if
they are what you want.

Here are some pointers if you want to get started with
the lowlevel stuff:

See erlang:trace and erlang:trace_pattern for what
processes and functions to trace, and what sort of
information you can generate. I think the flags 'call'
and 'return_to' might be a good start.

Here is essentially what I do (on one node), being a
trace novice:

  fun() ->
     process_flag(priority, high),
     Set_trace_flags(),   %%  set whatever you need
     Unset_trace_flags()  %% restore

trace_loop() ->
      Msg -> io:format("~p~n", [Msg]), trace_loop()
   after ?some_time ->

That is, set the trace flags (erlang:trace,
erlang:trace_pattern), run your function, unset the
flags. Print all incoming trace messages.

NOTE: In practice, you will probably want to do
"tracing" and "execution" in different processes. See
the docs for how to set what process receives the
trace messages. 

(It is also easy to generalize this to "trace system
for N seconds" or whatnot.)

You can do more as well, but I haven't gotten that far
- use match specifications
- send trace messages to other destinations
(ports/sockets, ...)

There are some gotchas:

Tracing does not have flow control, so it is quite
easy to make the emulator run out of memory. Doubly so
if you also are recording the function arguments.
(Someone more familiar with tracing may get better
results than I :-)

Slowdown can be considerable when lots of big messages
are being generated as part of tracing.

Setting tracing process priority to 'high' helps, but
it can still be quite fiddly to avoid crashes, in
particular for long-running sequential stuff which
records lots of data. (Concurrent code ought to do
better, since the tracer will run every time a process
yields and so can clear out its message queue more

(I also think one might want a 'set_on_send' trace
directive, but as far as I can tell, there isn't one.)

Finally, see also Mats Cronquist's 'pan' package in
jungerl for more serious tracing than the above. Mats
also had a paper in the 2004 Erlang workshop.


Do you Yahoo!?
Declare Yourself - Register online to vote today!

More information about the erlang-questions mailing list