How to list class methods in gdb?(如何在 gdb 中列出类方法?)
问题描述
我一直在谷歌上搜索并检查 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 中列出类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 gdb 中列出类方法?


基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05