-
Notifications
You must be signed in to change notification settings - Fork 139
GDB
This page is maintained by Ivan ...
Full gdb manual.
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:
- By using debugging programs which includes user interface for step-by-step program running e.g. GNU Debugger.
- 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.
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)
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'
//If it works you will instantly appear in (gdb) console.
GDB is a command line, it accepts its own commands. In this particular Wiki we will discuss most important commands to understand what is going on there.
This part of commands will help you to use GDB at the most primitive level.
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.
Breakpoint is a point in your program where GNU Debugger stops the program and then you can receive all current variables at this stage.
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.
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.
But sometimes we need to track some variable like my_object.my_field, GDB can`t set up a watchpoint for this one, but you can set up a watchpoint for its adress. Use
print &(my_object.my_field)
to find it out and then use
watch *((Type*)(&(my_object.my_field)))
to set it up.
//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.
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 'event'
setting a breakpoint when the program responds actively to an event. The event 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 LibName.
-
If you want to catch all exceptions you must only type in GDB console:
catch catch
But if you want to catch particular exception you must type:
catch catch 'exception'
It concerns throw
too.
// You can use info break
to list all
situations handled by the program.
// Currently, there are some limitations in the use of "catch". Be attentive. Look Full GDB manual at the top of the page if it is essential.
Also you can use tcatch
which works like tbreak
.
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. -
backtrace 'n'/'-n'
Print a backtrace of the n first frames(use -n instead n to print n last frames)
//You can stop the backtrace at any time by typing the system interrupt character, normally Ctrl-C.
Each line in the backtrace shows the frame number and the function name. The program counter value is also shown unless you use set print address off. The backtrace also shows the source file name and line number, as well as the arguments to the function. The program counter value is omitted if it is at the beginning of the code for that line number. Here is an example of a backtrace. It was made with the command ‘bt 3’, so it shows the innermost three frames.
#0 m4_traceon (obs=0x24eb0, argc=1, argv=0x2b8c8)
at builtin.c:993
#1 0x6e38 in expand_macro (sym=0x2b600, data=...) at macro.c:242
#2 0x6840 in expand_token (obs=0x0, t=177664, td=0xf7fffb08)
at macro.c:71
(More stack frames follow...)
MIPT-V / MIPT-MIPS — Cycle-accurate pre-silicon simulation.