Start Emacs with GDB directly

Hey folks out there,

as we all know, Emacs has a very powerful (graphical) interface to our beloved debugger GDB. Until five minutes ago, I used to start a program in debug mode by first starting emacs and then invoking M-x gdb, which can take a long time to navigate to the desired binary. Furthermore, the directory where the binary resides isn’t the actual runtime-path in many cases. Fortunately, Emacs can be started in GDB-mode directly, by calling it with the --eval parameter on the command-line. What we want to evaluate during startup is the (gdb) function. Its only parameter is a string describing how gdb should be invoked. Thus, we can write the following bash function:

debug () {
    emacs --eval "(gdb \"gdb --annotate=3 --cd=`pwd` $*\")" &
}

Here, the --annotate option tells gdb to generate annotations that can be interpreted by the Emacs gdb interface (the integer argument is the annotation-level). Furthermore, the --cd parameter instructs gdb to change its working dir to the specified one, which is the current working dir in our case. Thus we can call debug ./bin/prog, for instance.