Default Process Flags

Is there a way to instruct Erlang VM to apply a set of process flags to each new process that is spawned on the system?

For example, in a test environment, I would like each process to have the save_calls flag.

+6
source share
1 answer

One way to do this is to combine the Erlang trace functions with the .erlang file.

In particular, you can use the low-level trace capabilities provided by erlang: trace / 3 , or you can simply use dbg: tracer / 2 to create a new trace process that executes your custom handler function every time you receive a trace message.

To simplify things a bit, you could create an Erlang Start Up File in the directory where you use your code or in your home directory. Erlang Start Up File is a special file called .erlang that runs every time the runtime system starts.

Something like the following should do the following:

% -*- Erlang -*- erlang:display("This is automatically executed."). dbg:tracer(process, {fun ({trace, Pid, spawn, Pid2, {M, F, Args}}, Data) -> process_flag(Pid2, save_calls, Data), Data; (_Trace, Data) -> Data end, 100}). dbg:p(new, [procs, sos]). 

Basically, I am creating a new trace process that will track processes (first argument). I specify a handler function to get executed and some source data. In the handler function, I set the save_calls flag for newly created processes, while I ignore all other trace messages. I set save_calls ' parameter to 100 using the Initial Data parameter. In the last call, I tell dbg that I am only interested in created processes. I also set the sos parameter ( set_on_spawn ) to ensure trace flag inheritance.

Finally, pay attention to how you need to use the process_flag function option, which takes an additional argument (the Pid process for which you want to set a flag).

+1
source

Source: https://habr.com/ru/post/922013/


All Articles