Common commands of GDB

GDB is a debugger that can be used to debug C/C++ programs. This C/C++ needs to generate a symbol table before it can be called by GDB. For example C++:

g++ -g source.cpp

GDB common commands

First, an ELF file (exe curable and linkable format) with debugging symbols must be generated. Simply put, it is an executable file with debugging symbols. Add -gparameters to compile.

hello.cpp:
Please add a picture description

──(kali㉿kali)-[~/dev/Bear]
└─$ g++ -o world -g hello.cpp
──(kali㉿kali)-[~/dev/Bear]
└─$ ls
world hello.cpp

Start the gdb debugger

──(kali㉿kali)-[~/dev/Bear]
└─$ gdb

In order to facilitate our debugging, run before starting the adjustmentlayout next

This command will display the source code around the current position in the program. This view is helpful for those new to gdb, especially when working with source code you are not familiar with.
After starting GDB, for the first time enter

(gdb) layout next

Please add a picture description
Please add a picture description
Repeat the execution again layout next, and the program in assembly language will be displayed.
Please add a picture description
layout prevThis command is to return to the previous mode.

Use the world file for debugging, and use the file file to read in the symbol of the world file for debugging

Use file to load the symbols of the file used for debugging

(gdb) file world
Reading symbols from world...

set breakpoint

Breakpoint at specific line number:

(gdb) break 2
(gdb) break 7

You can also breakpoint on a function:

(gdb) break main

Please add a picture description

Run the world program

(gdb) run world

Or pass some parameters to the world program:

(gdb) run world "ling ling"

Please add a picture description

Use the next command to run to the next line

(gdb) next

If you encounter a function call and want to go in and have a look, you can use step

(gdb) step

Please add a picture description

If you have entered the function, and then you don't want to continue reading, and want to return to the place where it was called, you can use finish

(gdb) finish

If you want to go back to the calling point, you can use the return command:

Please add a picture description

Use print to check the value of some variables

Please add a picture description

In addition to using print to check the value of some variables, you can also use display, which is different from print in that it will reprint the information after each instruction.

insert image description here

Use clear to clear the breakpoint

For example, clear the breakpoint on line 7

(gdb) clear 7

Please add a picture description
Or count breakpoints on a method name:

(gdb) clear main

Please add a picture description

bt displays the function call stack, that is, every function that has been run since reaching this line, that is, displays the function call stack.

(gdb) bt

Please add a picture description

The continue command is used to continue from where the breakpoint stopped until the next breakpoint.

(gdb) continue

frame [number] goes to the selected frame in the call stack.

With the list command, print the area around the current line in the current source file. Show current line and called function stackPlease add a picture description

quit or exit are both gdb debuggers.

The above commands are commonly used. For more advanced use, you can find more useful information through the help command

(gdb) help

Please add a picture description
You can learn more advanced applications through the help command. It will not be expanded here. gdb is still very easy to learn.

Guess you like

Origin blog.csdn.net/weixin_40763897/article/details/128749102