Embedded system--GDB debugging

Introduction to GDB

GDB: GNU Debugger is a debugger developed by the GNU Project for the GNU operating system, but its use is not limited to the GNU operating system. GDB can run on UNIX, Linux and even Microsoft Windows.
• GDB can debug programs written in C, C++, Objective-C, Pascal, Ada and other languages; the debugged program can run on the same computer as GDB or on different computers.
• Using GDB we can:
-Set breakpoints to stop the program
-Monitor or modify the value of variables in the program
-Track the code execution process

GDB commands

• file Load the executable file you want to debug.
• kill terminates the program being debugged.
• list lists part of the source code that generates the executable file.
• next executes a line of source code but does not enter the function.
• step executes a line of source code and enters the function.
• run executes the currently debugged program.
• c Continue to run the program.
• quit terminates gdb. • watch allows you to monitor the value of a variable regardless of when it is changed.
• backtrace stack trace to find out who called the code.
• print to view the value of the variable.
• make allows you to regenerate executable files without exiting gdb.
• The shell allows you to execute UNIX shell commands without leaving gdb.
• whatis displays the variable or function type.
• break Set a breakpoint in the code, which will cause the program to be suspended when it reaches this point.
• info break displays a list of current breakpoints, including the number of times the breakpoint is reached.
• info files show detailed information about the files being debugged.
• info func displays all function names.
• info local displays the local variable information in the current function.
• info prog displays the execution status of the debugged program.
• delete [n] delete the nth breakpoint.
• disable[n] Turn off the nth breakpoint.
• enable[n] turns on the nth breakpoint.
• ptype shows the structure definition.
• set variable Set the value of a variable.
• call name(args) Call and execute the function named name and args.
• Finish terminates the current function and outputs the return value.
• return value Stop the current function and return value to the caller.
• Use of the break command
– set breakpoints based on the line number:
• (gdb) break linenum
– set breakpoints based on the function name: • (gdb) break funcname
– stop execution when executing a line or a function that is not the current source file: • (gdb) break filename:linenum
• (gdb) break filename:funcname
– stop program execution according to conditions: • (gdb) break linenum if expr
• (gdb) break funcname if expr

GDB debugging example

Use of gdb
• To be able to be debugged, the program must contain debugging information.
• When compiling the program, use the -g option of gcc to add debugging information to the program, for example:
gcc –g –o helloworld helloworld.c

Start gdb
• gdb [program name]
example
1. First, we use vim or gedit editor to write a program named RevertNum.c, the code is as follows:

#include <stdio.h>
void ShowRevertNum(int iNum)
{
    
    
 while (iNum > 10)
 {
    
    
printf("%d", iNum % 10);
 iNum = iNum / 10;
 }
 printf("%d\n", iNum);
}
int main(void)
{
    
    
 int iNum;
 printf("Please input a number :");
 scanf("%d", &iNum);
 printf("After revert : ");
 ShowRevertNum(iNum);
}

Insert picture description here
2. Compile with Gcc, the code is as follows:

gcc -o RevertNum -g RevertNum.c

3. Let's start gdb next

gdb RevertNum

Insert picture description here
4. We can enter l to display the code and line number
Insert picture description here
5. Set breakpoints by line number
Insert picture description here
6. View all breakpoint information of the breakpoint
Insert picture description here
7. Execute the program
Insert picture description here
8. View the type and value of INum
Insert picture description here
9. Single-step debugging
Insert picture description here

Block error debugging

1. Procedure
Insert picture description here
2. Debug The correct answer can be obtained by
Insert picture description here
Insert picture description here
single-step execution
Insert picture description here
.
3.
• In Linux, when a program crashes, a core file is generally generated, which records
the state before the process exits , and debugs segfault problems. With the help of this file, the problem can be quickly located.
• The core file can be generated and used according to the following steps
-Step 1: Let the system generate a core file
• ulimit -c num #Set the core file capacity (num is a number, 0
means no core file is generated)-Step 2: Run the program, Let the program crash and generate the core file
– Step 3: gdb cooperates with the core file to locate the problem
• gdb program name core file name
• For example: gdb segDemo core

Guess you like

Origin blog.csdn.net/weixin_47357131/article/details/109388941