Changing debug options after start

The start functions in the gen_server and gen_statem modules support providing debug options: {debug, Dbgs :: [sys:debug_option()]}.

The sys module exports functions to set these options on a running process.

How can I set these from the running process after it’s started?

Calling sys:trace(self(), true results in: {error,{calling_self,{sys,trace,[<0.87.0>,true]}}}.

Looking at the source code of gen_server, I don’t think there’s a way to do this. Though it would be nice to extend the gen_server and gen_statem to support this feature.

1 Like

This is because it is the equivelant of sending a message to yourself in a callback, which will never work :smile:

Now, you can do this, though it is documented, though perhaps a function could be added on to the sys module to keep the details opaque (i.e., a fun that spawns a proc, to send you the message, but the ramifications of doing so are not clear!)

With the above in mind, and that using or doing undocumented things can result in breakage in your own code later, here is how you would do this.

1> gen:call(your_proc_name_or_pid, system,  {debug, {log_to_file, "debug.log"}}, 5000).
{ok,ok}

So, you could do that or use the functions already on the sys module, within your gen_server, just spin up a proc to send you that message, not sure if you’d have to busy wait to keep the debug going, but I wouldn’t presume so. You may also want to keep tabs on that process, given that it all gets turned on and off within your own proc.

Now you should have a debug.log in the cwd your app is running in. Substitute that with trace, etc. and voila.

1 Like

While this seems like a good plan we are in fact stymied by the gen module just not having it with our hijinks.

Calling gen:call(self(), system, {debug, {trace, true}}) also results in the calling_self error.

Bypassing gen altogether and sending a system message:

self() ! {system, self(), {debug, {trace, true}}}

raises the exception:
no function clause matching gen:reply(<0.87.0>,ok)

While not particularily satisfying, this does work: spawn(sys, trace, [self(), true]).

1 Like

Yeah, so a PR cold be done to do this for you, which would make it opaque :slight_smile: sys:trace(self(), true), this could check to see if Pid =:= self(), if so, spawn/3, though there might be problems with it too, perhaps…

1 Like