1
我使用GDB來調試我的代碼,並在函數中產生斷點時卡住(例如:使用string.h中的strcpy)。使用GDB的斷點
1)在這段代碼中,gdb停在斷點內部的strcpy函數中。
(gdb) list
1 #include<stdio.h>
2 #include<string.h>
3 main()
4 {
5 char a[20],b[]="ffff";
6 strcpy(a,b);
7 printf("%s\n",a);
8 }
(gdb) break 6
Breakpoint 1 at 0x8048486: file thu.c, line 6.
(gdb) break strcpy
Breakpoint 2 at 0x8048370
(gdb) break 7
Breakpoint 3 at 0x804849a: file thu.c, line 7.
(gdb) run
Starting program: /home/m/a.out
Breakpoint 1, main() at thu.c:6
6 strcpy(a,b);
(gdb) c
Continuing.
Breakpoint 2, 0xb7ea2490 in ??() from /lib/i386-linux-gnu/libc.so.6
(gdb) c
Continuing.
Breakpoint 3, main() at thu.c:7
7 printf("%s\n",a);
你可以看到gdb停在breakpoint2(在strcpy裏面)。
2)在這段代碼中,GDB不是停在斷點2(在strcpy內部),而是到斷點3。
(gdb) list
1 #include<stdio.h>
2 #include<string.h>
3 main()
4 {
5 char a[20];
6 strcpy(a,"hello world!");
7 printf("%s\n",a);
8 }
(gdb) break 6
Breakpoint 1 at 0x8048449: file thu.c, line 6.
(gdb) break strcpy
Function "strcpy" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 2 (strcpy) pending.
(gdb) break 7
Breakpoint 3 at 0x8048469: file thu.c, line 7.
(gdb) run
Starting program: /home/m/a.out
Breakpoint 1, main() at thu.c:6
6 strcpy(a,"hello world!");
(gdb) c
Continuing.
Breakpoint 3, main() at thu.c:7
7 printf("%s\n",a);
那麼是什麼讓兩種情況不同呢? 我跟着書「黑客:開發的藝術」Jon Erickson,這是本書的一個例子,案例2與代碼char_array2.c在39頁相似,但輸出與案例中的書不相似的斷點2.
感謝您的閱讀!