问题描述
我一直在谷歌上搜索并检查 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 打印出给定类类型的所有方法的列表?print 命令似乎只显示数据成员和字段,没有显示任何方法.
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.
此外,更进一步,有没有办法打印给定基 * 指针的所有正确虚拟方法?比如说:
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();
} *
如果我想要真正的类型,我必须设置`print object'变量:
(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 中列出类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!