建立交叉調試環境

  大家都知道,開發的嵌入式應用程序怎麼最終能在嵌入式設備中運行起來,前期需要不斷的調試,但是,嵌入式程序與PC下的程序調試存在很大的差別。這不像PC機下的程序直接在PC機下邊運行邊調試(GDB調試),而嵌入式的程序需要在嵌入式設備下運行,在上位機(PC)中調試,這就產生了交叉調試。

  本文的環境:

   虛擬機:vmware7.0

   Linux系統:Fedora9.0

   開發板:mini2440

   交叉編譯工具鏈:arm-linux-gcc(友善之臂提供,可去其官網下載)

  好了,萬事俱備,只欠開始行動了。

一、設置NFS共享

  交叉調試的前提需要設置NFS共享,在上位機下配置NFS服務,建立NFS共享目錄。

1、PC機下開啓NFS服務

[root@localhost home]# service nfs restart

2、設置共享目錄

[root@localhost home]# vim /etc/exports

打開exports文件後,添加如下內容:

/home/NFS       *(rw,sync,no_root_squash)

保存退出。

說明一下,上面的含義爲:/home/NFS 表示我的共享目錄,* 表示任意的開發板的ip地址,rw表示可讀寫,sync表示同步,no_root_squash表示有root權限。

接下來使用如下命令使共享目錄生效:

[root@localhost home]# exportfs -ra

不信看看:

[root@localhost home]# exportfs -v

是不是出現了:/home/NFS       <world>(rw,wdelay,no_root_squash,no_subtree_check),這樣就設置成功了共享目錄。

3、交叉編譯gdb(前提你的系統已經安裝了交叉編譯工具鏈)

下載一個gdb源碼包(我的爲:gdb-7.0a.tar.bz2),在虛擬機下解壓並編譯:

[root@localhost toolchain]# tar jxvf gdb-7.0a.tar.bz2

解壓後進入其目錄,然後

[root@localhost gdb-7.0]# ./configure --target=arm-linux -prefix=/opt/toolchain/gdb7.0_arm

-prefix=/opt/toolchain表示安裝在/opt/toolchain/gdb7.0_arm這個目錄下。

[root@localhost gdb-7.0]# make

[root@localhost gdb-7.0]# make install

這樣就安裝成功了,瞧瞧/opt/toolchain/gdb7.0_arm中有bin、share、lib目錄,進入bin目錄可以發現發現:

修改配置:

[root@localhost /]# vim /etc/profile

在最後面將PATH環境變量寫成如下(大家不要跟我一樣啊,因爲我做了好多的配置,你們只要將/opt/toolchain/gdb7.0_arm/bin加入這個PATH中就行了,格式跟下面的一樣):

export PATH=/opt/Qt4.6.4_forArm/bin:/opt/cmake/bin:/opt/toolchain/4.4.3/bin:/opt/toolchain/gdb7.0_arm/bin:$PATH

保存退出,並設置生效:

[root@localhost /]# source /etc/profile

4、交叉編譯gdb-server

進入解壓的源碼包gdb7.0中,可以看到其中有個gdb目錄,gdb目錄下有個gdbserver目錄,進入這個目錄:

[root@localhost gdb-7.0]# cd gdb/gdbserver

交叉編譯安裝:

[root@localhost gdbserver]# ./configure --target=arm-linux --host=arm-linux

[root@localhost gdbserver]# make

這樣交叉編譯gdbserver成功了,可以發現在當前目錄下有個:gdbserver(可執行文件),將其拷貝之共享目錄(我的爲:/home/NFS)。

5、拷貝libthread庫至共享目錄(網上這麼說的,我沒有做這一步,大家可以參考下,不礙事的)

在交叉編譯工具鏈中有libthread_db-1.0.so這個動態鏈接文件、將其拷貝至/home/NFS/lib中(爲了方便管理,我在NFS目錄下建立一個lib目錄,所有外來庫文件都放在這個lib目錄下),建立符號鏈接:

[root@localhost lib]# ln -s libthread_db-1.0.so libthread_db.so

[root@localhost lib]# ln -s libthread_db-1.0.so libthread_db.so.1

當你在開發板mount的時候,先設置一下庫文件搜索路徑:LD_LIBRARAY_PATH

三、開發板下mount共享目錄

1、連接開發板,設置好網絡(開發板的ip地址,我的開發板ip地址爲:172.16.28.100,大家根據自己實際情況設定,我的Linux系統的ip爲:172.16.28.22)

2、開發板下執行:

[root@FriendlyARM /]# mount -t nfs -o intr,nolock,rsize=1024,wsize=1024 172.16.28.22:/home/NFS /mnt

mount上後,你可以在開發板的/mnt 目錄下發現其中的內容和PC機下共享目錄/home/NFS 中內容是一樣的。

3、測試

在PC的共享目錄下編寫一個print.c文件,如:

保存後並交叉編譯:

[root@localhost NFS]# arm-linux-gcc -Wall -g print.c -o print

得到了一個可執行的print文件。

進入開發板掛載的目錄中(我的/mnt),執行如下:

[root@FriendlyARM /mnt]# ./gdbserver 172.16.28.22:6666 print

172.16.28.22爲我的Linux系統的ip地址,6666爲端口號(可以自行設定,最好大於1024,使得不和系統的端口衝突),print爲要交叉調試的可執行文件。如果執行成功出現:

Process print created; pid = 752
Listening on port 6666

呵呵,pid可能各不相同(不必擔心)。

在Linux的共享目錄下執行:

[root@localhost NFS]# arm-linux-gdb print

出現:

GNU gdb (GDB) 7.0
Copyright (C) 2009 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/NFS/print...done.
(gdb) target remote 172.16.28.100:6666

(紅色表示敲入的命令),172.16.28.100(我的開發板的ip),回車後:

Remote debugging using 172.16.28.100:6666
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
0x400007b0 in ?? ()
(gdb) list 

(紅色爲命令),回車後:

1       #include <stdio.h>
2
3       int main(void)
4       {
5               int i;
6
7               for (i = 0; i < 10; i++)
8               {
9                       printf("i=%d ", i);
10                      printf("\n");
(gdb)
顯示了源碼,哈哈哈,交叉調試基本上成功了,現在可以用基本的GDB命令可以調試了(現在程序在開發板中運行,而我們在上位機下調試)。


注意:交叉調試不能用run命令來執行,因爲remote後,程序就開始了,只要設置斷點,然後c命令就會跑到斷點處。


在寫此文時參考了:http://www.linuxidc.com/Linux/2011-02/32733.htm,在此表示感謝,歡迎大家轉載,轉載請註明出處CSDN。email:[email protected]




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章