[erlang-questions] tracing with erlang made easy

Raimo Niskanen <>
Fri Oct 2 09:22:28 CEST 2009


I just want to point out a simplification possible
from R13B; the built in match spec alias 'x'
for the match spec [{'_',[],[{exception_trace}]}],
now Joel used {return_trace} but I would recommend
{exception_trace}.

This makes manual tracing from the shell something like:
    %% Start tracing
    dbg:tracer().
    dbg:p(all, c). % or maybe (self(), c)
    dbg:tpl(Mod, Fun, x).

    %% Stop tracing
    dbg:stop_clear().

See below...

On Thu, Oct 01, 2009 at 09:41:56PM +0100, Joel Reymont wrote:
> -module(t).
> 
> -compile([export_all]).
> 
> -include_lib("stdlib/include/ms_transform.hrl").
> 
> %%% Stop all tracing
> 
> ut() ->
>     dbg:ctp().
> 
> %%% Trace a module
> 
> t(Mod)
>   when is_atom(Mod) ->
>     t([Mod]);
> 
> %%% Trace Mod:Fun (any arity)
> 
> t({Mod, Fun})
>   when is_atom(Mod),
>        is_atom(Fun) ->
>     t([{Mod, Fun}]);
> 
> %%% Use a combination of Mod and {Mod, Fun} in a list
> 
> t(What) ->
>     dbg:tracer(),
>     dbg:p(all, [call]),
>     t1(What).
> 
> t1([]) ->
>     ok;
> 
> t1([{M, F}|T]) ->
>     dbg:tpl(M, F, dbg:fun2ms(fun(_) -> return_trace() end)),

      dbg:tpl(M, F, x),

>     t1(T);
> 
> t1([H|T]) ->
>     dbg:tpl(H, dbg:fun2ms(fun(_) -> return_trace() end)),

      %% If you can not remember the 'x', maybe this:
      dbg:tpl(M, F, exception_trace),

>     t1(T).
> 
> %%% What processes are running these modules?
> 
> mods2procs(Mods) ->
>     [P || M <- Mods, P <- processes(), erlang:check_process_code(P,  
> M)].
> 
> %%% What processes are running a given module
> %%% that is an implementation of gen_fsm, gen_server, etc.
> 
> p(Mod) ->
>     p(Mod, processes(), []).
> 
> p(_, [], Acc) ->
>     Acc;
> 
> p(Mod, [H|T], Acc) ->
>     {_, L} = process_info(H, dictionary),
>     case lists:keyfind('$initial_call', 1, L) of
>         {'$initial_call', {Mod, init, 1}} ->
>             p(Mod, T, [H|Acc]);
>         _ ->
>             p(Mod, T, Acc)
>     end.
> 
> %%% Remote process info
> 
> pi(P)
>   when is_pid(P) ->
>     rpc:call(node(P), erlang, process_info, [P]).
> 
> ---
> fastest mac firefox!
> http://wagerlabs.com
> 
> 
> 
> 
> 
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list