Last Updated : 12 Jul, 2024
Improve
GDB stands for GNU Project Debugger and is a powerful debugging tool for C (along with other languages like C++). It helps you to poke around inside your C programs while they are executing and also allows you to see what exactly happens when your program crashes. GDB operates on executable files which are binary files produced by the compilation process.
For demo purposes, the example below is executed on a Linux machine with the below specs.
uname -a
uname -a
Let’s learn by doing: –
Start GDB
Go to your Linux command prompt and type “gdb”.
gdb
gdb
Gdb open prompt lets you know that it is ready for commands. To exit out of gdb, type quit or q.
To quit
Compile the code
Below is a program that shows undefined behavior when compiled using C99. Note: If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate, where the indeterminate value is either an unspecified value or a trap representation. Now compile the code. (here test.c). g flag means you can see the proper names of variables and functions in your stack frames, get line numbers and see the source as you step around in the executable. -std=C99 flag implies use standard C99 to compile the code. -o flag writes the build output to an output file.
gcc -std=c99 -g -o test test.C
gcc -std=c99 -g -o test test.C
Run GDB with the generated executable
Type the following command to start GDB with the compiled executable.
gdb ./test
gdb ./test
Useful GDB commands:
Here are a few useful commands to get started with GDB.
Command | Description |
---|---|
run or r | Executes the program from start to end. |
break or b | Sets a breakpoint on a particular line. |
disable | Disables a breakpoint |
enable | Enables a disabled breakpoint. |
next or n | Executes the next line of code without diving into functions. |
step | Goes to the next instruction, diving into the function. |
list or l | Displays the code. |
print or p | Displays the value of a variable. |
quit or q | Exits out of GDB. |
clear | Clears all breakpoints. |
continue | Continues normal execution |
Display the code
Now, type “l” at gdb prompt to display the code.
Display the code
Set a breakpoint
Let’s introduce a break point, say line 5.
Set a breakpoint
If you want to put breakpoint at different lines, you can type “b line_number“.By default “list or l” display only first 10 lines.
View breakpoints
In order to see the breakpoints, type “info b”.
View breakpoints
Disable a breakpoint
Having done the above, let’s say you changed your mind and you want to revert. Type “disable b”.
Disable a breakpoint
Re-enable a disabled breakpoint
As marked in the blue circle, Enb becomes n for disabled. 9. To re-enable the recent disabled breakpoint. Type “enable b”.
Re-enable a disabled breakpoint
Run the code
Run the code by typing “run or r”.If you haven’t set any breakpoints, the run command will simply execute the full program.
Run the code
Print variable values
To see the value of variable, type “print variable_name or p variable_name“.
Print variable values
The above shows the values stored at x at time of execution.
Change variable values
To change the value of variable in gdb and continue execution with changed value, type “set variable_name“.
Debugging output
Below screenshot shows the values of variables from which it’s quite understandable the reason why we got a garbage value as output. At every execution of ./test we will be receiving a different output.
Exercise: Try using set x = 0 in gdb at first run and see the output of c.
Debugging output
GDB offers many more ways to debug and understand your code like examining stack, memory, threads, manipulating the program, etc. I hope the above example helps you get started with gdb.
Conclusion
In this article we have discussed GDB (GNU Debugger) which is a powerful tool in Linux used for debugging C programs. We have discussed some of the following steps so that we can compile your code with debugging information, run GDB, set breakpoint, examine variables, and analyze program behavior. We have also discussed GDB’s features, such as code examination, breakpoint management, variable manipulation, and program execution control which allow us to efficiently debug and issue resolution.
GDB (Step by Step Introduction) – FAQs
How to go step by step in GDB?
To step through a program in GDB, you can use the following commands:
step
(ors
): Step into functions.next
(orn
): Step over functions.continue
(orc
): Continue execution until the next breakpoint.Example:
gdb ./myprogram
(gdb) break main
(gdb) run
(gdb) step
(gdb) next
What is the overview of GDB?
GDB (GNU Debugger) is a powerful debugging tool for C, C++, and other programming languages. It allows developers to see what is happening inside their programs while they are running or what the program was doing at the moment it crashed. GDB provides features like breakpoints, stepping through code, inspecting variables, and changing program execution flow.
How to start a process in GDB?
To start a process in GDB, follow these steps:
- Compile your program with the
-g
flag to include debugging information.- Start GDB with your program.
- Run the program inside GDB.
gcc -g -o myprogram myprogram.c
gdb ./myprogram
(gdb) run
How to program with GDB?
To debug a program with GDB:
- Compile the program with debugging information.
- Start GDB.
- Set breakpoints, run the program, and use debugging commands.
gcc -g -o myprogram myprogram.c
gdb ./myprogram
(gdb) break main
(gdb) run
(gdb) step
(gdb) next
(gdb) print variable
How to generate core file in GDB?
To generate a core file in GDB:
- Enable core dumps in your shell session.
- Run your program until it crashes.
- A core file (
core
orcore.<pid>
) will be generated in the current directory.- Load the core file into GDB for analysis.
ulimit -c unlimited
./myprogram
gdb ./myprogram core
thelittleguy046
Improve
Next Article
The Ultimate Beginner's Guide For DSA