简体   繁体   English

按偏移量查找成员

[英]Find member by offset

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'. 给定偏移量4并假设sizeof(int) == 4 ,我想得到'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. 显然,我不想在运行时执行此操作,因此我已经使用nmobjdumpgdb已有一段时间了,但是没有做到这一点。

Had the same problem earlier today, 今天早些时候有同样的问题,

The most suitable tool I can find is pahole . 我能找到的最合适的工具是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>. 不幸的是,它不打印模板内容,例如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). 我还发现了pstruct (比pahole更容易混淆,根本不接受C ++), Clang -cc1 -fdump-record-layouts (它给了我一堆指向Clang地址空间的指针,但没有偏移量)和MSVC -d1reportAllClassLayout (当我尝试时,该标志没有区别)。

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. 将结构记忆设置为零,将相关偏移设置为0xFF,在gdb中打印结构(使用set print pretty on这样就不会将所有内容都放在一条大行上),对非零值进行grep输出,并希望STL漂亮打印机不要太困惑。

Maybe I should make a GDB module to automate this, it's quite tedious no matter what I do... 也许我应该制作一个GDB模块来自动执行此操作,无论我做什么都非常乏味...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM