Quick Start: Debugging in Erlang

If you need to quickly debug a module in Erlang I have a few easy steps for you (using my Erlang hgsvn module as an example):

1> c(hgsvn, [debug_info]).
{ok, hgsvn}
2> ii(hgsvn).
{module, hgsvn}
3> ib(hgsvn, start, 0).
4> iaa([break]).
5> hgsvn:start().

After this there is a short pause as the Erlang Debugger is launched. It provides a step-by-step debugging interface and gives the values of all variables in scope. It lets you control the execution of your Erlang program to see where subtle logic errors got in and wrecked things.

To further explain the commands:

  • c(hgsvn, [debug_info]). instructs the EShell to compile and reload hgsvn with debug symbols
  • ii(hgsvn) instructs the Erlang interpreter to load the debug info for the hgsvn module
  • ib(hgsvn, start, 0) instructs the Erlang interpreter to set a breakpoint on entry to hgsvn:start/0 (ib/2 lets you set line-based breakpoints for cases where they are useful)
  • iaa([break]) instructs the Erlang interpreter to raise the debugger when it encounters a breakpoint (without this you will have what appears to be an application hang in EShell)
  • hgsvn:start() invokes the entry-point function for the code I am debugging in this example

A big thing to watch out for is the way Step works on nested calls like f(g(X)). When you click Step on code like this it steps into f/1 not into g/1. Bear this in mind and avoid nested calls in code you intend to be able to debug.