Emacs (Debugging with ROCGDB)

Next: GDB/MI, Previous: TUI, Up: Top [Contents][Index]

## 27 Using ROCGDB under GNU Emacs

A special interface allows you to use GNU Emacs to view (and edit) the source files for the program you are debugging with ROCGDB.

To use this interface, use the command `M-x gdb` in Emacs. Give the executable file you want to debug as an argument. This command starts ROCGDB as a subprocess of Emacs, with input and output through a newly created Emacs buffer.

Running ROCGDB under Emacs can be just like running ROCGDB normally except for two things:

- All “terminal” input and output goes through an Emacs buffer, called the GUD buffer.

  This applies both to ROCGDB commands and their output, and to the input and output done by the program you are debugging.

  This is useful because it means that you can copy the text of previous commands and input them again; you can even use parts of the output in this way.

  All the facilities of Emacs’ Shell mode are available for interacting with your program. In particular, you can send signals the usual way—for example, `C-c C-c` for an interrupt, `C-c C-z` for a stop.

- ROCGDB displays source code through Emacs.

  Each time ROCGDB displays a stack frame, Emacs automatically finds the source file for that frame and puts an arrow (‘`=>`’) at the left margin of the current line. Emacs uses a separate buffer for source display, and splits the screen to show both your ROCGDB session and the source.

  Explicit ROCGDB `list` or search commands still produce output as usual, but you probably have no reason to use them from Emacs.

We call this text command mode. Emacs 22.1, and later, also uses a graphical mode, enabled by default, which provides further buffers that can control the execution and describe the state of your program. See GDB Graphical Interface in The GNU Emacs Manual.

If you specify an absolute file name when prompted for the `M-x gdb` argument, then Emacs sets your current working directory to where your program resides. If you only specify the file name, then Emacs sets your current working directory to the directory associated with the previous buffer. In this case, ROCGDB may find your program by searching your environment’s `PATH` variable, but on some operating systems it might not find the source. So, although the ROCGDB input and output session proceeds normally, the auxiliary buffer does not display the current source and line of execution.

The initial working directory of ROCGDB is printed on the top line of the GUD buffer and this serves as a default for the commands that specify files for ROCGDB to operate on. See Commands to Specify Files.

By default, `M-x gdb` calls the program called `gdb`. If you need to call ROCGDB by a different name (for example, if you keep several configurations around, with different names) you can customize the Emacs variable `gud-gdb-command-name` to run the one you want.

In the GUD buffer, you can use these special Emacs commands in addition to the standard Shell mode commands:

`C-h m`

  Describe the features of Emacs’ GUD Mode.

`C-c C-s`

  Execute to another source line, like the ROCGDB `step` command; also update the display window to show the current file and location.

`C-c C-n`

  Execute to next source line in this function, skipping all function calls, like the ROCGDB `next` command. Then update the display window to show the current file and location.

`C-c C-i`

  Execute one instruction, like the ROCGDB `stepi` command; update display window accordingly.

`C-c C-f`

  Execute until exit from the selected stack frame, like the ROCGDB `finish` command.

`C-c C-r`

  Continue execution of your program, like the ROCGDB `continue` command.

`C-c <`

  Go up the number of frames indicated by the numeric argument (see Numeric Arguments in The GNU Emacs Manual), like the ROCGDB `up` command.

`C-c >`

  Go down the number of frames indicated by the numeric argument, like the ROCGDB `down` command.

In any source file, the Emacs command `C-x SPC` (`gud-break`) tells ROCGDB to set a breakpoint on the source line point is on.

In text command mode, if you type `M-x speedbar`, Emacs displays a separate frame which shows a backtrace when the GUD buffer is current. Move point to any frame in the stack and type `RET` to make it become the current frame and display the associated source in the source buffer. Alternatively, click `Mouse-2` to make the selected frame become the current one. In graphical mode, the speedbar displays watch expressions.

If you accidentally delete the source-display buffer, an easy way to get it back is to type the command `f` in the ROCGDB buffer, to request a frame display; when you run under Emacs, this recreates the source buffer if necessary to show you the context of the current frame.

The source files displayed in Emacs are in ordinary Emacs buffers which are visiting the source files in the usual way. You can edit the files with these buffers if you wish; but keep in mind that ROCGDB communicates with Emacs in terms of line numbers. If you add or delete lines from the text, the line numbers that ROCGDB knows cease to correspond properly with the code.

A more detailed description of Emacs’ interaction with ROCGDB is given in the Emacs manual (see Debuggers in The GNU Emacs Manual).

Next: GDB/MI, Previous: TUI, Up: Top [Contents][Index]