erlangelixiractorgen-server

Elixir/Erlang - Trace when a message arrives in the mailbox


Fairly straight forward question, is it possible to trace messages arriving in (the mailbox of) a Process/GenServer? Note, this is different from tracing when a message is received (which would be once it leaves the mailbox and is handled). I've not found a way of doing this until now.


Solution

  • In erlang you have flags for it in dbg:p/2, s for sending and r for receiving:

    1> dbg:tracer().
    {ok,<0.82.0>}
    2> dbg:p(self(), r).
    (<0.80.0>) << {dbg,{ok,[{matched,nonode@nohost,1}]}}
    (<0.80.0>) << {io_reply,#Ref<0.2586582558.1779957764.183997>,319}
    {ok,[{matched,nonode@nohost,1}]}
    (<0.80.0>) << {io_reply,#Ref<0.2586582558.1779957764.184000>,
                            [{expand_fun,#Fun<group.0.82824323>},
                             {echo,true},
                             {binary,false},
                             {encoding,latin1}]}
    (<0.80.0>) << {io_reply,#Ref<0.2586582558.1779957764.184002>,ok}
    3> self() ! trace_me.
    (<0.80.0>) << {shell_cmd,<0.73.0>,
                             {eval,[{op,{1,8},
                                        '!',
                                        {call,{1,1},{atom,{1,1},self},[]},
                                        {atom,{1,10},trace_me}}]},
                             cmd}
    (<0.80.0>) << trace_me
    (<0.80.0>) << {io_reply,#Ref<0.2586582558.1779957764.184006>,319}
    trace_me
    (<0.80.0>) << {io_reply,#Ref<0.2586582558.1779957764.184008>,
                            [{expand_fun,#Fun<group.0.82824323>},
                             {echo,true},
                             {binary,false},
                             {encoding,latin1}]}
    (<0.80.0>) << {io_reply,#Ref<0.2586582558.1779957764.184011>,ok}