Using IDEs Debug mode¶
Code Blocks¶
When you debug a program using codeblocks and the program crashes you can check "Debug" → "Debugging windows" → "Call stack" and check in which line your sofware crashes
Please, use breakpoints.
GDB online Debugger¶
When you are debugging, and your program crashes it will provide information on which line it crashes and why.
Please, use breakpoints
Using gdb from command line¶
If you are using Windows you have different options to install
gcc
and gdb
- Installing Mingw
- Installing git and running the Git bash
- Using Ubuntu WSL
Compiling Code for GDB¶
To use
gdb
to debug a program, you need to compile your code with debugging information. You can do this by adding the -g
flag when you compile your code with gcc
:gcc -g -o my_program my_program.c
Basic GDB Usage¶
- Start GDB:
Start
gdb
with your compiled program as an argument: gdb my_program
- Set Breakpoints:
You can set breakpoints at specific lines of your code to pause execution:
break [filename:]linenumber
For example, to set a breakpoint at line 10, you would type:
break 10
- Run the Program:
Start your program within
gdb
: run [program arguments]
- Inspecting Variables:
When your program hits a breakpoint or crashes, you can inspect variable values:
print variable_name
- Stepping Through the Program:
- Next: Move to the next line of code in the current function (doesn’t step into functions):
- Step: Move to the next line of code, stepping into functions if they are called:
next
- Step: Move to the next line of code, stepping into functions if they are called:
step
- Viewing the Call Stack:
When your program is paused, you can view the call stack using:
backtrace
- Exiting GDB:
You can exit
gdb
anytime using:quit
Additional Commands:¶
- Continue: Continue program execution until the next breakpoint:
continue
- Clear: Remove breakpoints:
clear linenumber
- List: Show the source code around the current line:
list
- Info Breakpoints: View all set breakpoints:
info breakpoints
- Help: Get help on
gdb
commands: help [command_name]
Example Session:¶
$ gdb my_program
(gdb) break 10
(gdb) run arg1 arg2
(gdb) print my_variable
(gdb) next
(gdb) quit
Tips:¶
- Be sure to explore
- Check the manual pages (
gdb
's help command to find more features and functionalities.- Check the manual pages (
man gdb
) or the official GDB documentationfor more detailed information