Jump to content

GNU Debugger

From Wikipedia, the free encyclopedia
(Redirected from Gdb)

Template:Short description Template:Redirect {{#invoke:Infobox|infobox}}Template:Template other {{#invoke:Check for unknown parameters|check|unknown=Template:Main other|preview=Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y| AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo class | logo size | logo title | logo upright | logo_alt | logo_caption | logo_class | logo_size | logo_title | logo_upright | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | qid | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot class | screenshot size | screenshot title | screenshot upright | screenshot_alt | screenshot_class | screenshot_size | screenshot_upright | service_name | size | standard | title | ver layout | website }}Template:Main other{{#invoke:Check for conflicting parameters|check | template = Template:Infobox software | cat = Template:Main other | logo size; logo_size | logo class; logo_class | logo alt; logo_alt | logo title; logo_title | logo caption; logo_caption | screenshot size; screenshot_size | screenshot upright; screenshot_upright | screenshot class; screenshot_class | screenshot alt; screenshot_alt | screenshot title; screenshot title | latest release version; latest_release_version | latest release date; latest_release_date | latest preview version; latest_preview_version | latest preview date; latest_preview_date | latest release version; latest_release_version | latest release date; latest_release_date | latest preview version; latest_preview_version | latest preview date; latest_preview_date | programming language; programming_language | operating system; operating_system }}

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,[1] and partially others.[2] 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".[3] 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.[3]

From 1990 to 1993 it was maintained by John Gilmore.[4] Now it is maintained by the GDB Steering Committee which is appointed by the Free Software Foundation.[5]

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 (Template:As of)Template:Update inline 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.[6]

Stepping through code

Both the Template:Kbd and Template:Kbd command can be used to advance execution over the next n statements. If Template:Var is omitted it defaults to 1. The difference between the commands is that Template:Mono will follow the flow of execution into the internals of any function call whereas Template:Mono will execute the whole function and proceed to the next statement within the current routine.[7]

The Template:Kbd 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.[8]

Printing values and expressions

When a program is halted in mid execution the Template:Kbd (abbreviated as Template:Kbd) command can be used to display the value of a variable or an expression using C or C++ syntax. The Template:Kbd 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 Template:Kbd allows one to specify the number of bytes.

These commands can be used like this: Template:Pre

Additionally, the Template:Kbd command can invoke both library and user written functions and it display the returned value.[9]

Values displayed are automatically assigned to special value history variables which begin with a Template:Char sign followed by a sequence number which can then be redisplayed using Template:Kbd,[9]Template:Rp[10] e.g.:Template:Pre One can also use the Template:Kbd command to create convenience variables for use during a GDB session,[11] e.g.;Template:Pre If the argument of Template:Kbd is an array or struct all elements will be output. The following syntax can be used to show a sub range of the array:[9]Template:RpTemplate:Pre

Breakpoints and watchpoints

Breakpoints and watchpoints are used when one needs to examine a program prior to a known situation where things are likely to go wrong or once things have gone wrong. Both break- and watchpoints issued integer identification numbers, 1, 2, 3... which can be used to Template:Mono, Template:Mono or Template:Mono them. The command Template:Kbd displays all break-, watch-, trace- and catchpoints and their current status, and Template:Kbd displays all watchpoints and their current status.

Breakpoints are set to halt the flow of execution either on specific line numbers in one's code or on entry to a function when run within the debugger, e.g. Template:Kbd will stop on line 42 of the specified file. If the file name is omitted the reference is to the current file.

A conditional breakpoint halts on a specified line when a specified expression is true, e.g.:

Template:Pre

The Template:Kbd command allows one to add conditions to an already established breakpoint.[12]

Watchpoints halt the flow of execution when the value of a variable or an expression changes, irrespective of where in the program it occurs. By default, where possible, GDB monitors the memory location where the change takes place, a useful feature given that multiple pointers may refer to the same address. Conversely a software watchpoint, which are slower, track just the variables.

The Template:Mono and Template:Mono commands will halt the program whenever the memory location or variable is read.[13]

Another conditional feature of both watches and breakpoints is the command Template:Kbd which disregards the halting criteria until the execution has passed the specified point Template:Var many times.[14]

The Template:Mono command primes GDB to automatically output the value of an expression each time it stops. Multiple Template:Mono commands are cumulative and the output can be formatted using the same flags available to the Template:Mono command.[9]Template:Rp

Framestacks

When a program is halted it may be several layers deep in function calls.  Each level of function call is called a frame and the collection of frames is called a frame stack.  When execution is halted one can navigate up and down to a specific frame in order to examine the value of variables or expressions at a particular level. The Template:Kbd command will provide a list of all of the frames in the stack. The Template:Kbd command displays all of the arguments passed to the current frame and the Template:Kbd command displays a list of the variables available to it along with their values.[15]

Scripting support

GDB includes the ability to define command routines that can be used to automate frequently repeated sets of GDB instructions. These consisting of GDB commands placed between Template:Mono and Template:AnchorTemplate:Mono statements. Parameters to these routines are not declared by name, but are passed in special variables Template:Mono, Template:Mono, Template:Mono... with the special variable Template:Mono representing the number of command line arguments.

Additionally GDB includes Template:Mono/Template:Mono and Template:Mono blocks terminated by Template:Mono statements as well as Template:Mono and Template:Mono statements to manage flow of control,[16] e.g.:

Template:Pre

If one is for example debugging a C program with a generic linked list with a leading value and a Template:Mono field pointing to the next element, the following command would use a Template:Mono loop to display all the elements:[17]

Template:Pre

Command definitions placed in the local file .gdbinit are automatically loaded at the beginning of the GDB session. Command definitions can also be saved in ordinary files and loaded using the Template:Mono command.

As of version 7.0, new features include support for Python scripting[18] and as of version 7.8 GNU Guile scripting as well which is based on the Scheme programming language.[19]

Reversible debugging

Version 7.0 added support for "reversible debugging", which allows a debugging session to step backward, much like rewinding a crashed program to see what happened. The feature is highly memory intensive and slows execution with a default limit of 20,000 instructions. The recommended procedure is to set breakpoints before and after the suspected problem, then issue the Template:Mono command when the program stops at the first one. Continue execution to the second breakpoint. The Template:Mono, Template:Mono and Template:Mono commands can then be used to backtrack execution, undoing changes in variables piecemeal. However reverse debugging does not undo actions such as console output nor does it reissue external events such as interrupts or incoming network packets.Template:R[20]

Remote debugging

GDB offers a "remote" mode often used when debugging embedded systems. In remote operation GDB runs on one machine and the program being debugged on another. GDB can communicate to the remote "stub" that understands the GDB protocol through a serial device or TCP/IP.[21] 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.[22] 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).[23]

Graphical user interface

GDB 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, Gede,[24] Seer,[25] 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 the system call 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 registers:[26] Template:Aligned tableA breakpoint is implemented by replacing an instruction at a given memory address with another special instruction. Executing a breakpoint instruction causes the signal SIGTRAP.

See also

Template:Portal

References

Template:Reflist

Documentation

Tutorials

Template:GNU Template:RISC-V Template:Authority control

  1. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  2. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  3. ↑ 3.0 3.1 {{#invoke:citation/CS1|citation |CitationClass=web }}
  4. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  5. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  6. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  7. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  8. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  9. ↑ 9.0 9.1 9.2 9.3 Template:Cite book
  10. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  11. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  12. ↑ Template:Cite book
  13. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  14. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  15. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  16. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  17. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  18. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  19. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  20. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  21. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  22. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  23. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  24. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  25. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  26. ↑ Template:Cite book