Finding your calling module

Erik Stenman <>
Wed Feb 22 19:02:55 CET 2006

Tom Whitcomb wrote:

>I'm using a callback pattern similar to the approach used in the gen_server.  However, passing the module name around seems error prone and overly bound.  
>Can a called function can determine the name of the module of its calling function without passing the module name as a parameter?
It depends on what you mean by "module of its calling function".
If you are using a pattern like gen_server and the call actually is a rpc
(a send between the caller and the callee) then I do not know how to do
it without explicitly passing the caller as an argument.

To find the closest caller on the callstack I have found the following hack
useful on occations.
(Note that tail-calls are not saved on the stack, and the solution is
 quite hacky... erlang_fault is not guaranteed to give a good stacktrace...)

calling_mod() ->
  element(1,lists:nth(2,element(2,element(2,catch erlang:fault([]))))).
calling_fun() ->
  element(2,lists:nth(2,element(2,element(2,catch erlang:fault([]))))).

/Erik (Happi) Stenman

More information about the erlang-questions mailing list