在文件中的类成员函数上设置断点

发布于 2024-12-03 03:09:13 字数 926 浏览 1 评论 0原文

(gdb) b breakpoints.cpp:X::X()

Can't find member of namespace, class, struct, or union named "breakpoints.cpp:X::X"
Hint: try 'breakpoints.cpp:X::X()<TAB> or 'breakpoints.cpp:X::X()<ESC-?>
(Note leading single quote.)
Make breakpoint pending on future shared library load? (y or [n]) n

在以下代码上:

#include <stdio.h>
#include <iostream>

class X
{
    public:
        X   () 
        {
            std :: cout << "\nIn the default constructor";
        }

        X   (int) 
        {
            std :: cout << "\nIn the parameterized constructor";
        }

        ~X () {}
};

int main (int argc, char *argv[])
{
    X xObjA;
    X xObjB (11);

    while (--argc > 0)
    {
        printf("\n%s ", argv [argc]);
    }
    std :: cout << std :: endl << std :: endl;
}

文件名是:breakpoints.cpp

我缺少的要点是什么?

(gdb) b breakpoints.cpp:X::X()

Can't find member of namespace, class, struct, or union named "breakpoints.cpp:X::X"
Hint: try 'breakpoints.cpp:X::X()<TAB> or 'breakpoints.cpp:X::X()<ESC-?>
(Note leading single quote.)
Make breakpoint pending on future shared library load? (y or [n]) n

on the following code:

#include <stdio.h>
#include <iostream>

class X
{
    public:
        X   () 
        {
            std :: cout << "\nIn the default constructor";
        }

        X   (int) 
        {
            std :: cout << "\nIn the parameterized constructor";
        }

        ~X () {}
};

int main (int argc, char *argv[])
{
    X xObjA;
    X xObjB (11);

    while (--argc > 0)
    {
        printf("\n%s ", argv [argc]);
    }
    std :: cout << std :: endl << std :: endl;
}

File's name is: breakpoints.cpp

What's the point that I am missing?

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

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

发布评论

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

评论(3

陌伤浅笑 2024-12-10 03:09:13

这才是设置断点的正确方法。

您要么在错误的可执行文件上尝试(将breakpoints.cpp放入目录中并使用g++ -g Breakpoints.cpp进行编译,然后在a.out可执行文件上使用gdb),代码与发布的代码不同并且可能具有名称空间,或者您由于使用过时的 gdb 版本而偶然发现了一个旧错误。

That is the correct way to set a breakpoint.

You are either trying that on a wrong executable (put breakspoints.cpp in a directory and compile with g++ -g breakpoints.cpp and then use the gdb on the a.out executable), code that is different than posted and maybe having namespaces, or you stumbled on an old bug due to using an outdated gdb version.

傲影 2024-12-10 03:09:13

也许您需要在没有文件名的情况下定义断点。以下内容对我有用:

break FooNamespace::FooClass::doSomething()

我想这仅在类唯一时才有效,因此它应该位于命名空间中。

注意

如果有多个地方可以放置断点,我认为gdb会尝试在所有地方放置断点,所以你最终会得到一些 >像下面的

Breakpoint 1 at 0x7fe62f8e744d: file src/FooClass.cpp, line 42. (2 locations)
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   <MULTIPLE>
1.1                         y   0x00007fe62f8e744d in FooNamespace::FooClass::doSomething() at src/FooClass.cpp:42
1.2                         y   0x00007fe62f8e7c5d in FooNamespace::FooClass::doSomething() at src/FooClass.cpp:42

Perhaps you need to define your breakpoints without the file name. The following works for me:

break FooNamespace::FooClass::doSomething()

I imagine this only works when the class is unique however, so it should be in a namespace.

Note

If there are multiple places where the breakpoint can be placed, I think gdb will try to place breakpoints on all of the places, so you will end up with something like the following:

Breakpoint 1 at 0x7fe62f8e744d: file src/FooClass.cpp, line 42. (2 locations)
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   <MULTIPLE>
1.1                         y   0x00007fe62f8e744d in FooNamespace::FooClass::doSomething() at src/FooClass.cpp:42
1.2                         y   0x00007fe62f8e7c5d in FooNamespace::FooClass::doSomething() at src/FooClass.cpp:42
剑心龙吟 2024-12-10 03:09:13

如果为类定义了命名空间,您可能需要指定命名空间。如果它不是标准命名空间 std。
如果您正在执行正确的二进制文件,则文件名是可选的。
您可以通过以下方式验证该符号是否存在于可执行文件中。 “nm -C”命令,其中 -C 处理 C++ 的名称修改。

用一个例子来总结一下:
如果命名空间是“mySpace”,类是“X”,其成员是“Y”,
那么断点应该像下面这样,
“(gdb)b mySpace::X::Y”

You may need to specify the namespace if any defined for the class. If it other than the standard namespace std.
The file name is optional, if you are executing the correct binary.
You can verify if the symbol exists on the executable via. "nm -C" command, where -C handles name mangling for C++.

So to summarise with an Example:
If the namespace is "mySpace" and the class is "X" whose member is "Y",
then the breakpoint should be like the one below,
"(gdb) b mySpace::X::Y"

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