Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default process flags

Tags:

erlang

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

For example in testing environment I would like every process to have save_calls flag set.

like image 690
Maxim Vladimirsky Avatar asked Aug 03 '12 06:08

Maxim Vladimirsky


1 Answers

One way for doing this is to combine the Erlang tracing functionalities with a .erlang file.

Specifically, you could either use the low-level tracing capabilities provided by erlang:trace/3 or you could simply exploit the dbg:tracer/2 function to create a new tracing process which executes your custom handler function every time a tracing message is received.

To automate things a bit, you could then create an Erlang Start Up File in the directory where you're running your code or in your home directory. The Erlang Start Up File is a special file, called .erlang, which gets executed every time you start the run-time system.

Something like the following should do the job:

% -*- 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'm creating a new tracing process, which will trace processes (first argument). I'm specifying an handler function to get executed and some initial data. In the handler function, I'm setting the save_calls flag for newly spawned processes, whilst I'm ignoring all other tracing messages. I set the save_calls' option to 100, using the Initial Data parameter. In the last call, I'm telling dbg that I'm interested only in newly created processes. I'm also setting the sos (set_on_spawn) option to ensure inheritance of the tracing flags.

Finally, note how you need to use a variant of the process_flag function, which takes an extra argument (the Pid of the process you want to set the flag for).

like image 89
Roberto Aloi Avatar answered Nov 02 '22 05:11

Roberto Aloi