[erlang-questions] turning up debugging on ports

Scott Lystig Fritchie <>
Wed Oct 24 19:38:01 CEST 2007


>>>>> "jm" == jm  <> writes:

jm> Is there an option to erl which will turn up the debugging so I
jm> can see what erlang is doing in relation to a port module I'm
jm> attempting to debug.

One way is to turn on the Erlang tracing facility on the Erlang side
of the world.  See the "dbg" module in the docs (Tool Applications,
then runtime_tools, then dbg).

A small set of functions, originally posted by Serge Aleynikov, were a
big help in starting to use dbg, which is a *very* low level tool.
After seeing what these functions do, the "oh, yeah" moments follow
quickly when you need to do something different.

-Scott

--- snip --- snip --- snip --- snip --- snip --- snip --- snip ---

%% Put this in your .erlang file:

%% Shell hacks from Serge
code:load_abs("/path/to/user_default").

--- snip --- snip --- snip --- snip --- snip --- snip --- snip ---

%% user_default.erl

-module(user_default).
-author('').

-export([help/0,dbgtc/1, dbgon/1, dbgon/2,
         dbgadd/1, dbgadd/2, dbgdel/1, dbgdel/2, dbgoff/0,
         l/0]).

-import(io, [format/1]).

help() ->
     shell_default:help(),
     format("** user extended commands **~n"),
     format("dbgtc(File)   -- use dbg:trace_client() to read data from File\n"),
     format("dbgon(M)      -- enable dbg tracer on all funs in module M\n"),
     format("dbgon(M,Fun)  -- enable dbg tracer for module M and function F\n"),
     format("dbgon(M,File) -- enable dbg tracer for module M and log to File\n"),
     format("dbgadd(M)     -- enable call tracer for module M\n"),
     format("dbgadd(M,F)   -- enable call tracer for function M:F\n"),
     format("dbgdel(M)     -- disable call tracer for module M\n"),
     format("dbgdel(M,F)   -- disable call tracer for function M:F\n"),
     format("dbgoff()      -- disable dbg tracer (calls dbg:stop/0)\n"),
     format("l()           -- load all changed modules\n"),
%     format("nl()          -- load all changed modules on all known nodes\n"),
     format("mm()          -- list modified modules\n"),
     true.

dbgtc(File) ->
    Fun = fun({trace,_,call,{M,F,A}}, _) ->
                 io:format("call: ~w:~w~w~n", [M,F,A]);
             ({trace,_,return_from,{M,F,A},R}, _) ->
                 io:format("retn: ~w:~w/~w -> ~w~n", [M,F,A,R]);
             (A,B) ->
                 io:format("~w: ~w~n", [A,B])
          end,
    dbg:trace_client(file, File, {Fun, []}).

dbgon(Module) ->
    case dbg:tracer() of
    {ok,_} ->
       dbg:p(all,call),
       dbg:tpl(Module, [{'_',[],[{return_trace}]}]),
       ok;
    Else ->
       Else
    end.

dbgon(Module, Fun) when is_atom(Fun) ->
    {ok,_} = dbg:tracer(),
    dbg:p(all,call),
    dbg:tpl(Module, Fun, [{'_',[],[{return_trace}]}]),
    ok;

dbgon(Module, File) when is_list(File) ->
    {ok,_} = dbg:tracer(file, dbg:trace_port(file, File)),
    dbg:p(all,call),
    dbg:tpl(Module, [{'_',[],[{return_trace}]}]),
    ok.

dbgadd(Module) ->
    dbg:tpl(Module, [{'_',[],[{return_trace}]}]),
    ok.

dbgadd(Module, Fun) ->
    dbg:tpl(Module, Fun, [{'_',[],[{return_trace}]}]),
    ok.

dbgdel(Module) ->
    dbg:ctpl(Module),
    ok.

dbgdel(Module, Fun) ->
    dbg:ctpl(Module, Fun),
    ok.

dbgoff() ->
    dbg:stop().

l() ->
    FiltZip = lists:filter(
	fun({_Mod, Path}) when is_list(Path) ->
		case string:str(Path, "/kernel-") +
		     string:str(Path, "/stdlib-") of
			0 -> true;
			_ -> false
		end;
	    (_) -> false
	end, code:all_loaded()),
    {Ms, _} = lists:unzip(FiltZip),
    lists:foldl(fun(M, Acc) ->
			case shell_default:l(M) of
				{error, _} -> Acc;
				_          -> [M|Acc]
			end
		end, [], Ms).



More information about the erlang-questions mailing list