The GNU Debugger (GDB) is a
portable debugger that runs on many
Unix-like
A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
systems and works for many
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming l ...
s, including
Ada,
C,
C++,
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
,
Free Pascal,
Fortran,
Go, and partially others.
History
GDB was first written by
Richard Stallman
Richard Matthew Stallman (; born March 16, 1953), also known by his initials, rms, is an American free software movement activist and programmer. He campaigns for software to be distributed in such a manner that its users have the freedom to u ...
in 1986 as part of his
GNU system, after his
GNU Emacs
GNU Emacs is a free software text editor. It was created by GNU Project founder Richard Stallman, based on the Emacs editor developed for Unix operating systems. GNU Emacs has been a central component of the GNU project and a flagship proje ...
was "reasonably stable".
GDB is
free software
Free software or libre software is computer software distributed under terms that allow users to run the software for any purpose as well as to study, change, and distribute it and any adapted versions. Free software is a matter of liberty, ...
released under the
GNU General Public License
The GNU General Public License (GNU GPL or simply GPL) is a series of widely used free software licenses that guarantee end user
In product development, an end user (sometimes end-user) is a person who ultimately uses or is intended to ulti ...
(GPL). It was modeled after the
DBX debugger, which came with
Berkeley Unix
The Berkeley Software Distribution or Berkeley Standard Distribution (BSD) is a discontinued operating system based on Research Unix, developed and distributed by the Computer Systems Research Group (CSRG) at the University of California, Berke ...
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
The Free Software Foundation (FSF) is a 501(c)(3) non-profit organization founded by Richard Stallman on October 4, 1985, to support the free software movement, with the organization's preference for software being distributed under copyleft ("s ...
.
Technical details
Features
GDB offers extensive facilities for tracing and altering the execution of
computer program
A computer program is a sequence or set of instructions in a programming language for a computer to execute. Computer programs are one component of software, which also includes documentation and other intangible components.
A computer progra ...
s. The user can monitor and modify the values of programs' internal
variables, and even call
functions independently of the program's normal behavior.
GDB target processors (as of 2003) include:
Alpha,
ARM,
AVR,
H8/300,
Altera Nios/Nios II,
System/370,
System 390,
X86 and its 64-bit extension
X86-64
x86-64 (also known as x64, x86_64, AMD64, and Intel 64) is a 64-bit version of the x86 instruction set, first released in 1999. It introduced two new modes of operation, 64-bit mode and compatibility mode, along with a new 4-level paging ...
,
IA-64
IA-64 (Intel Itanium architecture) is the instruction set architecture (ISA) of the Itanium family of 64-bit Intel microprocessors. The basic ISA specification originated at Hewlett-Packard (HP), and was subsequently implemented by Intel in col ...
"Itanium",
Motorola 68000
The Motorola 68000 (sometimes shortened to Motorola 68k or m68k and usually pronounced "sixty-eight-thousand") is a 16/32-bit complex instruction set computer (CISC) microprocessor, introduced in 1979 by Motorola Semiconductor Products Sect ...
,
MIPS,
PA-RISC
PA-RISC is an instruction set architecture (ISA) developed by Hewlett-Packard. As the name implies, it is a reduced instruction set computer (RISC) architecture, where the PA stands for Precision Architecture. The design is also referred to a ...
,
PowerPC
PowerPC (with the backronym Performance Optimization With Enhanced RISC – Performance Computing, sometimes abbreviated as PPC) is a reduced instruction set computer (RISC) instruction set architecture (ISA) created by the 1991 Apple– IBM ...
,
SuperH
SuperH (or SH) is a 32-bit reduced instruction set computing (RISC) instruction set architecture (ISA) developed by Hitachi and currently produced by Renesas. It is implemented by microcontrollers and microprocessors for embedded systems.
At t ...
,
SPARC
SPARC (Scalable Processor Architecture) is a reduced instruction set computer (RISC) instruction set architecture originally developed by Sun Microsystems. Its design was strongly influenced by the experimental Berkeley RISC system developed ...
, and
VAX. Lesser-known target processors supported in the standard release have included
A29K
The AMD Am29000, commonly shortened to 29k, is a family of 32-bit RISC microprocessors and microcontrollers developed and fabricated by Advanced Micro Devices (AMD). Based on the seminal Berkeley RISC, the 29k added a number of significant impro ...
,
ARC,
ETRAX CRIS,
D10V,
D30V,
FR-30,
FR-V,
Intel i960
Intel's i960 (or 80960) was a RISC-based microprocessor design that became popular during the early 1990s as an embedded microcontroller. It became a best-selling CPU in that segment, along with the competing AMD 29000. In spite of its succes ...
,
68HC11,
Motorola 88000,
MCORE,
MN10200,
MN10300,
NS32K,
Stormy16, and
Z8000
The Z8000 ("''zee-'' or ''zed-eight-thousand''") is a 16-bit microprocessor introduced by Zilog in early 1979. The architecture was designed by Bernard Peuto while the logic and physical implementation was done by Masatoshi Shima, assisted by a ...
. (Newer releases will likely not support some of these.) GDB has compiled-in
simulators
A simulation is the imitation of the operation of a real-world process or system over time. Simulations require the use of models; the model represents the key characteristics or behaviors of the selected system or process, whereas the s ...
for even lesser-known target processors such like
M32R or
V850.
GDB is still actively being developed. As of version 7.0 new features include support for
Python scripting and as of version 7.8
GNU Guile scripting as well. Since version 7.0, support for "reversible debugging" — allowing a debugging session to step backward, much like rewinding a crashed program to see what happened — is available.
Remote debugging
GDB offers a "remote" mode often used when debugging embedded systems. Remote operation is when GDB runs on one machine and the program being debugged runs on another. GDB can communicate to the remote "stub" that understands 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
The Linux kernel is a free and open-source, monolithic, modular, multitasking, Unix-like operating system kernel. It was originally authored in 1991 by Linus Torvalds for his i386-based PC, and it was soon adopted as the kernel for the GNU ...
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
Ethernet () is a family of wired computer networking technologies commonly used in local area networks (LAN), metropolitan area networks (MAN) and wide area networks (WAN). It was commercially introduced in 1980 and first standardized in ...
. On
FreeBSD, it is also possible to debug using
FireWire
IEEE 1394 is an interface standard for a serial bus for high-speed communications and isochronous real-time data transfer. It was developed in the late 1980s and early 1990s by Apple in cooperation with a number of companies, primarily Sony a ...
direct memory access (DMA).
Graphical user interface
The debugger does not contain its own
graphical user interface
The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows User (computing), users to Human–computer interaction, interact with electronic devices through graphical icon (comp ...
, and defaults to a
command-line interface, although it does contain a
text user interface
In computing, text-based user interfaces (TUI) (alternately terminal user interfaces, to reflect a dependence upon the properties of computer terminals and not just text), is a retronym describing a type of user interface (UI) common as an ear ...
. Several front-ends have been built for it, such as
UltraGDB,
Xxgdb,
Data Display Debugger (DDD),
Nemiver,
KDbg, the
Xcode debugger, GDBtk/Insight, 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
An eclipse is an astronomical event that occurs when an astronomical object or spacecraft is temporarily obscured, by passing into the shadow of another body or by having another body pass between it and the viewer. This alignment of three ce ...
,
NetBeans, and
Visual Studio
Visual Studio is an integrated development environment (IDE) from Microsoft. It is used to develop computer programs including websites, web apps, web services and mobile apps. Visual Studio uses Microsoft software development platforms such ...
can interface with GDB.
GNU Emacs
GNU Emacs is a free software text editor. It was created by GNU Project founder Richard Stallman, based on the Emacs editor developed for Unix operating systems. GNU Emacs has been a central component of the GNU project and a flagship proje ...
has a "GUD mode" and tools for
Vim
Vim means enthusiasm and vigor. It may also refer to:
* Vim (cleaning product)
* Vim Comedy Company, a movie studio
* Vim Records
* Vimentin, a protein
* "Vim", a song by Machine Head on the album ''Through the Ashes of Empires''
* Vim (text ed ...
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 a system call named
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 register. A list of common gdb commands and corresponding ptrace calls are listed below:
* (gdb) start : PTRACE_TRACEME – makes parent a tracer (called by a tracee)
* (gdb) attach PID: PTRACE_ATTACH – attach to a running process
* (gdb) stop: kill(child_pid, SIGSTOP) (or PTRACE_INTERRUPT)
* (gdb) continue: PTRACE_CONT
* (gdb) info registers : PTRACE_GET(FP)REGS(ET) and PTRACE_SET(FP)REGS(ET)
* (gdb) x : PTRACE_PEEKTEXT and PTRACE_POKETEXT
A breakpoint is implemented by replacing an instruction at a given memory address with another special instruction. Executing breakpoint instruction causes SIGTRAP.
Examples of commands
An example session
Consider the following source-code written in
C:
#include
#include
#include
size_t foo_len( const char *s )
int main( int argc, char *argv[] )
Using the GNU Compiler Collection, GCC compiler on Linux, the code above must be compiled using the
-g
flag in order to include appropriate debug information on the binary generated, thus making it possible to inspect it using GDB. Assuming that the file containing the code above is named
example.c
, the command for the
compilation could be:
$ gcc example.c -Og -g -o example
And the binary can now be run:
$ ./example
Segmentation fault
Since the example code, when executed, generates a
segmentation fault
In computing, a segmentation fault (often shortened to segfault) or access violation is a fault, or failure condition, raised by hardware with memory protection, notifying an operating system (OS) the software has attempted to access a restrict ...
, GDB can be used to inspect the problem.
$ gdb ./example
GNU gdb (GDB) Fedora (7.3.50.20110722-13.fc16)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /path/example...done.
(gdb) run
Starting program: /path/example
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400527 in foo_len (s=0x0) at example.c:7
7 return strlen (s);
(gdb) print s
$1 = 0x0
The problem is present in line 7, and occurs when calling the function
strlen
(because its argument,
s
, is
NULL
).
Depending on the implementation of strlen (
inline or not), the output can be different, e.g.:
GNU gdb (GDB) 7.3.1
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /tmp/gdb/example...done.
(gdb) run
Starting program: /tmp/gdb/example
Program received signal SIGSEGV, Segmentation fault.
0xb7ee94f3 in strlen () from /lib/i686/cmov/libc.so.6
(gdb) bt
#0 0xb7ee94f3 in strlen () from /lib/i686/cmov/libc.so.6
#1 0x08048435 in foo_len (s=0x0) at example.c:7
#2 0x0804845a in main (argc=, argv=) at example.c:14
To fix the problem, the variable
a
(in the function
main
) must contain a valid string. Here is a fixed version of the code:
#include
#include
#include
size_t foo_len( const char *s )
int main( int argc, char *argv[] )
Recompiling and running the executable again inside GDB now gives a correct result:
GNU gdb (GDB) Fedora (7.3.50.20110722-13.fc16)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /path/example...done.
(gdb) run
Starting program: /path/example
size of a = 21
nferior 1 (process 14290) exited normally
GDB prints the output of
printf
in the screen, and then informs the user that the program exited normally.
See also
*
Binary File Descriptor library (libbfd)
*
dbx
*
DDD, a
GUI for GDB and other
debuggers
*
gdbserver
*
LLDB
References
External links
*
UltraGDB: Visual C/C++ Debugging with GDB on Windows and LinuxKGDB: Linux Kernel Source Level DebuggerThe website for "MyGDB: GDB Frontend" in the Korean languageA Visual Studio plugin for debugging with GDBComparison of GDB front-ends, 2013
Documentation
*
Richard M. Stallman,
Roland Pesch,
Stan Shebs, et al.
''Debugging with GDB''(
Free Software Foundation
The Free Software Foundation (FSF) is a 501(c)(3) non-profit organization founded by Richard Stallman on October 4, 1985, to support the free software movement, with the organization's preference for software being distributed under copyleft ("s ...
, 2011)
GDB Internals
Tutorials
(Ryan Michael Schmidt, not
Richard Matthew Stallman)
''GDB Tutorial''
{{DEFAULTSORT:Gnu Debugger
Debuggers
Debugger
Unix programming tools
Video game development software for Linux
Software that was rewritten in C++
Articles with example C code