The GNU Debugger (GDB) is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, Assembly, C, C++, D, Fortran, Haskell, Go, Objective-C, OpenCL C, Modula-2, Pascal, Rust, and partially others. It detects problems in a program while letting it run and allows users to examine program variables and machine registers.
History GDB was first written by Richard Stallman in 1986 as part of his GNU system, after his GNU Emacs was "reasonably stable". GDB is free software released under the GNU General Public License (GPL). It was modeled after the DBX debugger, which came with Berkeley Unix distributions.
From 1990 to 1993 it was maintained by John Gilmore. Now it is maintained by the GDB Steering Committee which is appointed by the Free Software Foundation.
Technical features
GDB offers extensive facilities for tracing, examining and altering the execution of computer programs. The user can monitor and modify the values of programs' internal variables, and even call functions independently of the program's normal behavior.
Supported platforms GDB target processors () include: Alpha, ARM, AVR, H8/300, Altera Nios/Nios II, System/370, System/390, x86 (32-bit and 64-bit), IA-64 "Itanium", Motorola 68k, MIPS, PA-RISC, PowerPC, RISC-V, SuperH, SPARC, and VAX. Lesser-known target processors supported in the standard release have included A29K, ARC, ETRAX CRIS, D10V, D30V, FR-30, FR-V, Intel i960, 68HC11, Motorola 88000, MCORE, MN10200, MN10300, NS32k, Stormy16, and Z8000 (newer releases will likely not support some of these).
GDB has compiled-in simulators for most targets.
Stepping through code Both the and command can be used to advance execution over the next n statements. If is omitted it defaults to 1. The difference between the commands is that will follow the flow of execution into the internals of any function call whereas will execute the whole function and proceed to the next statement within the current routine.
The command is used either to skip over a section of problematic code or go back to a previous statement in order to review execution again. The specified location may correspond to different parts of the executing program, but unexpected results may occur for those not accustomed to machine code.
Printing values and expressions When a program is halted in mid execution the (abbreviated as ) command can be used to display the value of a variable or an expression using C or C++ syntax. The command (meaning "examine") is similar but its argument is an address in memory including address expressions. Both commands use flags to indicate presentation format of the output though there are some differences as allows one to specify the number of bytes.
These commands can be used like this:
Additionally, the command can invoke both library and user written functions and it display the returned value.
Values displayed are automatically assigned to special value history variables which begin with a sign followed by a sequence number which can then be redisplayed using , e.g.: One can also use the command to create convenience variables for use during a GDB session, e.g.; If the argument of is an array or struct all elements will be output. The following syntax can be used to show a sub range of the array:
Breakpoints and watchpoints Breakpoints and watchpoints are used when one needs to examine a program prior to a known situation where things are likely to go wrong or once things have gone wrong. Both break- and watchpoints issued integer identification numbers, 1, 2, 3... which can be used to , or them. The command displays all break-, watch-, trace- and catchpoints and their current status, and displays all watchpoints and their current status.
Breakpoints are set to halt the flow of execution either on specific line numbers in one's code or on entry to a function when run within the debugger, e.g. will stop on line 42 of the specified file. If the file name is omitted the reference is to the current file.
A conditional breakpoint halts on a specified line when a specified expression is true, e.g.:
The command allows one to add conditions to an already established breakpoint.
Watchpoints halt the flow of execution when the value of a variable or an expression changes, irrespective of where in the program it occurs. By default, where possible, GDB monitors the memory location where the change takes place, a useful feature given that multiple pointers may refer to the same address. Conversely a software watchpoint, which are slower, track just the variables.
The and commands will halt the program whenever the memory location or variable is read.
Another conditional feature of both watches and breakpoints is the command which disregards the halting criteria until the execution has passed the specified point many times.
The command primes GDB to automatically output the value of an expression each time it stops. Multiple commands are cumulative and the output can be formatted using the same flags available to the command.
Framestacks When a program is halted it may be several layers deep in function calls. Each level of function call is called a frame and the collection of frames is called a frame stack. When execution is halted one can navigate up and down to a specific frame in order to examine the value of variables or expressions at a particular level. The command will provide a list of all of the frames in the stack. The command displays all of the arguments passed to the current frame and the command displays a list of the variables available to it along with their values.
Scripting support GDB includes the ability to define command routines that can be used to automate frequently repeated sets of GDB instructions. These consisting of GDB commands placed between and statements. Parameters to these routines are not declared by name, but are passed in special variables , , ... with the special variable representing the number of command line arguments.
Additionally GDB includes / and blocks terminated by statements as well as and statements to manage flow of control, e.g.:
If one is for example debugging a C program with a generic linked list with a leading value and a field pointing to the next element, the following command would use a loop to display all the elements:
Command definitions placed in the local file .gdbinit are automatically loaded at the beginning of the GDB session. Command definitions can also be saved in ordinary files and loaded using the command.
As of version 7.0, new features include support for Python scripting and as of version 7.8 GNU Guile scripting as well which is based on the Scheme programming language.
Reversible debugging Version 7.0 added support for "reversible debugging", which allows a debugging session to step backward, much like rewinding a crashed program to see what happened. The feature is highly memory intensive and slows execution with a default limit of 20,000 instructions. The recommended procedure is to set breakpoints before and after the suspected problem, then issue the command when the program stops at the first one. Continue execution to the second breakpoint. The , and commands can then be used to backtrack execution, undoing changes in variables piecemeal. However reverse debugging does not undo actions such as console output nor does it reissue external events such as interrupts or incoming network packets.
Remote debugging GDB offers a "remote" mode often used when debugging embedded systems. In remote operation GDB runs on one machine and the program being debugged on another. GDB can communicate to the remote "stub" that understands the GDB protocol through a serial device or TCP/IP. A stub program can be created by linking to the appropriate stub files provided with GDB, which implement the target side of the communication protocol. Alternatively, gdbserver can be used to remotely debug the program without needing to change it in any way.
The same mode is also used by KGDB for debugging a running Linux kernel on the source level with GDB. With KGDB, kernel developers can debug a kernel in much the same way as they debug application programs. It makes it possible to place breakpoints in kernel code, step through the code, and observe variables. On architectures where hardware debugging registers are available, watchpoints can be set which trigger breakpoints when specified memory addresses are executed or accessed. KGDB requires an additional machine which is connected to the machine to be debugged using a serial cable or Ethernet. On FreeBSD, it is also possible to debug using FireWire direct memory access (DMA).
Graphical user interface GDB does not contain its own graphical user interface, and defaults to a command-line interface, although it does contain a text user interface. Several front-ends have been built for it, such as UltraGDB, Xxgdb, Data Display Debugger (DDD), Nemiver, KDbg, the Xcode debugger, GDBtk/Insight, Gede, Seer, and HP Wildebeest Debugger GUI (WDB GUI). IDEs such as Codelite, Code::Blocks, Dev-C++, Geany, GNAT Programming Studio (GPS), KDevelop, Qt Creator, Lazarus, MonoDevelop, Eclipse, NetBeans, and Visual Studio can interface with GDB. GNU Emacs has a "GUD mode" and tools for Vim exist (e.g. clewn). These offer facilities similar to debuggers found in IDEs.
Some other debugging tools have been designed to work with GDB, such as memory leak detectors.
Internals GDB uses the system call ptrace (the name is an abbreviation of "process trace") to observe and control the execution of another process, and examine and change the process's memory and registers: A breakpoint is implemented by replacing an instruction at a given memory address with another special instruction. Executing a breakpoint instruction causes the signal SIGTRAP. See also
Binary File Descriptor library (libbfd) dbx DDD, a GUI for GDB and other debuggers gdbserver LLDB
References
External links , official GDB Git repository UltraGDB: Visual C/C++ Debugging with GDB on Windows and Linux The website for "MyGDB: GDB Frontend" in the Korean language A Visual Studio plugin for debugging with GDB
Documentation Richard M. Stallman, Roland Pesch, Stan Shebs, et al., Debugging with GDB (Free Software Foundation, 2011) GDB Internals
Tutorials RMS's gdb Tutorial (Ryan Michael Schmidt, not Richard Matthew Stallman) GDB Tutorial Using Eclipse as a Front-End to the GDB Debugger