如何在 gdb 中为 C++ 设置 operator() 断点?

发布于 2024-08-16 03:02:45 字数 475 浏览 1 评论 0原文

我在 C++ 类中有 2 个方法,如下所示:

 class myClass {
     public:
         void operator()( string myString ) {
             // Some code
         }
         void myMethod() { ... }
 }

对于常规方法,我可以简单地在 GDB 中将断点设置为:

b myClass::myMethod

但是如何为第一个方法设置断点?

更新:

最初答案 (b myClass ::operator()) 中的建议不起作用:(

b myClass::operator()
Function "myClass::operator()" not defined.

谢谢!

I have 2 methods in C++ class as follows:

 class myClass {
     public:
         void operator()( string myString ) {
             // Some code
         }
         void myMethod() { ... }
 }

For a regular method, I can simply set the breakpoint in GDB as:

b myClass::myMethod

But how do I set the breakpoint for the first method?

UPDATE:

The suggestions from initial answers (b myClass ::operator()) does not work :(

b myClass::operator()
Function "myClass::operator()" not defined.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

财迷小姐 2024-08-23 03:02:46

gdb 还会在特定行号处设置断点。例如
b 文件.cc:45

gdb will also take breakpoints at specific line numbers. For example
b file.cc:45

两相知 2024-08-23 03:02:46

一样。 myClass::operator()(string) 是一个常规方法。

如果您有多个重载的 operator() 方法(例如 const 和非 const 版本),gdb 应该提供在何处设置断点的选择:

http://sunsite.ualberta.ca/Documentation/Gnu/gdb-5.0/html_node/gdb_35.html# SEC35

您可能必须确保方法 operator()(string) 确实已编译。

编辑:

我测试了以下文件test.cpp:

#include <string>
#include <iostream>

class myClass {
        public:
        void operator()( int i ) {
                std::cout << "operator()";
        }

        void myMethod() {
                std::cout << "myMethod";
        }
};

int main() {
   myClass c;
   c(1);
   c.myMethod();
   return 0;
}

g++ test.cpp -o test编译,运行gdb test(版本GNU gdb 6.3.50-20050815(苹果版gdb-1344)),输入start才可以设置断点。

b 'myClass::operator()(string)'

b myClass::operator()

都有效。

Just the same. myClass::operator()(string) is a regular method.

If you have several overloaded operator() methods (e.g. a const and a non-const version) gdb should offer the choice where to set the breakpoint:

http://sunsite.ualberta.ca/Documentation/Gnu/gdb-5.0/html_node/gdb_35.html#SEC35

You may have to make sure that method operator()(string) is actually compiled.

Edit:

I've tested the following file test.cpp:

#include <string>
#include <iostream>

class myClass {
        public:
        void operator()( int i ) {
                std::cout << "operator()";
        }

        void myMethod() {
                std::cout << "myMethod";
        }
};

int main() {
   myClass c;
   c(1);
   c.myMethod();
   return 0;
}

Compiled with g++ test.cpp -o test, ran gdb test (version GNU gdb 6.3.50-20050815 (Apple version gdb-1344)), typed start and only then could I set breakpoints.

b 'myClass::operator()(string)' and

b myClass::operator()

both worked.

夜血缘 2024-08-23 03:02:46

一些 C++ 函数名称确实很难正确输入。更糟糕的是,gdb 的自动完成功能经常与 C++ 名称混淆。我使用这个技巧

gdb> break 'myClass::operator()<TAB>

注意函数开头的单引号。这有助于 gdb 的自动完成程序。

Some C++ functions names can be really hard to type out correctly. Worse yet, gdb's autocompletion often gets confused with c++ names. I use this trick

gdb> break 'myClass::operator()<TAB>

Note the single quote at the beginning of the function. That helps gdb's autocompleter.

云巢 2024-08-23 03:02:46
b myClass::operator()
b myClass::operator()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文