Linux嵌入式GDB調試環境搭建


======================= 我的環境 ==========================
PC 端: CPU:x86_64, 系統:Ubuntu,IP:172.16.2.212
開發板:CPU:sw9820c,系統:openwrt linux,IP:172.16.30.20
PC端安裝的 C 交叉編譯器為 arm-none-linux-gnueabi-gcc(gcc version 4.6.3),即交叉編譯工具鏈的前綴為 arm-none-linux-gnueabi。
===========================================================

 

1、GDB調試方式說明

  一般桌面系統如Ubuntu、Centos等可以直接運行gdb + 目標可執行程序, 而嵌入式系統則分情況, 如果性能強勁且調試的源碼文件比較少, 也可以編譯嵌入式版的gdb執行文件, 即嵌入式gdb + 目標可執行程序。

如果性能弱或者調試對象的源碼文件多可采用分離法, 即PC端運行gdb, 同時源碼也在PC端, 而目標可執行程序放到開發板, 那怎么聯系呢, 所以開發板端還需要運行gdbserver, 與PC端的gdb通過網絡通信。

PC端也稱客戶端, 開發板端為服務端,  gdbserver 接收 gdb 所傳送的命令(list, step等), 然后調度執行文件, 並把相關信息反饋給PC端, PC端解析后查詢源碼文件並顯示到控制台。

  注意,gdb調試除了可執行文件外還需要源碼! 而我的源碼文件比較多, 所以采用后者即 gdb + gdbserver。 在介紹安裝前, 請確保宿主機和目標機可以相互ping通且可以telnet訪問端口, 因為兩者是靠網絡通信的!

     

 

2、安裝

 a. 下載gdb

  ftp://ftp.gnu.org/gnu/gdb , 我選擇的是 gdb-6.8a.tar.bz2 

 b. 解壓編譯

tar jxvf gdb-6.8a.tar.bz2

//編譯PC端 gdb
cd gdb-6.8/
mkdir _install
./configure --target=arm-none-linux-gnueabi --disable-werror --prefix=/home/fuzk/tools/gdb/gdb-6.8/_install  //沒有設置host 默認= x86_64
make
make install

//編譯開發板端 gdbserver
cd gdb-6.8/gdb/gdbserver
./configure --host=arm-none-linux-gnueabi
make //當前路徑就有gdbserver

 

3、測試

  首先編寫測試用例:

#include <stdio.h>

void debug(char *str)
{
    printf("debug info :%s\n",str );
}

main(int argc,char *argv[])
{
    int i,j;
    j=0;
    for(i=0;i<10;i++){
        j+=5;
        printf("now a=%d\n", j);
    }
}

  arm-none-linux-gnueabi-gcc -g test.c 生成a.out可執行文件, 連同 gdb-6.8/gdb/gdbserver/gdbserver  一並拷貝到開發板, 然后運行

  當然也可以指定允許哪個IP地址訪問, 比如我的PC 172.16.2.212  訪問開發板

  PC端把可執行文件和源碼拷貝到gdb-6.8/_install/bin下,

   運行gdb:

fuzk@ubuntu:~/tools/gdb/gdb-6.8/_install/bin$ ./arm-none-linux-gnueabi-gdb
GNU gdb 6.8
Copyright (C) 2008 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-unknown-linux-gnu --target=arm-none-linux-gnueabi".
(gdb) target remote 172.16.30.20:777 重中之重!
Remote debugging using 172.16.30.20:777
[New Thread 319]
0xb6efced0 in ?? ()
(gdb) b main
No symbol table is loaded.  Use the "file" command.
(gdb) file a.out 
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
Reading symbols from /home/fuzk/tools/gdb/gdb-6.8/_install/bin/a.out...done.
(gdb) b main
Breakpoint 1 at 0x846c: file test.c, line 11.
(gdb) c
Continuing.

Breakpoint 1, main (argc=1, argv=0xbe8d4e54) at test.c:11
11          j=0;
(gdb) n
12          for(i=0;i<10;i++){
(gdb) n                  這里發生異常, 直接執行完, 同時開發板也打印全部, 猜測是版本問題

Program exited with code 012.
(gdb) 


開發板log:

/data/app/MAINAPP/data # ./gdbserver 172.16.2.212:777 /data/a.out
[ 7431.399932] c0 init: untracked pid 318 exited
Process /data/a.out created; pid = 319
Listening on port 777
Remote debugging from host 172.16.2.212
now a=5

 
         

now a=10

 
         

now a=15

 
         

now a=20

 
         

now a=25

 
         

now a=30

 
         

now a=35

 
         

now a=40

 
         

now a=45

 
         

now a=50

 
         


Child exited with retcode = a

 
         

Child exited with status 10
GDBserver exiting

 

  gdb-6.8的PC端gdb貌似有問題, 無法全部單步調試, 我的交叉編譯器是arm-none-linux-gnueabi-gcc, 同時也提供了arm-none-linux-gnueabi-gdb, 發現調試正常, 跟我自己編譯的區別在於版本是7.2, 所以應該是版本問題, 后續用系統自帶的

其log如下:

fuzk@ubuntu:~/test/gdb/simple$ arm-none-linux-gnueabi-gdb
GNU gdb (Sourcery CodeBench Lite 2012.03-57) 7.2.50.20100908-cvs
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 "--host=i686-pc-linux-gnu --target=arm-none-linux-gnueabi".
For bug reporting instructions, please see:
<https://support.codesourcery.com/GNUToolchain/>.
(gdb) target remote 172.16.30.20:777
Remote debugging using 172.16.30.20:777
0xb6ed7ed0 in ?? ()
(gdb) b main
No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (main) pending.
(gdb) b main
No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 2 (main) pending.
(gdb) file a.out 
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
Reading symbols from /home/fuzk/test/gdb/simple/a.out...done.
Cannot access memory at address 0x0
(gdb) b main
Cannot access memory at address 0x0
Note: breakpoints 1 and 2 also set at pc 0x846c.
Breakpoint 3 at 0x846c: file test.c, line 11.
(gdb) c
Continuing.

Breakpoint 1, main (argc=1, argv=0xbe9bbe54) at test.c:11
11          j=0;
(gdb) s
12          for(i=0;i<10;i++){
(gdb) s
13              j+=5;
(gdb) s
14              printf("now a=%d\n", j);
(gdb) n
12          for(i=0;i<10;i++){
(gdb) s
13              j+=5;
(gdb) s
14              printf("now a=%d\n", j);
(gdb) n
12          for(i=0;i<10;i++){
(gdb) n
13              j+=5;
(gdb) n
14              printf("now a=%d\n", j);
(gdb) n
12          for(i=0;i<10;i++){
(gdb) n
13              j+=5;
(gdb) n
14              printf("now a=%d\n", j);
(gdb)

(gdb) s
12 for(i=0;i<10;i++){
(gdb) s
13 j+=5;
(gdb) s  printf由於沒有-g編譯 調用step會導致異常, 所以建議使用next, 但如果用系統自帶的會有上面紅色提示 所以用step也不怕!
14 printf("now a=%d\n", j);
(gdb) s
12 for(i=0;i<10;i++){
(gdb) s
13 j+=5;
(gdb) s
14 printf("now a=%d\n", j);

=====================
對應開發板:
/data/app/MAINAPP/data # ./gdbserver 172.16.2.212:777 /data/a.out
[ 8046.924774] c0 init: untracked pid 326 exited
Process /data/a.out created; pid = 327
Listening on port 777
Remote debugging from host 172.16.2.212
now a=5
now a=10

now a=15

now a=20

 
         

now a=25

 

 

 

 4. 其他

igure: error: no termcap library found

  Makefile:10927: recipe for target 'configure-gdb' failed
  make[1]: *** [configure-gdb] Error 1
  make[1]: Leaving directory '/home/fuzk/tools/gdb/gdb-7.2'

 缺少 termcap庫, 先安裝該庫: https://ftp.gnu.org/gnu/termcap/

      tar zxvf termcap-1.3.1.tar.gz

      cd termcap-1.3.1/

      ./configure --prefix=/home/fuzk/tools/gdb/gdb-7.2/_install --target=arm-none-linux-gnueabi

      vi Makefile :     

        CC = /opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi-gcc
        AR = /opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi-ar

      make install

 

  重初始化: 

  CFLAGS=-I/home/fuzk/tools/gdb/gdb-7.2/_install/include LDFLAGS=-L/home/fuzk/tools/gdb/gdb-7.2/_install/lib ./configure --prefix=/home/fuzk/tools/gdb/gdb-7.2/_install --target=arm-none-linux-gnueabi --host=arm-none-linux-gnueabi

  make  

  make install

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM