$ gdb ./example.o
Reading symbols from ./example.o...done.
(gdb)
เอา GDB ไปเสียบ program ที่รันอยู่แล้วก็ได้ครับ (อาจจะต้องใช้ sudo เพราะเรื่อง security ของ Linux version ใหม่ๆ) sudo gdb ./<executable> -p <pid_of_running_prog>
$ sudo gdb ./example.o -p 18805
0x00007f123f97e081 in __GI___libc_read (fd=0, buf=0x55e5136ed670, nbytes=1024)
at ../sysdeps/unix/sysv/linux/read.c:27
27 ../sysdeps/unix/sysv/linux/read.c: No such file or directory.
(gdb)
ข้อสองเนี่ยมันดีตรงที่ เกิดโปรแกรมมีปัญหาต่างๆแต่ยังไม่ตาย (เช่น deadlock, data corrupt) GDB ก็สามารถเข้าไปดูได้เหมือนกัน
$ gdb ./example.o
Reading symbols from ./example.o...done.
(gdb) r
Starting program: /home/bankde/Desktop/tmp/example.o
Put string to split: hello world
First str: Hello
Second str: World
Put string to split:
สั่ง r หรือ run คือการรันโปรแกรมปกติครับ แต่มันไม่ค่อยมีประโยชน์เท่าไร หลายครั้งเราอยากหยุดโปรแกรมตรงสักจุดเพื่อดูว่าเกิดอะไรขึ้น เราจะใช้คำสั่ง break หรือ b ครับ
(gdb) b getInputAndRun
Breakpoint 1 at 0x90d: file example.c, line 20.
(gdb) b example.c:26
Breakpoint 2 at 0x966: file example.c, line 26.
(gdb) b *<instruction_addr>
(gdb) b *0x000055555555496d
Breakpoint 3 at 0x55555555496d: file example.c, line 26.
delete <breakpoint_no>
(gdb) delete 1
(gdb) delete
Delete all breakpoints? (y or n) y
(gdb) break split
Breakpoint 1 at 0x867: file example.c, line 7.
(gdb) r
Starting program: /home/bankde/Desktop/tmp/example.o
Put string to split: helloworld
Breakpoint 1, split (input=0x7fffffffdf40 "helloworld\n") at example.c:7
7 char** result = malloc(10*sizeof(char*));
(gdb) n
8 result[0] = input;
(gdb)
9 int j = 1;
(gdb)
10 for (int i=0; i<strlen(input); i++) {
(gdb)
11 if (input[i] == ' ') {
(gdb)
10 for (int i=0; i<strlen(input); i++) {
(gdb) until 17
split (input=0x7fffffffdf40 "helloworld\n") at example.c:17
17 return result;
(gdb) p result[0]
$1 = 0x7fffffffdf40 "helloworld\n"
(gdb) p result[1]
$2 = 0x0
(gdb) finish
Run till exit from #0 split (input=0x7fffffffdf40 "helloworld\n")
at example.c:17
0x0000555555554972 in getInputAndRun () at example.c:26
26 char** result = split(input);
Value returned is $3 = (char **) 0x555555756a80
(gdb) n
27 *result[0] = toupper(result[0][0]);
(gdb) p result[0]
$5 = 0x7fffffffdf40 "helloworld\n"
(gdb) n
28 *result[1] = toupper(result[1][0]);
(gdb) p result[0]
$6 = 0x7fffffffdf40 "Helloworld\n"
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x00005555555549a0 in getInputAndRun () at example.c:28
28 *result[1] = toupper(result[1][0]);