在程序編譯時增加-g選項以支持gdb調試
如:
$ gcc -g example.c -o example.x
通過上述命令對example.c編譯之后,使用下列命令進入到gdb調試:
$ gdb example.x
在gdb調試中,常用的命令有以下幾個:
$ list 縮略為 l
列出程序源碼,每次列出10行,按回車重復運行上一命令;
$ run 縮略為 r
程序開始運行,在r后可以加入程序啟動參數,程序運行到斷點處暫停;
$ continue 縮略為 c
程序繼續運行,到下一斷點處暫停;
單步調試
$ step 縮略為s
$ next 縮略為 n
程序繼續運行到下一斷點;
$ break 縮略為 b
在程序某一位置設置斷點;
$ info break 縮略為 i b
查看斷點信息;
設置/查看運行參數
$ set args ---/show args
加載運行中的進程進行調試(Attach to the running process to be debugged.):
$ gdb attatch pid
Specifying source directories
$ dir dirname …
以十六進制輸出內存塊數據
$ x/28hx ---
段錯誤調試,core文件樣例
通過ulimit命令查看一下系統是否配置支持了dump core的功能。通過ulimit -c或ulimit -a,可以查看core file大小的配置情況,如果為0,則表示系統關閉了dump core;可以通過ulimit -c unlimited來打開。若發生了段錯誤,但沒有core dump,是由於系統禁止core文件的生成。
$ gdb [exec file] [core file] | gdb -c corefile execfile
查看堆棧信息:
$ bt
查看堆棧中某一層的信息:
$ f $num
PS:對於python程序在linux下可以使用pdb做調試!
Published by Windows Livewriter.