This is the mail archive of the
gdb@sourceware.org
mailing list for the GDB project.
Re: Could GDB get offset of a field in virtual base class through NULL pointer
- From: hex <heixia108 at gmail dot com>
- To: Jan Kratochvil <jan dot kratochvil at redhat dot com>
- Cc: gdb at sourceware dot org
- Date: Mon, 30 Sep 2013 22:29:29 +0800
- Subject: Re: Could GDB get offset of a field in virtual base class through NULL pointer
- Authentication-results: sourceware.org; auth=none
- References: <CAB_AMN7niBPRf4Mc6Tq=NXnbQacc7DKmM4btodcmKJ4TjOf8Bw at mail dot gmail dot com> <20130928183852 dot GA12891 at host2 dot jankratochvil dot net> <CAB_AMN5ngrKWwwVwUBFQ1wS5fLiDCyDRO+qe9bOjPBJAMo-05Q at mail dot gmail dot com> <20130930081626 dot GA15265 at host2 dot jankratochvil dot net>
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.