Skip to content
Ivan edited this page Oct 9, 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.

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.

What is a breakpoint?

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:

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.

Clone this wiki locally