Emacs Mode for GDB

A graphical user interface is an essential part of any modern debugger. This mode provides one, without losing the power of Emacs.

The GNU symbolic debugger, GDB, is a powerful development tool, but one drawback is it works only from the command line. GDB has some clever features that make it easy to use on the command line, such as completion of GDB command names and procedure names; using Enter to repeat the previous command, which is useful for stepping through a program or looking at a listing; and the read-line interface.

For some tasks, however, it is useful have a graphical user interface (GUI). Graphical front ends are available for GDB, including Insight, but they require a separate editor. Until now, Emacs used a mode that originally was written by Eric Raymond in 1992. This comprised of the GUD buffer, where GDB commands were entered as though on the command line, and the source buffer, where a arrow string ("=>") pointed to the current line of program execution. In this article, I describe a new mode, which I call GDB-UI, that is a part of the next release of Emacs (21.4) and offers the GUI features of many modern debuggers. The advantage of this mode over the other debuggers is the powerful features of Emacs are available for tasks such as editing and searching to provide a truly integrated development environment.


Eric Raymond wrote gud.el, a grand unified debugger mode for running GDB and other debuggers, as part of Emacs. Shortly afterwards, Tom Lord and Jim Kingdon, then working at Cygnus, wrote another mode and called it gdba.el. Their mode was derived from gud.el but was designed for use only with GDB, with which it was distributed.

With gud.el, when GDB ran under Emacs, it used the -fullname option, which made it print a tag, called an annotation, that supplied the current filename, line number and address each time execution stopped. Lord and Kingdon added more annotations to GDB so that a front-end, such as Emacs, could understand better the state of execution. They called this mechanism level 2 annotations, the fullname option being level 1. This allowed Emacs to display more information, including the call stack, local variables of the current stack and the status of any breakpoints. In my opinion, this was a significant improvement, but for some reason their work didn't make its way into the Emacs community. Instead, it fell into disuse and was removed from the GDB distribution. Many GDB users may not have been aware of its existence, however, as it was distributed only with the source code for GDB.

By the time I first came across gdba.el, there had been many developments in Emacs. Notably, it had a toolbar and a speedbar. In addition, the CVS version became available, at Savannah, allowing images to be placed in the fringe to provide breakpoint icons. The resulting file, called gdb-ui.el, tries to use all of these features as well as those in gdba.el. It is stored in the lisp/progmodes sub-directory alongside gud.el.


The following simple program is used to illustrate how GDB-UI works.


1  typedef struct {
2    float a;
3    float b;
4  } substruct;
6  struct {
7    int i;
8    substruct r;
9  } c;
11 main() {
12   int n = 4;
13   int m[10] = {0,1,4,9,16,25,36,49,64,81};
14   n = 5;
15   myproc(n);
16   c.i = 1;
17   c.r.a = 0.5;
18   c.r.b = 0.25;
19 }


1  myproc(n)
2  {
3    int p;
4    p = 2*n;
5    printf("Two times %d is %d\n", n, p);
6  }

Compile with the -g option:

cc -g -c myproc.c
cc -g -o myprog myprog.c myproc.o

Type M-x gdb in the minibuffer to invoke GDB-UI. If necessary, type in the name of the executable, myprog, and press Enter. Emacs displays the GUD buffer, as it did in previous versions of Emacs. Now set the variable gdb-many-windows to t, either by selecting the checkbox on the menubar (Gud -> GDB-UI-> Display other windows) or through the minibuffer.

Emacs now displays the source code of the main routine, together with the GUD, locals, breakpoint and stack buffers, in a single frame. For the purposes of this tutorial, this arrangement is assumed. More generally, however, if the lisp variable, gdb-many-windows is nil, these other buffers appear only if the user specifically requests them. To do this, select GDB-windows or GDB-Frames from the menubar under the heading GUD and then choose the desired buffer.

If the toolbar is not present, type M-x toolbar-mode. Move the cursor slowly over the icons, and a tooltip tells you what function each button performs. Most of these icons have been taken from the Cygnus Insight debugger, but some are new. In particular, note that the Stop and Go buttons do not stop and start the program but set and clear breakpoints.

Figure 1. The Toolbar

Figure 2. The Source

At line 14 of the source buffer, left-click in the fringe and a red disk appears. This shows that a breakpoint has been set there. Left-click at the same place again to clear the breakpoint, and observe the disk disappear. The breakpoint also can be set by typing break 14 in the GUD buffer or by leaving the cursor on line 14 and clicking on the Stop button in the toolbar. Try this and then clear the breakpoint again, either by typing clear 14 in the GUD buffer or by leaving the cursor on line 14 and clicking on the Go button in the toolbar.

Set this breakpoint again and another one by typing break myproc in the GUD buffer, and look at the breakpoints buffer. This buffer shows the status of all the breakpoints and is shown in Figure 3 in its own frame.

Figure 3. Status of Breakpoints

Position the cursor on the details of the first breakpoint and press the spacebar. Doing this disables the breakpoint, and the breakpoint icon turns grey. Press the spacebar again, and the breakpoint once again is enabled. Right-click on the second breakpoint and the source buffer visits the file containing the breakpoint and displays its location.

With the breakpoint set, click on the Run button, which displays a man running, to start the program. A small arrow appears over the breakpoint icon at line 14, indicating that execution has stopped there.

Look at the locals buffer, and you can see that the value of the local variable n is 4. All local variable that are simple types display their values in this buffer. Complex types, such as arrays, structures and unions, simply give their type as shown in the Figure 4. This is the case for the variable m, which is an array.

Figure 4. Locals Buffer

Place the cursor on the array name--m, in this case--and click on the button showing a pair of glasses. If it is not present already, the speedbar should appear and display the array name and type. Note, however, that this feature requires GDB 6.0 or later.

Figure 5. Watch Expressions

Complex data types are represented in the speedbar in a tree format. To see all the values of m, right-click on the tag to the left of the expression. Click there again to contract this expression. These expressions are called watch expressions, and they update whenever a value changes. They should not be confused with watchpoints, which can stop the execution of the program being run under GDB. Structures such as the global variable c have a deeper level of nesting than arrays do, and their values can be explored in a similar manner.

Display the local variable n as a watch expression in the same way, and then step over one statement of myprog using either the toolbar or the GUD buffer. Notice that the font colour of this expression and value turns red to alert you that it has changed.

Resume execution of myprog, which now should stop at myproc. You can go up and down the stack using the toolbar or the GUD buffer. The source buffer updates accordingly, as does the locals buffer. You also can select the frame that you want to examine by right-clicking it in the stack buffer. This buffer displays the call stack, with the current frame displayed in inverse video.

Figure 6. The Stack Buffer

When you have finished debugging, kill the GUD buffer with C-x k, which also kills all the associated buffers. You need not do this if you wish to edit the source code and recompile within Emacs. This has the the advantage of keeping the shell history of the GUD buffer as well as GDB's breakpoints. You need to check, however, that the breakpoints in the recently edited code are where you want them.

This tutorial has covered the most basic features of GDB-UI. Other buffers exist and are left to the reader to explore. They include:

  • Input/Output Buffer: if the lisp variable gdb-use-inferior-io-buffer is non-nil, the executable program being debugged takes its input and displays its output here. Some of the commands from shell mode are available here.

  • Assembler Buffer: displays the current frame as machine code. An overlay arrow points to the current instruction, and you can set and remove breakpoints as with the source buffer. Breakpoint icons also appear in the fringe or margin.

  • Threads Buffer: displays a summary of all threads currently in your program. Point to any thread in the list and press Enter to make it become the current thread; it then displays the associated source in the source buffer. Alternatively, right-click to make the selected thread become the current one.

  • Registers Buffer: displays the values held by the registers.

You may not want to use all of the features described in this article, but many of them are customisable. Please refer to the Emacs Info manual, which is included in the distribution, for more details.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

The "hard R" version

Anonymous's picture

The "hard R" versions could be shown on a few cable channels, like the Playboy Channel, and the X-rated versions could be bought in a few video stores or by mail. This belief is now in bbw femdom. Two actors, Darren James and Lara Roxx, initially tested positive, and were barred from further sexually explicit content production. This belief is now in doubt.

So what should we do

buycialisonline's picture

So what should we do? Life is life.

Regarding Emacs's steep learning curve

Daniel's picture

I exclusively use Emacs for text editing for more than 7 years, but yet, I am amazed how difficult it is to get the basic things working. So much so that I wonder if there is another better text editor that all others are using behind my back and leave Emacs in its historic state.

I used both Emacs and XEmacs on Linux and on Windows. Recently, I was spending many hours trying to get XEmacs on Windows to always produce Windows text file format, but I couldn't find documentation with a solution that worked.

On Linux, I can do this:
C-x RET c iso-8859-1-dos C-x C-f msdos.txt

But on Windows, I haven't found out what I need to do to make iso-8859-1-dos available to me.

(2) I spent hours trying to find out how to stop Emacs from producing the file.txt~ backup files.

(3) I haven't found out how to stop Emacs from complaining about the absence of an ending-newline character. (I might have turned it on to try it out but forgot where I did that.)

I guess, in general, Emacs has so many features that I love, but why this editor has to cause so much wasted hours for things that supposedly are so easy to accomplish? In this regards, Emacs is still failing to be a pure productivity tool.

Please enlighten me if you know the answer to the above questions and also direct me to the right direction so that I won't be "so miserable" continually wasting time trying to "fix Emacs." Is my problem dued to me not having read a book about the Emacs editor? Or is my problem an isolated one that nobody else has to face?



Emacs help

Kai Grossjohann's picture

(1) (Problem to choose line endings in XEmacs on Windows.)

XEmacs comes in two variants, with Mule or without Mule. I believe that the Windows port is only available in the without-Mule variant. The C-x RET c command (like the other C-x RET commands) is something specific to Mule.

Generally speaking, you are comparing Emacs on GNU/Linux with XEmacs on Windows, and that is partly a comparison of GNU/Linux to Windows, but it is also to a significant degree a comparison of Emacs with XEmacs.

(2) (How to turn off backup files?)

Apropos is your friend. Try M-x apropos RET backup RET. It gives a list of considerable length, but the variable make-backup-files is amongst them. Note that M-x apropos RET and C-h a are two different things. Further note that make-backup-files exists in Emacs, but I'm not sure about XEmacs.

Perhaps you will like the backup-directory-alist variable? That way you can gather backup files in a central directory.

(3) (Emacs complains about missing newline at end of file.)

Apropos, once again, is your friend. M-x apropos RET newline RET and you will see the variable require-final-newline (or is it newlines?).

Does this (or apropos, your friend :-) help?


I hope they fix the icons

hackerb9's picture

I hope they fix the icons before the 21.4 release. It seems a bit ridiculous to click "go" and "stop" to set and delete break points. Couldn't they have just created new icons? Heck, here's one they could use:

    ppmmake darkgreen 32 32 | ppmlabel -color black -text "set" > set.ppm

While this isn't as big of a human--computer interface blunder as AfterStep's decision to use the recycle bin to logout, I don't want to be apologizing when I'm helping people learn emacs & gdb.

By the way, this mode, especially the "watch expressions" feature, is something I'm very happy to see in the default Emacs. It's always been possible, but now it's easy.


Re: I hope they fix the icons

Nick's picture

I agree that the "stop" and "go" are ambiguous. When they were created
it was not possible to create/delete a breakpoint by clicking on the
fringe. Maybe they're not necessary and could be removed now.

Toolbar icons should try to be internationally meaningful. The "stop" icon uses a road sign shape that is used in many countries. The example
that you offer might be meaningful to you but probably wouldn't make
much sense to a user in Thailand, say.

You don't need to apologise to people for the shortcomings of Emacs,
just point out it provide an opportunity to contribute something better. Emacs badly needs a graphic designer. Finding one that is
computer literate and willing to give up his time is not easy.

Anyway, thanks for the feedback.


I agree that the "stop" and "

Nick's picture

I agree that the "stop" and "go" are ambiguous. When they were created
it was not possible to create/delete a breakpoint by clicking on the
fringe. Maybe they're not necessary and could be removed now.

Toolbar icons should try to be internationally meaningful. The "stop" icon uses a road sign shape that is used in many countries. The example
that you offer might be meaningful to you but probably wouldn't make
much sense to a user in Thailand, say.

You don't need to apologise to people for the shortcomings of Emacs,
just point out it provide an opportunity to contribute something better. Emacs badly needs a graphic designer. Finding one that is
computer literate and willing to give up his time is not easy.

Anyway, thanks for the feedback.


Ummm. . . DDD

James Klaas's picture

Doesn't DDD exist any more? That's a great GUI for a wide variety of debuggers, including perl,java and ever since I started using it in the mid '90s, GDB.


Re: Ummm...DDD

Nick's picture

DDD is an excellent front-end, and another example of one that requires
a separate editor. However, the article was not intended as a review
of front ends to GDB.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState