简体   繁体   English

用ptype显示GDB中的所有C ++数据字段(甚至继承)

[英]displaying with ptype all C++ data fields (even inherited) in GDB

In a C++11 program (compiled with GCC 5 on Linux/Debian/x86-64, with g++ -Og -g3 -Wall -std=c++11 b.cc -o bprog ), when you have a class hierarchy such as in this file b.cc : 在C ++ 11程序中(在Linux / Debian / x86-64上与GCC 5一起编译 ,带有g++ -Og -g3 -Wall -std=c++11 b.cc -o bprog ),当您具有类层次结构时例如在此文件b.cc

  #include <string>
  #include <vector>
  class A {
    int a1i;
    double a2d;
  public:
    A(int x, double y) : a1i(x), a2d(y) {};
    ~A() = default;
  };

  class B : public A {
    std::string b1s;
    int b2ii[2];
    std::vector<long> b3vl;
  public:
    B (int x, double y, const char*s, int a, int b)
      : A(x,y), b1s(s), b2ii{a,b},
        b3vl{x,a,b,x+a,a*b} {};
    ~B() = default;
  };

  int main(int argc, char**argv) {
    B bobj {1,2.1,"bname",3,4};
    return 0;
  }      

and using a recent GDB (ie 7.10) I would like to display all the data fields (perhaps even the vtable pointer[s], if there is some) of the type. 并使用最新的GDB (即7.10),我想显示该类型的所有数据字段(如果有的话甚至是vtable指针)。 Using the ptype command is not extremely helpful (because inherited fields are not displayed): 使用ptype命令不是很有帮助(因为不会显示继承的字段):

 (gdb) ptype B
  type = class B : public A {
    private:
      std::__cxx11::string b1s;
      int b2ii[2];
      std::vector<long, std::allocator<long> > b3vl;

    public:
      B(int, double, const char *, int, int);
      ~B(int);
  }      

Most of the motivation is that I am interfacing to GCCJIT (or perhaps some other JIT library, but probably GCCJIT), and of course a JITing code need to know implementation details like types and offsets of every data field. 大部分动机是我正在与GCCJIT (或也许是其他一些JIT库,但可能还有GCCJIT)进行交互,当然,JITing代码需要了解实现细节,例如每个数据字段的类型和偏移量。 If you are interested in more details, see this thread . 如果您对更多细节感兴趣,请参阅此线程

For example, a read access to bobj.b3vl[i] should be inlined by the JIT-er, so I need to understand the machine layout ... 例如,JIT-er应该内联到bobj.b3vl[i]的读取访问权限,因此我需要了解机器布局...

I am aware that the fields inside C++ standard containers and strings are implementation specific (but I do know that they don't vary that much, but a tiny bit, in practice) 我知道C ++标准容器和字符串中的字段是特定于实现的(但我确实知道它们在实际中相差不大,但变化不大)

Do you have any trick or tips to get all that (field name, type, and perhaps offset) information easily thru GDB (including for inherited fields)? 您是否有任何技巧或窍门可以通过GDB轻松获取所有(字段名称,类型和偏移量)信息(包括继承字段的信息)?

I don't think there is a way to do this that is built in to gdb. 我认为没有gdb内置的方法可以做到这一点。 See this gdb feature request . 请参阅此gdb功能请求

However, it can be done relatively easily from Python. 但是,可以使用Python相对轻松地完成此操作。 If you have the pahole gdb command (not part of upstream gdb, but shipped in Fedora at least), that will do the trick (though it seems to generate really ugly output right now). 如果您拥有pahole gdb命令(不是上游gdb的一部分,但至少是在Fedora中提供的),那么就可以解决问题(尽管现在看起来似乎确实很丑陋)。 It will at least show how the code could be written. 至少将显示如何编写代码。

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

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