Description
This mode was introduced in Emacs 22.1. It is described in more detail in the Emacs manual, however, most of it should be fairly intuitive. There is an old Linux Journal tutorial describing how to use it.
Here's a screenshot of Emacs 23 debugging a multi-threaded application taken from here:
Notice that this version of Emacs has anti-aliased fonts and there are now tabs for toggling the threads and registers buffers with the breakpoints and locals buffer respectively.
If you set the alias below in your .cshrc, you can start debugging with the command "gdbtool <filename>" just like you would with a a graphical debugger like totalview or cvd. Be careful to ensure Emacs 22 is the first Emacs executable called emacs in your path, or adjust the alias accordingly.
alias gdbtool emacs --eval \'\(gdb \"gdb --annotate=3 '\!*'\"\)\'
Or if you use a sh variant:
gdbtool () { emacs --eval "(gdb \"gdb --annotate=3 $*\")";}
If you always have an instance of Emacs running then you could use emacsclient rather than emacs.
Eclipse CDT is another program that acts as an IDE and possibly has more features. If you have a large C++ project then this may be better suit your needs. Emacs, on the other hand is not project based, provides a lightweight means of managing your files with a standard makefile and is perhaps more suited to smaller C projects and embedded programming. I think the biggest advantage of this mode over other debuggers is that the powerful features of Emacs are available for tasks such as editing and searching. There may be better debuggers around but Emacs is surely the best `debeditor'. When did you last run a debug session and not end up browsing and editing your code?
The FreeBSD Wiki describes Kernel Debugging with dcons with a screenshot of this mode using Emacs from a terminal.
GDB/MI
The current mode uses an interface for GDB called annotations. This involves marking up GDB's output, which is really intended to be read by humans. This output changes with time and has variable formatting, so a more stable and precise machine interface called GDB/MI is being developed.
To help with this process, there is a beta package called gdb-mi in the Emacs Lisp Package Archive. It comprises of modified gud.el and a file called gdb-mi.el which replaces gdb-ui.el. When installed, this overrides the current files and invoking M-x gdb will use GDB/MI directly (starts with "gdb -i=mi"). When deleted ('d' followed by 'x' in Package Menu mode), the files are deleted and old functionality restored. This provides a convenient way to review the current status/contribute to its development. It has recently been updated and now provides a reasonable level of performance. It allows the inclusion of the latest features of GDB and will eventually be much better than the current mode. For now, however, for someone who just wants to use GDB, however, the current mode in Emacs 22 is a better option.
Debugging Multi-threaded/process Applications
Currently GDB can't debug multi-process applications such as MPI programs and control of multi-threaded applications is limited. For example, when one thread stops for a breakpoint, GDB stops all other threads in the program. Also while you inspect a stopped thread's state, the other threads must remain stopped. There are plans to allow GDB to debug multi-process programs and to allow threads to run independently as well as allow the user to inspect a stopped thread's state while other threads are still running.
I'm starting to a consider how this functionality could be implemented in Emacs and think that a good design could provide a debugging experience comparable to that of Totalview. The latest version of Emacs in the CVS repository. Emacs 23.0.60, already has several improvements for multi-threaded applications that are not present in released versions.
If you debug such applications and have ideas about how this mode could be developed then please contact me at the e-mail address at the bottom of the sidebar.
Getting Emacs 22
A Guided Tour of Emacs gives a good illustrated introduction to Emacs 22 and includes a screenshot of this mode.
All of the major Linux distributions, except Debian, now include Emacs 22. Debian users can download and install emacs22 or a related package e.g
apt-get install emacs22-gtk
which now forms part of the Debian testing (lenny) distribution. Versions of these packages are also available as backports in Ubuntu (Edgy, Dapper and Breezy).
Mac OS X 10.5 (Leopard) also includes Emacs 22.1 as part of Darwin 9.0. This version has been built to support carbon, but you need to put an Emacs.app wrapper around it. For some reason M-x gdb has been changed to start up with "gdb --annotate=1". This is the same as "gdb --fullname" and so gives the old text command mode used in Emacs 21. To get the new graphical interface you need to change this back to "gdb --annotate=3". You can do this by editing the line in the minibuffer, or permanently with the custom variable "gud-gdb-command-name" using M-x customize-variable. Dan McKinley has a screenshot in his blog.
Alternatively you can download Emacs 22 as a tarball from here and build it yourself. Please report any problems using M-x report-emacs-bug which will include your configuration and send your message to the bug-gnu-emacs mailing list.
If you wish to use the most recent version, and you're not behind a firewall, you can get this directly from CVS, as described on Savannah:
cvs -z3 -d:pserver:anonymous@cvs.savannah.gnu.org:/sources/emacs co emacs
Then read INSTALL.CVS in the emacs directory for further instructions. Once you've built Emacs you can keep updating to the new changes by doing:
cvs up
from the emacs directory and following INSTALL.CVS. which really isn't much harder than downloading a tarball and will also mean that you can submit up-to-date suggestions or patches to the emacs-devel mailing list.
Multiple debug sessions
The graphical interface described above can currently only be used to debug a single program at a time. Here are two files, multi-gud.el, multi-gdb-ui.el, that should allow multiple debug sessions within one invocation of Emacs. They still require Emacs 22 and need to be evaluated *instead* of gud.el and gdb-ui.el. For example you could put them in your load-path and have
(load-library "multi-gud.el") (load-library "multi-gdb-ui.el")
in your .emacs. It is important that multi-gud.el is loaded first, otherwise multi-gdb-ui.el might find gud.el.
The're a bit rough but should basically do the right thing. They are not currently part of Emacs so it's probably best to report bugs/improvements directly to me.
There is also a file, also called gdb-mi.el, a version of which is included the GDB distribution. This will probably only work with versions distributed with GDB 6.5 or later. Unlike the version in ELPA it works on top of gdb-ui.el and you can only start it with M-x gdbmi.