GDB交叉编译及远程调试记录-arm侧未能启动

-------GDB远程调试--------
参考文档 搭建交叉调试环境 arm-linux-gdb配合gdbserver(https://www.cnblogs.com/cherishui/p/4414013.html)

环境

操作系统环境
$ uname -r
4.15.0-50-generic
$ lsb_release -a
No LSB modules are available.
Distributor ID: LinuxMint
Description: Linux Mint 19.1 Tessa
编译环境
$ export PATH=$PATH:.:/opt/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabi/bin
$ arm-linux-gnueabi-gcc -v
gcc 版本 7.4.1 20181213 [linaro-7.4-2019.02 revision 56ec6f6b99cc167ff0c2f8e1a2eed33b1edc85d4] (Linaro GCC 7.4-2019.02)

编译

准备版本
xy@xydpc:/usr/local/src$ curl -O http://ftp.gnu.org/gnu/gdb/gdb-8.3.tar.xz
$tar Jxvf gdb-8.3.tar.xz

1. 编译宿主机的gdb

$ cd gdb-8.3/
$ ./configure --target=arm-linux
$ make
$ make install
上面的步骤指定了是用gcc编译,如果错误的制定了CC,需要删除 gdb-8.3目录重新解压。
安装前
xy@xydpc:/usr/local/src/gdb-8.3$ gdb -v
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
安装后检查,新生成的程序在/usr/local/bin/目录下,名字为arm-linux-gdb
xy@xydpc:/usr/local/src/gdb-8.3$ arm-linux-gdb -v
GNU gdb (GDB) 8.3
Copyright © 2019 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.
xy@xydpc:/usr/local/src/gdb-8.3$ which arm-linux-gdb
/usr/local/bin/arm-linux-gdb

2. 编译目标机的gdbserver

$ cd gdb/gdbserver
$ ./configure --target=arm-linux --host=arm-linux
$ make CC=arm-linux-gnueabi-gcc
报错:
linux-arm-low.c: In function ‘CORE_ADDR arm_sigreturn_next_pc(regcache*, int, int*)’:
linux-arm-low.c:782:29: error: ‘__NR_sigreturn’ was not declared in this scope
gdb_assert (svc_number == __NR_sigreturn || svc_number == __NR_rt_sigreturn);

参考:远程调试之gdb移植(https://blog.csdn.net/youjian124/article/details/75204860)
在这个linux-arm-low.c文件中加上
#ifndef __NR_sigreturn
#include <asm/unistd_32.h>
#endif
再次执行 make CC=arm-linux-gnueabi-gcc

报错2
CXX xml-builtin.o
CXXLD gdbserver
/usr/bin/ld: build-gnulib-gdbserver/import/libgnu.a(fcntl.o):普通ELF重定位(M: 40)
/usr/bin/ld: build-gnulib-gdbserver/import/libgnu.a(fcntl.o):普通ELF重定位(M: 40)
/usr/bin/ld: build-gnulib-gdbserver/import/libgnu.a(fcntl.o):普通ELF重定位(M: 40)
/usr/bin/ld: build-gnulib-gdbserver/import/libgnu.a(fcntl.o):普通ELF重定位(M: 40)
/usr/bin/ld: build-gnulib-gdbserver/import/libgnu.a(fcntl.o):普通ELF重定位(M: 40)
build-gnulib-gdbserver/import/libgnu.a: 无法添加符号: 文件格式错误
collect2: error: ld returned 1 exit status
Makefile:399: recipe for target ‘gdbserver’ failed
make: *** [gdbserver] Error 1

$ ./configure --target=arm-linux-gnueabi --host=arm-linux-gnueabi
make CC=arm-linux-gnueabi-gcc

解决方法:
清除后重新编译,要求GCC的前缀和host、target要保持一样。
$ make distclean
$ ./configure --target=arm-linux-gnueabi --host=arm-linux-gnueabi
$ make CC=arm-linux-gnueabi-gcc
验证
xy@xydpc:/usr/local/src/gdb-8.3/gdb/gdbserver$ file gdbserver
gdbserver: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-, for GNU/Linux 3.2.0, BuildID[sha1]=8c7ef34aae5c4ac527d6dcbfb1f9890bd7784002, with debug_info, not stripped

测试

检查lib库文件
xy@xydpc:/usr/local/src/gdb-8.3/gdb/gdbserver$ strings gdbserver |grep lib |grep so
/lib/ld-linux.so.3
libdl.so.2
libstdc++.so.6
libm.so.6
libgcc_s.so.1
libc.so.6
libthread_db.so.1
在目标机检查,对比发现,所有文件都已经有了。

cd /lib
# ls -al ld-linux.so.3 libdl.so.2 libstdc++.so.6 libm.so.6 libgcc_s.so.1  libc.s
o.6 libthread_db.so.1
lrwxrwxrwx    1 stb      stb             12 Oct 28  2014 ld-linux.so.3 -> ld-2.11.1.so
lrwxrwxrwx    1 stb      stb             14 Oct 28  2014 libc.so.6 -> libc-2.11.1.so
lrwxrwxrwx    1 stb      stb             15 Oct 28  2014 libdl.so.2 -> libdl-2.11.1.so
-rwxrwxrwx    1 stb      stb          42740 Oct 16  2014 libgcc_s.so.1
lrwxrwxrwx    1 stb      stb             14 Oct 28  2014 libm.so.6 -> libm-2.11.1.so
lrwxrwxrwx    1 stb      stb             19 Oct 28  2014 libstdc++.so.6 -> libstdc++.so.6.0.12
lrwxrwxrwx    1 stb      stb             19 Oct 28  2014 libthread_db.so.1 -> libthread_db-1.0o
# 

宿主机增加地址
$ sudo ip ad add 192.168.1.202/24 dev wlp7s0 注意一定要加掩码/24,否则默认是32位的。
搭建一个ftp服务器
$ sudo apt-get install vsftpd
$ sudo systemctl start vsftpd
说明:可以不用配置,即使打开匿名anonymous_enable=YES
也会报错
$ journalctl -u vsftpd
6月 07 20:48:57 xydpc vsftpd[22513]: pam_unix(vsftpd:auth): check pass; user unknown
6月 07 20:48:57 xydpc vsftpd[22513]: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=anomymous rhost=::ffff:192.168.1.202

目标机器

# busybox ftpget 192.168.1.202 /sbin/gdbserver /usr/local/src/gdb-8.3/gdb/gdbserver/gdbserver -u xy -p xxxpassword
库文件不匹配,无法运行
# chmod +x /sbin/gdbserver                                                                     
# gdbserver -v                                                                                 
gdbserver: /lib/libstdc++.so.6: no version information available (required by gdbserver)       
gdbserver: /lib/libstdc++.so.6: no version information available (required by gdbserver)       
gdbserver: /lib/libstdc++.so.6: no version information available (required by gdbserver)       
gdbserver: /lib/libstdc++.so.6: no version information available (required by gdbserver)       
gdbserver: /lib/libstdc++.so.6: no version information available (required by gdbserver)       
gdbserver: /lib/libstdc++.so.6: no version information available (required by gdbserver)       
gdbserver: /lib/libstdc++.so.6: no version information available (required by gdbserver)       
gdbserver: /lib/libstdc++.so.6: no version information available (required by gdbserver)       
gdbserver: /lib/libstdc++.so.6: no version information available (required by gdbserver)       
gdbserver: /lib/libc.so.6: version `GLIBC_2.15' not found (required by gdbserver)              
gdbserver: /lib/libc.so.6: version `GLIBC_2.14' not found (required by gdbserver)              
# 

重新编译2
xy@xydpc:/usr/local/src/gdb-8.3/gdb/gdbserver$ make distclean
$ LDFLAGS=-static CC=arm-linux-gnueabi-gcc ./configure --target=arm-linux-gnueabi --host=arm-linux-gnueabi --prefix=/usr/local/src/gdbserver8.3
这个前缀没有用,生成的文件还是在当前目录的;前面2个变量要写在一行。
$ make
xy@xydpc:/usr/local/src/gdb-8.3/gdb/gdbserver$ file gdbserver
gdbserver: ELF 32-bit LSB executable, ARM, EABI5 version 1 (GNU/Linux), dynamically linked, interpreter /usr/lib, for GNU/Linux 3.2.0, BuildID[sha1]=e50899074c882c230ff686d54c342366bd366b8b, with debug_info, not stripped
运行还是报错

# gdbserver  -v                                                                                
-/bin/sh: gdbserver: not found 

重新编译3
$ LDFLAGS=-static CC=arm-linux-gnueabi-gcc
$ ./configure --target=arm-linux-gnueabi --host=arm-linux-gnueabi --prefix=/usr/local/src/gdbserver8.3
$ make
xy@xydp
xy@xydpc:/usr/local/src/gdb-8.3/gdb/gdbserver$ file gdbserver
gdbserver: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-, for GNU/Linux 3.2.0, BuildID[sha1]=8c7ef34aae5c4ac527d6dcbfb1f9890bd7784002, with debug_info, not stripped
目标机

busybox ftpget 192.168.1.202 /root/libstdc++.so.6 /opt/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/lib/libstdc++.so.6 -u xy -p xxx 
busybox ftpget 192.168.1.202 /root/libc-2.25.so /opt/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/libc/lib/libc-2.25.so -u xy -p xxx 
ln -s libc-2.25.so libc.so.6
export LD_LIBRARY_PATH=/root
# gdbserver -v
gdbserver: error while loading shared libraries: /root/libc.so.6: unexpected reloc type 0xa0
# export LD_LIBRARY_PATH=                                                                      
# uname -a                                                                                     
Linux (none) 3.10.0_s40 #47 SMP Tue Oct 28 16:49:26 CST 2014 armv7l GNU/Linux                  

busybox ftpget 192.168.1.202 /root/ld-2.25.so /opt/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/libc/lib/ld-2.25.so -u xy -p xxx 

暂时解决不了。

利用本机调试验证

准备号测试程序
xy@xydpc:~/x$ cat hello.c
#include <stdio.h>
int main ( void )
{ int i=0;
for (i=0;i<5;i++)
{ printf("%d\n",i);
}
}

xy@xydpc:~/x$ gcc -g hello.c -o hello
启动服务器端
xy@xydpc:~/x$ gdbserver x:22345 ./hello
Process ./hello created; pid = 26414
Listening on port 22345

启动客户端进行调试
如果是交叉应该运行,进入程序所在目录

xy@xydpc:~/x$ arm-linux-gdb
GNU gdb (GDB) 8.3
Copyright (C) 2019 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-pc-linux-gnu --target=arm-linux".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) 
(gdb) file ./hello
"/home/xy/x/./hello": not in executable format: file format not recognized
(gdb) target remote 192.168.1.202:22345
Remote debugging using 192.168.1.202:22345
warning: Can not parse XML target description; XML support was disabled at compile time
Reading /home/xy/x/hello from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
warning: "target:/home/xy/x/hello": not in executable format: file format not recognized
Reading /home/xy/x/hello from remote target...
warning: `target:/home/xy/x/hello': can't read symbols: file format not recognized.
Remote register badly formatted: T0506:0000000000000000;07:30deffffff7f0000;10:9060ddf7ff7f0000;thread:p6770.6770;core:3;
here: 00000000;07:30deffffff7f0000;10:9060ddf7ff7f0000;thread:p6770.6770;core:3;
(gdb) 
另外编译一个文件来测试
xy@xydpc:~/x$ arm-linux-gnueabi-gcc -g hello.c -o  hello.arm
(gdb) file ./hello.arm 
Reading symbols from ./hello.arm...
(gdb) target remote 192.168.1.202:22345
Remote debugging using 192.168.1.202:22345
warning: Can not parse XML target description; XML support was disabled at compile time
Remote register badly formatted: T0506:0000000000000000;07:30deffffff7f0000;10:9060ddf7ff7f0000;thread:p6770.6770;core:3;
here: 00000000;07:30deffffff7f0000;10:9060ddf7ff7f0000;thread:p6770.6770;core:3;
(gdb) b main
Breakpoint 1 at 0x103d2: file hello.c, line 3.
(gdb) c
The program is not being run.
(gdb) 

远端不能对应运行。

模拟连接远程

xy@xydpc:~$ cd x
xy@xydpc:~/x$ gdb
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) file ./hello
Reading symbols from ./hello...done.
(gdb) target remote 192.168.1.201:22345
192.168.1.201:22345: 没有到主机的路由.
(gdb) target remote 192.168.1.202:22345
Remote debugging using 192.168.1.202:22345
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
Reading symbols from target:/lib64/ld-linux-x86-64.so.2...Reading /lib64/ld-2.27.so from remote target...
Reading /lib64/.debug/ld-2.27.so from remote target...
(no debugging symbols found)...done.
0x00007ffff7dd6090 in ?? () from target:/lib64/ld-linux-x86-64.so.2
(gdb)

连接成功后,在gdbserver端显示
Remote debugging from host 192.168.1.202

开始调试

(gdb) b main
Breakpoint 1 at 0x555555554652: file hello.c, line 3.
(gdb) c
Continuing.
Reading /lib/x86_64-linux-gnu/libc.so.6 from remote target...
Reading /lib/x86_64-linux-gnu/libc-2.27.so from remote target...
Reading /lib/x86_64-linux-gnu/.debug/libc-2.27.so from remote target...

Breakpoint 1, main () at hello.c:3
3		{ int i=0;
(gdb) c
Continuing.
[Inferior 1 (process 26414) exited normally]
(gdb) c
The program is not being run.
(gdb) c
The program is not being run.
(gdb) c
The program is not being run.
(gdb) q

服务器断会显示结果

0
1
2
3
4

Child exited with status 0

图形化界面测试

从命令行启动ddd工具
ddd -debugger gdb &
file-open program打开程序,在界面底端输入target remote命令开始调试。
这个工具不能粘贴,不能显示中文。

emacs工具也可以调试
Tools-debugger(gdb)进入调试,可以粘贴。

二者都可以显示源码。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值