tracing with erlang made easy

Joel Reymont joelr1@REDACTED
Thu Oct 1 22:41:56 CEST 2009


-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)),
     t1(T);

t1([H|T]) ->
     dbg:tpl(H, dbg:fun2ms(fun(_) -> return_trace() end)),
     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






More information about the erlang-questions mailing list