The content of this article is basically from the http://www.cs.cmu.edu /~ Gilpin/tutorial/In this tutorial, I encountered "<iostream. h>
Is not a file or directory "error, so the original source code is modified.
Source code: in order to allow readers to learn gdb more quickly, this article provides a sample program with bugs and a simple makefile. I will package them and upload them here without points to download them. In the course of learning this article, you can debug this sample program to enjoy a better experience. This example contains two classes: Node and worker list. To facilitate debugging, we put these two classes in one file. Prerequisites
First, check whether gdb is installed. If your system has g ++, gdb has been installed. You can enter gdb-v in the command line to check whether gdb is installed.
Debugging Symbols
Gdb can only use the symbol generated by g ++ for debugging. If you use the Sun Cc compiler, you can use a debugging tool similar to gdb: dbx.
When debugging a program with debugging symbol, gdb can be used as a fish. Use the-g option of g ++ to compile the debugging symbol program with gdb. In addition to the-g option, you can also use the-ggdb option. The-ggdb option is used in the makefile in this article.
Use GDB to debug and compile a program
First, switch to the directory containing the two files downloaded earlier, and then use the make command to compile.
Make-f makefile
After compilation, an executable file named main is generated.
Load programs
Use the gdb main command to load the main executable file to gdb. On my terminal, the result of using this command is as follows:
GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3 +: gnu gpl version 3 or later This is free software: you are free to change and redistribute it.
There is no warranty, to the extent permitted by law. Type "show copying"
And "show warranty" for details.
This GDB was configured as "i686-linux-gnu ".
For bug reporting instructions, please see:
<Http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from/home/jubin/Downloads/gdb_sample/main... done.
(Gdb)
(Note: In emacs, you can use M-x gdb to use gdb in emacs. Emacs is divided into two windows. The second window displays code and an arrow pointing to the code line of the executed command .)
After gdb is started, you are waiting for the user to enter the next command. To check the program error, run the program and enter the run command:
(Gdb) run
Starting program:/home/jubin/Downloads/gdb_sample/main
Creating Node, 1 are in existence right now
Creating Node, 2 are in existence right now
Creating Node, 3 are in existence right now
Creating Node, 4 are in existence right now
The fully created list is:
4
3
2
1
Now removing elements:
Creating Node, 5 are in existence right now
Destroying Node, 4 are in existence right now
4
3
2
1
Program received signal SIGSEGV, Segmentation fault.
0x08048cb4 in Node <int>: next (this = 0x0) at main. cc: 30
30 Node <T> * next () const {return next _;}
(Gdb)
Obviously, this program has an error. Next we will analyze what the error is.
Check error information
From the error information above, we can see that in row 30th of main. cc, this Pointer Points to 0. At the same time, we also want to know who called 30th rows and the current status of the calling program. Enter backtrace at the gdb prompt.
(GDB) backtrace
#0 0x08048cb4 in node <int>: Next (this = 0x0) at main. CC: 30
#1 0x08048bea in each list <int>: Remove (this = 0x804c008, item_to_remove = @ 0xbffff2c4) at main. CC: 79
#2 0x080488d6 in main (argc = 1, argv = 0xbffff3a4) at main. CC: 122
(GDB)
From the above information, we can not only see the error method and local variables, but also find the storage address of the Program Calling line 1 and the item_to_remove parameter used for the call. The X command allows us to obtain the value of item_to_remove Based on the item_to_remove address:
(GDB) x 0xbffff2c4
0xbffff2c4: 0x00000001
(GDB)
From the above information, we can see that when the parameter "1" is used to call the sequence list <int>: remove, the program has an error.
Conditional breakpoint
Now we know what went wrong. The next step is to check the status of the program before the error occurs. One method is to step until the location where the error is fast, and the other is to set the breakpoint, which is implemented in GDB as follows:
(GDB) break secret list <int>: Remove
Breakpoint 1 at 0x8048ab3: file main. CC, line 54.
(GDB)
In this way, the breakpoint "1" in the deleted list <int>: remove is set. If you only want to view the status when item_to_remove = 1, you need to use the conditional breakpoint and enter:
(Gdb) condition 1 item_to_remove = 1
(Gdb)
This command means that the breakpoint "1" takes effect only when "item_to_remove = 1.
Step by step
The step command in gdb is step. Gdb has a good feature. When you only enter the carriage return, the previous command is executed by default. Therefore, you only need to enter the step in the first step and press enter directly.
(Gdb) run
The program being debugged has been started already.
Start it from the beginning? (Y or n) y
Starting program:/home/jubin/Downloads/gdb_sample/main
Creating Node, 1 are in existence right now
Creating Node, 2 are in existence right now
Creating Node, 3 are in existence right now
Creating Node, 4 are in existence right now
The fully created list is:
4
3
2
1
Now removing elements:
Creating Node, 5 are in existence right now
Destroying Node, 4 are in existence right now
4
3
2
1
Breakpoint 1, shortlist <int>: remove (this = 0x804c008, item_to_remove = @ 0xbffff2f4)
At main. cc: 54
54 Node <T> * marker = head _;
(Gdb) step
55 Node <T> * temp = 0; // temp points to one behind as we iterate
(Gdb)
57 while (marker! = 0 ){
(Gdb)
58 if (marker-> value () = item_to_remove ){
(Gdb)
Node <int >:: value (this = 0x804c058) at main. cc: 32
32 const T & value () const {return value _;}
(Gdb)
Revoke list <int >:: remove (this = 0x804c008, item_to_remove = @ 0xbffff2f4) at main. CC: 77
77 marker = 0; // reset the marker
(GDB)
78 temp = marker;
(GDB)
79 marker = marker-> next ();
(GDB)
Node <int>: Next (this = 0x0) at main. CC: 30
30 node <t> * Next () const {return next _;}
(GDB)
Program received signal SIGSEGV, segmentation fault.
0x08048cb4 in node <int>: Next (this = 0x0) at main. CC: 30
30 node <t> * Next () const {return next _;}
(GDB)
Exit GDB command: Q or quit
Finally ----------------------------------------------------------------- through the above content, I believe you can use gdb with confidence, the following will add some commonly used gdb command: continue or c (abbreviation of continue ): after the program is interrupted, continue to run next: after the program is interrupted, it does not enter the function, corresponding to the step info break: view the breakpoint disable break <breakpoint number>, such as disable break 1: make the <breakpoint No.> breakpoint No. Invalid delete break <breakpoint No.>: delete the <breakpoint No.> breakpoint list: list code lines. Is 10 rows clear <breakpoint sentence>: corresponds to the break, followed by the content after the break when the breakpoint is created, clear a breakpoint or B: Create a breakpoint, there are two usage.
B <Function Name>: Set the function breakpoint B <file name: row number>: Set the breakpoint in a row of a file