How to show Erlang call stack?

前端 未结 4 815
耶瑟儿~
耶瑟儿~ 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:45

    Here is my code for doing this:

    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).
    

    Lists is a custom module in the system. Use your foo module instead.

提交回复
热议问题