1.gdb and gdbserver Debugging principle

adopt linux In the virtual machine gdb, To the development board gdbserver dispatch orders , For example, setting breakpoints , function setp etc. , And then develop the gdbserver After receiving the order , Will execute the application to do the corresponding action , To realize the function of debugging

And the bare board I learned before GDB debugging equally , It's just that what I learned before was win Under the , This time it's in linux Inside gdb

1.1 Again , They all need a compiled file with debugging information .

adopt Makefile Inside arm-linux-gcc -g To the , -g: Indicates that the compiled file contains gdb Debugging information

1.2 Why do I need a compiled file for debugging information ?

For example, read the variables in the application of the development board a:

First gdb Through the application's compiled file with debugging information , To find the variables a The location of the deposit address

Then send the address to gdbserver, To read it out a The value of the address

2. install gdb and gdbserver

First, go to the official website to download gdb-7.4: http://ftp.gnu.org/gnu/gdb/

2.1 Install... On the virtual machine GDB:

# tar xjf gdb-7.4.tar.bz2                  // decompression # cd gdb-7.4/                              // Get into gdb-7.4 Catalog #./configure --target=arm-linux         
              //GDB Need to be in pc Running in this machine , And debug the application in the development board , therefore --target Set to arm-linux#make                                      // compile #mkdir    tmp                            #make install prefix=$PWD/tmp              // The installation to ./tmp Under the table of contents sudo cp tmp/bin/arm-linux-gdb  /bin/       // Copied to the /bin Under the table of contents /bin/arm-linux-gdb  -v                     //-v:  Make sure gdb Version of VID, Whether it is 7.4

2.2 Install... On the development board GDBServer:

cd gdb/gdbserver/                                   // stay gdb-7.4 Enter... In the directory ./configure --target=arm-linux --host=arm-linux              // set up GDBServer Working environment make             // compile 

The following error occurred :
 Insert picture description here
Referring to linux-arm-low.c in , Can't find PTRACE_GETSIGINFO Definition

2.3 solve :


#echo $PATH     // Check it out. PATH Environment variables find compiler gcc be located /work/tools/gcc-3.4.5-glibc-2.3.6/bin


#cd /work/tools/gcc-3.4.5-glibc-2.3.6/ # grep "PTRACE_GETSIGINFO"  *  -nR

stay gcc The root directory , We found that linux/ptrace.h In the definition of :
 Insert picture description here

#vi linux-arm-low.c

add to : #define PTRACE_GETSIGINFO 0x4202

4) Finally, I will try again make, Generate gdbserver Command file

And then gdbserver Command file , Put it in the root directory of our development board /bin in , You can use it

cp  gdbserver  /nfs_root/bin/          //nfs_root: Development board nfs System root 

3. The test procedure is as follows (test_debug.c)

#include <stdio.h>void  C(int *p){ *p = 0x12;}void  B(int *p){ C(p);}void  A(int *p){ B(p);}void  A2(int *p){ C(p);}int  main(int argc, char **argv){ int a; int *p = NULL; A2(&a);  // A2 > C printf("a = 0x%x\n", a); A(p);    // A > B > C return 0;}

among A2(&a) Would call A2()->C(), And then a The assignment is 0x12.

A§ Would call A()->B()->C(), because p It's a null pointer , There's going to be a mistake .

Next , Let's take this application as an example .

4. compile

#arm-linux-gcc -g -o test_debug test_debug.c   //-g: With debugging information 

5. debugging test_debug.c

On the development board :

First , Need make gdbserver Set up a local server , And which file to test :

#gdbserver ./test_debug// Local IP Address //2345: Port number , Used to make gdb To connect //./test_debug: Which file to test 

On a virtual machine :

#/bin/arm-linux-gdb   ./test_debug    //  start-up gdb, Specifies that the debug file is test_debug#target remote    // And gdbserver Establishing a connection 

5.1 Successful connection , Then use gdb Command to debug

The common commands are as follows :


List all the source code

break [file]:[row]

Breaking point , such as :

break test_debug.c:21     // stay test_debug.c File first 21 Break point at line 

info br

View breakpoints

info file

List the current files , Shared library .

delete num

Delete the number of breakpoints , As shown in the figure below
 Insert picture description here

Start the program run


Step by step


Step by step , and step The difference is , such as : When there are function calls in the current line ,next Go straight to the next sentence ,step It goes into the function

print a

Print a The value of the variable


sign out gdb

6. It can also be done through gdb+coredump To debug test_debug.c

When the program runs in error , It will be generated core file , And save the running status of the program to core in , That is to say coredump, supply gdb To debug

6.1 First , adopt ulimit Check it out. coredump Resource size for

ulimit command (user limit), Mainly used to limit the user's various process resources .

In the development board , Input
 Insert picture description here
As shown in the figure above , You can see coredump The resource size of is 0, in other words , When the program runs in error , Will not generate core file

6.2 Set up core file

Set up core The resource size of the file is unlimited , Input :

ulimit -c unlimited //-c: Corresponding coredump

6.3 Generate core file

perform :


There was a paragraph error , And generate core file , As shown in the figure below :
 Insert picture description here

6.4 Enter virtual machine

take core Copy it over , And then execute :

#/bin/arm-linux-gdb ./test_debug ./core

Then input bt, You can view the call relationship :
 Insert picture description here