- To debug a program, the program must be compiled with -g flag.
$ gcc -g gdb_ex.c -o gdb_ex
- To debug a program called gdb_ex, run gdb as follow:
$ gdb gdb_ex
- To setup a break point, use 'b' command.
(gdb) b main
Breakpoint 1 at 0x8048389: file gdb_ex.c, line 13.
(gdb)
- To run the debugging command in gdb, use 'run' command:
(gdb) run
Starting program: /home/weesan/public_html/cs152/lab1/gdb_ex
Breakpoint 1, main () at gdb_ex.c:13
13 func1();
(gdb)
- To list the source code in gdb, use 'list' command:
(gdb) list
8 void func1(void) {
9 func2();
10 }
11
12 int main(void) {
13 func1();
14 return (0);
15 }
(gdb)
- To single step a statement, use 'n' command:
(gdb) n
14 func1();
(gdb)
- To step into a function, use 's' command:
(gdb) s
func1 () at gdb_ex.c:9
9 func2();
(gdb)
(gdb) s
func2 () at gdb_ex.c:4
4 int *p = NULL;
(gdb)
- To examine the content of a variable, use 'p' command:
(gdb) p pt
$1 = (int *) 0xbfc8f63c
(gdb) n
5 *pt = 1234;
(gdb) p pt
$2 = (int *) 0x0
(gdb)
- To list all breakpoints, use 'info b' command:
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x08048389 in main at gdb_ex.c:13
breakpoint already hit 1 time
(gdb)
- To delete a breakpoints, use 'd' command:
(gdb) d 1
(gdb)
- To continue the program after stopping at the breakpoint, use 'cont' command:
(gdb) cont
Continuing.
- To quit gdb, use 'q' command:
(gdb) q
The program is running. Exit anyway? (y or n) y
$
- To examine a core file, do the following from command line:
$ gdb_ex
Segmentation fault (core dumped)
$ gdb gdb_ex core
GNU gdb 6.3
...
Core was generated by `gdb_ex'.
Program terminated with signal 11, Segmentation fault.
warning: current_sos: Can't read pathname for load map: Input/output error
Reading symbols from /lib/tls/libc.so.6...done.
Loaded symbols for /lib/tls/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
#0 0x08048364 in func2 () at gdb_ex.c:5
5 *pt = 1234;
(gdb)
- To examine the stack frames, use 'bt' command:
(gdb) bt
#0 0x08048364 in func2 () at gdb_ex.c:5
#1 0x08048377 in func1 () at gdb_ex.c:9
#2 0x08048395 in main () at gdb_ex.c:14
(gdb)
- Go to the offended frame, use 'f' command:
(gdb) f 0
#0 0x08048364 in func2 () at gdb_ex.c:5
5 *pt = 1234;
(gdb)
- Go to the upper frame, use 'up' command:
(gdb) up
#1 0x08048377 in func1 () at gdb_ex.c:9
9 func2();
(gdb)
- Go to the lower frame, use 'down' command:
(gdb) down
#0 0x08048364 in func2 () at gdb_ex.c:5
5 *pt = 1234;
(gdb)
- To quit gdb, use 'q' command:
(gdb) q
$