如何在gdb中列出类方法?

时间:2021-07-09 20:44:23

I've been googling for this and checking through the gdb manual but can't seem to find an answer to what I'm trying to do.

我一直在谷歌上搜索并检查gdb手册,但似乎无法找到我正在尝试做的答案。

Is there a way to get gdb to print out a listing of all the methods for a given class type? The print command only seems to show the data members and fields, none of the methods are displayed for it.

有没有办法让gdb打印出给定类类型的所有方法的列表? print命令似乎只显示数据成员和字段,没有显示任何方法。

Additionally, to take it a step further, is there a way to print all the correct virtual methods given a base *pointer? Say like for example:

另外,为了更进一步,有没有办法在给定base *指针的情况下打印所有正确的虚拟方法?比如说:

struct A
{
  virtual void foo() {}
};

struct B : public A
{
  void foo() {}
};

int main()
{
  A *b = new B;
}

How can I get gdb to print variable *b and have it show the correct virtual method(s)?

如何让gdb打印变量* b并让它显示正确的虚拟方法?

Thanks

谢谢

1 个解决方案

#1


34  

You can use ptype.

你可以使用ptype。

Suppose I add these lines to your example:

假设我将这些行添加到您的示例中:

A alpha;
B beta;

Now in gdb I can ask for a description of a class type (or an instance of one):

现在在gdb中我可以要求描述类类型(或一个实例):

(gdb) ptype alpha
type = class A {
  public:
    virtual void foo();
}

(gdb) ptype A
type = class A {
  public:
    virtual void foo();
}

(gdb) ptype beta
type = class B : public A {
  public:
    virtual void foo();
}

(gdb) ptype B
type = class B : public A {
  public:
    virtual void foo();
}

If I try that with a pointer, I get the declared type:

如果我用指针尝试,我得到声明的类型:

(gdb) ptype b
type = class A {
  public:
    virtual void foo();
} *

If I want the real type, I must set the `print object' variable:

如果我想要真正的类型,我必须设置`print object'变量:

(gdb) set print object on
(gdb) ptype b
type = /* real type = B * */
class A {
  public:
    virtual void foo();
} *

and then call ptype again to see what B has (I don't know how to do it in one step).

然后再次调用ptype以查看B有什么(我不知道如何一步完成)。

#1


34  

You can use ptype.

你可以使用ptype。

Suppose I add these lines to your example:

假设我将这些行添加到您的示例中:

A alpha;
B beta;

Now in gdb I can ask for a description of a class type (or an instance of one):

现在在gdb中我可以要求描述类类型(或一个实例):

(gdb) ptype alpha
type = class A {
  public:
    virtual void foo();
}

(gdb) ptype A
type = class A {
  public:
    virtual void foo();
}

(gdb) ptype beta
type = class B : public A {
  public:
    virtual void foo();
}

(gdb) ptype B
type = class B : public A {
  public:
    virtual void foo();
}

If I try that with a pointer, I get the declared type:

如果我用指针尝试,我得到声明的类型:

(gdb) ptype b
type = class A {
  public:
    virtual void foo();
} *

If I want the real type, I must set the `print object' variable:

如果我想要真正的类型,我必须设置`print object'变量:

(gdb) set print object on
(gdb) ptype b
type = /* real type = B * */
class A {
  public:
    virtual void foo();
} *

and then call ptype again to see what B has (I don't know how to do it in one step).

然后再次调用ptype以查看B有什么(我不知道如何一步完成)。