Create a cross-Debugger gdb and gdbserver on the ARM platform
Hansel@163.com
2007.10.18
The gdb-6.6 uses autoconf/automake, so you can easily port the -- target, -- host, -- prefix parameter of the configure script to another platform by setting it. -- Target specifies the target of the compiled file
Board, usually set to the prefix of the cross compiler, such as -- target = arm-linux, -- target = mips-linux, -- target = armv5-linux-uclibc, -- target defaults to i386-linux, that is, PC. -- Host indicates
Specifies the runtime environment of the compiled file, which can be the prefix of the i386-linux or cross compiler. The default is the i386-linux, -- prefix specifies the directory to install.
1. Download gdb-6.6.tar.gzto/tmpto/optat http://www.gnu.org/software/gdb.
# Cd/opt
# Tar xzvf/tmp/gdb-6.6.tar.gz
2. Create configuration files and compile
Gdb allows you to place the compilation configuration and compilation results in any directory. Therefore, you can create a directory dedicated to the compilation file outside the gdb directory.
# Cd/opt
# Mkdir-p arm-gdb/build
# Cd arm-gdb/build
#/Opt/gdb-6.6/configure -- Host = i386-linux -- target = armv5-linux-uclibc -- prefix =/opt/ARM-GDB
# Make
# Make install
Where: Host specifies the runtime environment to i386 machine, target specifies the target machine environment to debug (the arm toolchain I am using is a armv5-linux-uclibc-gcc, so this specifies, if you use arm-Linux-GCC
-- Target = arm-Linux), prefix specifies the location where the compiled results are stored, that is, the installation directory.
If "sys/Reg. H" is not found when compiling the arm-linux-low.c file, modify the arm-linux-low.c and comment out # include "sys/Reg. H ".
Executable armv5-linux-uclibc-gdb, armv5-linux-uclibc-gdbtui, armv5-linux-uclibc-run can be found under/opt/ARM-GDB/bin.
3. Port gdbserver
Gdbserver uses some header files in the gdb source code directory, so it is impossible to create a compilation file outside the gdb source code directory.
# Cd/opt/gdb-6.6/GDB/gdbserver
# Vi build. Arm
The content is as follows:
./Configure -- target = armv5-linux-uclibc-host = armv5-linux-uclibc -- prefix =/opt/arm-gdb/gdbserver
Make
Make install
# Chmod + x build. arm
#./Build. arm
Note: Both the target parameter and the host parameter must be set to armv5-linux-uclibc because gdbserver runs on the target board.
After compilation, you can find the armv5-linux-uclibc-gdbserver under/opt/arm-gdb/gdbserver/bin, download the file to the target board and rename it gdbserver,
Download the library file libthread_db-x.x.x.so required by gdbserver, and create two symbolic links libthread_db.so and libthread_db.so.1.
4. Use gdbserver
Run gdbserver on the target board
#./Gdbserver 192.168.0.2: 2345 hello
192.168.0.2 is the IP address of the target board, which can be set to localhost or not. 2345 is the port opened by gdbserver. You can set it yourself.
#./Gdbserver: 2345 hello
Run on the host machine
# Armv5-linux-uclibc-gdb hello
(Gdb) target remote 192.168.0.2: 2345
(Gdb) B main
(Gdb) continue
Use DDD in X-Windows
# Ddd -- debugger armv5-linux-uclibc-gdb
In the GDB terminal control window of DDD, enter:
(Gdb) target 192.168.0.2: 2345
6. Problems
The transplanted gdbserver can be connected to mips-linux-gdb on the Host, but gdbserver appears
Killing inferior Error
Solution: after the target command is executed, the target board program is already running. Use the "continue" command instead of the "run" command.
7. Appendix 1: Basic commands of GDB:
Load: load a program
Symbol-file: loads the symbol library file, which can be an executable file compiled with the-g parameter.
F (ile): Specifies an executable file for debugging. gdb reads the debugging messages of some files, such as f a.exe.
L (ist): The column program outputs the source file.
R (un): After loading the executable file to be debugged, you can run the executable file using the run Command.
B (reak): sets a breakpoint (break point). For example, B 25 sets a breakpoint in line 2 of the source program. When the program is executed to line 2, it will interrupt; you can also use B funcname and funcname as the function name,
When a program calls some functions, it will interrupt
C (ontinue): The c command can be executed on another interrupted program until the next breakpoint or program ends.
P (rint): Enter the value of a variable. If the program defines an int aa, p aa will output the current value of aa.
N (ext): the execution is interrupted when the program is executed to the breakpoint. You can use n commands for one-step execution.
S (tep): the execution is interrupted when the program is executed to the breakpoint. You can use the s command to execute a single step into a function.
Q (uit): Exit GDB
8. Appendix 2: Debug drivers and shared libraries
(1) debug LKM (Loadable Kernel Module)
Insert the module on the target board and output the map information to view the. text entry address:
# Insmod-m hello. ko> map
# Grep. text map
For example, 0xc68c0060.
. Debugging information transferred to LKM in gdb:
(Gdb) add-symbol-file hello. ko 0xc68c0060/* Note that the entry address of LKM. text must be used)
. Normal debugging
(2) debug the Shared Library
. Sample program:
Test. c:
# Include <stdio. h>
Int test (int a, int B)
{
Int s = a B;
Printf ("% d/n", s );
Return s;
}
Main. c:
# Include <stdio. h>
Extern int test (int a, int B );
Int main (int argc, char * argv [])
{
Int s = test (10, 20 );
Return s;
}
Makefile:
All: so main
So:
Armv5-linux-uclibc-gcc-g test. c-shared-o libtest. so
Main:
Armv5-linux-uclibc-gcc-g main. c-L./-ltest-o test.exe
Clean:
Rm-f *. exe *. so
. Compile and set Environment Variables
# Make
On the target board:
# Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH :./
# Gdbserver localhost: 2345./test.exe
. Run the gdb Client
# Armv5-linux-uclibc-gdb
(Gdb) symbol-file test.exe
(GDB) target remote 192.168.0.2: 2345
(GDB) B Main
(GDB) c
. View the location of libtest. So code in the memory.
(The ID of the Process test.exe can be obtained by using psfrom the output terminal of gdbserver. the PID is assumed to be 11547)
On the target board:
# Cat/proc/11547/maps
Output:
.........
0076a000-0076c000 rwxp 0076a000 00:00 0
00bbe000-00bbf000 R-XP 00bbe000 00:00 0
00fcc000-00fcd000 R-XP 00000000 0:01 1238761/root/test/gdbservertest/libtest. So
00fcd000-00fce000 rwxp 00000000 0:01 1238761/root/test/gdbservertest/libtest. So
08048000-08049000 R-XP 00000000 1238765/root/test/gdbservertest/test.exe
08049000-0804a000 RW-P 00000000 01 1238765/root/test/gdbservertest/test.exe
........
It can be seen that the libtest. So code is between 00fcc000-00fcd000.
. View the offset position of the. Text Segment of libtest. So in the memory:
Armv5-linux-uclibc-objdump-H libtest. So | grep. Text
Output:
. Text 00000130 00000450 00000450 2 ** 4
That is, the offset is 0x00000450.
. Return to the gdb window on the board and load the libtest. So symbol table.
(GDB) Add-symbol-file libtest. So 0x00fcc450
(0x00fcc450 = 0x00fcc000 0x00000450)
. Set the breakpoint in the function of the shared library.
(GDB) B Test
. Continue to debug the Shared Library