This is the mail archive of the gdb@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

How to call operator<< functions?


Hello,

I have searched the web for an answer without success. The following problem does not seem to exist. Either my configuration is broken in a unique way or my requirements are considered weird and outrageous (so no one tries to do similar things). Read on and tell me what you think.

I have a complex data structure MyClass (e.g. a parse tree) which I want to examine in GDB. It is impractical to traverse and print it node by node from GDB. For that I have written both MyClass::Print(ostream&) and operator<< that invokes this Print. These method and operator can print the data structure in a very nice and readable form. I can use them to print the data into some stream when my application operates in verbose mode.

Now I want to invoke the  operator<<(ostream& , MyClass const &) function,
or alternatively the MyClass::Print(ostream&) const -- from GDB;

This does not work even with gcb-6.5, not to mention older versions. The compiler RedHat's gcc-3.4.

Passing 'std::cout' seems impossible. Any attempt to pass std::cout crashes. To overcome this I define my own global
ostream gecLog(cout.rdbuf()); (or something similar) and recompile the code. This lets me pass gecLog instead of cout, and it _sometimes_ works:


(gdb) p pd.Print(gecLog)
Cannot resolve method (null)Print to any overloaded instance
-----------
Now, going through function pointers helps for some weird reason:

(gdb) p pd.Print
$13 = &MyClass::Print(std::ostream&) const
(gdb) p $13(&pd, gecLog)
Enter Print()
(gdb) p $15(&pd, std::cout)

Program received signal SIGSEGV, Segmentation fault.
0x0042cf88 in std::ostream::sentry::sentry () from /usr/lib/libstdc++.so.6
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 (MyClass::Print(std::ostream&) const) will be abandoned.


--------------------------------------
I wanted to do the same for operator<<:
(gdb) p 'operator<<(std::ostream&, MyClass const&)'
$17 = {ostream &(ostream &, const class MyClass
    &)} 0x8068a00 <operator<<(std::ostream&, MyClass const&)>
(gdb) p $17(gecLog, *pd)
Program received signal SIGSEGV, Segmentation fault.
-----------------------

Now, I wanted to write a GDB macro to call Print for me. When I try to use
 p $-1(&pd, gecLog)
I get a SIGSEGV (when with p $14(....) it would "just" work).
---------------------

Is there any way to overcome any of the above problems? All I want is to pass a stream such as cerr/cout/clog to a printing function, is that unreasonable?

Thanks
  Michael

P.S.
To overcome this, I am writing a method MyClass::DebugPrint() that calls Print(cerr). Sometimes, while debugging a piece of code, that a vital object has <<, but no DebugPrint, and *that* really frustrates me - especially when I loose a 60 minutes debugging session to a SIGSEGV.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]