Article title: Novice: debugging tool GDB basic knowledge full contact. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
1. what is GDB?
GDB (GNU symbolic debugger) is a debugging tool. It is a free software protected by a General Public License (GPL.
2. GDB features
Like all debuggers, GDB allows you to debug a program, including stopping the program where you want it to be. at this time, you can view variables, registers, memory, and stacks. Further, you can modify the variables and memory values. GDB is a powerful debugger that can debug multiple languages. Here we only involve debugging of C and C ++, not other languages. Another point to note is that GDB is a debugger, not an integrated environment like VC. You can use some front-end tools such as XXGDB and DDD. They all have graphical interfaces, so they are more convenient to use, but they are only a layer of GDB shells. Therefore, you should still be familiar with the GDB command. In fact, when you use these graphical interfaces for a long time, you will find that you are familiar with the importance of GDB commands. Next we will introduce some important common commands of GDB with simple examples. Before you debug your program, when you compile your source program, do not forget the-g option or other corresponding options to add the debugging information to the program you want to debug. For example, gcc-g-o hello. c.
3. Introduction to common GDB commands
There are many GDB commands. This article will not introduce them all, but will only introduce some of the most commonly used commands. Before introducing it, we will first introduce a very useful function in GDB: completing the function. It is just like completing commands in SHELL in Linux. When you enter the first few characters of a command, and then enter the TAB key, if the first few characters of other commands are the same, SHELL will complete the command. If the first few characters of other commands are the same, you will hear a warning and enter the TAB key. SHELL will list all the first few characters of the commands. The completion function in GDB not only complements the GDB command, but also complements the parameters.
This article first introduces common commands, and then demonstrates how to use these commands with a specific example. All the following commands except the first command to start GDB are input in SHELL, and others are commands in GDB. Most GDB commands can only input the first few characters, as long as they do not conflict with other commands. For example, quit can be abbreviated as q, because only quit is used for the q-hitting command. List can be abbreviated to l.
3.1 start GDB
You can enter GDB to start the GDB program. The GDB program has many parameters, so there is no need to introduce them in detail here, but the most common one is to introduce: If you have compiled a program, let's assume the file name is hello, if you want to debug it with GDB, you can input gdb hello to start GDB and load your program. If you only start GDB, you must load your program in GDB after it is started.
3.2 Load the program === file
In GDB, loading the program is very simple. use the file command. For example, file hello. Of course, the program path name must be correct.
Exit GDB === quit
In the command mode of GDB, enter quit to exit GDB. You can also enter 'C-d' to exit GDB.
3.3 run the program === run
After you load the program to be debugged in GDB, you can run the run command. If your program requires parameters, you can enter the parameters after the run command, just like executing a command that requires parameters in SHELL.
3.4 View Program Information === info
The info command is used to view program information. when you use help info to view help, the parameters of the info command take up two screens. There are many parameters, but most of them are not commonly used. I use the most info command to view the breakpoint information.
3.4.1 view breakpoint information
Info br
Br is the abbreviation of breakpoint break. remember the completion function of GDB. With this command, you can obtain detailed information about all breakpoints you have set. Including the breakpoint number, type, status, memory address, and position of the breakpoint in the source program.
3.4.2 view the current source program
Info source
3.4.3 view stack information
Info stack
With this command, you can see the calling hierarchy of the program.
3.4.4 view current parameters
Info args
3.5 list source code segments = list
3.5.1 List a function
List FUNCTION
3.5.2 display a source program in the middle of a behavior of the current source file
List LINENUM
3.5.3 continue the previous display
List
3.5.4 display the previous source code
List-
3.5.5 a program that displays another file
List FILENAME: FUNCTION or list FILENAME: LINENUM
3.6 set the breakpoint === break
[1] [2] [3] [4] Next page