背景

项目:开发智能家居项目

chip:IMX6

host:Ubuntu16.04

eclipse IDE:version 2018-09(4.9.0)

搭建gdb+gdbserver+eclipse远程调试环境

记录搭建过程遇到的坑

 

gdbserver交叉编译

下载gdb源代码,官网:http://ftp.gnu.org/gnu/gdb/

下载最新的版本gdb-10.1版本,解压目录到/home/gateway/gdb下

新建build目录

$ mkdir bulid

再build目录下执行configure

$ ../gdb-10.1/configure --target=arm-poky-linux --host=arm-poky-linux --prefix=/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gdb/

 configure报错,提示:

checking build system type... x86_64-pc-linux-gnu
checking host system type... arm-poky-linux-gnu
checking target system type... arm-poky-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether ln works... yes
checking whether ln -s works... yes
checking for a sed that does not truncate output... /bin/sed
checking for gawk... no
checking for mawk... mawk
checking for gdbserver support... yes
checking for arm-poky-linux-gcc... arm-poky-linux-gcc
checking whether the C compiler works... no
configure: error: in `/home/gateway/gdb/build10.1':
configure: error: C compiler cannot create executables
See `config.log' for more details

 打开 bulid目录下的config.log日志文件,查看到错误信息:

gcc version 7.3.0 (GCC) 
configure:4336: $? = 0
configure:4325: arm-poky-linux-gcc -V >&5
arm-poky-linux-gcc: error: unrecognized command line option '-V'
arm-poky-linux-gcc: fatal error: no input files
compilation terminated.
configure:4336: $? = 1
configure:4325: arm-poky-linux-gcc -qversion >&5
arm-poky-linux-gcc: error: unrecognized command line option '-qversion'; did you mean '--version'?
arm-poky-linux-gcc: fatal error: no input files
compilation terminated.
configure:4336: $? = 1
configure:4356: checking whether the C compiler works
configure:4378: arm-poky-linux-gcc    conftest.c  >&5
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find crt1.o: No such file or directory
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find crti.o: No such file or directory
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find crtbegin.o: No such file or directory
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -lgcc
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -lgcc_s
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -lc
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -lgcc
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -lgcc_s
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find crtend.o: No such file or directory
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find crtn.o: No such file or directory

 查阅资料,需要添加CC字段,添加c compiler command,指明cross compiler的路径和sysroot的路径;再次执行configure命令

$ ../gdb-10.1/configure --target=arm-poky-linux --host=arm-poky-linux --prefix=/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gdb/ CC="/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux/arm-poky-linux-gcc --sysroot=/opt/fsl-imx-fb/4.14-sumo/sysroots/cortexa7hf-neon-poky-linux-gnueabi"

 还是报错,打开config.log日志文件,查看到错误信息:

/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: error: a.out uses VFP register arguments, /tmp/ccZCkn0C.o does not
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: failed to merge target specific data of file /tmp/ccZCkn0C.o

 查阅资料,说需要添加浮点运算处理方式,最开始是添加CFLAGS="-mfloat-abi=softfp"

$ ../gdb-10.1/configure --target=arm-poky-linux --host=arm-poky-linux --prefix=/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gdb/ CC="/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux/arm-poky-linux-gcc --sysroot=/opt/fsl-imx-fb/4.14-sumo/sysroots/cortexa7hf-neon-poky-linux-gnueabi" CFLAGS="-mfloat-abi=softfp"

编译还是报错,查看config.log,发现错误信息

/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: error: a.out uses VFP register arguments, /tmp/ccVSaX3d.o does not
/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: failed to merge target specific data of file /tmp/ccVSaX3d.o

 然后修改使用CFLAGS="-mfloat-abi=hard"

$ ../gdb-10.1/configure --target=arm-poky-linux --host=arm-poky-linux --prefix=/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gdb/ CC="/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux/arm-poky-linux-gcc --sysroot=/opt/fsl-imx-fb/4.14-sumo/sysroots/cortexa7hf-neon-poky-linux-gnueabi" CFLAGS="-mfloat-abi=hard"

configure执行成功,生成Makefile文件 

 

接下来执行make,编译报错,错误为:

make[2]: Entering directory '/home/gateway/gdb/build10.1/gdbsupport'
make  all-am
make[3]: Entering directory '/home/gateway/gdb/build10.1/gdbsupport'
  CXX      agent.o
In file included from ../../gdb-10.1/gdbsupport/common-defs.h:86:0,
                 from ../../gdb-10.1/gdbsupport/agent.cc:20:
../gnulib/import/stdio.h:43:15: fatal error: stdio.h: No such file or directory
 #include_next <stdio.h>

               ^~~~~~~~~
compilation terminated.
Makefile:503: recipe for target 'agent.o' failed

查阅资料,说可以屏蔽掉这一句,找到这个文件屏蔽后,还是报错

后续还有报错,重复定义变量,查阅资料说屏蔽掉,继续编译,后面继续报错说有变量未定义,放弃编译

换成gdb7.10版本编译

编译通过

在make install 过程中报错

说是权限不够,使用

$ sudo make install

 报错找不到runlib命令,说原因是切换到root权限后,没有把cross编译路径加入到环境变量来,后面修改/etc/profile,将我本地的cross编译路径

/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux

加入到环境变量中去,并且运行

$ source /etc/profile

安装成功

 

在输出路径下生成了

gcore

gdb

gdbserver

run

4个文件,再用cross编译器自带的 arm-poky-linux-strip 裁剪大小

$ arm-poky-linux-strip gdbserver

最终文件大小400多KB 

 

搭建调试

首先切换工程为debug模式编译

  1. Project->Properties->C/C++ Build 中确保Configuration是Debug的,如果不是切换到debug模式
  2. Run->Debug Configuration->C/C++ Remote Application中,创建对应工程的debug调试工程

注意点,刚开始我用的是GDB(DSF)Automatic Remote Debugging Laucher,但是gdb+gdbserver一直建立不起来通讯

后面还是换成GDB(DSF)Manaul Remote Debugging Laucher

  

在红框这里修改

切换到Debugger,修改GDB debugger对应的输出框内容为你的交叉编译器gdb版本的所在路径,我这里就是

/opt/fsl-imx-fb/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux/arm-poky-linux-gdb

     

 切换到Debugger->Shared Libraries,将交叉编译环境的lib路径包含进来,我这里是

/opt/fsl-imx-fb/4.14-sumo/sysroots/cortexa7hf-neon-poky-linux-gnueabi/lib

 

 切换到Debugger->Connection,Type选TCP,IP改为目标机IP地址,端口子定义,与在目标机上运行gdbserver时输入的端口号相同即可,我这里设置为2000

当然也可以选串口相连,我这里选了TCP连接方式

    3. 将生成的gdbserver拷贝到目标机上,我把gdbserver宝贝到和要调试的程序同一路径,cd到该路径下,运行

$ gdbserver localhost:2000 yourpogram_path

 成功的会显示

process yourpogram_path created; pid=xxx

Listening on port 2000

 这时在eclipse点击debug,就可以开始调试了

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐