by clemens (01.10.2024)

How to use Erlang dbg in a remote Elixir shell

tl;dr

I was recently trying to trace some problems in production, but was unable to get the trace to be printed in the remote shell.

The correct syntax for the tracer is in the remote shell is:

:dbg.tracer(:process, {fn (msg, n) -> IO.inspect(msg); n+1 end, 0})

I.e. the default tracer using :dbg.tracer() will not print to the remote shell, but rather the primary shell of the node. So you need to check the erlang.log.1 etc files to see that output.

Short reminder on how to trace:

:dbg.start()
:dbg.tracer(:process, {fn (msg, n) -> IO.inspect(msg); n+1 end, 0})

# trace a public function
:dbg.tp(MyApp.Module, :public_function, [])
# trace a private function
:dbg.tpl(MyApp.Module, :private_function, [])

# start tracing, no need to repeat this call if you add more trace points
:dbg.p(:all, :c)

# trace output here
...

# stop tracing
:dbg.stop_clear()

The :dbg.tp and :dbg.tpl functions also have variants for all functions in a module, or for specific arities of the functions. For more information have a look in the Erlang documentation.