How to show Erlang call stack?

前端 未结 4 806
耶瑟儿~
耶瑟儿~ 2021-02-14 18:06

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

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-14 18:26

    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.

提交回复
热议问题