Debugging is an indispensable task in the development process. In Linux programming, gdb is usually used to debug our c/c ++ program. Because gdb can introduce many things, I will introduce several articles in detail here. I will introduce some articles on the Internet in great detail and deserve to be added to my favorites: Use GDB to debug programs (1), (2), (3), and (4), (5), (6), and (7 ).
Preparations
During program compilation, the function name and variable name will be replaced by the corresponding memory address, but these memory addresses are not readable for us. We often need to find the actual address based on the function name and variable name to read its value.
To facilitate debugging, gcc adds a-g parameter. during compilation, you can add the corresponding symbol information in the Assembly.
Gcc-g hello. c-o hello
Start GDB
The following methods are used to start GDB:
- Gdb <program> uses gdb to associate a program. You can run the run Command to start the associated program process.
- Gdb <program> core uses gdb to debug the core file. The core is the file generated after the core is dumped after the program is illegally executed.
- Gdb <program> <PID> uses gdb to debug the currently running process, similar to "attach to process" in.
These methods can also be used in combination. For example, you can use gdb <program> to associate the program and attach <pid> to debug the currently running process.
Introduction to the GDB Environment
First, we will introduce the GDB environment. GDB is also a Shell interactive environment. In GDB, the process can see its prompt: (gdb)
(Gdb)Break
In gdb, you also want to have the following smart notification functions for:
- When you enter a command, you can use only the first few characters of the command.
- After entering the first few characters, you can use the Tab key to automatically complete
- If there are many commands starting with a few characters, the Tab key cannot be fully supplemented. If you press the TAB key again, all commands starting with this character can be displayed.
- You can use the Enter key to repeatedly execute commands (commonly used for single-step tracking)
- You can use the up/down key to switch the history command
For example, if you set the breakpoint command to break, you can directly enter B. You can also enter B and press the Tab key twice to query all the commands starting with B (if there is only one, you can directly complete the command ):
(Gdb) B
Backtrace bookmark break-range bt
This smart prompt can also be used to automatically associate the function name when the breakpoint is set. For example, if you want to set a breakpoint for the main function, when you enter B mai and press the Tab key, it will automatically complete as main
(Gdb) B main
In addition, you can use the help <command> command to view the help information of unfamiliar commands, similar to man in shell, which is very convenient.
Exit GDB
The method for exiting gdb is relatively simple. Simply enter the q (quit) command in Shell:
(Gdb) q
Tianfang>
Due to space limitations, this article will introduce how to set breakpoints in the next article.