inspecting the call stack from a program

Martin Bjorklund mbj@REDACTED
Wed Mar 19 20:28:19 CET 2003


Chris Pressey <cpressey@REDACTED> wrote:
> On Fri, 14 Mar 2003 14:45:49 -0000
> Chandrashekhar Mullaparthi <Chandrashekhar.Mullaparthi@REDACTED>
> wrote:
> 
> > Another way to look at the stack...but it is not as pretty as what you'd
> > see in a crash report.
> > 
> > (camelns@REDACTED)212> erlang:process_display(whereis(cns_server),
> > backtrace).
> > [...]
> 
> Sorry... I should have been more specific.  I meant - a way for a program
> to inspect the call stack.
> 
> I'm currently using this function:
> 
>   call_stack() ->
>     {'EXIT', {Error, CallStack}} = (catch 1 = 2),
>     tl(CallStack).

Here's a snippet from our user_default.erl, which I posted to the list
a couple of years (?) ago.  (our user_default also contains pi() for
port inspection, i/1 with a pid or registered name as argument etc).

Can be used like this from the shell:

Eshell V5.2.b1  (abort with ^G)
1> bt(self()).
program counter = 0x8168454 (unknown function)
cp = 0x8490954 (user_default:pinfo/2 + 172)

0x4009bc64 Return addr 0x849131C (user_default:bt/1 + 40)
y(0)     <0.23.0>
y(1)     backtrace

[... deleted]

----------------------------------------------------------

%% Doesn't do process_display, which means it can be used when
%% remotely connecting to a node.
bt(Pid) when pid(Pid) ->
    case pinfo(Pid, backtrace) of
	{backtrace, Bin} ->
	    io:format("~s\n", [binary_to_list(Bin)]);
	_ ->
	    undefined
    end;
bt(Name) when atom(Name) ->
    case whereis(Name) of
	undefined -> undefined;
	Pid -> bt(Pid)
    end.

bt(X,Y,Z) ->
    bt(c:pid(X,Y,Z)).

pinfo(Pid, Item) ->
    case is_alive() of
	true -> rpc:call(node(Pid), erlang, process_info, [Pid, Item]);
	false -> process_info(Pid, Item)
    end.



/martin



More information about the erlang-questions mailing list