Lesson 45 of 50
cDebugging with GDB
12 mins
Course progress90%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 45 of 50
c12 mins
Keep these ideas in mind as you work through the lesson.
The GNU Debugger (gdb) lets you step through code, inspect variables, and set breakpoints.
Typical workflow:
-g flag to embed debug symbols.gdb ./a.out.break main or break file.c:line.run arg1 arg2.next (line‑wise) or step (into functions).print var, info locals.continue.quit.$ gcc -g -o sum sum.c
$ gdb ./sum
(gdb) break main
Breakpoint 1 at 0x4005b6: file sum.c, line 5.
(gdb) run 5 7
Starting program: ./sum 5 7
[New Thread 0x7ffff7fd4700 (LWP 1234)]
Breakpoint 1, main (argc=3, argv=0x7fffffffdfd8) at sum.c:5
5 int a = atoi(argv[1]);
(gdb) next
6 int b = atoi(argv[2]);
(gdb) next
7 printf("%d
", a + b);
(gdb) print a
$1 = 5
(gdb) print b
$2 = 7
(gdb) continue
12
[Inferior 1 (process 1234) exited normally]
(gdb) quit
layout src for a source window.watch var stops when var changes.backtrace (bt) shows the call stack after a crash.