GNU Debugger
Developer(s) | GNU Project |
---|---|
Initial release | 1986 |
Stable release | 14.2[1]
/ 3 March 2024 |
Repository | |
Written in | C, C++, Python |
Operating system | Unix-like, Windows |
Type | Debugger |
License | GPLv3 |
Website | www |
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,[2] and partially others.[3]
History
GDB was first written by
From 1990 to 1993 it was maintained by John Gilmore.[5] Now it is maintained by the GDB Steering Committee which is appointed by the Free Software Foundation.[6]
Technical details
Features
GDB offers extensive facilities for tracing and altering the execution of
GDB target processors (as of 2003) include:
GDB is still actively being developed. As of version 7.0 new features include support for Python scripting[8] and as of version 7.8 GNU Guile scripting as well.[9] 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.[10]
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.[11] 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.[12] 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
Graphical user interface
The debugger does not contain its own
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' memory and registers.
Common gdb commands | Corresponding ptrace calls |
---|---|
(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
$ gdb program
|
Debug "program" (from the shell) |
---|---|
(gdb) run -v
|
Run the loaded program with the parameters |
(gdb) bt
|
Backtrace (in case the program crashed) |
(gdb) info registers
|
Dump all registers |
(gdb) disas $pc-32, $pc+32
|
Disassemble |
An example session
Consider the following source-code written in C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t foo_len( const char *s )
{
return strlen( s );
}
int main( int argc, char *argv[] )
{
const char *a = NULL;
printf( "size of a = %lu\n", foo_len(a) );
exit( 0 );
}
Using the
$ 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, 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 <https://gnu.org/licenses/gpl.html>
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:
<https://www.gnu.org/software/gdb/bugs/>...
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
).
Depending on the implementation of strlen (inline or not), the output can be different, e.g.:
s
, is NULL
GNU gdb (GDB) 7.3.1
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
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:
<https://www.gnu.org/software/gdb/bugs/>...
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=<optimized out>, argv=<optimized out>) 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t foo_len( const char *s )
{
return strlen(s);
}
int main( int argc, char *argv[] )
{
const char *a = "This is a test string";
printf( "size of a = %lu\n", foo_len(a) );
exit( 0 );
}
Recompiling and running the executable again inside GDB now gives a correct result:
$ 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 <https://gnu.org/licenses/gpl.html>
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:
<https://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /path/example...done.
(gdb) run
Starting program: /path/example
size of a = 21
[Inferior 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
References
- ^ Joël Brobecker (3 March 2024). "GDB 14.2 released!". Retrieved 3 March 2024.
- ^ "GDB Documentation - Supported Languages". Archived from the original on 2017-12-28. Retrieved 2011-11-28.
- ^ "GDB Documentation - Summary". Archived from the original on 2012-07-01. Retrieved 2011-11-28.
- ^ a b "Richard Stallman lecture at the Royal Institute of Technology, Sweden (1986-10-30)". Retrieved 2006-09-21.
Then after GNU Emacs was reasonably stable, which took all in all about a year and a half, I started getting back to other parts of the system. I developed a debugger which I called GDB which is a symbolic debugger for C code, which recently entered distribution. Now this debugger is to a large extent in the spirit of DBX, which is a debugger that comes with Berkeley Unix.
- ^ "John Gilmore (activist)". hyperleap.com.
- ^ "GDB Steering Committee". Retrieved 2008-05-11.
- ^ "GDB Documentation - Summary - Contributors". Archived from the original on 2011-09-29. Retrieved 2011-12-01.
- ^ "GDB 7.0 Release Notes". Retrieved 2011-11-28.
- ^ Joel Brobecker (2014-07-29). "GDB 7.8 released!". Retrieved 2014-07-30.
- ^ "Reverse Debugging with GDB". Retrieved 2014-01-20.
- ^ "Howto: GDB Remote Serial Protocol: Writing a RSP Server" (PDF).
- ^ "Implementing a remote stub".
- ^ "Kernel debugging with Dcons".
External links
- Official website
- UltraGDB: Visual C/C++ Debugging with GDB on Windows and Linux Archived 2017-12-12 at the Wayback Machine
- KGDB: Linux Kernel Source Level Debugger
- The website for "MyGDB: GDB Frontend" in the Korean language
- A Visual Studio plugin for debugging with GDB
- Comparison of GDB front-ends, 2013
- Using Eclipse as a Front-End to the GDB Debugger
Documentation
Tutorials
- RMS's gdb Tutorial (Ryan Michael Schmidt, not Richard Matthew Stallman)
- GDB Tutorial