* Could GDB get offset of a field in virtual base class through NULL pointer
@ 2013-09-28 16:23 hex
2013-09-28 18:39 ` Jan Kratochvil
0 siblings, 1 reply; 5+ messages in thread
From: hex @ 2013-09-28 16:23 UTC (permalink / raw)
To: gdb
Hi All,
I defined two classes as following:
// test.cpp
class A{
public:
int a;
};
class B: public virtual A{
public:
int b;
};
GDB could print &(((B *)0)->a), but it could not print &(((B *)0)->a).
I debugged GDB, and found it try to visit the virtual table of (B *)0,
then it failed. It is reasonable. But I think whether GDB could
support this case, it could just regard the NULL pointer as a special
case: it could get offset using TYPE_BASECLASS_BITPOS(). But the
TYPE_BASECLASS_BITPOS() returns 0 now because we have not set a value
for virtual base class. Then problem is whether we could store an
offset for virtual base class to support above case. But I do not
know how to get this offset from sections of its binary. Could you
give some suggestions please? Thank you!
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Could GDB get offset of a field in virtual base class through NULL pointer
2013-09-28 16:23 Could GDB get offset of a field in virtual base class through NULL pointer hex
@ 2013-09-28 18:39 ` Jan Kratochvil
2013-09-29 1:59 ` hex
0 siblings, 1 reply; 5+ messages in thread
From: Jan Kratochvil @ 2013-09-28 18:39 UTC (permalink / raw)
To: hex; +Cc: gdb
On Sat, 28 Sep 2013 18:23:22 +0200, hex wrote:
> I defined two classes as following:
> // test.cpp
> class A{
> public:
> int a;
> };
> class B: public virtual A{
> public:
> int b;
> };
>
> GDB could print &(((B *)0)->a), but it could not print &(((B *)0)->a).
^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
Those two expressions are the same and they really od not work:
(gdb) p &(((B *)0)->a)
Cannot access memory at address 0x0
> it could just regard the NULL pointer as a special
> case: it could get offset using TYPE_BASECLASS_BITPOS().
I do not see what it should do. In the following case &(((B *)&OBJECT)->a)
prints once 12 and once 16 for different OBJECT so what it should print for 0?
class X:public virtual A,public B {};
class C {
public:
int c;
};
class Y:public virtual A,public C,public B {};
#include <iostream>
int main() {
X x;
Y y;
std::cout << (char *)&(((B *)&x)->a)-(char *)&x << std::endl;
std::cout << (char *)&(((B *)&y)->a)-(char *)&y << std::endl;
}
Jan Kratochvil
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Could GDB get offset of a field in virtual base class through NULL pointer
2013-09-28 18:39 ` Jan Kratochvil
@ 2013-09-29 1:59 ` hex
2013-09-30 8:16 ` Jan Kratochvil
0 siblings, 1 reply; 5+ messages in thread
From: hex @ 2013-09-29 1:59 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb
>> GDB could print &(((B *)0)->a), but it could not print &(((B *)0)->a).
> ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
> Those two expressions are the same and they really od not work:
>
> (gdb) p &(((B *)0)->a)
> Cannot access memory at address 0x0
>
Sorry, a mistake, I meant "GDB could print &(((B *)0)->b), but it could not
print &(((B *)0)->a)."
> I do not see what it should do. In the following case &(((B *)&OBJECT)->a)
> prints once 12 and once 16 for different OBJECT so what it should print for 0?
>
> class X:public virtual A,public B {};
> class C {
> public:
> int c;
> };
> class Y:public virtual A,public C,public B {};
> #include <iostream>
> int main() {
> X x;
> Y y;
> std::cout << (char *)&(((B *)&x)->a)-(char *)&x << std::endl;
> std::cout << (char *)&(((B *)&y)->a)-(char *)&y << std::endl;
> }
>
If we use &(((B *)0)->a), we are likely to get offset of 'a' in class
B. If GDB could
support this specific case, we do not need a real object to get the offset.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Could GDB get offset of a field in virtual base class through NULL pointer
2013-09-29 1:59 ` hex
@ 2013-09-30 8:16 ` Jan Kratochvil
2013-09-30 14:29 ` hex
0 siblings, 1 reply; 5+ messages in thread
From: Jan Kratochvil @ 2013-09-30 8:16 UTC (permalink / raw)
To: hex; +Cc: gdb
On Sun, 29 Sep 2013 03:59:54 +0200, hex wrote:
> > I do not see what it should do. In the following case &(((B *)&OBJECT)->a)
> > prints once 12 and once 16 for different OBJECT so what it should print for 0?
> >
> > class X:public virtual A,public B {};
> > class C {
> > public:
> > int c;
> > };
> > class Y:public virtual A,public C,public B {};
> > #include <iostream>
> > int main() {
> > X x;
> > Y y;
> > std::cout << (char *)&(((B *)&x)->a)-(char *)&x << std::endl;
> > std::cout << (char *)&(((B *)&y)->a)-(char *)&y << std::endl;
> > }
> >
>
> If we use &(((B *)0)->a), we are likely to get offset of 'a' in class
> B. If GDB could
> support this specific case, we do not need a real object to get the offset.
This would apply if you had s/virtual A/A/. But with the inheritance of
A being virtual the memory location of A inside the whole object instance is
"random", it does not depend on B but it depends on X or Y. Specifically it
depends on virtual tables used for the specific instance, the virtual tables
specify the location of A. This is what I am trying to show you in the
example above.
The same expression (char *)&(((B *)&OBJECT)->a) produces different result
depending on which OBJECT you pass there. Therefore which result should
produce passing 0 instead of &OBJECT there? It cannot be a single number.
Jan Kratochvil
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Could GDB get offset of a field in virtual base class through NULL pointer
2013-09-30 8:16 ` Jan Kratochvil
@ 2013-09-30 14:29 ` hex
0 siblings, 0 replies; 5+ messages in thread
From: hex @ 2013-09-30 14:29 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb
2013/9/30 Jan Kratochvil <jan.kratochvil@redhat.com>:
> On Sun, 29 Sep 2013 03:59:54 +0200, hex wrote:
>> > I do not see what it should do. In the following case &(((B *)&OBJECT)->a)
>> > prints once 12 and once 16 for different OBJECT so what it should print for 0?
>> >
>> > class X:public virtual A,public B {};
>> > class C {
>> > public:
>> > int c;
>> > };
>> > class Y:public virtual A,public C,public B {};
>> > #include <iostream>
>> > int main() {
>> > X x;
>> > Y y;
>> > std::cout << (char *)&(((B *)&x)->a)-(char *)&x << std::endl;
>> > std::cout << (char *)&(((B *)&y)->a)-(char *)&y << std::endl;
>> > }
>> >
>>
>> If we use &(((B *)0)->a), we are likely to get offset of 'a' in class
>> B. If GDB could
>> support this specific case, we do not need a real object to get the offset.
>
> This would apply if you had s/virtual A/A/. But with the inheritance of
> A being virtual the memory location of A inside the whole object instance is
> "random", it does not depend on B but it depends on X or Y. Specifically it
> depends on virtual tables used for the specific instance, the virtual tables
> specify the location of A. This is what I am trying to show you in the
> example above.
>
> The same expression (char *)&(((B *)&OBJECT)->a) produces different result
> depending on which OBJECT you pass there. Therefore which result should
> produce passing 0 instead of &OBJECT there? It cannot be a single number.
>
>
> Jan Kratochvil
Thank you for the explanation.
I hope &(((B *)0)->a) to be regarded as a special case that gets the
same value as (B object; (char *)&((&object)->a) - (char *)&object).
If G++ emits A's offset in B to the program's DAWRF file, GDB could
support this case by saving the offset. But I checked, only found this
offset in the binary code of B's constructor function.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2013-09-30 14:29 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-28 16:23 Could GDB get offset of a field in virtual base class through NULL pointer hex
2013-09-28 18:39 ` Jan Kratochvil
2013-09-29 1:59 ` hex
2013-09-30 8:16 ` Jan Kratochvil
2013-09-30 14:29 ` hex
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox