Emacs Mode for GDB
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.
myprog.c
1 typedef struct {
2 float a;
3 float b;
4 } substruct;
5
6 struct {
7 int i;
8 substruct r;
9 } c;
10
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 }
myproc.c
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.
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.
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.
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.
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Linux Systems Administrator
- New Products
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Have you tried Boxen? It's a
4 hours 41 min ago - seo services in india
9 hours 13 min ago - For KDE install kio-mtp
9 hours 14 min ago - Evernote is much more...
11 hours 14 min ago - Reply to comment | Linux Journal
19 hours 59 min ago - Dynamic DNS
20 hours 33 min ago - Reply to comment | Linux Journal
21 hours 32 min ago - Reply to comment | Linux Journal
22 hours 22 min ago - Not free anymore
1 day 2 hours ago - Great
1 day 6 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?








Comments
The "hard R" version
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
So what should we do? Life is life.
Regarding Emacs's steep learning curve
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.
(1)
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?
Thanks,
Daniel.
Emacs help
(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?
Kai
I hope they fix the icons
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.ppmWhile 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.
--Ben
Re: I hope they fix the icons
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.
Nick
I agree that the "stop" and "
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.
Nick
Ummm. . . DDD
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.
James
Re: Ummm...DDD
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.