Without development board, how to debug and run arm program?
This article mainly explains how to build an arm cross-compiling and running environment on Ubuntu.
Install the cross-compilation tool chain arm-linux-gnueabihf-gcc:
sudo apt-get install gcc-arm-linux-gnueabihf
After installation, you can see that so many cross-compilation tools have been added to the system:
helloworld@ubuntu:~$ arm-linux-gnueabihf-
arm-linux-gnueabihf-addr2line arm-linux-gnueabihf-gcov-7
arm-linux-gnueabihf-ar arm-linux-gnueabihf-gcov-dump
arm-linux-gnueabihf-as arm-linux-gnueabihf-gcov-dump-7
arm-linux-gnueabihf-c++filt arm-linux-gnueabihf-gcov-tool
arm-linux-gnueabihf-cpp arm-linux-gnueabihf-gcov-tool-7
arm-linux-gnueabihf-cpp-7 arm-linux-gnueabihf-gprof
arm-linux-gnueabihf-dwp arm-linux-gnueabihf-ld
arm-linux-gnueabihf-elfedit arm-linux-gnueabihf-ld.bfd
arm-linux-gnueabihf-gcc arm-linux-gnueabihf-ld.gold
arm-linux-gnueabihf-gcc-7 arm-linux-gnueabihf-nm
arm-linux-gnueabihf-gcc-ar arm-linux-gnueabihf-objcopy
arm-linux-gnueabihf-gcc-ar-7 arm-linux-gnueabihf-objdump
arm-linux-gnueabihf-gcc-nm arm-linux-gnueabihf-ranlib
arm-linux-gnueabihf-gcc-nm-7 arm-linux-gnueabihf-readelf
arm-linux-gnueabihf-gcc-ranlib arm-linux-gnueabihf-size
arm-linux-gnueabihf-gcc-ranlib-7 arm-linux-gnueabihf-strings
arm-linux-gnueabihf-gcov arm-linux-gnueabihf-strip
Execute the following code to establish a soft link, otherwise an error that the dynamic library cannot be found will be reported when executed later:
sudo ln -s /usr/arm-linux-gnueabihf/lib/libc.so.6/lib/libc.so.6
sudo ln -s /usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3/lib/ld-linux-armhf.so.3
Write test code main.c
:
# include <stdio.h>
int main(){printf("helloworld\n");return0;}
The following command first compiles main.c
to generate the executable file a.out
under the arm platform, and then through the file
command, you can see that a.out
is the elf executable file under the arm platform:
helloworld@ubuntu:~$ arm-linux-gnueabihf-gcc main.c
helloworld@ubuntu:~$ file a.out
a.out: ELF 32-bit LSB shared object, ARM, EABI5 version 1(SYSV), dynamically linked, interpreter /lib/ld-,for GNU/Linux 3.2.0, BuildID[sha1]=7592a0494955ca8bb953948ea4cfbefc90b2e2e9, not stripped
Install arm emulator qemu:
sudo apt-get install qemu
Execute the executable file a.out
of the arm platform. As you can see, the program outputs the correct result helloworld
:
helloworld@ubuntu:~$ qemu-arm a.out
helloworld
The principle of using gdb to debug arm programs on Ubuntu: the qemu end serves as a gdb server to start an executable program, and the other end serves as a gdb client to connect to the gdb server for local remote debugging.
First install the multi-platform gdb tool:
sudo apt-get install gdb-multiarch
Recompile the sample code main.c
, note that the parameter --static
is added this time. After adding this parameter, the generated executable file is statically linked. **If you don't add this parameter, the single step function will be abnormal when debugging, and the symbol table will not be found. ** arm-linux-gnueabihf-gcc --static -g main.c
Start the executable program a.out
with the following command, the option -g
specifies the listening port of gdb, here is 1234. After this command is executed, the current window will be blocked. qemu-arm -g 1234 a.out
Open a new command line window, start gdb client, enter the gdb interactive interface: gdb-multiarch a.out
Enter the following in the gdb interactive interface to connect to the server side: target remote localhost:1234
Next, you can use the related functions of gdb to debug the program normally: (gdb) b main Breakpoint 1 at 0x102e8: file main.c, line 5. (gdb) c Continuing. Breakpoint 1, main () at main.c: 5 5 printf("helloworld\n");
Recommended Posts