erlang trace tutorial

mats cronqvist mats.cronqvist@REDACTED
Mon Oct 25 15:19:41 CEST 2004


Thomas Lindgren wrote:
> (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
> myself:
> - use match specifications
> - send trace messages to other destinations
> (ports/sockets, ...)

   thought i'd post what i use. background is that i wanted a trace  
facility that was less likely to choke the emulator that dbg is. i also  
had the most excellent constraint of not being allowed to use beam files  
or the compiler(*). Basically, i wanted something that wouldn't flood the  
shell with io, and that exited if things got difficult.
   solution was to spawn a process that ran a trace, collected messages for  
a while, but did not print anything until after the tracing finished. it  
will exit if it ever finds more than 100 messages in it's inbox.

   mats

(*) This is why the source code looks so wierd; one can take all the funs  
in rdbg/0, remove extra whitespace and get a oneliner that can be pasted  
into the shell... this has been dubbed the "ugliest line of code ever" by  
the Top Poster (ulf wiger).

NAME
   rdbg - restricted debugging utility

SYNOPSIS
   rdbg:rdbg(Time,Msgs,Proc,Trc).

DESCRIPTION
   An interface to the trace mechanism, safer (hopefully) than
   dbg. Disallows some of the more dangerous traces and exits if it
   gets flooded by trace messages. It runs in the background,
   collecting trace messages, until it reaches one of it's termination
   criteria (number of messages or maximum time). Prints the resulting
   trace messages like this;
[{Process, Timestamp, Data}...]
   Process is the registered name, if any, or else the initital call  
{M,F,A}.
   Timestamp is {Hour, Minute, Second, Microsecond}

   Proc can be; the atom 'all', a registered name, a pid, or the tuple  
{pid,I1,I2} (which specifies the pid <0.I1.I2>).
   Trc can be; the atom 'send', the atom 'receive', or a list of restricted  
trace patterns (RTP).
   An RTP is a tuple {Module, Function, RMSs, LocalGlobal}
    where LocalGlobal is one of the atoms 'local' or 'global'. 'global'  
means that only fully qualified function
   calls (i.e. M:F()) are traced. Defaults to 'global'.
   RMSs is a list of restricted match specs (RMS). It defaults to [].
   An RMS can be; the atom 'stack', the atom 'return', or a description of  
the argument list. E.g. the list;
[1,'_',x]
   would match is the arity of the called function is 3, the first argument  
is 1 and the third argument is 'x'.

EXAMPLES

rdbg:rdbg(3000,3,prfSys,'receive').
** rdbg, 1 msgs **
[{'receive',{12,18,22,9867},{timeout,#Ref<0.0.2.57760>,tick}}]

rdbg:rdbg(3000,3,all,{erlang,now}).
** rdbg, 2 msgs **
[{prfSys,{12,19,0,21791},{erlang,now,[]}},
  {prfSys,{12,19,2,42},{erlang,now,[]}}]

rdbg:rdbg(3000,3,all,{erlang,now,[return]}).
** rdbg, 2 msgs **
[{prfSys,{12,19,48,10871},{erlang,now,[]}},
  {return,{erlang,now,0},{1098,699588,10873}}]

rdbg:rdbg(3000,3,all,{erlang,now,[return,stack]}).
(erl@REDACTED)1> erlang:now().
{1098,699830,908663}
** rdbg, 2 msgs **
[{{erlang,apply,2},
   {12,23,50,908660},
   {{stack,['shell:eval_loop/2 ']},
    {erlang,now,[]}}},
  {return,{erlang,now,0},{1098,699830,908663}}]

rdbg:rdbg(3000,3,all,{ets,tab2list,[{inet_db}]}).
(erl@REDACTED)1> ets:tab2list(inet_db).
(erl@REDACTED)2> ets:tab2list(ac_tab).
** rdbg, 1 msgs **
[{{erlang,apply,2},{14,52,1,357496},{ets,tab2list,[inet_db]}}]
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: rdbg.erl
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20041025/de496898/attachment.ksh>


More information about the erlang-questions mailing list