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. 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 x allows one to specify the number of bytes.
e.g.:
Additionally the command invokes both library and user written functions and the returned value will be displayed.
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 .
i.e.:
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:
i.e.:
