问题描述
我一直在谷歌搜索这个,检查通过gdb手册,但似乎找不到我想要做的解答。
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.
此外,为了进一步,打印所有正确的虚方法给一个base *指针?例如:
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:
struct A
{
virtual void foo() {}
};
struct B : public A
{
void foo() {}
};
int main()
{
A *b = new B;
}
如何获取gdb打印变量* b并显示正确的虚拟方法?
How can I get gdb to print variable *b and have it show the correct virtual method(s)?
感谢
推荐答案
ptype
。
假设我将这些行添加到您的示例中:
Suppose I add these lines to your example:
A alpha;
B beta;
现在在gdb中,我可以要求一个类类型(或一个实例)的描述:
Now in gdb I can ask for a description of a class type (or an instance of one):
(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();
} *
如果我想要真正的类型,打印对象的变量:
(gdb) set print object on
(gdb) ptype b
type = /* real type = B * */
class A {
public:
virtual void foo();
} *
然后调用 ptype
再次看到 B
有(我不知道如何一步完成)。
and then call ptype
again to see what B
has (I don't know how to do it in one step).
这篇关于如何在gdb中列出类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!