C + + Common GDB commands

Source: Internet
Author: User

the current project uses: find./-name "Infocheckstat"   Ps-ef|grep Workordercon   Ps-ef|grepCtpclient Export process_id=1003 gdb format Workordergen set args-y-t 3-n 100000-i f82-s ext     Export process_id=1201 gdbBussevent Set args-A  

(GDB) B main

(GDB) r (+ parameter )   find./-name "settlefeefreeze"
Settlefeefreeze Reference: http://blog.csdn.net/jubincn/article/details/6774524Source code: In order to enable readers to learn gdb faster, this article provides a sample program with bugs, and a simple makefile that I upload to this place without the need for points to download. During this study, the reader can debug the sample program to get a better experience. This sample program is simple and contains two classes: node and LinkedList. To facilitate debugging, we put these two classes in a file. Pre-Prepare environment settings First check if GDB is installed. If you have g++ in your system, GDB is already installed. You can check whether GDB is installed by entering GDB-V on the command line.
Debugging Symbolsgdb can only be debugged using the symbol generated by g++. If the reader is using the Sun cc compiler, then you can use a debug tool that is similar to GDB: DBX
GDB can be a duck when debugging a program with debugging symbol. Using the-G option of g++, you can compile a program with GDB's debugging symbol. In addition to the-G option, the-GGDB option is available, and the-GGDB option is used in the makefile of this article.
Debug the compiler with GDB first, switch to the directory containing the two files that you downloaded earlier, and then compile with the Make command.
Make-f Makefile
When the compilation is complete, an executable file named Main is generated.
The loader uses the GDB main command to load the main executable file into GDB. In my terminal, the result of using this command is as follows:
GNU gdb (Ubuntu/linaro 7.2-1ubuntu11) 7.2
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was 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 is configured as "I686-linux-gnu".
For bugs reporting instructions, please see:
Reading symbols From/home/jubin/downloads/gdb_sample/main...done.
(GDB)
(Note: In Emacs, you can use GDB in Emacs using the M-x gdb.) Emacs is divided into two windows, the second window displays the code, and an arrow points to the line of code where the instruction is executing. )

When GDB starts, it is waiting for the user to enter the next instruction. Because you need to see where the program is wrong, you first need to run the program and enter the Run command:
(GDB) Run
Starting program:/home/jubin/downloads/gdb_sample/main
Creating Node, 1 is in existence right now
Creating Node, 2 is in existence right now
Creating Node, 3 is in existence right now
Creating Node, 4 is in existence right now
The fully created list is:
4
3
2
1
Now removing elements:
Creating Node, 5 is in existence right now
Destroying Node, 4 is 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
30node<t>* Next () const {return next_;}
(GDB)
Obviously, this program has gone wrong, let's analyze where the error is.
Check the error message from the above error message can be seen in the 30th line of main.cc, the this pointer to 0. But at the same time we want to know who called Line 30th and the state of the calling program. In the GDB prompt, enter: backtrace
(GDB) BackTrace
#0 0x08048cb4 in Node<int>::next (this=0x0) at main.cc:30
#1 0x08048bea in Linkedlist<int>::remove (this=0x804c008, [e-mail protected]) at main.cc:79
#2 0x080488d6 in Main (Argc=1, ARGV=0XBFFFF3A4) at main.cc:122
(GDB)
From the above information, you can see not only the method and local variables, but also the storage address of the program that called Line 30th and the parameter item_to_remove used when calling. The x command allows us to get the value of Item_to_remove based on the address of Item_to_remove:
(GDB) x 0xbffff2c4
0xbffff2c4:0x00000001
(GDB)

As can be seen from the above information, when using the parameter "1" Call Linkedlist<int>::remove, the program error.

Conditional breakpoints Now that we know where something went wrong, the next step is to look at the state of the program before the error. One way is to step in, until the point where the error is fast, and the other is to set a breakpoint, which is implemented in GDB:

(GDB) Break Linkedlist<int>::remove

Breakpoint 1 at 0x8048ab3:file main.cc, line 54.

(GDB)

So the breakpoint "1" in Linkedlist<int>::remove is set. If we just want to see the status of Item_to_remove = = 1 o'clock, then we need to use a conditional breakpoint and enter it in GDB:


(GDB) Condition 1 Item_to_remove = = 1
(GDB)
This command means that the breakpoint "1" will only take effect if the "item_to_remove = = 1" is the only case.
Stepping into GDB is step. GDB has a very good feature, when the user only enters enter the default execution of the previous command, so the step only need to enter step in the first step, followed by directly hit enter on it.
(GDB) Run
The program being debugged have been started already.
Start it from the beginning? (Y or N) y

Starting program:/home/jubin/downloads/gdb_sample/main
Creating Node, 1 is in existence right now
Creating Node, 2 is in existence right now
Creating Node, 3 is in existence right now
Creating Node, 4 is in existence right now
The fully created list is:
4
3
2
1

Now removing elements:
Creating Node, 5 is in existence right now
Destroying Node, 4 is in existence right now
4
3
2
1


Breakpoint 1, Linkedlist<int>::remove (this=0x804c008, [email protected])
At main.cc:54
node<t> *marker = Head_;
(GDB) Step
Node<t> *temp = 0; Temp points to one behind as we iterate
(GDB)
(Marker! = 0) {
(GDB)
if (marker->value () = = Item_to_remove) {
(GDB)
Node<int>::value (this=0x804c058) at main.cc:32
Const t& Value () const {return value_;}
(GDB)
Linkedlist<int>::remove (this=0x804c008, [email protected]) at main.cc:77
marker = 0; Reset the Marker
(GDB)
temp = marker;
(GDB)
Marker = Marker->next ();
(GDB)
Node<int>::next (this=0x0) at main.cc: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
node<t>* Next () const {return next_;}
(GDB)
command to leave GDB: Q or Quit------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------through the above content, I believe you can already use GDB confidently, the following will supplement some common GDB commands: Continue or C (continue): After the program is interrupted to continue running next: program interrupted after the function, and step corresponding info Break: View breakpoints Disable breaks < breakpoint numbers, such as disable break 1: Make < breakpoint number > Breakpoint Invalid delete break < breakpoint number: Delete < breakpoint number > Breakpoint list: Lists lines of code, typically 10 lines clear < breakpoint sentence;: Corresponding to break, the following is the content after break when creating breakpoint, clear a breakpoint breakpoint or B: Create breakpoint, there are two ways to use it.
b < functions;: Set function Breakpoint B < file name: line number;: Set breakpoint on a line of a file

C + + Common GDB commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.