如何显示Erlang调用栈?

mo49yndu  于 2022-12-08  发布在  Erlang
关注(0)|答案(4)|浏览(169)

I need to debug some module in foreign system. The module has public function foo() - how can I know place (module and function name) from which foo() given module was called? I mean stack of calls.
I cannot stop system, all work I can do by reload this module (but with some debug info).

-module(given).
-export(foo/0).

foo() ->
    %% here is my debug - and
    %% i need here(!) known about unknown_module:unknown_foo!
    ok.

---
-module(unknown_module).
..

unknown_foo() ->
    given:foo().  %% see above
rjzwgtxy

rjzwgtxy1#

这里有一个简单的技巧:

Trace = try throw(42) catch 42 -> erlang:get_stacktrace() end,
erlang:display(Trace)
cwdobuhd

cwdobuhd2#

This might work:

where_am_i() ->
    try throw(a)
    catch throw:a:Stacktrace ->
            Stacktrace
    end.

Except that it doesn't work for tail calls. For example, given these two functions:

foo() ->
    where_am_i().

bar() ->
    X = where_am_i(),
    {ok, X}.

I get these results:

4> foo:foo().
[{foo,where_am_i,0},
 {erl_eval,do_apply,5},
 {shell,exprs,6},
 {shell,eval_exprs,6},
 {shell,eval_loop,3}]
5> foo:bar().
{ok,[{foo,where_am_i,0},
     {foo,bar,0},
     {erl_eval,do_apply,5},
     {shell,exprs,6},
     {shell,eval_exprs,6},
     {shell,eval_loop,3}]}

That is, I can only see bar , since foo 's call frame has been left already when where_am_i is called.

4bbkushb

4bbkushb3#

io:format("~s~n", [element(2, process_info(self(), backtrace))]).

self()可以被任何其他pid代替(rpc:pinfo甚至可以和远程procs一起工作)。2如果你甚至不能修改源或光束,这会很有帮助。

g52tjvyc

g52tjvyc4#

下面是我的代码:

format_stack_entry(S) ->
    {Module,Fun,Arity,[{file,File},{line,Line}]}=S,
    io_lib:format("{~p,~p,~p,[{file,~p},{line,~p]}",[Module,Fun,Arity,File,Line]).
stacktop([Top|_]) ->
    Top.
ancestor(N) ->
    {_,Stacktrace}=erlang:process_info(self(),current_stacktrace),
    ancestor(N+1,Stacktrace).
ancestor(1,S) ->
    format_stack_entry(stacktop(S));
ancestor(N,[_|T]) ->
    ancestor(N-1,T).

info(Format)      -> io:format(lists:concat([ancestor(2),Format,"\r"])).
info(Format,Args) -> io:format(lists:concat([ancestor(2),Format,"\r"]),Args).

列表是系统中的自定义模块。请使用您的foo模块。

相关问题