WatchPoint

Image link

Using GDB in Emacs to improve your debug experience

Introduction

In this episode of gdbWatchPoint Finn Grimwood, a senior Software Engineer at Undo, shows how he uses GDB inside the Emacs editor.


Hi – I’m Finn. In this episode of gdbWatchPoint, Greg has asked me to show you how to use Emacs and GDB, and some of the techniques to improve your debugging experience.

You can start by just starting Emacs:

$ emacs

 

gdb-emacs

 

In the minibuffer, type the command

M-x gdb

Hit return after which the minibuffer will show how to start gdb, which is more or less exactly what you’d type on the command line:

gdb -i=mi ./ls

(A useful thing to know here is that -i=mi tells GDB that it’s running inside an IDE.)

Hit return on the command in the minibuffer to start GDB inside Emacs.

 

Emacs running GDB

When running GDB inside Emacs, the IDE environment changes to suit Emacs. For example, the toolbar has changed to show the next/previous and up/down stack commands:

 

gdb-emacs-2

 

Emacs also adapts the toolbar as you use it, adding and removing options to help your debugging experience.

 

Multiple windows with GDB in Emacs

If I set a breakpoint at main and run GDB, Emacs automatically opens up the file at the breakpoint for you. You can then use the toolbar, which Emacs has customized, to navigate around the program.

Another useful automatic feature is that if I continue the program to the end, the output is shown in a new Emacs window for input/output:

 

gdb-emacs-3

 

This is really useful – Emacs separates out the source code, the program output, the interpreter into separate windows so you can navigate your program more easily. However, there’s even more that Emacs can show in separate windows by navigating to the GUD menu and then GDB-Windows option:

 

gdb-emacs-4

 

GDB Many Windows in Emacs

All these views on the GDB process are really useful but it can start looking cluttered as you open up each window separately. I tend not to do this because there’s a better way of managing all these windows which is called gdb-many-windows.

To use this, open up the minibuffer and run:

gdb-many-windows

After this, you’re shown a much more organised, much neater layout which looks like something you’d expect from an IDE.

 

gdb-emacs-6

 

This provides local registers, input/output, the source code and the GDB command which is much more useful than opening up each window individually.

 

Optimising your GDB/Emacs setup

All these techniques are useful, but there is still a problem with this setup: it doesn’t persist over multiple Emacs sessions so if I quit and then go back into Emacs, my setup is lost which is really frustrating.

To make GDB many windows persist you can do M-x customize. This opens up the Emacs config options which you can search for GDB:

 

gdb-emacs-7

You’ll then find the many windows option and you can toggle it on and off. You’ll also find some other interesting options in here such as GDB Show main which displays the source file containing the main routine of the startup.

Once you’ve done that, quit Emacs completely, restart it and start GDB again then all the useful windows have been reinstated. The GDB prompt, the source file, the local registers, locals, and breakpoints.

 

Setting breakpoints in GDB

As one final tip, you can set a GDB breakpoint in Emacs by clicking in the gutter by the line you want to set a breakpoint at. When you do a red dot appears.

 

GDB & Emacs

What you see in the video is almost exactly how I have GDB setup in Emacs for debugging. I hope this has been useful and see you again for the next gdbWatchPoint.

GDB Training Course

Don’t miss my next C++ debugging tutorial: sign up to my WatchPoint mailing list below.
Get tutorials straight to your inbox

Become a GDB Power User. Get Greg’s debugging tips directly in your inbox every 2 weeks.

Want GDB pro tips directly in your inbox?

Share this tutorial