[C + +] GDB debugging C + + classes

Source: Internet
Author: User

GDB debugging C + + classes

The common tool for debugging on Linux is gdb. Learn about GDB's general debugging skills with the opportunity to learn C + + virtual function tables and memory layouts.

One, test cases

1,c++ header file (szyu_test_gdb.h)

/******************************** author : szyu** date : 2016.10.25************* /#ifndef  __szyu_gdb__#define __szyu_gdb__#include <iostream>class  base{public:    base ()  { };    base ( int v  )  : non_static_member1 ( v )  { };    virtual ~base ()  { ;p ublic:    void    non_static_func1 ()      {           std::cout <<  " IN&NBSP;NON_STATIC_FUNC1 () " << std::endl;    }     &NBSP;&NBSP;&NBSP;STATIC&NBSP;VOID&NBSP;&NBSP;&NBSP;&NBSP;STATIC_FUNC1 ()     {         std::cout <<  "In static_func1 ()"  <<  std::endl;     }    virtual void    virtual_func1 ()      {        std::cout <<  "In virtual_func1 ()"  << std::endl;    }private:    int non_static_ member1;    static int static_member1;}; int base::static_member1 = 99; #endif

2,c++ test Case (Szyu_test_gdb.cpp)

/************************** author:szyu** date:2016.10.25*******************************/#include "szyu_test_    Gdb.h "Voidtest1 () {/* static function Access */BASE::STATIC_FUNC1 ();    /* Create object */Base BB (57);    /* Non-static function access */BB.NON_STATIC_FUNC1 (); /* Virtual function Access */BB.VIRTUAL_FUNC1 ();}    Intmain (int argc, char *argv[]) {test1 (); return 0;}

Second, commissioning

1,gdb you need to compile the executable before debugging, and you need to add debugging information to the executable file. The-G parameter can do this. To use: g++-G szyu_test_gdb.cpp (default build a.out executable file)

2. Start gdb debug: GdB a.out

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/89/5C/wKioL1gQxDvjFrHaAAKznoik1y4381.png "title=" screen shot 2016-10-25 23.50.05.png "alt=" Wkiol1gqxdvjfrhaaakznoik1y4381.png "/>

3, set the breakpoint to get the stack information at run time.

Set breakpoints separately for the following locations:

1) constructor: Base (int v)

2) Virtual destructor: Virtual ~base ()

3) static function call: Bb.static_func1 ()

4) Non-static function call: Bb.non_static_func1 ()

5) virtual function call: Bb.virtual_func1 ()

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/89/5E/wKiom1gQxFyiwmZ1AATu_SAwNMs043.png "title=" screen shot 2016-10-25 23.58.52.png "alt=" Wkiom1gqxfyiwmz1aatu_sawnms043.png "/>

4, run the program, encounter the first breakpoint: a static function call. The Step tracking S (step) command goes inside the function and prints the address as follows:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/89/5C/wKioL1gQxIzQ8_ahAAHRxyPe-Gs050.png "title=" screen shot 2016-10-26 20.23.55.png "alt=" Wkiol1gqxizq8_ahaahrxype-gs050.png "/>

The 5,c (continue) recovery program runs, and then the program stops at the second breakpoint, the base (int v) constructor. Print the base class object bb as follows:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/89/5E/wKiom1gQxMbiFZONAAGO6CGGfZM431.png "title=" screen shot 2016-10-26 22.17.07.png "alt=" Wkiom1gqxmbifzonaago6cggfzm431.png "/>

The address of the class object by graph can be: 0x7fffffffe320

Dereference the first address of an object gets the address: 0x400cb0 is the virtual function table address, so the virtual function table in the compiler is at the forefront of the object instance.

From the printed base class object, the virtual function table is created, and the virtual function table address is 0x400cb0. For both static and non-static members, it is all initialized. At this point, the contents of the virtual function table are as follows:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/89/5E/wKiom1gQxNvi4gO3AAHbaWC1L_8284.png "title=" screen shot 2016-10-26 21.03.08.png "alt=" Wkiom1gqxnvi4go3aahbawc1l_8284.png "/>

Because the virtual function table is a level two pointer. so use void * * conversion. Then use the dereference operator to become a first-level pointer.

It also involves set print array,@ and/A three knowledge points:

1) The default array display is off (that is, when you print an array, each element is separated by commas ). When the array display state is turned on, each element is printed on one line.

2) p/a A is just one of the parameter options in the print statement, and the following are common parameters:

x Displays the variable in hexadecimal format.
d Displays the variable in decimal format.
u displays unsigned integers in hexadecimal format.
o Displays the variable in octal format.
T displays the variable in binary format.
a displays the variable in hexadecimal format.
c Displays the variable in character format.
f Displays the variable in floating-point number format.

3) Theleft side of "@" is the value of the address of the first memory, the right side of "@" you want to see the length of the memory. The 4 representation prints out four segments of memory length. The last value is a random number because there are three virtual function memory segment addresses stored in the virtual function table.

in order to support RTTI (run time type identification, runtime types recognize ), the type_info pointer is stored in front of the virtual function table.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/89/5C/wKioL1gQxQ6Q91ikAABx1c2iOu4468.png "style=" float: none; "title=" screen shot 2016-10-26 21.38.33.png "alt=" Wkiol1gqxq6q91ikaabx1c2iou4468.png "/>


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/89/5C/wKioL1gQxQ7wtLpeAAB9-oN_4S4759.png "style=" float: none; "title=" screen shot 2016-10-26 21.40.06.png "alt=" Wkiol1gqxq7wtlpeaab9-on_4s4759.png "/> and Static member variables and non-static member variables are obtained as follows:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/89/5E/wKiom1gQxS6Q-LvuAAFu1g2ny8s403.png "title=" screen shot 2016-10-26 21.28.00.png "alt=" Wkiom1gqxs6q-lvuaafu1g2ny8s403.png "/>

6,c (continue) continues to run, and then the program stays at the third breakpoint, the non-static method call.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/89/5C/wKioL1gQxU7Q0PToAACdT_r6P18438.png "title=" screen shot 2016-10-26 21.49.14.png "alt=" Wkiol1gqxu7q0ptoaacdt_r6p18438.png "/>

7,c (continue) continues to run, and then the program stays at the fourth breakpoint, the virtual function call.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/89/5E/wKiom1gQxV-SL96kAACl5bBzyNQ282.png "title=" screen shot 2016-10-26 21.52.12.png "alt=" Wkiom1gqxv-sl96kaacl5bbzynq282.png "/>

The address of the virtual function printed out is the same as the address stored in the virtual function table.

8,c (continue) continues to run, then the program stays at the fifth breakpoint, the virtual destructor.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/89/5C/wKioL1gQxXHzMewkAADzBf0vBmQ326.png "title=" screen shot 2016-10-26 21.57.14.png "alt=" Wkiol1gqxxhzmewkaadzbf0vbmq326.png "/>

The address is consistent with the virtual function table via info line.


This article gdb debug Command Main reference: http://blog.csdn.net/haoel/article/details/2879

This article is from the "11988200" blog, please be sure to keep this source http://11998200.blog.51cto.com/11988200/1866100

[C + +] GDB debugging C + + classes

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.