From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28994 invoked by alias); 28 Feb 2014 17:14:48 -0000 Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org Received: (qmail 28919 invoked by uid 89); 28 Feb 2014 17:14:46 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: smtp2.ugent.be Received: from smtp2.ugent.be (HELO smtp2.ugent.be) (157.193.49.126) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 28 Feb 2014 17:14:45 +0000 Received: from localhost (mcheck2.ugent.be [157.193.49.249]) by smtp2.ugent.be (Postfix) with ESMTP id 2F43112C725 for ; Fri, 28 Feb 2014 18:14:43 +0100 (CET) Received: from smtp2.ugent.be ([IPv6:::ffff:157.193.49.126]) by localhost (mcheck2.UGent.be [::ffff:157.193.43.11]) (amavisd-new, port 10024) with ESMTP id cSyGEEpSJ2mq for ; Fri, 28 Feb 2014 18:14:37 +0100 (CET) Received: from mail.elis.ugent.be (mail.elis.UGent.be [157.193.206.48]) by smtp2.ugent.be (Postfix) with ESMTP id CB11912C6DA for ; Fri, 28 Feb 2014 18:14:37 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mail.elis.ugent.be (Postfix) with ESMTP id 86E1C918C5F for ; Fri, 28 Feb 2014 18:14:37 +0100 (CET) Received: from mail.elis.ugent.be ([127.0.0.1]) by localhost (mail.elis.ugent.be [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 07K467TFb41q for ; Fri, 28 Feb 2014 18:14:37 +0100 (CET) Received: from [157.193.204.46] (daedalus.elis.UGent.be [157.193.204.46]) by mail.elis.ugent.be (Postfix) with ESMTP id 545B9918C5E for ; Fri, 28 Feb 2014 18:14:37 +0100 (CET) Message-ID: <5310C3FC.6000006@elis.ugent.be> Date: Fri, 28 Feb 2014 17:14:00 -0000 From: Tim Besard User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 MIME-Version: 1.0 To: gdb@sourceware.org Subject: Accessing struct fields without casting Content-Type: multipart/mixed; boundary="------------000403060102070703070209" X-j-chkmail-Enveloppe: 5310C3FD.001 from mail.elis.UGent.be/mail.elis.UGent.be/157.193.206.48/mail.elis.ugent.be/ X-j-chkmail-Score: MSGID : 5310C3FD.001 on smtp2.ugent.be : j-chkmail score : . : R=. U=. O=. B=0.000 -> S=0.000 X-j-chkmail-Status: Ham X-SW-Source: 2014-02/txt/msg00069.txt.bz2 This is a multi-part message in MIME format. --------------000403060102070703070209 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-length: 1640 Hi list, I'm working with some code base which expresses most internally used values in terms of some struct "value_t" only containing a single field, "kind". This field is used as an discriminator, and based on its value the object is cast after which its available fields can be accessed (see attached test case). Since this is pretty hard to work with from within GDB, especially because the "kind" field doesn't contain human readable text but rather raw memory (a pointer to a globally defined object), I wrote a pretty printer which detects the actual type of "value_t" objects, casts them, and displays the actual typename when called with to_string() and returns the casted value its fields when called with children(). However, this doesn't ease debugging much, because despite implementing a children() method which lists all available fields GDB doesn't allow to access those without an explicit cast (see attached sample). > (gdb) print value->foo > There is no member named foo. > (gdb) print ((example_t*)value)->foo > $1 = 42 This is even worse in the actual code, where value_t "subtypes" often contain pointers to other value_t "subtypes", but since those pointers are always of type "value_t*" I need to pretty print them in order to know the type and then cast them in before I can finally access its fields. Is it possible to teach GDB about the "actual" type of these objects, or work around this problem in some other way so that I can access subtype fields without having to cast each object manually? Thanks, -- Tim Besard Computer Systems Lab Department of Electronics & Information Systems Ghent University --------------000403060102070703070209 Content-Type: text/x-csrc; name="test.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="test.c" Content-length: 523 #include #include #define KIND int kind; #define example_kind 1 typedef struct { KIND } value_t; typedef struct { KIND int foo; } example_t; int main() { value_t *value = (value_t *)malloc(sizeof(example_t)); ((example_t *)value)->kind = example_kind; ((example_t *)value)->foo = 42; switch (value->kind) { case example_kind: printf("example_t with foo=%d\n", ((example_t *)value)->foo); break; default: printf("unknown value kind\n"); } return 0; } --------------000403060102070703070209--