Gradual Epiphany

Running Erl in a Debugger

Let’s say you need to debug a port driver in Erlang. This typically involves gdb (unless you prefer the printf route). Go to where erlang is installed and edit the bin/erl script. Change the last line from:

1
2
3
 
 exec $BINDIR/erlexec ${1+"$@"}
 

to:

1
2
3
4
5
6
7
 
if [ ! -z "$USE_GDB" ]; then
        gdb $BINDIR/erlexec --args $BINDIR/erlexec ${1+"$@"}    
else
        exec $BINDIR/erlexec ${1+"$@"}
fi
 

Now all you have to do to get Erlang running in gdb is:

1
2
3
4
 
$ export USE_GDB=1
$ erl
 

If all goes well, you should see something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
(dizzyd@sigr).(~)% export USE_GDB=1
(dizzyd@sigr).(~)% erl
GNU gdb 6.3.50-20050815 (Apple version gdb-1344) (Fri Jul  3 01:19:56 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .. done

(gdb) r
Starting program: /Users/dizzyd/Applications/erlang-r13b03/lib/erlang/erts-5.7.4/bin/erlexec 
Reading symbols for shared libraries +. done

Program received signal SIGTRAP, Trace/breakpoint trap.
0x00007fff5fc01028 in __dyld__dyld_start ()
(gdb) c
Continuing.
Reading symbols for shared libraries ... done
Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:8:8] [rq:8] [async-threads:0] [kernel-poll:false]

Eshell V5.7.4  (abort with ^G)
1>