[erlang-questions] How to get the line number of current executable code?

Mats Cronqvist <>
Tue Aug 26 10:54:50 CEST 2008


Richard Carlsson wrote:
> Ahmed Ali wrote:
>   
>> Hi All,
>>
>> This discussion is interesting. Just wanted to know if it is possible
>> to get the function that called current function. Is this possible?
>>     
>
> The only way of doing that is to force an exception, and then look
> at the stack trace. 

  there is no way to reliably get the name of the calling function.
  it is true that the stack sometimes contain the name of the calling 
function.
  it is not true that the only way to inspect the stack is by forcing an 
exception.

  i'm too lazy to write actual code, but try this in the shell;

()41> {_,BT}=erlang:process_info(self(),backtrace).
()42> S=fun(B,{_,[_,{F,L}|_]})-> 
<<_:F/binary,R:L/binary,_/binary>> =B, R end.
()43> S(BT,re:run(BT,"Return addr\\s[0-9a-z]+\\s\\(([^\\s]*)")).
<<"erl_eval:expr/5">>

  clear as the day!

  mats



More information about the erlang-questions mailing list