inspecting the call stack from a program (was: Re: Extending Functionality: an expedient and questionable approa ch)
Ulf Wiger
etxuwig@REDACTED
Wed Mar 19 23:51:26 CET 2003
On Wed, 19 Mar 2003, Chris Pressey wrote:
>I'm currently using this function:
>
> call_stack() ->
> {'EXIT', {Error, CallStack}} = (catch 1 = 2),
> tl(CallStack).
Not that it's necessarily better, but out of curiosity, I
tried to write a program that presented a call stack based
on process_info(Pid,backtrace) (actually, it does have the
nice property that it can be used on any process.)
Interestingly, the (badly written) regexp:first_match() in
the commented version seems to loop endlessly. Bug or
feature? The same pattern passed to egrep works fine.
I cannot guarantee that this version always gives a correct
answer. My "parsing" of the backtrace is sloppy, to say the
least.
/Uffe
all_stack() ->
{_, Stack} = process_info(self(),backtrace),
case format(binary_to_list(Stack)) of
[{?MODULE,call_stack,0}|Tail] ->
Tail;
Other ->
Other
end.
% format(Str) ->
% case regexp:first_match(
%
Str,"\\([a-z]+[a-z0-9_]*:[a-z]+[a-z0-9_]*+/[0-9]+") of
% {match,Start,Length} ->
% Fun = string:substr(Str,Start+1,Length-1),
% [M,F,A] = string:tokens(Fun,":/"),
%
[{list_to_atom(M),list_to_atom(F),list_to_integer(A)}|
% format(string:substr(Str,Start+Length))];
% nomatch ->
% []
% end.
format(Str) ->
Tokens = string:tokens(Str,"\n "),
funs(Tokens).
funs(["(" ++ Rest|Toks]) ->
case string:tokens(Rest,":/") of
[M,F,A] ->
[{list_to_atom(M),list_to_atom(F),list_to_integer(A)}|
funs(Toks)];
_ ->
funs(Toks)
end;
funs([_|Toks]) ->
funs(Toks);
funs([]) ->
[].
--
Ulf Wiger, Senior Specialist,
/ / / Architecture & Design of Carrier-Class Software
/ / / Strategic Product & System Management
/ / / Ericsson AB, Connectivity and Control Nodes
More information about the erlang-questions
mailing list