Skip to content
Pavel I. Kryukov edited this page Oct 10, 2018 · 21 revisions

This page is maintained by Ivan ...


Definitions

Debugging is the process of finding and resolving defects or problems within a computer program that prevent correct operation of computer software or a system.

Two tactics of debugging exists:

  1. By using debugging programs which includes user interface for step-by-step program running e.g. GNU Debugger.
  2. Output of the current state of the program using output statements located at critical points of the program on the monitor.

The GNU Project Debugger (GDB) is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, C, C++, Objective-C, Free Pascal, Fortran, Go, Java and partially others.

How to build a program to be used by GDB?

If you are using Linux, you probably already have GDB. You only have to follow the guide below. But if you are using Windows, you will need to use this guide on YouTube to install and this one to build program to be used by GDB there.

1. Open console. (Ctrl + Alt + T for Linux)

2. Open directory with your program file. (Use command "ls -al" to view your directory list. Use "cd 'path'")

3. Compile your program with "-g" key in order to include information about debugging into your running file. Write in console "gcc -g 'NameOfFile.c' -o 'NameOfRunFile'".

// GNU Debugger is usually used to debug C or C++ files.

4. And then you can upload your program into GDB. Write in console "gdb 'NameOfRunFile'" for Linux. If it works you will instantly appear in (gdb) console.

Working with GDB

_I expect some words about the general interface here, like "GDB is a command line, it accepts its own commands etc."

Main commands

You can use only one symbol pointed in parenthesis too. These are synonyms.

  • run (r) - starts running the program till the first breakpoint or error e.g. Floating point exception or Segmentation fault.
  • run <'Input' >'Output' - redirecting input/output.
  • continue (c) - continue running the program until the next stop or error point.
  • step (s)/step (s) n - execute the following line/ n lines of program source text (even if this line is inside the called function) and stop.
  • next (n) n - execute the following line/ n lines of the program source code (the lines of the called functions are not included in the number n and there is no stop in them) and stop.
  • finish - continue running current program till the returning, display returned value.

Breakpoints

Breakpoint is a point in your program where GNU Debugger stops the program and then you can receive all current variables at this stage.

How to set up a breakpoint?

Type in (gdb) console:

break 'NumberOfLine' (b) to set up the breakpoint at any line in your code.

break 'NameOfFunction' to set up the breakpoint at any function in your code you want.

and

clear 'NumberOfLine' to delete the breakpoint at any line in your code.

clear 'NameOfFunction' to delete the breakpoint at any function in your code you want.

It means that command "break" sets up the breakpoint for permanent using.

In this regard, there is another console command "tbreak" with the same usage but it sets up the breakpoint only once.

break 'line' if 'cond' Set a conditional breakpoint. Execution stops on this line or function if the cond condition is met. Example: break 120 if a==15.

delete 'interval' - removing all points in pointed interval.

disable 'interval' - disabling all points in pointed interval.

enable 'interval' - enable breakpoints from this interval.

enable once 'interval' - breakpoints from this interval are enabled until the first operation, and then disabled.

enable delete 'interval' - breakpoints from this interval are included before the first operation, and then removed.

condition n 'cond' - Turning a breakpoint with number n into a conditional breakpoint with a condition cond.

condition n - Turning a breakpoint with number n into an unconditional breakpoint.

info break - Display information about all available breakpoints.

Watchpoints: how to track what writes to a variable?

If you want to track some variable you must be in position where this variable is defined and enter in console:

watch 'expr' - set up a Watchpoint for expression expr. Watchpoint is a modified breakpoint.

//GDB will interrupt the program when expr changes the value.

rwatch expr - Set a watchpoint that will break when the value of expr is read by the program.

awatch expr - Set a watchpoint that will break when expr is either read from or written into by the program.

info watchpoints - This command prints a list of watchpoints, using the same format as info break

Also you can use:

display 'expr' - add expression expr to automatically displaying list. Each one expression receiving his own number.

//The expression will display after each program stopping.

delete display n - delete the expression with number n from automatically displaying list.

disable display n - Disable displaying element with number n from the list, but not delete.

enable display n - To enable the display element n from the list that were previously disabled.

display - Print the values of all automatically displayed expressions, as it happens when the program stops.

info display - Display a list of automatically displayed expressions.

How to catch exceptions?

You can use GDB to investigate the cause the occurrence of an exceptional situation in your program and list exceptional situations prepared by your program for processing at this point in time.

catch 'exceptions' setting a breakpoint when the program responds actively to an exception.

// exceptions - list of identified exceptional situations.

// The exception can be one of following:

throw - Throwing a C++ exception.

catch - Catching a C++ exception.

exec - call the exec command.

fork - call the fork command.

vfork - call the vfork command.

load 'LibName' - Dynamically load any shared library, or load a library LibName.

unload 'LibName' - Upload any dynamically loaded shared library, or upload a library name library.

// You can use 'info catch' or 'info exceptions' to list all exceptional situations handled by the program.

// Currently, there are some limitations in the use of "catch". Be attentive.

Also you can use tcatch which works like tbreak.

What useful information do backtraces contain?

The call chain provides information about how your program ended up where it is. It displays one line for each frame, starting with the currently running frame (frame 0), followed by the frame from which it was called (frame 1), and then up the stack.

You can use some commands below:

backtrace (bt) - Print a backtrace of the entire stack: one line per frame for all frames in the stack.

//You can stop the backtrace at any time by typing the system interrupt character, normally Ctrl-c.

Clone this wiki locally