[erlang-questions] Who called my gen_server?

Frank Muller frank.muller.erl@REDACTED
Thu Apr 27 06:10:37 CEST 2017


Hi Mikael,

Thank you for the hint about rpc module. I'll try to read it to understand
its behaviour

I'm still considering myself an Erlang newbie.

The idea here is to log all calls to my gen_server for debugging purposes:
who called me, from where, when, with which arguments ...

For "handle_call", it's more or less easy. Not so for "handle_cast" and
"handle_info".

Finally, I would like to generalize this idea to more than one gen_server.
It'll be very tedious if I've to add log calls to every gen_server in my
app. Maybe there's a better way.

Thanks again for your time Mikael.

/Frank

<mikpelinux@REDACTED> wrote :

> Frank Muller writes:
>  > Hi guys,
>  >
>  > I've two connected nodes: N1, N2.
>  >
>  > N1 is mine and I've a gen_server running on it.
>  > N2 is external and doesn't belong to me.
>  >
>  > A process on N2 is calling my gen_server (on N1) via an rpc:call.
>  >
>  > Is there a way to know the Pid of that process?
>
> Based on a half hour study of lib/kernel/src/rpc.erl in OTP-19.3 I didn't
> think so, but now I think I may have found a way.
>
> On the callee node, rpc has a gen_server (rex) which handles incoming
> calls.
> When it calls the {Mod,Fun,Args} from the rpc:call, it does so
> asynchronously
> via a temporary process (see handle_call_call/6).  There is a mapping from
> that process' Pid to the From passed to rex' handle_call (called 'To' in
> the code)
> in rex' state, which you could dig out using sys:get_state or :get_status.
>
> But on the caller node, rpc:call also uses a temporary process when doing
> the
> gen_server:call to the callee node (see do_call/3), and the only
> association
> between that process and the application process doing the rpc:call is
> buried
> in the local variables of do_call/3 and the temporary process.  (So if you
> do
> the sys:get_state on the callee node, you'd end up with the Pid of the
> temporary
> process on the caller node.)
>
> ... but now it struck me that the temporary process is monitored by the
> process
> doing the rpc:call, which erlang:process_info(Pid, monitored_by) should be
> able
> to retrieve.  So a reverse rpc:call to the caller node to do that could
> work.
>
> Not tested, will break if the rpc implementation changes, etc.
>
> Why do you need this anyway?
>
>  > Or, can I log all rpc_call(s) calling my gen_server?
>
> rex doesn't have that built-in, but you could modify it to do so (it's on
> the
> node you control)
>
> /Mikael
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170427/822aa4ad/attachment.htm>


More information about the erlang-questions mailing list