GNU Debugger

Last updated
GNU Debugger
Developer(s) GNU Project
Initial release1986;38 years ago (1986)
Stable release
14.2 [1]   OOjs UI icon edit-ltr-progressive.svg / 3 March 2024
Repository
Written in C, C++, Python
Operating system Unix-like, Windows
Type Debugger
License GPLv3
Website www.gnu.org/software/gdb

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]

Contents

History

GDB was first written by Richard Stallman in 1986 as part of his GNU system, after his GNU Emacs was "reasonably stable". [4] 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. [4]

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 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.

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, IA-64 "Itanium", Motorola 68000, MIPS, PA-RISC, PowerPC, 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 even lesser-known target processors such like M32R or V850. [7]

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 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). [13]

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. 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, 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 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)startPTRACE_TRACEME – makes parent a tracer (called by a tracee)
(gdb)attach PIDPTRACE_ATTACH – attach to a running process
(gdb)stopkill(child_pid, SIGSTOP) (or PTRACE_INTERRUPT)
(gdb)continuePTRACE_CONT
(gdb)info registersPTRACE_GET(FP)REGS(ET) and PTRACE_SET(FP)REGS(ET)
(gdb)xPTRACE_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

$ gdbprogramDebug "program" (from the shell)
(gdb)run -vRun the loaded program with the parameters
(gdb)btBacktrace (in case the program crashed)
(gdb)info registersDump all registers
(gdb)disas $pc-32, $pc+32Disassemble

An example session

Consider the following source-code written in C:

#include<stdio.h>#include<stdlib.h>#include<string.h>size_tfoo_len(constchar*s){returnstrlen(s);}intmain(intargc,char*argv[]){constchar*a=NULL;printf("size of a = %lu\n",foo_len(a));exit(0);}

Using the 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:

$ gccexample.c-Og-g-oexample 

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)runStarting program: /path/exampleProgram received signal SIGSEGV, Segmentation fault.0x0000000000400527 in foo_len (s=0x0) at example.c:77   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.1Copyright (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)runStarting program: /tmp/gdb/exampleProgram received signal SIGSEGV, Segmentation fault.0xb7ee94f3 in strlen () from /lib/i686/cmov/libc.so.6(gdb)bt#00xb7ee94f3instrlen()from/lib/i686/cmov/libc.so.6 #10x08048435infoo_len(s=0x0)atexample.c:7 #20x0804845ainmain(argc=<optimizedout>,argv=<optimizedout>)atexample.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_tfoo_len(constchar*s){returnstrlen(s);}intmain(intargc,char*argv[]){constchar*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)runStarting program: /path/examplesize 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

Related Research Articles

<span class="mw-page-title-main">Free software</span> Software licensed to be freely used, modified and distributed

Free software, libre software, or libreware 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, not price; all users are legally free to do what they want with their copies of a free software regardless of how much is paid to obtain the program. Computer programs are deemed "free" if they give end-users ultimate control over the software and, subsequently, over their devices.

<span class="mw-page-title-main">GNU</span> Free software collection

GNU is an extensive collection of free software, which can be used as an operating system or can be used in parts with other operating systems. The use of the completed GNU tools led to the family of operating systems popularly known as Linux. Most of GNU is licensed under the GNU Project's own General Public License (GPL).

<span class="mw-page-title-main">GNU Lesser General Public License</span> Free-software license

The GNU Lesser General Public License (LGPL) is a free-software license published by the Free Software Foundation (FSF). The license allows developers and companies to use and integrate a software component released under the LGPL into their own software without being required by the terms of a strong copyleft license to release the source code of their own components. However, any developer who modifies an LGPL-covered component is required to make their modified version available under the same LGPL license. For proprietary software, code under the LGPL is usually used in the form of a shared library, so that there is a clear separation between the proprietary and LGPL components. The LGPL is primarily used for software libraries, although it is also used by some stand-alone applications.

<span class="mw-page-title-main">GNU Hurd</span> Operating system kernel designed as a replacement for Unix

GNU Hurd is a collection of microkernel servers written as part of GNU, for the GNU Mach microkernel. It has been under development since 1990 by the GNU Project of the Free Software Foundation, designed as a replacement for the Unix kernel, and released as free software under the GNU General Public License. When the Linux kernel proved to be a viable solution, development of GNU Hurd slowed, at times alternating between stasis and renewed activity and interest.

<span class="mw-page-title-main">GNU Project</span> Free software project

The GNU Project is a free software, mass collaboration project announced by Richard Stallman on September 27, 1983. Its goal is to give computer users freedom and control in their use of their computers and computing devices by collaboratively developing and publishing software that gives everyone the rights to freely run the software, copy and distribute it, study it, and modify it. GNU software grants these rights in its license.

In computing, a bus error is a fault raised by hardware, notifying an operating system (OS) that a process is trying to access memory that the CPU cannot physically address: an invalid address for the address bus, hence the name. In modern use on most architectures these are much rarer than segmentation faults, which occur primarily due to memory access violations: problems in the logical address or permissions.

The GNU coding standards are a set of rules and guidelines for writing programs that work consistently within the GNU system. The GNU Coding Standards were written by Richard Stallman and other GNU Project volunteers. The standards document is part of the GNU Project and is available from the GNU website. Though it focuses on writing free software for GNU in C, much of it can be applied more generally. In particular, the GNU Project encourages its contributors to always try to follow the standards—whether or not their programs are implemented in C.

In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.

Tivoization is the practice of designing hardware that incorporates software under the terms of a copyleft software license like the GNU General Public License, but uses hardware restrictions or digital rights management (DRM) to prevent users from running modified versions of the software on that hardware. Richard Stallman of the Free Software Foundation (FSF) coined the term in reference to TiVo's use of GNU GPL licensed software on the TiVo brand digital video recorders (DVR), which actively block modified software by design. Stallman believes this practice denies users some of the freedom that the GNU GPL was designed to protect. The FSF refers to tivoized hardware as "proprietary tyrants".

In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric Evans and Martin Fowler.

<span class="mw-page-title-main">History of free and open-source software</span>

In the 1950s and 1960s, computer operating software and compilers were delivered as a part of hardware purchases without separate fees. At the time, source code, the human-readable form of software, was generally distributed with the software providing the ability to fix bugs or add new functions. Universities were early adopters of computing technology. Many of the modifications developed by universities were openly shared, in keeping with the academic principles of sharing knowledge, and organizations sprung up to facilitate sharing. As large-scale operating systems matured, fewer organizations allowed modifications to the operating software, and eventually such operating systems were closed to modification. However, utilities and other added-function applications are still shared and new organizations have been formed to promote the sharing of software.

A kernel debugger is a debugger present in some operating system kernels to ease debugging and kernel development by the kernel developers. A kernel debugger might be a stub implementing low-level operations, with a full-blown debugger such as GNU Debugger (gdb), running on another machine, sending commands to the stub over a serial line or a network connection, or it might provide a command line that can be used directly on the machine being debugged.

License compatibility is a legal framework that allows for pieces of software with different software licenses to be distributed together. The need for such a framework arises because the different licenses can contain contradictory requirements, rendering it impossible to legally combine source code from separately-licensed software in order to create and publish a new program. Proprietary licenses are generally program-specific and incompatible; authors must negotiate to combine code. Copyleft licenses are commonly deliberately incompatible with proprietary licenses, in order to prevent copyleft software from being re-licensed under a proprietary license, turning it into proprietary software. Many copyleft licenses explicitly allow relicensing under some other copyleft licenses. Permissive licenses are compatible with everything, including proprietary licenses; there is thus no guarantee that all derived works will remain under a permissive license.

<span class="mw-page-title-main">Nemiver</span>

Nemiver is computer software, a graphical standalone debugger for the programming languages C and C++, which integrates in the GNOME desktop environment. It currently features a backend which uses the well known GNU Debugger (GDB). The creator and the current lead developer is Dodji Seketeli.

<span class="mw-page-title-main">Copyleft</span> Practice of mandating free use in all derivatives of a work

Copyleft is the legal technique of granting certain freedoms over copies of copyrighted works with the requirement that the same rights be preserved in derivative works. In this sense, freedoms refers to the use of the work for any purpose, and the ability to modify, copy, share, and redistribute the work, with or without a fee. Licenses which implement copyleft can be used to maintain copyright conditions for works ranging from computer software, to documents, art, scientific discoveries and even certain patents.

<span class="mw-page-title-main">GNU General Public License</span> Series of free software licenses

The GNU General Public License is a series of widely used free software licenses or copyleft that guarantee end users the four freedoms to run, study, share, and modify the software. The license was the first copyleft for general use and was originally written by Richard Stallman, the founder of the Free Software Foundation (FSF), for the GNU Project. The license grants the recipients of a computer program the rights of the Free Software Definition. These GPL series are all copyleft licenses, which means that any derivative work must be distributed under the same or equivalent license terms. It is more restrictive than the Lesser General Public License and even further distinct from the more widely used permissive software licenses BSD, MIT, and Apache.

Getopt is a C library function used to parse command-line options of the Unix/POSIX style. It is a part of the POSIX specification, and is universal to Unix-like systems. It is also the name of a Unix program for parsing command line arguments in shell scripts.

<span class="mw-page-title-main">LLDB (debugger)</span> Software debugger

The LLDB Debugger (LLDB) is the debugger component of the LLVM project. It is built as a set of reusable components which extensively use existing libraries from LLVM, such as the Clang expression parser and LLVM disassembler. LLDB is free and open-source software under the University of Illinois/NCSA Open Source License, a BSD-style permissive software license. Since v9.0.0, it was relicensed to the Apache License 2.0 with LLVM Exceptions.

Bionic is an implementation of the standard C library, developed by Google for its Android operating system. It differs from the GNU C Library (glibc) in being designed for devices with less memory and processor power than a typical Linux system. It is a combination of new code and code from FreeBSD, NetBSD, and OpenBSD released under a BSD license, rather than glibc, which uses the GNU Lesser General Public License. This difference was important in the early days of Android, when static linking was common, and since bionic has its own ABI, it can't be replaced by a different libc without breaking all existing apps.

A code sanitizer is a programming tool that detects bugs in the form of undefined or suspicious behavior by a compiler inserting instrumentation code at runtime. The class of tools was first introduced by Google's AddressSanitizer of 2012, which uses directly mapped shadow memory to detect memory corruption such as buffer overflows or accesses to a dangling pointer (use-after-free).

References

  1. Joël Brobecker (3 March 2024). "GDB 14.2 released!" . Retrieved 3 March 2024.
  2. "GDB Documentation - Supported Languages". Archived from the original on 2017-12-28. Retrieved 2011-11-28.
  3. "GDB Documentation - Summary". Archived from the original on 2012-07-01. Retrieved 2011-11-28.
  4. 1 2 "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.
  5. "John Gilmore (activist)". hyperleap.com.
  6. "GDB Steering Committee" . Retrieved 2008-05-11.
  7. "GDB Documentation - Summary - Contributors". Archived from the original on 2011-09-29. Retrieved 2011-12-01.
  8. "GDB 7.0 Release Notes" . Retrieved 2011-11-28.
  9. Joel Brobecker (2014-07-29). "GDB 7.8 released!" . Retrieved 2014-07-30.
  10. "Reverse Debugging with GDB" . Retrieved 2014-01-20.
  11. "Howto: GDB Remote Serial Protocol: Writing a RSP Server" (PDF).
  12. "Implementing a remote stub".
  13. "Kernel debugging with Dcons".

Documentation

Tutorials