GDB學習筆記(二) 除錯段錯誤

2021-07-31 09:42:01 字數 3939 閱讀 5788

1. 編譯錯誤 --- 主要為語法錯誤

2. 執行錯誤 --- 主要為段錯誤

3. 邏輯錯誤 --- 主要為**邏輯出錯,執行不一定會報錯

訪問系統資料區

// test1.c

#include int main(void)

輸出結果

[root@localhost test]# gcc -wall -g test1.c -o test1

[root@localhost test]# ./test1

段錯誤[root@localhost test]#

記憶體越界

比如:陣列越界(不是所有的記憶體越界都報錯)

// test2.c

#include int main(void)

; printf("%d\n", arr[600]);

arr[600] = 100;

printf("%d\n", arr[600]);

return 0;

}執行結果

[root@localhost test]# gcc -wall -g test2.c -o test2

[root@localhost test]# ./test2

0100

[root@localhost test]#

這個例子中,陣列越界訪問arr[600],並且修改了arr[600]的值,

編譯器雖然沒有報錯,但是這是乙個極大的隱患。

以除錯test1.c為例

首先執行「gdb test1」,啟動gdb。

然後執行「run」,執行程式。

當程式收到「sigsegv」訊號後,使用「bt」進行回溯。

[root@localhost test]# gdb test1

gnu gdb (gdb) red hat enterprise linux (7.2-83.el6)

license gplv3+: gnu gpl version 3 or later this is free software: you are free to change and redistribute it.

there is no warranty, to the extent permitted by law. type "show copying"

and "show warranty" for details.

this gdb was configured as "x86_64-redhat-linux-gnu".

for bug reporting instructions, please see:

...reading symbols from /home/lwp_workspace/test/test1...done.

(gdb) r

starting program: /home/lwp_workspace/test/test1

program received signal sigsegv, segmentation fault.

0x0000000000400484 in main () at test.c:7

7 *iptr = 100;

missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6.x86_64

(gdb) bt

#0 0x0000000000400484 in main () at test.c:7

以除錯test1.c為例

(1)執行ulimit -c unlimited命令,允許生成core檔案

常用的ulimit命令

ulimit -c // 檢視core dump的狀態

ulimit -a // 檢視core dump的所有資訊

ulimit -c unlimited // 設定core檔案的大小為無限制

ulimit -c number // 設定core檔案的大小,單位:blocks

(2)執行可執行檔案時會產生core檔案

[root@localhost test]# gcc -wall -g test1.c -o test1

[root@localhost test]# ls

test1 test1.c

[root@localhost test]# ./test1

段錯誤 (core dumped)

[root@localhost test]# ls

core.2065 test1 test1.c

[root@localhost test]#

(3)使用core檔案除錯程式

使用core檔案除錯程式的命令格式為「gdb 程式檔名 core檔名」

首先執行「gdb test1 core.2065」命令,啟動gdb。

然後使用「bt」命令進行回溯。

[root@localhost test]# gdb test1 core.2065 

gnu gdb (gdb) red hat enterprise linux (7.2-83.el6)

license gplv3+: gnu gpl version 3 or later this is free software: you are free to change and redistribute it.

there is no warranty, to the extent permitted by law. type "show copying"

and "show warranty" for details.

this gdb was configured as "x86_64-redhat-linux-gnu".

for bug reporting instructions, please see:

...reading symbols from /home/lwp_workspace/test/test1...done.

[new thread 2065]

missing separate debuginfo for

try: yum --enablerepo='*-debug*' install /usr/lib/debug/.build-id/e4/ca84a6408746bd4f92041f890408bf47f3acec

reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.

loaded symbols for /lib64/libc.so.6

reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.

loaded symbols for /lib64/ld-linux-x86-64.so.2

core was generated by `./test1'.

program terminated with signal 11, segmentation fault.

#0 0x0000000000400484 in main () at test.c:7

7 *iptr = 100;

missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6.x86_64

(gdb) bt

#0 0x0000000000400484 in main () at test.c:7

(gdb)

gdb 除錯段錯誤

開發嵌入式linux的時候經常會遇到segmentation fault,也就是段異常錯誤,一般是使用錯誤的指標訪問記憶體導致。這種錯誤可以通過開啟核心的異常資訊輸出,再用gdb對發生段異常的位址進行定位。1.開啟核心的異常資訊輸出 mips的核心 關閉了arch mips mm fault.c的d...

gdb 除錯段錯誤

利用執行時產生core檔案,再利用gdb除錯找出段錯誤在哪一行 ulimit c unlimited使用該bash命令,可以使執行時段錯誤產生core檔案。1.gcc g 編譯 gcc g o a a.c2.執行檔案,便會產生乙個core.檔案 a3.除錯core檔案 gdb a core.便可以直...

gdb除錯段錯誤

一 gdb的除錯命令。c語言是 cc g tst.c o tst c 是g g o 生成的檔案 file.cpp c 除錯程式命令 gdb file 啟動,羅列 行數ist 1,break 行數 info break,run r 除錯執行,step s 單步除錯,檢視變數 print p 變數名,檢...