1. 程式人生 > >GDB arm-linux交叉編譯移植和使用方法(特別是對於正在執行的程式或者段錯誤的程式進行分析)

GDB arm-linux交叉編譯移植和使用方法(特別是對於正在執行的程式或者段錯誤的程式進行分析)

測試程式碼中的test1是用來定位堆疊段錯誤,Delay函式是用來定位程式阻塞,都可以用gdb定位出來,如下:

 (1)測試程式執行時首先會有個段錯誤:./gdbtest &
[[email protected] user0]$ [65334.020000] pgd = c3e14000
[65334.020000] [00000000] *pgd=43b87031, *pte=00000000, *ppte=00000000
用移植好的gdb來測試執行如下:

 ./gdb gdbtest
GNU gdb (GDB) 7.1
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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 "arm-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/user0/gdbtest...done.
(gdb) r
Starting program: /home/user0/gdbtest 
warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.
warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.
[65352.710000] pgd = c1cbc000read_db matching inferior's thread library, thread debugging will not be available.


[65352.710000] [00000000] *pgd=41c9c031, *pte=00000000, *ppte=00000000


Program received signal SIGSEGV, Segmentation fault.
0x400ca048 in strcpy () from /lib/libc.so.6
(gdb) bt
#0  0x400ca048 in strcpy () from /lib/libc.so.6
#1  0x00008540 in f2 (str=0x0) at src/gdbtest.c:45
#2  0x0000855c in main () at src/gdbtest.c:73
(gdb) 

[1]+  Segmentation fault         ./gdbtest

可以看出程式碼段錯誤問題出在strcpy上。

(2)測試程式中把

Delay(10000);

test1();
D
elay放到test1之前,這樣程式會阻塞,阻塞的時候用gdb定位正在執行的程序非常好用:

./gdb gdbtest 1865     第一是執行gdb 第二個程序名稱 第三個程序pid,順序這樣子來要
GNU gdb (GDB) 7.1
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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 "arm-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/user0/gdbtest...done.
Attaching to program: /home/user0/gdbtest, process 1865
Reading symbols from /lib/libpthread.so.0...(no debugging symbols found)...done.


warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.
Loaded symbols for /lib/libpthread.so.0
Reading symbols from /lib/libdl.so.2...(no debugging symbols found)...done.


warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.
Loaded symbols for /lib/libdl.so.2
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.


warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.


warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.
Loaded symbols for /lib/ld-linux.so.2
0x401198dc in select () from /lib/libc.so.6
(gdb) bt
#0  0x401198dc in select () from /lib/libc.so.6
#1  0x000086e0 in Delay (uiTimeS=<value optimized out>) at src/gdbtest.c:67
#2  0x00008598 in _start ()


(gdb) q
A debugging session is active.

bt用來檢視堆疊資訊,發現阻塞在select函式中,和我們的程式碼一致。