Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* How to call operator<< functions?
@ 2006-08-30 10:11 Michael Veksler
  2006-08-30 11:13 ` Frederic RISS
  2006-08-30 12:46 ` How to call operator<< functions? Daniel Jacobowitz
  0 siblings, 2 replies; 23+ messages in thread
From: Michael Veksler @ 2006-08-30 10:11 UTC (permalink / raw)
  To: gdb

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.


^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2006-08-31 19:52 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-08-30 10:11 How to call operator<< functions? Michael Veksler
2006-08-30 11:13 ` Frederic RISS
2006-08-30 13:30   ` Frederic RISS
2006-08-30 13:40     ` Breakpoint Handling in GDB Veenu Verma (AS/EAB)
2006-08-30 13:43       ` Daniel Jacobowitz
2006-08-30 20:30       ` Michael Snyder
2006-08-31 11:34     ` Get versioned minsyms from dynamic symtab (Was: Re: How to call operator<< functions?) Frederic RISS
2006-08-31 12:09       ` Michael Veksler
2006-08-31 12:26         ` Frederic RISS
2006-08-31 13:02           ` Michael Veksler
2006-08-31 13:23             ` Frederic RISS
2006-08-31 16:48               ` Frederic RISS
2006-08-31 16:57                 ` Daniel Jacobowitz
2006-08-31 17:41                   ` Frédéric Riss
2006-08-31 17:45                     ` Daniel Jacobowitz
2006-08-31 19:48                   ` Michael Veksler
2006-08-31 19:52                     ` Daniel Jacobowitz
2006-08-30 12:46 ` How to call operator<< functions? Daniel Jacobowitz
2006-08-30 20:05   ` Michael Veksler
2006-08-30 20:24     ` Daniel Jacobowitz
2006-08-30 20:45       ` Michael Veksler
2006-08-30 20:54         ` Daniel Jacobowitz
2006-08-31 12:05           ` Michael Veksler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox