Simple use of GDB tools

GDB (GNU Debugger) is an open source command-line tool for debugging programs. It is part of the GNU project and is available for several programming languages ​​such as C, C++, Ada, Objective-C, etc. GDB allows developers to inspect and modify the state of a program during program execution to help locate and fix errors.

GDB can do the following:

  1. Setting Breakpoints: Developers can set breakpoints in the code that instruct the program to stop execution at a specific point. This way, you can examine the code line by line, observing the values ​​of variables and the state of the program.

  2. Stepping: You can step through a program statement by statement, tracking the program's execution flow by executing one line of code at a time. This helps to spot bugs and unusual behavior in the code.

  3. Variables and Memory Inspection: You can inspect the current values ​​of variables in your program, and you can also inspect and modify memory regions used by your program to aid in debugging.

  4. Backtraces and stack traces: When a program crashes or terminates abnormally, GDB can provide information about where the program crashed and the associated call stack.

  5. Remote debugging: GDB supports debugging a running program from a remote computer over a network. This is useful for multi-core system or embedded device debugging.

GDB is a powerful debugging tool, but it also takes some learning and practice to use it proficiently to debug programs. For beginners, you may need to consult GDB documentation and tutorials to learn more details about how to use GDB for program debugging.

Notice:

Once you have compiled an executable with debug information, you can use it with GDB for debugging. Remember, when releasing your program in production, make sure to compile your code with optimization options to improve performance, and to avoid including debug information.


In an embedded system, you can use the following steps to debug with GDB in a C language program:

1. Add debugging information when compiling the program: First, make sure to add debugging information when compiling your C language program. In the GCC compiler, use -goptions to generate the symbol table needed for debugging and embed it in the executable file. These symbol tables contain variable names, function names, and other information needed for debugging. For example:

gcc -g program.c -o program

2. Start GDB: Enter the command in the terminal gdbto start the GDB debugger. Then, fileload the executable with the command. For example:

$:gdb program

(gdb)file

3. Set breakpoints: Use breakcommands to set breakpoints, instructing the program to stop execution at a specific location. You can choose to set a breakpoint at the main function entry or elsewhere in the code. For example, set a breakpoint at the entry of the main function:

break main

4. Start the program: use runthe command or abbreviated rcommand to start the program. For example:

run

5. Debug and observe program status: Once the program stops at a breakpoint, you can use various GDB commands to observe and debug the program. For example:

  • Use printthe command (short for p) to see the value of a variable.
  • Use nextcommands (short for n) to execute a program statement by statement.
  • Use stepthe command (shorthand s) to step into a function call.
  • Use infocommands (for example info locals) to view information such as local variables.

6. Continue to execute the program: Use continuecommands or abbreviated ccommands to make the program continue to execute until the next breakpoint or the end of the program.

7. Exit GDB: After the debugging session ends, use quitthe command or the shorthand qcommand to exit the GDB debugger.

Here is a basic usage example of GDB. For more complex debugging needs, you can consult GDB documentation or refer to related tutorials to learn more advanced debugging skills and commands.

Guess you like

Origin blog.csdn.net/FLM19990626/article/details/131508972