c++ - 列出gdb中类的所有功能

标签 c++ gdb g++

我正在尝试学习使用 gdb 进行调试。我已经开始了。

我只想知道是否可以列出一个类的所有函数,包括编译器提供的默认函数?

或者甚至不使用 IDE 的其他方式

谢谢

============================================= ==============================

我正在尝试的代码:

#include <iostream>

class MyClass
{
    public:
        void Hello() {
        }
        int a;
};

int main(int argc, char **argv)
{
    MyClass a;
    MyClass b = a;
    MyClass c;
    c = a;
    return 0;
}

=更新 3============================================ ========================

如果可能的话,我还想在 gdb 中列出编译器提供的函数名称。

我的问题是除了问题张贴在

How to list class methods in gdb?

最佳答案

你写的类太简单了。这个建议:

Once you have the executable loaded in gdb, type break (or b) and hit the tab key.

通常是正确的,但在您的情况下,MinGW 不会为 MyClass 创建任何内容。我用 MinGW 编译了你的程序并反汇编了它:

(gdb) disassemble /m main
Dump of assembler code for function main(int, char**):
13      {
   0x0040138c <+0>:     push   %ebp
   0x0040138d <+1>:     mov    %esp,%ebp
   0x0040138f <+3>:     and    $0xfffffff0,%esp
   0x00401392 <+6>:     sub    $0x10,%esp
   0x00401395 <+9>:     call   0x40193c <__main>

14          MyClass a;
15          MyClass b = a;
   0x0040139a <+14>:    mov    0xc(%esp),%eax
   0x0040139e <+18>:    mov    %eax,0x8(%esp)

16          MyClass c;
17          c = a;
   0x004013a2 <+22>:    mov    0xc(%esp),%eax
   0x004013a6 <+26>:    mov    %eax,0x4(%esp)

18          return 0;
   0x004013aa <+30>:    mov    $0x0,%eax

19      }
   0x004013af <+35>:    leave
   0x004013b0 <+36>:    ret

End of assembler dump. 

如您所见,只有移动指令。例如,您的分配 c = a; 结果只有两条移动指令,没有函数调用:

0x004013a2 <+22>:    mov    0xc(%esp),%eax
0x004013a6 <+26>:    mov    %eax,0x4(%esp)

如您所见,编译器选择不为您的类生成任何内容。在我看来,您选择的示例太简单,无法了解您想要的内容。

我让你的例子有点复杂

#include <iostream>

class MyClass
{
    public:
        void Hello()
        {
          std::cout << "Hello\n";
        }
        int a;
};

int main(int argc, char **argv)
{
    MyClass a;
    a.Hello();
    MyClass b = a;
    MyClass c;
    c = a;
    return 0;
}

gdb 下 break My 显示如下:

(gdb) b MyClass
MyClass           MyClass::Hello()
(gdb) b MyClass

这是 nm 的输出:

D:\src-c++\test.names>nm -C ./m.exe | grep MyClass
00404060 r .eh_frame$_ZN7MyClass5HelloEv
00401c20 t .text$_ZN7MyClass5HelloEv
00401c20 T MyClass::Hello()

I just wanted to see what are the default function generated by the class, if one does not write it

将您的成员类变量从 'int a' 更改为 std::string a,您将看到编译器生成的默认函数

#include <iostream>
#include <string>

class MyClass
{
public:
  void Hello() {
  }
  std::string a;
};

int main(int argc, char **argv)
{
  MyClass a;
  MyClass b = a;
  MyClass c;
  c = a;
  return 0;
}

这些是编译生成的函数:

>nm -C ./a.out | grep My
00000000004009b8 W MyClass::MyClass(MyClass const&)
0000000000400964 W MyClass::MyClass()
000000000040097c W MyClass::~MyClass()
0000000000400994 W MyClass::operator=(MyClass const&)

关于c++ - 列出gdb中类的所有功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14940682/

相关文章:

c++ - 从 STL 容器中并行读取

c - 如何使用一个终端调试 QEMU?

gdb:按名称搜索函数以设置断点

c++ - ISO C++ 禁止声明没有类型的 ‘vector’

c++ - 复制构造函数+浅复制和深复制

c++ - 在 C++ 中显示字符串、vector<int> 映射

c++ - cpp预处理器如何从子目录中获取 header

c++ - 使用 clang 强制对 constexpr 进行编译时间评估

C++ 使用 vector 从文本文件中读取和写入数字

c++ - 使用 gdb 使用 Rcpp 调试 R 包的 C++ 代码无法使用 R_PV(未知返回类型)打印变量值