arm_linux 交叉编译环境建立之 - gdb和gdbserver 和QT远程调试

建立环境

cd ~/arm-linux
mkdir -p gdb/src
mkdir -p gdb/build
export SRCDIR=~/arm-linux/gdb/src
export BUILDDIR=~/arm-linux/gdb/build
export TARGET=arm-linux-gnueabi
export PREFIX=/usr/local/arm/4.8.2
export TARGET_PREFIX=$PREFIX/$TARGET
export PATH=$PATH:$PREFIX/bin

下载gdb源码

cd $SRCDIR
下载版本:7.6.1
wget -c http://ftp.gnu.org/gnu/gdb/gdb-7.6.1.tar.bz2
解压:
tar -jxvf gdb-7.6.1.tar.bz2

编译 gdb

cd $BUILDDIR
$SRCDIR/gdb-7.6.1/configure --prefix=$PREFIX --target=$TARGET
make

常见错误:

...
/bin/bash ./libtool --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I../../../src/gdb-7.6.1/bfd -I. -I../../../src/gdb-7.6.1/bfd -I../../../src/gdb-7.6.1/bfd/../include  -DHAVE_bfd_elf32_littlearm_vec -DHAVE_bfd_elf32_bigarm_vec -DHAVE_bfd_elf32_little_generic_vec -DHAVE_bfd_elf32_big_generic_vec  -DBINDIR='"/usr/local/arm/4.8.2/bin"'  -W -Wall -Wstrict-prototypes -Wmissing-prototypes -Wshadow -Werror -g -O2 -MT opncls.lo -MD -MP -MF .deps/opncls.Tpo -c -o opncls.lo ../../../src/gdb-7.6.1/bfd/opncls.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I../../../src/gdb-7.6.1/bfd -I. -I../../../src/gdb-7.6.1/bfd -I../../../src/gdb-7.6.1/bfd/../include -DHAVE_bfd_elf32_littlearm_vec -DHAVE_bfd_elf32_bigarm_vec -DHAVE_bfd_elf32_little_generic_vec -DHAVE_bfd_elf32_big_generic_vec -DBINDIR=\"/usr/local/arm/4.8.2/bin\" -W -Wall -Wstrict-prototypes -Wmissing-prototypes -Wshadow -Werror -g -O2 -MT opncls.lo -MD -MP -MF .deps/opncls.Tpo -c ../../../src/gdb-7.6.1/bfd/opncls.c -o opncls.o
In file included from ../../../src/gdb-7.6.1/bfd/opncls.c:26:0:
../../../src/gdb-7.6.1/bfd/opncls.c: In function ‘bfd_fopen’:
./bfd.h:529:65: error: right-hand operand of comma expression has no effect [-Werror=unused-value]
 #define bfd_set_cacheable(abfd,bool) (((abfd)->cacheable = bool), TRUE)
                                                                 ^
../../../src/gdb-7.6.1/bfd/opncls.c:263:5: note: in expansion of macro ‘bfd_set_cacheable’
     bfd_set_cacheable (nbfd, TRUE);
     ^
cc1: all warnings being treated as errors
Makefile:1607: recipe for target 'opncls.lo' failed
make[4]: *** [opncls.lo] Error 1
make[4]: Leaving directory '/home/weisf/arm-linux/build/gdb/bfd'
Makefile:1649: recipe for target 'all-recursive' failed
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory '/home/weisf/arm-linux/build/gdb/bfd'
Makefile:1135: recipe for target 'all' failed
make[2]: *** [all] Error 2
make[2]: Leaving directory '/home/weisf/arm-linux/build/gdb/bfd'
Makefile:2579: recipe for target 'all-bfd' failed
make[1]: *** [all-bfd] Error 2
make[1]: Leaving directory '/home/weisf/arm-linux/build/gdb'
Makefile:828: recipe for target 'all' failed
make: *** [all] Error 2

原因是比较新的编译器如gcc4.5 以上的编译器,对 #define bfd_set_cacheable(abfd,bool) (((abfd)->cacheable = bool), TRUE) 这样的语法不支持,解决方法是加上忽略告警的选项 --disable-werror:

make clean
$SRCDIR/gdb-7.6.1/configure --prefix=$PREFIX --target=$TARGET --disable-werror
make

编译完成后,安装:

sudo make install

有错误提示:Makefile:370: recipe for target ‘gdb.info’ failed
但是已经安装成功了。在/usr/local/arm/4.8.2/bin 下面能够看到 arm-linux-gnueabi-gdb 这个文件

$ ls /usr/local/arm/4.8.2/bin/
arm-linux-addr2line  arm-linux-gdb                arm-linux-gnueabi-gcc-4.8.2   arm-linux-gnueabi-objdump  arm-linux-objcopy
arm-linux-ar         arm-linux-gdbtui             arm-linux-gnueabi-gcc-ar      arm-linux-gnueabi-ranlib   arm-linux-objdump
arm-linux-as         arm-linux-gnueabi-addr2line  arm-linux-gnueabi-gcc-nm      arm-linux-gnueabi-readelf  arm-linux-ranlib
arm-linux-c++        arm-linux-gnueabi-ar         arm-linux-gnueabi-gcc-ranlib  arm-linux-gnueabi-run      arm-linux-readelf
arm-linux-c++filt    arm-linux-gnueabi-as         arm-linux-gnueabi-gcov        arm-linux-gnueabi-size     arm-linux-run
arm-linux-cpp        arm-linux-gnueabi-c++        arm-linux-gnueabi-gdb         arm-linux-gnueabi-strings  arm-linux-size
arm-linux-elfedit    arm-linux-gnueabi-c++filt    arm-linux-gnueabi-gprof       arm-linux-gnueabi-strip    arm-linux-strings
arm-linux-g++        arm-linux-gnueabi-cpp        arm-linux-gnueabi-ld          arm-linux-gprof            arm-linux-strip
arm-linux-gcc        arm-linux-gnueabi-elfedit    arm-linux-gnueabi-ld.bfd      arm-linux-ld               rename.sh
arm-linux-gcc-4.6.3  arm-linux-gnueabi-g++        arm-linux-gnueabi-nm          arm-linux-ld.bfd
arm-linux-gcov       arm-linux-gnueabi-gcc        arm-linux-gnueabi-objcopy     arm-linux-nm

编译gdbserver

cd $BUILDDIR
mkdir gdbserver
cd gdbserver
$SRCDIR/gdb/gdbserver/configure --host=arm-linux-gnueabi --target=arm-linux-gnueabi --prefix=/usr/local/arm/4.8.2 --disable-werror

如果不加 --disable-werror,会报出一个错误:

In file included from /usr/local/arm/4.8.2/arm-linux-gnueabi/include/sys/sysinfo.h:24:0,
                 from ../../src/gdb-7.6.1/gdb/gdbserver/../common/linux-osdata.c:29:
/usr/local/arm/4.8.2/arm-linux-gnueabi/include/linux/kernel.h:720:2: error: #warning Attempt to use kernel headers from user space, see http: [-Werror=cpp]
 #warning Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders
  ^
cc1: all warnings being treated as errors
Makefile:534: recipe for target 'linux-osdata.o' failed
make: *** [linux-osdata.o] Error 1

用 --disable-werror 忽略这个错误
另外,还会报如下的错误:

arm-linux-gnueabi-gcc -g -O2    -I. -I../../src/gdb-7.6.1/gdb/gdbserver -I../../src/gdb-7.6.1/gdb/gdbserver/../common -I../../src/gdb-7.6.1/gdb/gdbserver/../regformats -I../../src/gdb-7.6.1/gdb/gdbserver/../../include -I../../src/gdb-7.6.1/gdb/gdbserver/../gnulib/import -Ibuild-gnulib-gdbserver/import -Wall -Wdeclaration-after-statement -Wpointer-arith -Wformat-nonliteral -Wno-char-subscripts -Wempty-body -Werror -DGDBSERVER -c -o hostio.o -MT hostio.o -MMD -MP -MF .deps/hostio.Tpo ../../src/gdb-7.6.1/gdb/gdbserver/hostio.c
../../src/gdb-7.6.1/gdb/gdbserver/hostio.c: In function ‘require_filename’:
../../src/gdb-7.6.1/gdb/gdbserver/hostio.c:67:20: error: ‘PATH_MAX’ undeclared (first use in this function)
       if (count >= PATH_MAX - 1)
                    ^

这是因为交叉编译器的头文件中,PATH_MAX 是在 linux/limits.h 中定义的而不是 limits.h,因此,打开报错的文件,在 #include <limits.h> 这样后面,新加一行 #include <linux/limits.h>
vim …/…/src/gdb-7.6.1/gdb/gdbserver/hostio.c

 21 #include "server.h"
 22 #include "gdb/fileio.h"
 23 
 24 #include <fcntl.h>
 25 #include <limits.h>
 26 
 27 #include <unistd.h>
 28 /* HAOJC ADD */
 29 #include <linux/limits.h>
 30 
 31 extern int remote_debug;
 32 

这个同样的错误,还会出现在另外一个文件 …/…/src/gdb-7.6.1/gdb/gdbserver/thread-db.c,用同样的方法处理它。
然后,编译就通过了。
然后,可以安装一下:
sudo make install
这会生成 /usr/local/arm/4.8.2/bin/arm-linux-gnueabi-gdbserver

部署到 arm linux

在主机上安装 nfs server

sudo apt install nfs-kernel-server

设置服务访问配置:

sudo vim /etc/exports
添加一行:
/home/weisf/nfs *(rw,sync,no_root_squash,no_subtree_check)

/home/weisf/nfs 是用于共享文件的目录,
“*” 表示所有主机可以访问
rw - 表示可以读和写;
no_root_squash - 不降低root用户的权限(不会将root用户及所属组都映射为匿名用户或用户组)
no_subtree_check - 即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率。

启动 nfs 服务:

sudo service nfs-kernel-server start

然后,就可以从 arm 板上加载 nfs 实现共享了。

在arm linux 上加载 nfs 目录

192.168.1.20 是主机IP地址

mkdir /mnt/nfs
mount -t nfs -o nolock 192.168.1.20:/home/weisf/nfs /mnt/nfs

如果想开机就加载,则新建初始化文件 rc.local

vi /etc/rc.local
输入:
mount -t nfs -o nolock 192.168.1.20:/home/weisf/nfs /mnt/nfs
保存
chmod a+x /etc/rc.local

然后在 /etc/init.d/rc 脚本里面调用它,在文档的最后,加上:

#Uncomment to cause psplash to exit manually, otherwise it exits when it sees a
#if [ "x$runlevel" != "xS" ] && [ ! -x /etc/init.d/xserver-nodm ]; then
#    if type psplash-write >/dev/null 2>&1; then
#        TMPDIR=/mnt/.psplash psplash-write "QUIT" || true
#       umount /mnt/.psplash
#    fi
#fi

/etc/rc.local

将 gdbserver 通过 nfs 发送给arm板

加载nfs目录后,从主机上将gdbserver程序复制到 nfs目录:
cp /usr/local/arm/4.8.2/bin/arm-linux-gnueabi-gdbserver /home/weisf/nfs/

从arm板上,就可以看到:
/mnt/nfs/arm-linux-gnueabi-gdbserver

arm linux 目标板远程调试

主机:192.168.1.20
目标机:192.168.1.232

在主机上,编写程序:
vim test.c

#include <stdio.h>
int func(int n)
{
    int sum=0, i;
    for (i=0; i<n; i++)
    {
        sum += i;
    }
    return sum;
}

int main(int argc, char* argv[])
{
    int i;
    long result = 0;
    for (i=0; i<=100; i++)
    {
        result += i;
    }
    printf("result[1-100] = %d \n",result);
    printf("resutl[1-225] = %d \n",func(255));
    return 0;
 }

在主机上编译:
arm-linux-gcc -g test.c -o test
一定要用 -g,才可以在目标文件中带上调试信息,否则不可调试。
然后,将 test 复制到 nfs:
cp test /home/weisf/nfs/

在arm板上:
ls /mnt/nfs
可以看到 test。
执行:

root@ci335xa:/mnt/nfs# ./arm-linux-gnueabi-gdbserver 192.168.1.20:1234 test
Process test created; pid = 1107
Listening on port 1234

向 192.168.1.20 打开监听端口 1234 来调试 test 程序。

在主机上进行调试:

weisf@worksch:~/work/test$ arm-linux-gnueabi-gdb test
Python Exception <type 'exceptions.ImportError'> No module named gdb: 

warning: 
Could not load the Python gdb module from `/usr/local/arm/4.8.2/share/gdb/python'.
Limited Python support is available from the _gdb module.
Suggest passing --data-directory=/path/to/gdb/data-directory.

GNU gdb (GDB) 7.6.1
Copyright (C) 2013 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-linux-gnueabi".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/weisf/work/test/test...done.
(gdb) l
Cannot access memory at address 0x0
6	    {
7	        sum += i;
8	    }
9	    return sum;
10	}
11	 
12	int main(int argc, char* argv[])
13	{
14	    int i;
15	    long result = 0;
(gdb) b 15
Breakpoint 1 at 0x8478: file test.c, line 15.
(gdb) b func
Breakpoint 2 at 0x8410: file test.c, line 4.
(gdb) c
Continuing.
warning: Could not load shared library symbols for 2 libraries, e.g. /lib/libc.so.6.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?

Breakpoint 1, main (argc=1, argv=0xbefffbc4) at test.c:15
15	    long result = 0;
(gdb) n
16	    for (i=0; i<=100; i++)
(gdb) n
18	        result += i;
(gdb) n
16	    for (i=0; i<=100; i++)
(gdb) p result
$1 = 0
(gdb) n
18	        result += i;
(gdb) n
16	    for (i=0; i<=100; i++)
(gdb) p result
$2 = 1
(gdb) 

注意到有个警告:
Could not load the Python gdb module from `/usr/local/arm/4.8.2/share/gdb/python’.
Limited Python support is available from the _gdb module.

这是缺少 python 模块,将源码中的python 库文件复制过来:

cd /usr/local/arm/4.8.2/share/gdb/python/gdb
sudo cp -R ~/arm-linux/gdb/src/gdb-7.6.1/gdb/python/lib/gdb/* ./

QT远程调试

在主机上,设置QT的生成文件目录到 nfs 目录下,这样可以即时与目标板共享编译生成的目标程序。
在这里插入图片描述
然后,执行编译,会在 /home/weisf/nfs 目录下生成目标程序。

在目标板上,执行:

cd /mnt/nfs/dae/debug
 /mnt/nfs/arm-linux-gnueabi-gdbserver 192.168.1.20:1234 ./DAE-gui -qws

然后,在主机上qtcreator界面中,选择 Debug -> Start Debugging -> Attach to Running Debug Server…
在这里插入图片描述
设置远程arm板上的地址和端口,点击OK。可以选择 break at “main”,这样可以执行的时候,在main入口处设置断点暂停。
然后,就可以随意在主机上,执行各种调试操作了,设置断点、监视变量、调用堆栈等。
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译 `arm-linux-gnueabih-gdb` 和 `arm-linux-gnueabih-gdbserver` 的步骤如下: 1. 下载 GNU 工具链 - 从官网下载源代码:https://ftp.gnu.org/gnu/binutils/binutils-2.36.tar.gz - 解压缩源代码:`tar -zxvf binutils-2.36.tar.gz` 2. 配置交叉编译环境变量 - `export CC=arm-linux-gnueabihf-gcc` - `export CXX=arm-linux-gnueabihf-g++` - `export AR=arm-linux-gnueabihf-ar` - `export AS=arm-linux-gnueabihf-as` - `export LD=arm-linux-gnueabihf-ld` - `export RANLIB=arm-linux-gnueabihf-ranlib` 3. 配置和编译 `binutils` - 进入源代码目录:`cd binutils-2.36` - 创建编译目录:`mkdir build && cd build` - 配置交叉编译环境:`../configure --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf --prefix=/usr/local/arm-linux-gnueabihf` - 编译:`make` - 安装:`make install` 4. 配置和编译 `gdb` - 下载 GDB 源代码:https://ftp.gnu.org/gnu/gdb/gdb-10.1.tar.gz - 解压缩源代码:`tar -zxvf gdb-10.1.tar.gz` - 进入源代码目录:`cd gdb-10.1` - 创建编译目录:`mkdir build && cd build` - 配置交叉编译环境:`../configure --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf --prefix=/usr/local/arm-linux-gnueabihf` - 编译:`make` - 安装:`make install` 5. 配置和编译 `gdbserver` - 下载 GDB 源代码:https://ftp.gnu.org/gnu/gdb/gdb-10.1.tar.gz - 解压缩源代码:`tar -zxvf gdb-10.1.tar.gz` - 进入源代码目录:`cd gdb-10.1/gdb/gdbserver` - 创建编译目录:`mkdir build && cd build` - 配置交叉编译环境:`export CC=arm-linux-gnueabihf-gcc` - 配置编译选项:`../configure --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf --prefix=/usr/local/arm-linux-gnueabihf` - 编译:`make` - 安装:`make install` 注意:以上步骤仅供参考,实际操作时可能会因为环境和版本差异而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值