tags:

views:

107

answers:

3
+3  Q: 

erlang call stack

i need to debug some module in foreign system, 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.

PS: I cannot stop system, all work I can do by reload this module (but with som 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
+3  A: 

Here's a simple trick:

Trace = try throw(42) catch 42 -> erlang:get_stacktrace() end,
erlang:display(Trace)
RichardC
+1  A: 

This might work:

where_am_i() ->
    try throw(a)
    catch throw:a ->
            erlang:get_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.

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

self() can be replaced by any other pid (rpc:pinfo should even work with remote procs). This helps if you cannot even modify the source or beam.