0

I have a (big) class and an offset into that class. How can I efficiently find the member defined at that offset?

Example:

struct Dummy {
    int a, b, c;
}

Given an offset of 4 and assuming sizeof(int) == 4, I would like to get 'b'.

Obviously I wouldn't want do this at runtime, so I have been playing around with nm, objdump and gdb for a while now, but don't manage to do this.

mbschenkel
  • 1,865
  • 1
  • 18
  • 40
  • 1
    There's no reflection in C++, but you can manually compare `offesetof` each member with your given value, provided your class is standard-layout. – Kerrek SB Nov 13 '15 at 16:20
  • @KerrekSB: Well, for a small class this is feasible, but I have literally hundreds of members, STL containers, etc. I expected that the compiler or another tool should be able to just dump out the internal structure including offsets. – mbschenkel Nov 13 '15 at 16:22
  • http://stackoverflow.com/questions/13180842/how-to-calculate-offset-of-a-class-member-at-compile-time – Unimportant Nov 13 '15 at 16:22
  • @user1320881: Thanks, but I want to do the *opposite*. – mbschenkel Nov 13 '15 at 16:23
  • 1
    May I ask why are you trying to do this? – Anon Mail Nov 13 '15 at 16:26
  • @AnonMail: I know that the particular value is not initialized in some cases and want to know what it is. Plus, it should be easy for a tool to tell me the structure of a class. – mbschenkel Nov 13 '15 at 16:28
  • 1
    Can you use an editor to search for a member name? Otherwise you will need a `std::map`, variable names and member offsets. – Thomas Matthews Nov 13 '15 at 16:28
  • A good static analysis tool will show you all uninitialized variables. – Thomas Matthews Nov 13 '15 at 16:29
  • @mbschenkel I would think a debugger would help you in this case. – Anon Mail Nov 13 '15 at 16:31
  • @AnonMail: I would think so too, I just haven't found a convenient way of doing it, so if you do, please let me know! – mbschenkel Nov 13 '15 at 16:32
  • How about using a proper diagnostic tool like ASAN for finding uninitialized-value bugs? – Kerrek SB Nov 13 '15 at 16:37
  • @kerreksb thanks for pointing out static analysis, however we are using it already. This is a bit of a more involved case and I am also just curious about how to inspect structures in any case. – mbschenkel Nov 13 '15 at 17:03

1 Answers1

2

Had the same problem earlier today,

The most suitable tool I can find is pahole. Usage example:

$ cat dummy.cpp
#include <string>

struct Dummy {
    int a, b, c;
    std::string d;
};
struct Dummy x; // gcc doesn't emit debug info for unused stuff
$ g++ -c dummy.cpp -ggdb3
$ pahole dummy.o
die__process_class: tag not supported (template_type_parameter)!
//trimmed structs __va_list_tag, tm and lconv
struct Dummy {
    int                        a;                    /*     0     4 */
    int                        b;                    /*     4     4 */
    int                        c;                    /*     8     4 */
    string                     d;                    /*    16     8 */
    //trimmed some constructors
};

Unfortunately, it doesn't print template contents, like std::string aka std::basic_string< char>.

I also found pstruct (easier confused than pahole, doesn't accept C++ at all), Clang -cc1 -fdump-record-layouts (it gave me a pile of pointers to Clang's address space, but no offsets) and MSVC -d1reportAllClassLayout (that flag made no difference when I tried).

Or the manual method:

Memset the struct to zero, set the relevant offsets to 0xFF, print struct in gdb (use set print pretty on so it doesn't put everything on a single huge line), grep the output for nonzeroes, and hope the STL pretty-printers don't get too confused.

Maybe I should make a GDB module to automate this, it's quite tedious no matter what I do...

Community
  • 1
  • 1
Alcaro
  • 1,664
  • 16
  • 21