linux核心調試技術之GDB和GDBServer

來源:互聯網
上載者:User

標籤:version   listening   test   instr   ant   watch   gdb   opp   copyright   

1、調試原理

  GDB調試是應用程式在開發板上運行,然後在PC機上對開發板上得應用程式進行調試,PC機運行GDB,開發板上運行GDBServer。在應用程式調試的時候,pc機上的gdb向開發板上的GDBServer發出命令,而開發板上的gdbserver就會嚮應用程式發出訊號,使應用程式停下來或者完成其他一些工作。

 

2、安裝GDB和GDBServer(gdb-7.4.tar.bz2 )

  2.1、GDB      1、下載: http://ftp.gnu.org/gnu/gdb/     2、解壓:tar xvf gdb-7.4.tar.bz2    3、配置:cd gdb-7.4/                ./configure --target=arm-linux    4、編譯:make    5、安裝:mkdir tmp                 make install prefix=$PWD/tmp    6、拷貝:cp tmp/bin/arm-linux-gdb  /bin/    7、查看版本 /bin/arm-linux-gdb -v (使用絕對路徑使用gdb)  2.2、GDBServer    1、cd gdb/gdbserver/    2、配置: ./configure --target=arm-linux --host=arm-linux    3、編譯: make CC=arm-linux-gcc   2.3、編譯GDBServer的時候會出現以下錯誤
linux-arm-low.c: In function `arm_stopped_by_watchpoint‘:linux-arm-low.c:642: error: `PTRACE_GETSIGINFO‘ undeclared (first use in this function)linux-arm-low.c:642: error: (Each undeclared identifier is reported only oncelinux-arm-low.c:642: error: for each function it appears in.)

   該錯誤是因為找不到PTRACE_GETSIGINFO宏,導致編譯錯誤。我們到交叉編譯鏈去搜尋一下,我們交叉編譯地址為 /work/tools/gcc-3.4.5-glibc-2.3.6

# cd  /work/tools/gcc-3.4.5-glibc-2.3.6# grep "PTRACE_GETSIGINFO" * -nRarm-linux/sys-include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO    0x4202arm-linux/include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO    0x4202distributed/arm-linux/sys-include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO    0x4202distributed/arm-linux/include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO    0x4202

 

  可以看到,在交叉編譯鏈裡面,定義了PTRACE_GETSIGINFO宏為0x4202,標頭檔為include<linux/ptrace.h>中。

  有兩種解決辦法,可任選其一:

  ① 在linux-arm-low.c中直接添加宏 #define PTRACE_GETSIGINFO 0x4202

  ② 在linux-arm-low.c中將#include <sys/ptrace.h> 更改為 #include <linux/ptrace.h>

  再次編譯,編譯通過。

  2.4、將gdbserver拷貝到開發板的bin目錄下
#cp gdbserver /work/nfs_root/first_fs/bin

 

 3、調試  3.1、在編譯要進行調試的應用程式 加上 -g   測試程式如下(test_debug.c)  
#include <stdio.h>void C(int *p){    *p = 0x12;}void B(int *p){    C(p);}void A(int *p){    B(p);}void A2(int *p){    C(p);}int main(int argc, char **argv){    int a;    int *p = NULL;    A2(&a);  // A2 > C    printf("a = 0x%x\n", a);    A(p);    // A > B > C    return 0;}

   編譯:

#arm-linux-gcc -g -o test_debug test_debug.c
3.2、在開發板上:  列印出如下資訊:
#gdbserver 192.168.1.10:123 ./test_debug
Process ./test_debug created; pid = 751Listening on port 2345

 

  注釋:192.168.1.10:本開發板的ip          123:連接埠號碼,自己隨便寫的          ./test_debug:要調試的程式3.3、在PC上輸入:
    /bin/arm-linux-gdb ./test-debug           target remote 192.168.183.127:2345
3.4、正式調試!介紹幾個常用的命令  (1)l:列出所有原始碼  (2)break main:在main處打斷點          break test_debug.c:11:在test_debug.c的11行打斷點  (3)c:運行到斷點處  (4)step:逐步執行  (5)next:逐步執行,但是step會進入函數裡面,但是next不會  (6)print a:列印a這個變數的值  (7)quit:退出,輸入此命令則開發板上的gdbserver也退出   (8)  詳細的GDB調試命令4、另外一種調試方法  讓程式在開發板上直接運行,當它發生錯誤時,令它產生core dump檔案,然後使用gdb根據core dump檔案找到發生錯誤的地方   在ARM板上:
  4.1、 ulimit -c unlimited
  4.2、 執行應用程式 : 程式出錯時會在目前的目錄下產生名為core的檔案   在PC上:  4.3、首先將core檔案拷貝到pc機上       然後:/bin/arm-linux-gdb ./test_debug ./core   列印出如下資訊: 
GNU gdb (GDB) 7.4Copyright (C) 2012 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=i686-pc-linux-gnu --target=arm-linux".For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/>...Reading symbols from /home/share/jz2440/test_debug...done.[New LWP 748]warning: `/lib/libc.so.6‘: Shared library architecture unknown is not compatible with target architecture arm.warning: `/lib/ld-linux.so.2‘: Shared library architecture unknown is not compatible with target architecture arm.Core was generated by `./test_debug‘.Program terminated with signal 11, Segmentation fault.#0  0x000084ac in C (p=0x0) at test_debug.c:66               *p = 0x12;

 

   4.4、bt:可以顯示調用關係 
#0  0x000084ac in C (p=0x0) at test_debug.c:6#1  0x000084d0 in B (p=0x0) at test_debug.c:12#2  0x000084f0 in A (p=0x0) at test_debug.c:17#3  0x00008554 in main (argc=1, argv=0xbeb32eb4) at test_debug.c:34

 

linux核心調試技術之GDB和GDBServer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.