- GNU 偵錯程式有用的資源
- GDB - 快速指南
- GDB - 資源
GDB - 除錯示例 2
讓我們編寫另一個程式,該程式會因未初始化記憶體而產生核心轉儲。
#include <iostream>
using namespace std;
void setint(int*, int);
int main()
{
int a;
setint(&a, 10);
cout << a << endl;
int* b;
setint(b, 10);
cout << *b << endl;
return 0;
}
void setint(int* ip, int i)
{
*ip = i;
}
為了啟用除錯,必須在 -g 選項下編譯程式。
$g++ -g crash.cc -o crash
注意:我們使用 g++ 編譯器,因為我們使用了 C++ 原始碼。
在 Linux 計算機上執行此程式時,它將產生以下結果
segmentation fault (core dumped)
現在讓我們使用 gdb 進行除錯
$ gdb crash (gdb) r Starting program: /home/tmp/crash 10 10 Program received signal SIGSEGV, Segmentation fault. 0x4000b4d9 in _dl_fini () from /lib/ld-linux.so.2 (gdb) where #0 0x4000b4d9 in _dl_fini () from /lib/ld-linux.so.2 #1 0x40132a12 in exit () from /lib/libc.so.6 #2 0x4011cdc6 in __libc_start_main () from /lib/libc.so.6 #3 0x080485f1 in _start () (gdb)
遺憾的是,該程式不會在使用者定義的任何函式中(main 或 setint)崩潰,因此沒有有用的跟蹤或區域性變數資訊。在這種情況下,逐步瀏覽程式可能更有用。
(gdb) b main # Set a breakpoint at the beginning of the function main (gdb) r # Run the program, but break immediately due to the breakpoint. (gdb) n # n = next, runs one line of the program (gdb) n (gdb) s setint(int*, int) (ip=0x400143e0, i=10) at crash2.C:20 # s = step, is like next, but it will step into functions. # In this case the function stepped into is setint. (gdb) p ip $3 = (int *) 0x400143e0 (gdb) p *ip 1073827128
*ip 的值是由 ip 指向的整數的值。在本例中,它是一個非正常值,有力證明存在問題。本例中的問題是,指標從未正確初始化,因此它指向記憶體中的某個隨機區域(地址 0x40014e0)。非常幸運的是,向 *ip 賦值的過程並不會導致程式崩潰,但它引發的問題會在程式結束後導致其崩潰。
gdb_debugging_examples.htm
廣告