1
votes

I have a complicated class for which I have written a clean printing method, more-or-less explicitly for the purposes of debugging. However, I can't seem to figure out the syntax to actually use it to print when I'm using gdb. Basically I want to be able to type something like "myObject->print()" and have it run my print method but instead I get the following error:

Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0x00000000000000a1 0x00007fff814c0684 in std::ostream::sentry::sentry () The program being debugged was signaled while in a function called from GDB. GDB remains in the frame where the signal was received. To change this behavior use "set unwindonsignal on" Evaluation of the expression containing the function (wfSamplePath::print_traj(std::ostream&)) will be abandoned.

where "wfSamplePath" is my class and "print_traj" is my print method (with std::cout as the default argument). So clearly something is wrong with how I think I can do this. I'm using gdb from within xcode 3. "myObject" is definitely in scope, as I can access some of its other methods.

1
Please include your code which is generating the error, as well. - Flimzy

1 Answers

1
votes

The expression evaluator in GDB is quite limited, particularly with C++ expressions, so try to keep it simple. Particularly, do not use default arguments. Using cout is also probably a bad idea. So are inline functions.

I've got good results with a simple member function that returns a string. For example this code works as expected:

#include <sstream>

struct S
{
    int x, y, z;
    std::string debug();
};

std::string S::debug()
{
    std::ostringstream os;
    os << x << ", " << y << ", " << z;
    return os.str();
}

int main()
{
    S s;
    s.x = 1;
    s.y = 2;
    s.z = 3;
    return 0;
}

Then, compile and debug with:

$ g++ -O0 -g test.cpp
$ gdb ./a.out
....
$start
....
19      s.x = 1;
(gdb) n
20      s.y = 2;
(gdb) n
21      s.z = 3;
(gdb) n
22      return 0;
(gdb) p s.debug()
$1 = "1, 2, 3"