Debugging C Program using gdb

Debugging a C Program Using GDB

Step 1: Compile the C Program with Debugging Option -g

Compile your C program using the -g option.
This allows the compiler to include debugging information.

Example:

$ cc -g test.c

(test.c is the sample program to debug)


Step 2: Launch GDB

Start the C debugger (gdb) as shown below:

$ gdb a.out

Step 3: Set Up a Breakpoint Inside the C Program

Syntax:

break [file_name]:line_number

Example:

break test.c:3

This sets a breakpoint at line number 3.

Breakpoints are placed where you suspect errors.
During execution, the debugger stops at the breakpoint and allows you to debug.


Step 4: Execute the C Program in GDB

Run the program using:

run [args]

(args are optional command-line arguments)


Step 5: Print Variable Values Inside GDB

Syntax:

print {variable}

Example:

print i

Step 6: Continue, Step Over, and Step Into

When the program stops at a breakpoint, you can:

Continue Execution

c or continue

Runs until the next breakpoint.

Step Over

n or next

Executes the next line as a single instruction.

Step Into

s or step

Similar to next, but enters functions and executes them line by line.


GDB Command Shortcuts

l → list source code p → print variable c → continue execution s → step into ENTER → repeat previous command

Miscellaneous GDB Commands

List Source Code

l

Shows source code in debug mode.

l line-number

Shows a specific line.

l function

Shows a specific function.


Backtrace (Call Stack)

bt

Prints backtrace of all stack frames (or innermost COUNT frames).


Help

help TOPICNAME

Shows help for a specific topic.


Quit GDB

quit

Exits the debugger.


Demo Video

https://www.youtube.com/watch?v=sCtY--xRUyI


1️⃣ Simple C Program to Debug (test.c)

This program intentionally contains a logical bug.

#include <stdio.h> int add(int a, int b) { int result = a - b; // BUG: should be a + b return result; } int main() { int x = 10; int y = 5; int sum = add(x, y); printf("Sum = %d\n", sum); return 0; }

Expected output:

Sum = 15

Actual output (buggy):

Sum = 5

2️⃣ Compile with Debug Option

cc -g test.c

This creates:

a.out

3️⃣ Launch GDB

gdb a.out

You will see:

(gdb)

4️⃣ List Source Code

(gdb) l

Shows the program lines.


5️⃣ Set Breakpoint at main()

Option A — by function name

(gdb) break main

Option B — by line number

(gdb) break test.c:10

6️⃣ Run the Program

(gdb) run

Execution stops at the breakpoint:

Breakpoint 1, main () at test.c:10

7️⃣ Step Through the Program

Execute next line

(gdb) n

Keep pressing:

n n

Watch variables:

(gdb) print x (gdb) print y

Output:

$1 = 10 $2 = 5

8️⃣ Step Into the Function Call

When cursor reaches:

int sum = add(x, y);

Type:

(gdb) s

Now you enter:

add(int a, int b)

9️⃣ Inspect Function Variables

(gdb) print a (gdb) print b

Output:

10 5

Step to next line:

(gdb) n

Now check:

(gdb) print result

Output:

5

🚨 Now you see the problem:

result = a - b

Instead of:

result = a + b

Bug identified!


🔟 Continue Execution

(gdb) c

Program finishes.


1️⃣1️⃣ Fix the Bug

Edit program:

int result = a + b;

Recompile:

cc -g test.c

Run again:

./a.out

Correct output:

Sum = 15

🧠 Quick Debugging Command Summary

CommandMeaning
gdb a.out        Start debugger
l        List source
break main        Set breakpoint
run        Run program
n        Next line
s        Step into function
p var        Print variable
c        Continue
bt        Backtrace
quit        Exit

Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Single and Multi Dimensional Arrays