ArticleslgStudy

computer science

GNU Debugger

GNU Debugger is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand GNU Debugger rather than just read about it. In short: 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, and partially others. It detects problems in a program while letting it run and allows users to examine program variables and machine registers.

GNU Debugger — main illustration
GNU Debugger — illustration

Key takeaways

  • GNU Debugger belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect GNU Debugger to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of GNU Debugger from memory before moving on to harder problems.

Reference excerpt

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, and partially others. 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". 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. 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.

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 (as of 2003) 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.

Stepping through code Both the next n and step n command can be used to advance execution over the next n statements. If n is omitted it defaults to 1. The difference between the commands is that step will follow the flow of execution into the internals of any function call whereas next will execute the whole function and proceed to the next statement within the current routine. The jump location 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.

Printing values and expressions When a program is halted in mid execution the print (abbreviated as p) command can be used to display the value of a variable or an expression using C or C++ syntax. The x 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 x allows one to specify the number of bytes. These commands can be used like this:

print /f myVar # Prints a double precision floating point number x /8bx &foo # Print 8 bytes in hex format starting at the memory location of foo

Additionally, the call command can invoke both library and user written functions and it display the returned value.

Values displayed are automatically assigned to special value history variables which begin with a $ sign followed by a sequence number which can then be redisplayed using print, e.g.:call getpid() $1 = 23995 One can also use the set command to create convenience variables for use during a GDB session, e.g.;set $foo=i+42 If the argument of print is an array or struct all elements will be output. The following syntax can be used to show a sub range of the array:# show 10 elements of the array starting at myArray[4] print myArray[10]@10

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 enable, disable or delete them. The command info break displays all break-, watch-, trace- and catchpoints and their current status, and info watch 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. break sourcefile:42 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.:

break sourcefile:275 if productNum=1275 The condition breakNum expression command allows one to add conditions to an already established breakpoint. 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 rwatch and awatch commands will halt the program whenever the memory location or variable is read. Another conditional feature of both watches and breakpoints is the command ignore breakNum count which disregards the halting criteria until the execution has passed the specified point count many times. The display command primes GDB to automatically output the value of an expression each time it stops. Multiple display commands are cumulative and the output can be formatted using the same flags available to the x command.

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 backtrace command will provide a list of all of the frames in the stack. The info args command displays all of the arguments passed to the current frame and the info local command displays a list of the variables available to it along with their values.

… excerpt ends here. Continue reading the full article.

Illustrations

GNU Debugger illustration
GNU Debugger illustration

Worked examples

Example 1 — a first encounter with GNU Debugger

Start with the simplest possible case. Write down what GNU Debugger claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to GNU Debugger before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about GNU Debugger ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of GNU Debugger

In research
GNU Debugger appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses GNU Debugger in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
GNU Debugger is common in secondary-school and first-year university syllabi. It links to neighbouring topics Debuggers, Free software programmed in C, GNU Project software, so understanding it makes those chapters shorter.
In everyday life
Look for GNU Debugger outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “GNU Debugger” →

How to study GNU Debugger in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what GNU Debugger means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain GNU Debugger out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is GNU Debugger in simple terms?

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, and partially others. It detects problems in a program while letting i…

Why does GNU Debugger matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study GNU Debugger?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on GNU Debugger.

Tags

  • Debuggers
  • Free software programmed in C
  • GNU Project software
  • Software that was rewritten in C++
  • Software using the GNU General Public License
  • Unix programming tools
  • Video game development software for Linux

Keep exploring