linux之gdb調試

一、編譯要加上-g選項html

gcc -g -o hello hello.clinux

二、經常使用命令android

l 查看源碼 , b 加斷點, r 開始運行調試, n 下一步, s下一步可是會進入子函數. p 輸出數據.redis

info args查看當前函數參數值;info locals 看當前函數棧上值信息;info registers 表示查看寄存器值多線程

三、設置條件斷點:函數

b _add if g_val==10工具

四、刪除斷點ui

d + 斷點索引1,2,3..線程

五、多線程調試調試

info threads 查看全部運行的線程信息;

thread 3表示切換到第三個線程;

六、gdb在ARM開發板上調試

到http://www.gnu.org/software/gdb/download/下載gdb包

(1)在本機PC安裝arm-linux-gdb客戶端

#tar jxvf gdb-7.2-tar-bz2
#cd gdb-7.2
#./configure --target=arm-linux --prefix=/usr/local/arm-gdb –v(--target配置gdb的目標平臺,--prefix配置安裝路徑)
#make
#make install

這裏使用的arm-linux-gcc版本必定要與編譯開發板內核的gcc版本一致
這樣arm-linux-gdb的客戶端就安裝到了--prefix所配置安裝路徑裏。

(2)在ARM板上安裝gdbserver

#cd gdb-7.2/gdb/gdbserver
#./configure --target=arm-linux --host=arm-linux(--target=arm-linux表示目標平臺,--host表示主機端運行的是arm-linux-gdb,不須要配置—prefix,由於gdbserver不在主機端安裝運行)
#make CC=/usr/local/arm-linux-gcc-3.4.1/bin/arm-linux-gcc
把生成的 gdbserver 拷貝進目標板,通常在/usr/bin

這裏使用的arm-linux-gcc版本必定要與編譯開發板內核的gcc版本一致

ARM板上的gdb版本和linux下的gdb版本要一致:arm-linux-gdb -v查看版本號

(3)編譯程序

arm-linux-gcc -g hello.c -o hello

在目標板運行   #gdbserver 10.88.33.14:777 hello (#gdbserver 客戶端IP地址:端口 調試的程序名)
在客戶機運行   #arm-linux-gdb hello  (我都是在同一個目錄裏進行的,即mount到目標板的那個目錄)

(gdb) target remote 10.88.33.1:777 (target remote 目標板IP地址:端口)     與開發板握手

(4)調試

設置好斷點以後輸入c(continue)繼續運行,由於可執行程序hello已經在開發板上運行,不一樣於在linux上調試的代碼輸入r

七、安卓系統使用gdb調試:安卓對於C/C++代碼的調試

(1)在system/bin目錄下查看gdbserver是否存在,若是不存在按上面的方法copy到/system/bin目錄下;

(2)查看mediaserver進程號:ps -df |  grep mediaserver    獲得1668

(3)啓動gdbserver:

gdbserver remote:1235 --attach 1668      //1235是監控的中斷號;1668是調試程序的進程ID

(4) 客戶端啓動gdb: 在android源碼中./prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-gdb

GNU gdb (GDB) 7.6
Copyright (C) 2013 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 "--host=x86_64-linux-gnu --target=arm-linux-android".
For bug reporting instructions, please see:
<http://source.android.com/source/report-bugs.html>.
(gdb)

target remote 192.168.31.127:2019       握手

(5)指定調試代碼

file ~/**xxx**/out/target/product/aries/symbols/system/bin/mediaserver

注意調試代碼在Android.mk中要添加LOCAL_CFLAGS += -g,而後編譯image燒入開發板

八、gdb調試u-boot和linux內核

https://www.cnblogs.com/shenlian/archive/2011/06/15/2081392.html

 九、trace跟蹤

使用arm-linux-gcc編譯trace工具並安裝,將trace安裝到開發板

trace -o log.txt  ./hello      //跟蹤./hello使用了哪些系統調用

trace -o log.txt rmmod first_drv    //當卸載驅動出錯時,也可使用trace跟蹤rmmod first_drv命令

發生系統調用時,都會發生異常中斷swi,進入中斷處理,當./hello是子進程時說明。/hello程序有被跟蹤,發信號給主進程trace.trace 根據swi,  #num中的num知道是sys_open仍是sys_read或其餘系統調用

十、分析core dump文件定位程序出錯的地方

ulimit -c    查看core dump文件的大小

ulimit -c unlimited      //不限制core dump文件的大小

./hello     產生core dump文件

將產生的名爲core的文件copy到PC虛擬機中的Linux系統中來

arm-linux-gdb  ./hello  ./core

這種方法能夠快速找到程序錯誤的地方,也不用在ARM板上運行gdbserver,但不能打斷點

相關文章
相關標籤/搜索