From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18348 invoked by alias); 30 Aug 2006 10:11:50 -0000 Received: (qmail 18340 invoked by uid 22791); 30 Aug 2006 10:11:49 -0000 X-Spam-Check-By: sourceware.org Received: from mailgw4.technion.ac.il (HELO mailgw4.technion.ac.il) (132.68.238.36) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 30 Aug 2006 10:11:47 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mailgw4.technion.ac.il (Postfix) with ESMTP id 1158D23FB7E for ; Wed, 30 Aug 2006 13:11:47 +0300 (IDT) Received: from mailgw4.technion.ac.il ([127.0.0.1]) by localhost (mailgw4.technion.ac.il [127.0.0.1]) (amavisd-new, port 10024) with LMTP id b1Gal+tTQIdR for ; Wed, 30 Aug 2006 13:11:46 +0300 (IDT) Received: from techunix.technion.ac.il (techunix.technion.ac.il [132.68.1.28]) by mailgw4.technion.ac.il (Postfix) with ESMTP id E695E23F9EF for ; Wed, 30 Aug 2006 13:11:46 +0300 (IDT) Received: from [127.0.0.1] (techunix.technion.ac.il [132.68.1.28]) by techunix.technion.ac.il (Postfix) with ESMTP id 91B6814A64 for ; Wed, 30 Aug 2006 13:11:43 +0300 (IDT) (envelope-from mveksler@tx.technion.ac.il) Message-ID: <44F5645F.4000301@tx.technion.ac.il> Date: Wed, 30 Aug 2006 10:11:00 -0000 From: Michael Veksler User-Agent: Thunderbird 1.5.0.5 (X11/20060726) MIME-Version: 1.0 To: gdb@sourceware.org Subject: How to call operator<< functions? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2006-08/txt/msg00237.txt.bz2 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 (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.