制作交叉调试器

来源:互联网 发布:网络电视机顶盒 编辑:程序博客网 时间:2024/05/16 15:07
 

 先看一篇转载的关于gdb和gdbserver的生成。

在对嵌入式进行调试之前,必须要生成调试器。经典地,Linux平台下的调试器为gdb。


1、下载gdb:
下载地址为:

按照一般的想法,最新版本越好,因此下载7.2这个版本。当然,凡事无绝对。
我们以gdb-7.2.tar.bz2 这个文件为例。
2、解压缩:$ tar jxvf gdb-7.2.tar.bz2

注:小技巧:Linux下一般压缩文件后缀为.tar.bz2和.tar.gz,它们解压命令有两三个选项是一致的:

xf(v),前者再加上j选项,后者再加上z选项。

3、进入该目录:

$ cd gdb-7.2/

4、配置:

$./configure --target=arm-linux --program-prefix=arm-linux- --prefix=/opt/gdb/

注:--target=arm-linux意思是说目标平台是运行于ARM体系结构的linux内核;--program-prefix=arm-linux-是指生成的可执行文件的前缀,比如arm-linux-gdb,--prefix是指生成的可执行文件安装在哪个目录,这个目录需要根据实际情况作选择。如果该目录不存在,会自动创建,当然,权限足够的话。

5、编译、安装

$  make

$ make install

幸运的话,会在--prefix指定的目录下生成三个子目录:bin、lib、share,我们需要的arm-linux-gdb就在其中的bin目录下。
如果你不小心查看它的大小的话,会发觉它有14MB那么大!天呐!怎么会占这么多空间?没关系,我们可以为它瘦身。没错!就是使用strip命令!

$ strip arm-linux-gdb -o arm-linux-gdb-stripped
$ ls -lh
总计 33M

-rwxr-xr-x 1  root 14M 12-14 16:16 arm-linux-gdb

-rwxr-xr-x 1  root 3.1M 12-14 16:25 arm-linux-gdb-stripped

 

可以看到,strip后的文件大小只有3.1MB,瘦身效果明显!如果做广告的话,绝对有说服力。
这个文件就是我们以后远程调试时在主机上运行的交叉调试器了:在主机上执行,调试的却是另一种体系结构的代码。但是,光有主机的调试器还不够。还需要在目标板上运行一个叫gdbserver的东东。这个东东是怎么来的呢?

1、在刚才那个gdb解压后的目录:gdb-7.2,进入./gdb/gdbserver子目录

$ cd gdb/gdbserver

$ pwd

/home/gotohell/soft/gdb-7.2/gdb/gdbserver

2、配置

$ ./configure --target=arm-linux --host=arm-linux

这里的--host指定了生成可执行文件运行的平台和系统:运行于ARM平台的Linux系统
3、编译

$  make

一切如果没有问题的话,会在当前目录下生成gdbserver这个只有在ARM下运行的文件。

注:网上绝大部分资料显示,这里的make会出错,说某个头文件不存在。经本人亲自挂帅出征,在gdb的7.2版本不存在那个问题。低于7.2版本的就要注意了。解决方法也简单,在配置后(执行./configure后),将生成的config.h文件中

#define HAVE_SYS_REG_H 1

语句注释掉即可。
另外,也有资料说编译gdbserver需要指定交叉编译器(即执行make CC=arm-linux-gcc),但此处直接make,也没问题。当然,交叉编译器需要安装好并设置好环境变量。我们看一下这个是什么家伙:

$ file gdbserver

gdbserver: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses

shared libs), for GNU/Linux 2.6.14, not stripped

32位,ARM平台,动态链接,未strip。
同样,我们也减小它的体积:、

$ arm-linux-strip gdbserver -o gdbserver-stripped

$ ls -lh | grep gdbserver

-rwxr-xr-x 1  root 534K 12-14 15:50 gdbserver

-rwxr-xr-x 1  root 173K 12-14 16:36 gdbserver-stripped

瘦身效果同样那么明显!
注意,这里必须使用strip的交叉版本,也就是arm-linux-strip。

到此,我们生成了两个重量级别的文件:arm-linux-gdb和gdbserver。它们的版本是一致的,这一点非常重要。我们需要将gdbserver下载到开发板中,——可以通过各种各样的手段,包括但不限于NFS。调试时需要在开发板中运行这个程序。同时在主机中执行arm-linux-gdb调试器。

-----------------------------------------------------------------------------------------------------分割线

下面 说说 在按照上述过程中遇到的问题:

1   我先按 。./configure

                        make 

                        make install

的顺序安装了gdb7.2,那么接下来按 $./configure --target=arm-linux --program-prefix=arm-linux- --prefix=/opt/gdb/

在接下来make的过程中会出现如下错误:

Configuring in ./intl
configure: loading cache ./config.cache
configure: error: `target_alias' has changed since the previous run:
configure:   former value:  `i686-pc-linux-gnu'
configure:   current value: `arm-linux'
configure: error: in `/home/w/下载/gdb-7.2/intl':
configure: error: changes in the environment can compromise the build
configure: error: run `make distclean' and/or `rm ./config.cache' and start over
make[1]: *** [configure-intl] 错误 1
make[1]:正在离开目录 `/home/w/下载/gdb-7.2'
make: *** [all] 错误 2

解决的方法:暂时没去想

2 换到另一个虚拟机上,这次不再直接安装 按照转载的文章的步骤可以顺利完成gdb的编译安装。但是在编译gdbserver的时候出现了问题,虽然 ./configure --target=arm-linux --host=arm-linux 指定了目标平台和 主机都是arm-linux但是编译出来结果

gdbserver: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped

还是intel 80386的。

即使是指定CC为交叉编译器还是不行。

export CC=arm-none-linux-gnueabi-gcc

通过分析输出的信息

checking for arm-linux-gcc... no
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no

由于我安装的交叉编译器是codesource的交叉编译工具链,似乎是我的交叉编译名字不符合它的要求,一定要叫 arm-linux-gcc ?于是创建一系列软链接

n -s arm-none-linux-gnueabi-gdbtui arm-linux-gdbtui
ln -s arm-none-linux-gnueabi-addr2line arm-linux-addr2line
ln -s arm-none-linux-gnueabi-gprof arm-linux-gprof
ln -s arm-none-linux-gnueabi-ar arm-linux-ar
ln -s arm-none-linux-gnueabi-as arm-linux-as
ln -s arm-none-linux-gnueabi-nm arm-linux-nm
ln -s arm-none-linux-gnueabi-ld arm-linux-ld
ln -s arm-none-linux-gnueabi-c++ arm-linux-c++
ln -s arm-none-linux-gnueabi-objcopy arm-linux-objcopy
ln -s arm-none-linux-gnueabi-c++filt arm-linux-c++filt
ln -s arm-none-linux-gnueabi-cpp arm-linux-cpp
ln -s arm-none-linux-gnueabi-ranlib arm-linux-ranlib
ln -s arm-none-linux-gnueabi-objdump arm-linux-objdump
ln -s arm-none-linux-gnueabi-readelf arm-linux-readelf
ln -s arm-none-linux-gnueabi-size arm-linux-size
ln -s arm-none-linux-gnueabi-gcov arm-linux-gcov
ln -s arm-none-linux-gnueabi-strings arm-linux-strings
ln -s arm-none-linux-gnueabi-gdb arm-linux-gdb
ln -s arm-none-linux-gnueabi-strip arm-linux-strip
ln -s arm-none-linux-gnueabi-sprite arm-linux-sprite

ln -s arm-none-linux-gnueabi-gcc-4.3.3 arm-linux-gcc-4.3.3
ln -s arm-none-linux-gnueabi-gcc arm-linux-gcc


重新执行命令 CC=arm-linux-gcc ./configure --target=arm-linux --host=arm-linux

发现 找到了交叉编译器,变成yes了

checking for arm-linux-gcc... arm-linux-gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... yes


以为大功告成了,结果又出现问题

/opt/tools/arm-2009q1/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/../../../../arm-none-linux-gnueabi/bin/ld: xml-builtin.o: Relocations in generic ELF (EM: 3)
/opt/tools/arm-2009q1/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/../../../../arm-none-linux-gnueabi/bin/ld: xml-builtin.o: Relocations in generic ELF (EM: 3)
/opt/tools/arm-2009q1/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/../../../../arm-none-linux-gnueabi/bin/ld: xml-builtin.o: Relocations in generic ELF (EM: 3)
xml-builtin.o: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
make: *** [gdbserver] Error 1


有点郁闷,错误提示文件格式不对。执行命令 file xml-builtin.o

xml-builtin.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

 


不对呀,已经指定了是交叉编译了怎么生成的目标文件还是 intel 80386呢?

 

原来是之前执行过没有设置好交叉编译的时候,执行过了make命令,所以生成了这样的目标文件。于是 执行 make clean 清除掉这些之前生成的文件

重新 执行CC=arm-linux-gcc ./configure --target=arm-linux --host=arm-linux make

再来查看生成的gdbserver

执行命令 file gdbserver
gdbserver: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.14, not stripped

终于 看到ARM!

单枪匹马摸索嵌入式的这些东西还真是让人头疼,各种问题让人摸不着头脑。好在经过长时间的尝试和摸索找到了解决的方法。

 

原创粉丝点击