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.
This is because it is the equivelant of sending a message to yourself in a callback, which will never work
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.
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.
Yeah, so a PR cold be done to do this for you, which would make it opaque 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…