开发者

Setting breakpoint on a class's member function in a file

(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?


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.


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


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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜