Recently I encountered a problem that a gen_server process would receive a message that should not have been received. The message has no information to tell where it was sent from, and the project code is complicated. I need to know a way to know where the message was sent from, so does anyone know how?
Maybe by tracing send and receive ?
Edbg examples look promising …
If you can narrow down the situation where the event happens, you could try to
turn on tracing for a short time
erlang:trace/3 with the flag send, maybe also ‘receive’.
If you do this within a gen_server, you can filter the callbacks from tracer for the interesting messages.
thank for the link!
That’s a good idea, This method can find out msg from which process. I’ve based on GitHub - rabbitmq/looking_glass: An Erlang/Elixir/BEAM profiler tool write a wheel. It’s time to use it.
Technically a message could be sent from anywhere (including ERTS itself, not even Erlang code).
So there is no reliable way to know the sender, unless sender adds that information into the message itself.
In addition to function call tracing (
erlang/trace), there is also Erlang -- seq_trace feature, which may prove useful to understand the entire flow caused by a message. Just be careful with it in production, as it may eventually add the trace marker to all messages in your system.
Even if you turn on tracing you will see when a message arrives at a process but you will only see the message itself and not from where it came. For a simple experiment try starting
observer and use it to trace messages to/from the process in which you are interested.
Thank you for your advice.
I trace send events that contain the process that sent and received the message
To debug message handling in an Elixir/Erlang project, you can:
1) add metadata to the message, 2) enable process tracing, 3) add logging, or 4) use the debugger. Instrument your code and gather information to understand the issue.
That is basically the only way to do it.
When you trace messages being sent by a process then you can see to where it is sent but that is about it.
Very good advice, thank you!