Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query an Erlang process for its state?

Tags:

erlang

A common pattern in Erlang is the recursive loop that maintains state:

loop(State) ->
  receive
    Msg ->
      NewState = whatever(Msg),
      loop(NewState)
  end.

Is there any way to query the state of a running process with a bif or tracing or something? Since crash messages say "...when state was..." and show the crashed process's state, I thought this would be easy, but I was disappointed that I haven't been able to find a bif to do this.

So, then, I figured using the dbg module's tracing would do it. Unfortunately, I believe because these loops are tail call optimized, dbg will only capture the first call to the function.

Any solution?

like image 993
mwt Avatar asked Aug 13 '09 21:08

mwt


People also ask

What is an Erlang process?

Erlang processes are lightweight, operate in (memory) isolation from other processes, and are scheduled by Erlang's Virtual Machine (VM). The creation time of process is very low, the memory footprint of a just spawned process is very small, and a single Erlang VM can have millions of processes running.

What is pid in Erlang?

Every process in Erlang is identified by a unique process identifier (pid). The function self/0 returns the process identifier of the running process. Erlang processes can be created with any of the following functions: spawn, spawn link, spawn monitor, spawn opt, etc.

How do I terminate a process in Erlang?

A process can terminate itself by calling one of the BIFs exit(Reason), erlang:error(Reason), erlang:error(Reason, Args), erlang:fault(Reason) or erlang:fault(Reason, Args). The process then terminates with reason Reason for exit/1 or {Reason,Stack} for the others.

Which module is used to spawn processes manually in Erlang?

spawn() creates a new process and returns the pid. The new process starts executing in Module:Name(Arg1,...,ArgN) where the arguments are the elements of the (possible empty) Args argument list.


2 Answers

If your process is using OTP, it is enough to do sys:get_status(Pid).

The error message you mentions is displayed by SASL. SASL is an error reporting daemon in OTP.

The state you are referring in your example code is just an argument of tail recursive function. There is no way to extract it using anything except for tracing BIFs. I guess this would be not a proper solution in production code, since tracing is intended to be used only for debug purposes.

Proper, and industry tested, solution would be make extensive use of OTP in your project. Then you can take full advantage of SASL error reporting, rb module to collect these reports, sys - to inspect the state of the running OTP-compatible process, proc_lib - to make short-lived processes OTP-compliant, etc.

like image 92
gleber Avatar answered Sep 30 '22 15:09

gleber


It turns out there's a better answer than all of these, if you're using OTP:

sys:get_state/1

Probably it didn't exist at the time.

like image 35
Michael Terry Avatar answered Sep 30 '22 15:09

Michael Terry