Transferred from: https://linux.cn/article-4302-1.html
Compiled from: http://xmodulo.com/gdb-command-line-debugger.html Adrien Brochard
Original: LCTT https://linux.cn/article-4302-1.html Translator: Spccman
This address: https://linux.cn/article-4302-1.html
2014-11-25 21:48 Comments: 31 Favorites: 19 share:
This article navigation
- -Install gdb10%
- -Sample code 16%
- Use of-gdb 24%
What's the worst situation when writing a program without a debugger? On your knees at compile time don't make mistakes? Summon Demons to help you run the program with a blood sacrifice? or add a printf ("test") statement between each line of code to locate the error point? As you know, it is inconvenient to write a program without using a debugger. Fortunately, the Linux debugging is very convenient. Most of the Ides used by most people are integrated with the debugger, but Linux's most famous debugger is the command-line form of the C + + debugger gdb. However, consistent with other command-line tools, DGB requires some exercise to be fully mastered. Here, I'll tell you about GDB's basic situation and how to use it.
Installing GDB
GDB is available in most of the distribution repositories
Debian or Ubuntu
- $ sudo apt-get install gdb
Arch Linux
- $ sudo Pacman -S gdb
Fedora,centos or RHEL:
- $sudo Yum install gdb
If not found in the warehouse, you can download it from the official website.
Sample code
When learning gdb, it's best to have a code, try it out. The following code is a simple example of what I have written, which can well reflect GDB's characteristics. Copy it down and experiment with it-this is the best way to do it.
- #include <stdio. H>
- #include <stdlib. H>
- int main(int argc, char * *argv)
- {
- int i;
- int A=0, b=0, c=0;
- double d;
- for (i=0; i<; i+ +)
- {
- a+ +;
- if (i>)
- D = i / 2.0;
- b+ +;
- }
- return 0;
- }
Use of GDB
First and foremost, you need to use the compiler's "-g" option to compile the program so that the executable program can run through GDB. Start debugging with the following statement:
- $ gdb -tui [executable program name]
Use the "-tui" option to display the code in a nice interactive window (so called "Text user interface Tui"), where you can use the cursor to manipulate it, and enter commands in the following GDB shell.
Now we can set breakpoints anywhere in the program. You can set a breakpoint on a line of the current source file by using the following command.
- Break [line number]
or set breakpoints for a specific function:
- Break [function name]
You can even set conditional breakpoints
- Break [line number] if [condition]
For example, in our sample code, you can set the following:
- Break one-off if i >
This way, the program loops 97 times and then stays on the "a++" statement. This is very handy and avoids the need to manually cycle 97 times.
Last but not least, we can set a "observe breakpoint", and when the observed variable changes, the program is stopped.
- Watch [variable]
Here we can set the following:
- Watch D
When the value of D changes, the program stops running (for example, when i>97 is true).
When a breakpoint is set, use the "Run" command to start running the program or as follows:
- R [Input parameters of the program (if any)]
In gdb, most command words can be abbreviated to a single letter.
No accident, the program will stay in line 11. Here, we can do some interesting things. The following command:
- Bt
The backtracking function (BackTrace) lets us know how the program reached this statement.
- Info Locals
This statement shows all the local variables and their values (as you can see, I didn't set the initial value for D, so it's now a value of any value).
Of course:
- P [variable]
This command can display the value of a specific variable and further:
- PType [variable]
You can display the type of the variable. So here you can be sure that D is a double type.
Now that we have reached this point, I might as well do this:
- Set var [variable] = [new value]
This overrides the value of the variable. However, it is important to note that you cannot create a new variable or change the type of the variable. We can do this:
- Set var a = 0
Like other good debuggers, we can step through the steps:
- Step
Using the command as above, it is possible to enter into a function by running to the next statement. or use:
- Next
This can run the next statement directly without entering the inside of the child function.
After the test is finished, delete the breakpoint:
- Delete [line number]
To continue running the program from the current breakpoint:
- Continue
Exit GDB:
- Quit
In short, with GDB, compile without praying to God, run without blood sacrifice, no longer printf ("test"). Of course, what is said here is not complete, and GDB's function is much more than that. So I strongly advise you to learn it more deeply. What I am interested in now is the integration of GDB into VIM. At the same time, here is a memo that records all of GDB's command lines for review.
What do you think of gdb? Will you compare it to the graphical debugger, and what advantages does it have? What do you think about integrating GDB into vim? Write your thoughts in the comments.
Via:http://xmodulo.com/gdb-command-line-debugger.html
Adrien Brochard Translator: Spccman proofreading: Wxy
This article by LCTT original translation, Linux China honors launch
Using the GDB command-line debugger to debug a C + + program "Go"