Using GDB in the New Emacs 22

Thursday, September 13th, 2007

 

Contents

 

Why

Using GDB is necessary for every C/C++ developer in the Linux world. Though many gurus like the traditional command line, many people want a more integrated interface. At least they don’t want to do list all the time.

Among the current solutions xxgdb is a good choice, however it is available in X environment only, and pretty unmaintained. The last release was back in 1995.

Another choice is cgdb. This is curses-based and well maintained. However you have to be a VI user to use it, and you can’t benefit from the good X stuff. For us Emacs users, we have the GUD/GDB mode, and the best of all, as Emacs itself, this works under both X and terminal environment.

 

Using GDB Before Emacs 22

Before Emacs 22, the GDB interface in Emacs looks pretty much like cgdb. That is, you got two windows, one is the source window, and the other is used to interactive with GDB.

As always, you need to add the -g option to gcc when compiling. And on the Emacs part, you need to add:

(setq gdb-show-main t)

to your ~/.emacs file. Then you just run M-x gdb. Emacs will ask how to run GDB like:

gdbannotate=3 your_executable_name

in the mini buffer. Normally the only thing you need to change is your_executable_name. Hit RET and Emacs will split the frame with two windows. One is the GDB interface, called GUD-XX where XX is the name of your executable, and the other is the source file with the main function in it. Emacs will find the source file automatically, even if you haven’t openned it before.

Now you can move normally in the source file buffer, and hit C-x SPACE to set a breakpoint on the current line. A small red ball will shown on the left of the line, as you seen from most other IDEs. If you are running Emacs under X, you can also use your mouse to left click on the space area to the left of the line (called Fringe in Emacs) to set a breakpoint. If you do C-x SPACE or left click on a line that already have a breakpoint, the breakpoint is canceled.

Two window GDB in Emacs

You can use the GDB buffer as well as in a terminal session. Except you don’t need to do list all the time. An arrow will appear in the source buffer showing the current line of code if a breakpoint is hit, or you use the next command. If your program involves another source file, Emacs will find it and open it for you. There are also hollow arrows in the fringe area of source buffer. These indicates the current line in a higher frame where a function is called.

Most GDB commands get their key bindings. You can use either the key bindings or type directly in the GDB buffer. If you just want to see the flow of your program, key bindings might be a better choice cause you can hide the GDB buffer and get more code displayed. A tip: As most of the key bindings takes two or three key strokes, you may find the Emacs build-in command repeat useful, which is bind to C-x z by default. For example, if you want many next command, you can hit C-x C-a C-s to run one next first, then C-x z for another next, and you can hit z z z z z, as many times as you want next. And as expected, you can see the arrow jumps around your source file buffer.

 

The new interface with Emacs 22

With the new coming Emacs 22, we get a more powerful debugger, to use it, just add:

(setq gdb-many-windows t)

in your ~/.emacs file. You could start GDB mode as before, and… Bang! A lot of windows shows up :)

Multi window GDB in Emacs

Actually there are five windows there (by default). In the middle, the big window is the source file buffer. On the top left, is the GUD buffer, as we seen before. The other three windows are brand new:

  • On the top right corner is the locals buffer. In this buffer are the variables and their values of the current frame, and they are updated in real time. What’s more, if you hit RET or left click on the values, you can change them on the fly.
  • On the bottom left corner is the stack buffer. Here shows the calling stack, one function call per line. What’s more, if you hit RET on a line here, the source buffer will get updated to show the function of that line. Meanwhile, the locals buffer on the top right corner will be updated too: showing the variables in this frame.
  • On the bottom right corner is the breakpoint buffer. Here shows all the breakpoints. And, you can use SPACE to enable or disable a breakpoint here, use D to delete a break point, and as in the stack buffer, you can use RET to make the source buffer updated to where the breakpoint is.

Besides this, we can have lots more dedicated buffers: A separate IO buffer instead of output in the GDB buffer, a register buffer, an assembler buffer, a memory buffer, and even a threads buffer.

There’s lots more to discover: for example, you can drag the arrow in the source file buffer to execute the program. Just give it a try, and if you like it, go and read the manual :)

Tags: , , ,

Post a Comment

You could use <code type="name"> to get your code colorized

Your email is never published nor shared. Required fields are marked *

Close
E-mail It