Gdb issues with virtual inheritance

M

Mahesh

Hi,
I encounted some problems using GDB with classes that use virtual
inheritance. To illustrate this issue, I have created a simple test
case.
Here it goes.




#include <iostream>
using namespace std;


///////////////////////////////////////////////////////
class ViBase
{
public:

ViBase(const char* s)
: m_s(s)
{
cout << "- ViBase constructor called: 0x" << hex << this
<< " s=" << s << dec << endl << endl;
};

const char* GetS() const
{
return m_s;
};

private:

const char* m_s;
};


///////////////////////////////////////////////////////
class Base
: virtual public ViBase
{
public:

Base(const int k)
: ViBase("bad"),
m_k(k)
{
cout << "- Base constructor called: 0x" << hex << this
<< dec << endl << endl;
};

const int GetK() const
{
return m_k;
};

private:

const int m_k;
};


///////////////////////////////////////////////////////
class Final
: public Base
{
public:

Final(const char *s,
const int k)
: Base(k),
ViBase(s)
{
cout << "- Final constructor called: 0x" << hex << this
<< dec << endl << endl;
};

void Print()
{
cout << endl << "Base : S = " << GetS() << endl
<< " K = " << GetK() << endl << endl;
};

private:

int m_a;
};


///////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
Final *x = new Final("ObjectX", 2007);
if (x == NULL)
cout << endl << "Out of memory." << endl << endl;

x->Print();

cout << "GetS() = " << x->GetS() << endl;
cout << "GetK() = " << x->GetK() << endl;

delete x;
return 0;
}

****************************************************************************************************************************
////There are 3 levels here. ViBase <--Base<--Final
At each level as the object is constructed I print the address of the
"this" pointer
I also have a simple function to access some member variable name.
If I run the program I have no problems at all. The problem arises
when I use gdb to step into the code.
Here's what I get. (I have provided a snapshot of the gdb session)

****************************************************************************************************************************



gdb ./a.out
GNU gdb Red Hat Linux (5.2-2)
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i386-redhat-linux"...
(gdb) b main.cpp:88
Breakpoint 1 at 0x80489ec: file main.cpp, line 88.
(gdb) r
Starting program: /usr2/maheshs/vir_inherit/a.out
- ViBase constructor called: 0x0x804a4bc s=ObjectX

- Base constructor called: 0x0x804a4b0

- Final constructor called: 0x0x804a4b0


Breakpoint 1, main (argc=1, argv=0xbfffa934) at main.cpp:88
88 x->Print();
(gdb) s
Final::print() (this=0x804a4b0) at main.cpp:71
71 cout << endl << "Base : S = " << GetS() << endl
(gdb) p *this
$1 = {<Base> = {<ViBase> = {m_s = 0x8048f20 "ObjectX"}, _vptr.Base =
0x8049014, m_k = 2007}, m_a = 0}
(gdb) n

Base : S = ObjectX
K = 2007

73 };
(gdb) p x
No symbol "x" in current context.
(gdb) l
68
69 void Print()
70 {
71 cout << endl << "Base : S = " << GetS() << endl
72 << " K = " << GetK() << endl <<
endl;
73 };
74
75 private:
76
77 int m_a;
(gdb) n
main (argc=1, argv=0xbfffa934) at main.cpp:90
90 cout << "GetS() = " << x->GetS() << endl;
(gdb) p x
$2 = (Final *) 0x804a4b0
(gdb) p x->GetS()
$3 = 0x8048f20 "ObjectX"
(gdb) p x->GetK()
Cannot access memory at address 0x0
(gdb)


As you can see above, the pointers printed by the cout statments do
not correspond to the this pointer which I print. Also in the last
line, I'm unable to call "x->GetK()"


Is this a GDB issue?? HAs anyone seen this in the past?
Is there a patch or some fix available?
Do let me know
Thanks,
Mahesh
 
R

Ron Natalie

Mahesh said:
Is this a GDB issue?? HAs anyone seen this in the past?
Is there a patch or some fix available?
Do let me know

Almost certainly, and completely off topic for this group, try
gnu.something.

It looks like gdb doesn't know how to adjust the pointer for the
virtual base which isn't too surprising, it's a bit tricky.
 
J

jason.rothfuss

Hi,
I encounted some problems using GDB with classes that use virtual
inheritance. To illustrate this issue, I have created a simple test
case.
Here it goes.

#include <iostream>
using namespace std;

///////////////////////////////////////////////////////
class ViBase
{
public:

ViBase(const char* s)
: m_s(s)
{
cout << "- ViBase constructor called: 0x" << hex << this
<< " s=" << s << dec << endl << endl;
};

const char* GetS() const
{
return m_s;
};

private:

const char* m_s;

};

///////////////////////////////////////////////////////
class Base
: virtual public ViBase
{
public:

Base(const int k)
: ViBase("bad"),
m_k(k)
{
cout << "- Base constructor called: 0x" << hex << this
<< dec << endl << endl;
};

const int GetK() const
{
return m_k;
};

private:

const int m_k;

};

///////////////////////////////////////////////////////
class Final
: public Base
{
public:

Final(const char *s,
const int k)
: Base(k),
ViBase(s)
{
cout << "- Final constructor called: 0x" << hex << this
<< dec << endl << endl;
};

void Print()
{
cout << endl << "Base : S = " << GetS() << endl
<< " K = " << GetK() << endl << endl;
};

private:

int m_a;

};

///////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
Final *x = new Final("ObjectX", 2007);
if (x == NULL)
cout << endl << "Out of memory." << endl << endl;

x->Print();

cout << "GetS() = " << x->GetS() << endl;
cout << "GetK() = " << x->GetK() << endl;

delete x;
return 0;

}

****************************************************************************************************************************
////There are 3 levels here. ViBase <--Base<--Final
At each level as the object is constructed I print the address of the
"this" pointer
I also have a simple function to access some member variable name.
If I run the program I have no problems at all. The problem arises
when I use gdb to step into the code.
Here's what I get. (I have provided a snapshot of the gdb session)

****************************************************************************************************************************

gdb ./a.out
GNU gdb Red Hat Linux (5.2-2)
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i386-redhat-linux"...
(gdb) b main.cpp:88
Breakpoint 1 at 0x80489ec: file main.cpp, line 88.
(gdb) r
Starting program: /usr2/maheshs/vir_inherit/a.out
- ViBase constructor called: 0x0x804a4bc s=ObjectX

- Base constructor called: 0x0x804a4b0

- Final constructor called: 0x0x804a4b0

Breakpoint 1, main (argc=1, argv=0xbfffa934) at main.cpp:88
88 x->Print();
(gdb) s
Final::print() (this=0x804a4b0) at main.cpp:71
71 cout << endl << "Base : S = " << GetS() << endl
(gdb) p *this
$1 = {<Base> = {<ViBase> = {m_s = 0x8048f20 "ObjectX"}, _vptr.Base =
0x8049014, m_k = 2007}, m_a = 0}
(gdb) n

Base : S = ObjectX
K = 2007

73 };
(gdb) p x
No symbol "x" in current context.
(gdb) l
68
69 void Print()
70 {
71 cout << endl << "Base : S = " << GetS() << endl
72 << " K = " << GetK() << endl <<
endl;
73 };
74
75 private:
76
77 int m_a;
(gdb) n
main (argc=1, argv=0xbfffa934) at main.cpp:90
90 cout << "GetS() = " << x->GetS() << endl;
(gdb) p x
$2 = (Final *) 0x804a4b0
(gdb) p x->GetS()
$3 = 0x8048f20 "ObjectX"
(gdb) p x->GetK()
Cannot access memory at address 0x0
(gdb)

As you can see above, the pointers printed by the cout statments do
not correspond to the this pointer which I print. Also in the last
line, I'm unable to call "x->GetK()"

Is this a GDB issue?? HAs anyone seen this in the past?
Is there a patch or some fix available?
Do let me know
Thanks,
Mahesh

It looks like you are using an old version of gdb (5.2). Try using a
newer version (6.5 or 6.6). C++ support has improved quite a bit
since.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top