Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: Michael Veksler <mveksler@techunix.technion.ac.il>
To: gdb@sourceware.org
Subject: How to call operator<< functions?
Date: Wed, 30 Aug 2006 10:11:00 -0000	[thread overview]
Message-ID: <44F5645F.4000301@tx.technion.ac.il> (raw)

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.


             reply	other threads:[~2006-08-30 10:11 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-30 10:11 Michael Veksler [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=44F5645F.4000301@tx.technion.ac.il \
    --to=mveksler@techunix.technion.ac.il \
    --cc=gdb@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox