erlangdbg

Erlang tracing (collect data from processes only in my modules)


While tracing my modules using dbg, I encountered with the problem how to collect messages such as spawn, exit, register, unregister, link, unlink, getting_linked, getting_unlinked, which are allowed by erlang:trace, but only for those processes which were spawned from my modules directly? As an examle I don't need to know which processes io module create, when i call io:format in some module function. Does anybody know how to solve this problem?


Solution

  • Short answer:

    one way is to look at call messages followed by spawn messages.

    Long answer:

    I'm not an expert on dbg. The reason is that I've been using an (imho much better, safer and even handier) alternative: pan , from https://gist.github.com/gebi/jungerl/tree/master/lib/pan

    The API is summarized in the html doc.

    With pan:start you can trace specifying a callback module that receives all the trace messages. Then your callback module can process them, e.g. keep track of processes in ETS or a state data that is passed into every call. The format of the trace messages is specified under pan:scan.

    For examples of callback modules, you may look at src/cb_*.erl.

    Now to your question: With pan you can trace on process handling and calls in your favourit module like this:

    pan:start({ip, CallbackModule}, Node, all, [procs,call], {Module}).
    

    where Module is the name of your module (in this case: sptest) Then the callback module (in this case: cb_write) can look at the spawn messages that follow a call message within the same process, e.g.:

    32      - {call,{<6761.194.0>,{'fun',{shell,<node>}}},{sptest,run,[[97,97,97]]},{1332,247999,200771}}
    33      - {spawn,{<6761.194.0>,{'fun',{shell,<node>}}},{{<6761.197.0>,{io,fwrite,2}},{io,fwrite,[[77,101,115,115,97,103,101,58,32,126,115,126,110],[[97,97,97]]]}},{1332,247999,200805}}
    

    As pan is also using the same tracing back end as dbg, the trace messages (and the information) can be collected using the Erlang trace BIF-s as well, but pan is much more secure.