table of Contents
Four, gdb commonly used commands
1. Principle
Embedded systems generally run the gdb tool on the PC side, and the source code is also on the PC side. The executable file corresponding to the source code is placed on the development board to run. For this we need to run gdbserver on the development board and communicate with gdb on the PC side through the network. Therefore, if you want to debug embedded programs through gdb on the PC, you need two things: gdb and gdbserver, where gdb runs on the PC, and gdbserver needs us to be transplanted to the development board.
Two, environment construction
Generally, cross-compilation already comes with gdb and gdbserver, you can use the built-in cross-compiler directly without porting, copy gdbserver directly to the /usr/bin directory of the root file system , and use the nfs service after the development board is started. The file system under ubuntu ; if the cross compiler does not come with it, you need to download the source code for installation
1. The cross compiler comes with
installation manual:
Copy gdbserver to rootfs root file system /usr/bin :
sudo cp /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/gdbserver /home/denghengli/linux/nfs/rootfs/usr/bin/
2. Source code download and installation steps
slightly. . .
Three, the debugging process
1. Compile the source code into an executable file with debugging information, and then copy it to the development board. For example, a gdbtest.c file is written under ubuntu, and the gdbtest executable file will be sent to the development board after the compilation is completed
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
unsigned int times = 0;
while (1)
{
printf("runging times: %d \r\n", times);
times++;
sleep(1);
}
}
Compile:
arm-linux-gnueabihf-gcc gdbtest.c -o gdbtest -g //编译测试程序,注意 -g选项
Copy to rootfs root file system (after the development board is started, use the root file system under ubuntu through the nfs service):
sudo cp gdbtest /home/denghengli/linux/nfs/rootfs/lib/modules/4.1.15/
2. In the development board, start gdbserver and execute the executable file to be debugged
gdbserver 192.168.0.107:2000 gdbtest //启动开发板上的 gdbserver
192.168.0.107 is the designated ubuntu host ip
2000 is the port to be opened for debugging, which can be specified at will
gdbtest is an executable file for debugging, so be sure to bring the path of gdbtest! !
3. Open gdb in ubuntu and connect to gdbserver on the development board
arm-linux-gnueabihf-gdb gdbtest //启动gdb调试
target remote 192.168.0.20:2000 //连接到开发板,192.168.0.20为开发板ip
After the connection is successful, it will prompt Remote debugging using 192.168.0.20:2000
4. Debug through debugging commands
Set the breakpoint at line 12, enter c to run to the breakpoint, then the program will run on the development board and print information on the console
Enter p to print the times information, and finally enter q to exit debugging, and the gdbserver on the development board will also stop.
Four, gdb commonly used commands
1. l (list) command
List all program source codes; after the command, all the source codes of the debugging program are printed out. If the source code is not printed, press the "l" command repeatedly, or press the Enter key in the gdb debugging tool to repeat the previous one command!
2. b (break) command
Used to set a breakpoint, followed by a specific function or line number
3. The c command
Used to run to the breakpoint
4. s command
The s command (step) is executed in a single step, and this function will enter the function.
5. n command
The n command (next) is also a single step operation, but the n command will not enter the function.
6, p command
The p command (print) is used to print a variable value.
7, q command
The q command (quit) is used to exit debugging, and the gdbserver on the development board will also stop.