Search code examples
c++gdbbreakpointsmember-functions

C++ GDB breakpoint for member functions


I am having trouble with using GDB on my c++ program. I want to set up a break point for my class member function and I'm not sure on the syntax of how to do it. My program is working find right now and I'm just trying to learn to use GDB. My problem is all the information I find on line only really deals with just a main() file and no other functions or classes and if they involve classes its only using a function with a void return statement.

I have a binary search tree class. I want to set a break point at a function in my program. here's the section of my header file.

class BST
{
    BST()
    ...
    private:
    int add((BST * root, BST *src);
}

I am telneting into a command line linux server for school. I can get GDB running with my program just fine with g++ -g *.cpp (there are other files that are working fine) and the file is saved as a.out. I run GDB with

gdb ./a.out

and I get into GDB. I can get a break point for the void display function just fine with

b BST::disp_block()

but how do I do it with the add function I have tried

b BST::int add(BST*, BST *)
b int BST::add(BST*, BST *)
b BST::add(BST*, BST *)

and I even tried with the argument names

b BST::int add(BST * root, BST * src)
b int BST::add(BST * root, BST * src)
b BST::add(BST * root, BST * src)

and I keep getting the error

Function "____" not defined.
Make break point pending on future shared library load? (y or [n])

How do I set up a break point for a member function like this one? Im assuming watch points would be the same format, if not could you explain that too.


Solution

  • As Dark Falcon said, break BST::add should work if you don't have overloads.

    You can also type:

    (gdb) break 'BST::add(<TAB>
    

    (note the single quote). This should prompt GDB to perform tab-completion, and finish the line like so:

    (gdb) break 'BST::add(BST*, BST*)
    

    and which point you can add the terminating '' and hit Enter to add the breakpoint.

    I can get a break point for the void display function

    Function return type is not part of its signature and has nothing to do with what's happening.