Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
GNU Debugger
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Source-level debugger}} {{Redirect|GDB}} {{Infobox software | logo = GDB Archer Fish by Andreas Arnez.svg | screenshot = GNU gdb 12.1 screenshot.png | developer = [[GNU Project]] | released = {{Start date and age|1986}} | latest release version = {{wikidata|property|preferred|references|edit|Q464113|P348|P548=Q2804309}} | latest release date = {{wikidata|qualifier|preferred|single|Q464113|P348|P548=Q2804309|P577}} | latest preview version = {{wikidata|property|preferred|references|edit|Q464113|P348|P548=Q51930650}} | latest preview date = {{wikidata|qualifier|preferred|single|Q464113|P348|P548=Q51930650|P577}} | programming language = [[C (programming language)|C]], [[C++]], [[Python (programming language)|Python]] | operating system = [[Unix-like]], [[Microsoft Windows|Windows]] | genre = [[Debugger]] | license = [[GNU General Public License#Version 3|GPLv3]] | website = {{URL|https://www.gnu.org/software/gdb}} }} The '''GNU Debugger''' ('''GDB''') is a [[Software portability|portable]] [[debugger]] that runs on many [[Unix-like]] systems and works for many [[programming language]]s, including [[Ada (programming language)|Ada]], [[Assembly language|Assembly]], [[C (programming language)|C]], [[C++]], [[D (programming language)|D]], [[Fortran]], [[Haskell]], [[Go (programming language)|Go]], [[Objective-C]], [[OpenCL|OpenCL C]], [[Modula-2]], [[Pascal (programming language)|Pascal]], [[Rust (programming language)|Rust]],<ref>{{cite web |title=GDB Documentation - Supported Languages |url=http://sourceware.org/gdb/current/onlinedocs/gdb/Supported-Languages.html#Supported-Languages |access-date=2011-11-28 |archive-date=2017-12-28 |archive-url=https://web.archive.org/web/20171228100403/https://sourceware.org/gdb/current/onlinedocs/gdb/Supported-Languages.html#Supported-Languages |url-status=dead }}</ref> and partially others.<ref>{{cite web |title=GDB Documentation - Summary |url=http://sourceware.org/gdb/current/onlinedocs/gdb/Summary.html#Summary |access-date=2011-11-28 |archive-date=2012-07-01 |archive-url=https://web.archive.org/web/20120701062129/http://sourceware.org/gdb/current/onlinedocs/gdb/Summary.html#Summary |url-status=dead }}</ref> It detects problems in a program while letting it run and allows users to examine different registers. ==History== GDB was first written by [[Richard Stallman]] in 1986 as part of his [[GNU]] system, after his [[GNU Emacs]] was "reasonably stable".<ref name="stallman-quote-1986">{{cite web|title=Richard Stallman lecture at the Royal Institute of Technology, Sweden (1986-10-30)|url=https://www.gnu.org/philosophy/stallman-kth.html|access-date=2006-09-21|quote=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.}}</ref> GDB is [[free software]] released under the [[GNU General Public License]] (GPL). It was modeled after the [[dbx (debugger)|DBX]] debugger, which came with [[BSD|Berkeley Unix]] distributions.<ref name="stallman-quote-1986" /> From 1990 to 1993 it was maintained by [[John Gilmore (activist)|John Gilmore]].<ref>{{Cite web|title=John Gilmore (activist)|url=https://hyperleap.com/topic/John_Gilmore_(activist)|website=hyperleap.com|access-date=2020-10-13|archive-date=2021-02-26|archive-url=https://web.archive.org/web/20210226173853/https://hyperleap.com/topic/John_Gilmore_(activist)|url-status=dead}}</ref> Now it is maintained by the GDB Steering Committee which is appointed by the [[Free Software Foundation]].<ref>{{cite web|title=GDB Steering Committee|url=https://www.gnu.org/software/gdb/committee/|access-date=2008-05-11}}</ref> ==Technical details== ===Features=== GDB offers extensive facilities for tracing and altering the execution of [[computer program]]s. The user can monitor and modify the values of programs' internal [[variable (programming)|variables]], and even call [[subroutine|functions]] independently of the program's normal behavior. GDB target processors (as of 2003){{needs update|date=March 2025}} include: [[DEC Alpha|Alpha]], [[ARM architecture family|ARM]], [[Atmel AVR|AVR]], [[Hitachi H8|H8/300]], Altera [[Nios (computer processor)|Nios]]/[[Nios II]], [[System/370]], [[System 390]], [[x86]] and its 64-bit extension [[x86-64]], [[Itanium|IA-64]] "Itanium", [[Motorola 68000]], [[MIPS architecture|MIPS]], [[PA-RISC]], [[PowerPC]], [[RISC-V]], [[SuperH]], [[SPARC]], and [[VAX]]. Lesser-known target processors supported in the standard release have included [[A29K]], [[ARC (processor)|ARC]], [[ETRAX CRIS]], [[D10V]], [[D30V]], [[FR-30]], [[FR-V]], [[Intel i960]], [[Motorola 68HC11|68HC11]], [[Motorola 88000]], [[MCORE]], [[MN10200]], [[MN10300]], [[320xx microprocessor|NS32K]], [[Stormy16]], and [[Zilog Z8000|Z8000]]. (Newer releases will likely not support some of these.) GDB has compiled-in [[Instruction set simulator|simulators]] for even lesser-known target processors such like [[M32R]] or [[V850]].<ref>{{cite web | title = GDB Documentation - Summary - Contributors | url = http://sourceware.org/gdb/current/onlinedocs/gdb/Contributors.html#Contributors | access-date = 2011-12-01 | archive-date = 2011-09-29 | archive-url = https://web.archive.org/web/20110929153415/http://sourceware.org/gdb/current/onlinedocs/gdb/Contributors.html#Contributors | url-status = dead }}</ref> GDB is still actively being developed. As of version 7.0 new features include support for [[Python (programming language)|Python]] scripting<ref>{{cite web | title = GDB 7.0 Release Notes | url = http://sourceware.org/gdb/wiki/GDB_7.0_Release | access-date = 2011-11-28 }}</ref> and as of version 7.8 [[GNU Guile]] scripting as well.<ref>{{cite web |url=https://sourceware.org/ml/gdb/2014-07/msg00032.html |title=GDB 7.8 released! |author=Joel Brobecker |date=2014-07-29 |access-date=2014-07-30}}</ref> 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.<ref>{{cite web | title = Reverse Debugging with GDB | url = https://www.gnu.org/software/gdb/news/reversible.html | access-date = 2014-01-20 }}</ref> ===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.<ref>{{cite web|url=http://www.embecosm.com/appnotes/ean4/embecosm-howto-rsp-server-ean4-issue-2.pdf|title=Howto: GDB Remote Serial Protocol: Writing a RSP Server}}</ref> 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.<ref>{{cite web|url=http://davis.lbl.gov/Manuals/GDB/gdb_17.html#SEC140|title=Implementing a remote stub}}</ref> 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).<ref>{{cite web|url=http://www.freebsd.org/doc/en/books/developers-handbook/kerneldebug-dcons.html|title=Kernel debugging with Dcons}}</ref> ===Graphical user interface=== The debugger does not contain its own [[graphical user interface]], and defaults to a [[command-line interface]], although it does contain a [[Text User Interface|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 [https://gede.dexar.se/], Seer [https://github.com/epasveer/seer], and HP Wildebeest Debugger GUI (WDB GUI). [[Integrated development environment|IDE]]s such as [[Codelite]], [[Code::Blocks]], [[Dev-C++]], [[Geany]], GNAT Programming Studio (GPS), [[KDevelop]], [[Qt Creator]], [[Lazarus (IDE)|Lazarus]], [[MonoDevelop]], [[List of Eclipse projects#Tools projects|Eclipse]], [[NetBeans]], and [[Visual Studio]] can interface with GDB. [[GNU Emacs]] has a "GUD mode" and tools for [[Vim (text editor)|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 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. {{aligned table|class=wikitable|cols=2|col1header=y|col1align=left|row1header=y | Common gdb commands | Corresponding ptrace calls | <syntaxhighlight lang="console" inline>(gdb) start</syntaxhighlight> | {{mono|PTRACE_TRACEME}} β makes parent a tracer (called by a tracee) | <syntaxhighlight lang="console" inline>(gdb) attach PID</syntaxhighlight> | {{mono|PTRACE_ATTACH}} β attach to a running process | <syntaxhighlight lang="console" inline>(gdb) stop</syntaxhighlight> | {{code|kill(child_pid, SIGSTOP)}} (or {{mono|PTRACE_INTERRUPT}}) | <syntaxhighlight lang="console" inline>(gdb) continue</syntaxhighlight> | {{mono|PTRACE_CONT}} | <syntaxhighlight lang="console" inline>(gdb) info registers</syntaxhighlight> | {{mono|PTRACE_GET(FP)REGS(ET)}} and {{mono|PTRACE_SET(FP)REGS(ET)}} | <syntaxhighlight lang="console" inline>(gdb) x</syntaxhighlight> | {{mono|PTRACE_PEEKTEXT}} and {{mono|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== {| class="wikitable" ! style="text-align:left" | <syntaxhighlight lang="console" inline>$ gdb program</syntaxhighlight> | Debug "program" (from the shell) |- ! style="text-align:left" | <syntaxhighlight lang="console" inline>(gdb) run -v</syntaxhighlight> | Run the loaded program with the parameters |- ! style="text-align:left" | <syntaxhighlight lang="console" inline>(gdb) bt </syntaxhighlight> | Backtrace (in case the program crashed) |- ! style="text-align:left" | <syntaxhighlight lang="console" inline>(gdb) info registers</syntaxhighlight> | Dump all registers |- ! style="text-align:left" | <syntaxhighlight lang="console" inline>(gdb) disas $pc-32, $pc+32</syntaxhighlight> | Disassemble |} ==An example session== Consider the following source-code written in [[C (programming language)|C]]: <syntaxhighlight lang="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); } </syntaxhighlight> Using the [[GNU Compiler Collection|GCC]] compiler on [[Linux]], the code above must be compiled using the <code>-g</code> 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 <code>example.c</code>, the command for the [[Compilation (programming)|compilation]] could be: <syntaxhighlight lang="console"> $ gcc example.c -Og -g -o example </syntaxhighlight> And the binary can now be run: <syntaxhighlight lang="console"> $ ./example Segmentation fault </syntaxhighlight> Since the example code, when executed, generates a [[segmentation fault]], GDB can be used to inspect the problem. <syntaxhighlight lang="console"> $ 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 </syntaxhighlight> The problem is present in line 7, and occurs when calling the function <code>[[Strlen#strlen|strlen]]</code> (because its argument, <code>s</code>, is <code>[[Null pointer#Null pointer|NULL]]</code>). Depending on the implementation of strlen ([[Inline function|inline]] or not), the output can be different, e.g.: <syntaxhighlight lang="console"> 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 </syntaxhighlight> To fix the problem, the variable <code>a</code> (in the function <code>main</code>) must contain a valid string. Here is a fixed version of the code: <syntaxhighlight lang="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 = "This is a test string"; printf("size of a = %lu\n", foo_len(a)); exit(0); } </syntaxhighlight> Recompiling and running the executable again inside GDB now gives a correct result: <syntaxhighlight lang="console"> $ 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] </syntaxhighlight> GDB prints the output of <code>printf</code> in the screen, and then informs the user that the program exited normally. ==See also== {{Portal|Free and open-source software|Computer programming}} *[[Binary File Descriptor library]] (libbfd) *[[dbx (debugger)|dbx]] *[[Data Display Debugger|DDD]], a [[Graphical user interface|GUI]] for GDB and other [[debugger]]s *[[gdbserver]] *[[LLDB (debugger)|LLDB]] {{Clear}} ==References== {{Reflist|30em}} ==External links== *{{Official website|https://www.gnu.org/software/gdb}} *[http://www.ultragdb.com/ UltraGDB: Visual C/C++ Debugging with GDB on Windows and Linux] {{Webarchive|url=https://web.archive.org/web/20171212091410/http://www.ultragdb.com/ |date=2017-12-12 }} *[http://linsyssoft.com/product_kgdb.php KGDB: Linux Kernel Source Level Debugger] *[https://web.archive.org/web/20100829051021/http://kldp.net/projects/mygdb/ The website for "MyGDB: GDB Frontend" in the Korean language] *[https://visualgdb.com/ A Visual Studio plugin for debugging with GDB] *[https://www.drdobbs.com/testing/13-linux-debuggers-for-c-reviewed/240156817 Comparison of GDB front-ends, 2013] *[https://web.archive.org/web/20141022045312/https://developer.palm.com/content/api/dev-guide/pdk/eclipse-gdb-debugging.html Using Eclipse as a Front-End to the GDB Debugger] ===Documentation=== *[[Richard Stallman|Richard M. Stallman]], [[Roland Pesch]], [[Stan Shebs]], et al., [http://sourceware.org/gdb/current/onlinedocs/gdb.html ''Debugging with GDB''] ([[Free Software Foundation]], 2011) {{ISBN|978-0-9831592-3-0}} *[https://web.archive.org/web/20070303045900/http://sourceware.org/gdb/current/onlinedocs/gdbint.html GDB Internals] ===Tutorials=== *[http://www.unknownroad.com/rtfm/gdbtut/gdbtoc.html ''RMS's gdb Tutorial''] (Ryan Michael Schmidt, not [[Richard Matthew Stallman]]) *[https://www.gdb-tutorial.net ''GDB Tutorial''] {{GNU}} {{Authority control}} {{DEFAULTSORT:Gnu Debugger}} [[Category:Debuggers]] [[Category:GNU Project software|Debugger]] [[Category:Unix programming tools]] [[Category:Video game development software for Linux]] [[Category:Software that was rewritten in C++]] [[Category:Articles with example C code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Aligned table
(
edit
)
Template:Authority control
(
edit
)
Template:Cite web
(
edit
)
Template:Clear
(
edit
)
Template:GNU
(
edit
)
Template:ISBN
(
edit
)
Template:Infobox software
(
edit
)
Template:Needs update
(
edit
)
Template:Official website
(
edit
)
Template:Portal
(
edit
)
Template:Redirect
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Webarchive
(
edit
)