<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 建立ARM平臺上的交叉調(diào)試器gdb和gdbserver

          建立ARM平臺上的交叉調(diào)試器gdb和gdbserver

          作者: 時間:2016-11-11 來源:網(wǎng)絡(luò) 收藏
          (有的時候執(zhí)行環(huán)境的權(quán)限真的是很重要的)

          gdb-6.6使用了autoconf/automake,因此通過設(shè)置configure腳本的--target,--host,--prefix參數(shù)就可以方便的移植到別的平臺。

          本文引用地址:http://www.ex-cimer.com/article/201611/316783.htm

          --target指定編譯后的文件針對的是什么目標(biāo)板,一般設(shè)置為交叉編譯器的前綴,比如--target=arm-linux, --target=mips-linux,--target=armv5-linux-uclibc, --target的缺省值為i386-linux, 也就是PC機(jī)。

          --host指定編譯后的文件的運(yùn)行環(huán)境,取值可以是i386-linux或者交叉編譯器的前綴,缺省為i386-linux,

          --prefix指定要安裝的目錄。

          1、到http://www.gnu.org/software/gdb下載gdb-6.6.tar.gz到/tmp解壓到/opt下
          #cd /opt
          #tar xzvf /tmp/gdb-6.6.tar.gz

          2、建立配置文件、編譯
          gdb允許把編譯配置和編譯結(jié)果放到任意的目錄,因此可以在gdb目錄之外建立一個專門放編譯文件的目錄。
          #cd /opt
          #mkdir -p arm-gdb/build
          #cd arm-gdb/build

          #/opt/gdb-6.6/configure --host=i386-linux --target=armv5-linux-uclibc --prefix=/opt/arm-gdb
          #make
          #make install
          其中:host指定了運(yùn)行環(huán)境為i386機(jī)器,target指定了需要調(diào)試的目標(biāo)機(jī)環(huán)境(我使用的ARM toolchain是armv5-linux-uclibc-gcc,因此這樣指定,如果是用arm-linux-gcc,則 --target=arm-linux),prefix指定了編譯后的結(jié)果存放的位置,也就是安裝目錄。
          如果在編譯arm-linux-low.c這個文件時提示找不到“sys/reg.h”, 則修改arm-linux-low.c,注釋掉#include "sys/reg.h"。

          可以在/opt/arm-gdb/bin下找到可執(zhí)行的armv5-linux-uclibc-gdb, armv5-linux-uclibc-gdbtui, armv5-linux-uclibc-run。

          3、gdbserver的移植
          gdbserver要用到gdb源碼目錄下的一些頭文件,因此無法在gdb源碼目錄之外建立編譯文件。

          #cd /opt/gdb-6.6/gdb/gdbserver
          #vi build.arm
          內(nèi)容如下:
          ./configure --target=armv5-linux-uclibc --host=armv5-linux-uclibc --prefix=/opt/arm-gdb/gdbserver
          make
          make install
          #chmod +x build.arm
          #./build.arm

          注意:此處target參數(shù)和host參數(shù)都要設(shè)置為armv5-linux-uclibc,因為gdbserver是放在目標(biāo)板上運(yùn)行的。
          編譯后,可以在/opt/arm-gdb/gdbserver/bin下找到armv5-linux-uclibc-gdbserver,下載該文件到目標(biāo)板并重命名為gdbserver,
          同時要下載gdbserver需要的庫文件libthread_db-x.x.x.so,再建立兩個符號鏈接libthread_db.so和libthread_db.so.1。


          4、使用gdbserver
          在目標(biāo)板上運(yùn)行g(shù)dbserver
          #./gdbserver 192.168.0.2:2345 hello

          其中192.168.0.2為目標(biāo)板的IP,可以寫localhost,也可以不寫。2345為gdbserver打開的端口,可以自己設(shè)置。
          #./gdbserver :2345 hello

          在宿主機(jī)上運(yùn)行
          #armv5-linux-uclibc-gdb hello
          (gdb)target remote 192.168.0.2:2345
          (gdb)b main
          (gdb)continue

          使用X-Windows下的DDD
          #ddd --debugger armv5-linux-uclibc-gdb

          在DDD的GDB終端控制窗口中健入:
          (gdb) target 192.168.0.2:2345

          6、問題
          移植的gdbserver可以和Host上的mips-linux-gdb連接上,但是run的時候gdbserver出現(xiàn)
          Killing inferior錯誤
          解決方法:因為執(zhí)行完target命令后,目標(biāo)板程序已經(jīng)在運(yùn)行,所有應(yīng)該用continue命令而不是run命令。


          7、附1:GDB的基本指令:

          load:裝入一個程序
          symbol-file:裝入符號庫文件,可以是用-g參數(shù)編譯的可執(zhí)行文件。
          f(ile):指定一個可執(zhí)行文件進(jìn)行調(diào)試,gdb將讀取些文件的調(diào)試訊息,如f a.exe
          l(ist):列程序出源文件
          r(un) :裝載完要調(diào)試的可執(zhí)行文件后,可以用run命令運(yùn)行可執(zhí)行文件
          b(reak):設(shè)置斷點(diǎn)(break point),如b 25,則在源程序的第25行設(shè)置一個斷點(diǎn),當(dāng)程序執(zhí)行到第25行時,就會產(chǎn)生中斷;也可以使用b funcname,funcname為函數(shù)的名稱,

          當(dāng)程序調(diào)用些函數(shù)時,則產(chǎn)生中斷
          c(ontinue):c命令可以另中斷的程序繼續(xù)執(zhí)行,直到下一個中斷點(diǎn)或程序結(jié)束
          p(rint):輸入某個變量的值,如程序定義了一個int aa的就是,p aa就會輸出aa的當(dāng)前值
          n(ext):程序執(zhí)行到斷點(diǎn)時中斷執(zhí)行,可以用n指令進(jìn)行單步執(zhí)行
          s(tep):程序執(zhí)行到斷點(diǎn)時中斷執(zhí)行,可以用s指令進(jìn)行單步執(zhí)行進(jìn)某一函數(shù)
          q(uit):退出GDB

          8、附2:調(diào)試驅(qū)動程序和共享庫

          (1)調(diào)試LKM(Loadable Kernel Module)
          .在目標(biāo)板上插入模塊并輸出map信息,查看.text入口地址:
          #insmod -m hello.ko > map
          #grep .text map
          比如為0xc68c0060
          .在gdb中轉(zhuǎn)入LKM的調(diào)試信息:
          (gdb) add-symbol-file hello.ko 0xc68c0060 /*注意必須是LKM .text的入口地址)
          .正常調(diào)試

          (2)調(diào)試共享庫
          .樣例程序:
          test.c:
          #include
          int test(int a, int b)
          {
          int s = a b;
          printf("%dn", s);
          return s;
          }

          main.c:
          #include
          extern int test(int a, int b);
          int main(int argc, char* argv[])
          {
          int s = test(10, 20);
          return s;
          }

          Makefile:
          all: so main
          so:
          armv5-linux-uclibc-gcc -g test.c -shared -o libtest.so
          main:
          armv5-linux-uclibc-gcc -g main.c -L./ -ltest -o test.exe
          clean:
          rm -f *.exe *.so

          .編譯并設(shè)置環(huán)境變量
          #make

          在目標(biāo)板上:
          #export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./
          #gdbserver localhost:2345 ./test.exe

          .運(yùn)行g(shù)db客戶端
          #armv5-linux-uclibc-gdb
          (gdb)symbol-file test.exe
          (gdb)target remote 192.168.0.2:2345
          (gdb)b main
          (gdb)c

          .查看libtest.so的代碼在內(nèi)存中的位置。
          (從gdbserver的輸出或者用ps可以得到test.exe的進(jìn)程ID,這里假設(shè)PID是11547)
          在目標(biāo)板上:
          #cat /proc/11547/maps
          輸出:

          .........
          0076a000-0076c000 rwxp 0076a000 00:00 0
          00bbe000-00bbf000 r-xp 00bbe000 00:00 0
          00fcc000-00fcd000 r-xp 00000000 03:01 1238761 /root/test/gdbservertest/libtest.so
          00fcd000-00fce000 rwxp 00000000 03:01 1238761 /root/test/gdbservertest/libtest.so
          08048000-08049000 r-xp 00000000 03:01 1238765 /root/test/gdbservertest/test.exe
          08049000-0804a000 rw-p 00000000 03:01 1238765 /root/test/gdbservertest/test.exe
          ........

          由此可以知道:libtest.so的代碼在00fcc000-00fcd000之間。

          .查看libtest.so的.text段在內(nèi)存中的偏移位置:
          armv5-linux-uclibc-objdump -h libtest.so |grep .text
          輸出:
          .text 00000130 00000450 00000450 00000450 2**4
          即偏移位置為0x00000450

          .回到板子上的gdb窗口,加載libtest.so的符號表。
          (gdb)add-symbol-file libtest.so 0x00fcc450
          (這里0x00fcc450 = 0x00fcc000 0x00000450)

          .在共享庫的函數(shù)中設(shè)置斷點(diǎn)
          (gdb)b test

          .繼續(xù)調(diào)試共享庫



          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();