linux x86_64调试linux arm程序
环境:
OS1: ubuntu 20.04 x86_64
OS2: Raspbian GNU/Linux 8.0 (jessie) armv7l
1. raspberry目标机
1.1 linux下编译测试代码
main.c
#include <stdio.h>
int main(void)
{
printf("hello world\n");
return 0;
}
编译(也可以在宿主机上交叉编译)
$ cd ~/hello
$ gcc main.c -o hello -g
$ ./hello
hello world
1.2 linux下gdbserver启动调试
$ gdbserver :1234 hello
2. ubuntu目标机
2.1 安装ubuntu下支持linux arm调试的arm-linux-gnueabihf-gdb
安装第三方交叉调试器linaro arm-linux-gnueabihf-gdb
https://releases.linaro.org/components/toolchain/binaries/4.9-2016.02/arm-linux-gnueabihf/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf.tar.xz
2.2 同步源码及arm可执行程序
$ tree
hello
├── hello
└── main.c
2.3 使用arm-linux-gnueabihf-gdb调试arm程序
安装第三方交叉调试器linaro arm-linux-gnueabihf-gdb
设置gdb的环境变量
export PATH=~/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin:$PATH
调试
$ arm-linux-gnueabihf-gdb hello
(gdb) target remote 192.168.1.101:1234
3. 结果
- ubuntu 20.04宿主机
$ gdb hello
GNU gdb (GDB) 9.2
Copyright (C) 2020 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-linux-gnu --target=arm-linux-gnueabihf".
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"...
Reading symbols from hello...
(gdb) target remote 192.168.1.101:1234
Remote debugging using 192.168.1.101:1234
Reading /lib/ld-linux-armhf.so.3 from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /lib/ld-linux-armhf.so.3 from remote target...
Reading symbols from target:/lib/ld-linux-armhf.so.3...
Reading /lib/ld-2.19.so from remote target...
Reading /lib/.debug/ld-2.19.so from remote target...
Reading /usr/local/lib/debug//lib/ld-2.19.so from remote target...
Reading /usr/local/lib/debug/lib//ld-2.19.so from remote target...
Reading target:/usr/local/lib/debug/lib//ld-2.19.so from remote target...
(No debugging symbols found in target:/lib/ld-linux-armhf.so.3)
0x76fcfca0 in ?? () from target:/lib/ld-linux-armhf.so.3
(gdb) l
warning: Source file is more recent than executable.
1 #include <stdio.h>
2
3 int main(void)
4 {
5 printf("hello world\n");
6 return 0;
7 }
(gdb) b 5
Breakpoint 1 at 0x10424: file main.c, line 5.
(gdb) c
Continuing.
Reading /usr/lib/arm-linux-gnueabihf/libarmmem.so from remote target...
Reading /lib/arm-linux-gnueabihf/libc.so.6 from remote target...
Reading /lib/arm-linux-gnueabihf/libc-2.19.so from remote target...
Reading /lib/arm-linux-gnueabihf/.debug/libc-2.19.so from remote target...
Reading /usr/local/lib/debug//lib/arm-linux-gnueabihf/libc-2.19.so from remote target...
Reading /usr/local/lib/debug/lib/arm-linux-gnueabihf//libc-2.19.so from remote target...
Reading target:/usr/local/lib/debug/lib/arm-linux-gnueabihf//libc-2.19.so from remote target...
Breakpoint 1, main () at main.c:5
5 printf("hello world\n");
(gdb) c
Continuing.
[Inferior 1 (process 25834) exited normally]
(gdb) q
- raspberry目标机
$ gdbserver :1234 hello
Process hello created; pid = 25821
Listening on port 1234
Remote debugging from host 192.168.1.100
hello world
Child exited with status 0
GDBserver exiting