gdb command
Absolutely — the gdb (GNU Debugger) is one of the most powerful tools for teaching program debugging, process control, and memory management, which directly connects to core Operating Systems concepts.
gdb (GNU Debugger)
Purpose
gdb is the GNU Project Debugger, used to debug programs written in languages like C, C++, and Fortran.
It allows you to:
-
Run programs step by step
-
Examine variables and memory
-
Set breakpoints to stop execution at specific points
-
Inspect call stacks and understand crashes
Basic Syntax
Typical usage:
Here, a.out is your compiled executable (from a C or C++ program).
⚙️ Before Using gdb
You must compile your C program with debugging information included:
The -g option tells the compiler to include debugging symbols (function names, variable names, etc.) in the binary.
📘 Starting GDB
Start gdb with your program:
You’ll see:
Now you’re inside the gdb interactive prompt.
🧩 Commonly Used GDB Commands
| Command | Description |
|---|---|
run | Start the program under gdb |
break <line_number> | Set a breakpoint at a specific line |
break <function_name> | Set a breakpoint at a function |
next | Execute the next line (skips over function calls) |
step | Execute the next line (enters into functions) |
continue | Continue execution until the next breakpoint |
print <variable> | Display the value of a variable |
backtrace | Show the function call stack |
list | Show source code around the current line |
info locals | Show all local variables |
quit | Exit gdb |
| Breakpoint Related Commands |
|---|
info breakpoints | List all breakpoints |
delete <num> | Delete a specific breakpoint |
delete | Delete all breakpoints |
disable <num> | Temporarily disable a breakpoint |
enable <num> | Re-enable a disabled breakpoint |
🧪 Example: Debugging a Simple Program
C Program: buggy.c
Step 1: Compile with -g
Step 2: Run with gdb
Step 3: Start the Program
At the (gdb) prompt, type:
You’ll see:
This shows exactly where the program crashed and why.
Step 4: Inspect Variables
You can see that the division by zero occurred because b = 0.
Step 5: List Source Around Current Line
Step 6: Quit
🧩 Setting and Using Breakpoints
Example Program: example.c
Steps:
Inside gdb:
You can watch the program execute line by line, see when functions are called, and inspect values as they change.
🧩 Debugging a Running Process
Attach gdb to a process that’s already running:
This is useful for diagnosing stuck or crashed processes.
Sample Lab Exercise
Task:
-
Write a small program that computes a factorial but forgets to initialize a variable.
-
Compile it with
gcc -g. -
Use
gdbto: -
Run the program
-
Set a breakpoint at the loop
-
Step through the code
-
Print variable values at each iteration
🧩 Step 1: Write the C Program (with a Bug)
File name: fact.c
🔍 Problem
The variable fact is not initialized, so it contains garbage data.
This causes the output to be incorrect — a perfect case to use gdb for investigation.
🧩 Step 2: Compile with Debug Information
The -g option adds debugging symbols needed for gdb.
🧩 Step 3: Start the Debugger
You’ll see the GDB prompt:
🧩 Step 4: Set a Breakpoint at the Loop
List the code first:
You’ll see something like:
Now set a breakpoint at the loop (say, line 8):
🧩 Step 5: Run the Program
Enter a number when prompted, for example:
The program stops at the breakpoint:
🧩 Step 6: Step Through the Code
Execute one line at a time:
or if you want to step inside function calls (not needed here):
Now inside the loop, you can repeatedly press next to move line by line.
🧩 Step 7: Print Variable Values
Before the loop starts:
After the first iteration, press next again and recheck:
You’ll see the value changing incorrectly — this helps students understand the effect of using uninitialized variables.
🧩 Step 8: Fix the Bug
Now exit gdb:
Modify the program to initialize fact = 1;
✅ Corrected Version:
Compile and run again:
Output:
Learning Outcome
Students will:
-
Understand the role of debugging symbols (
-gflag). -
Learn how to set breakpoints, run, step through, and print variable values in
gdb. -
See the practical effect of an uninitialized variable
Comments
Post a Comment