From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 124775 invoked by alias); 15 Apr 2016 03:12:42 -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 124761 invoked by uid 89); 15 Apr 2016 03:12:41 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qg0-f41.google.com Received: from mail-qg0-f41.google.com (HELO mail-qg0-f41.google.com) (209.85.192.41) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Fri, 15 Apr 2016 03:12:39 +0000 Received: by mail-qg0-f41.google.com with SMTP id f52so74694265qga.3 for ; Thu, 14 Apr 2016 20:12:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=NHNUGn0gJIxxpmC+gZkybzU5rsyd4RWj0j1Af5Q3IEQ=; b=K7J5uZoEsi+DoLnoi9nyBlOg7Sak9Wgo75YLfWcsRvrbZ0MxDmF/XW8Tal9LXYnsyw HNYSB+n5+Slzv6/Fkd7W25kKEul2k0Sw/gM1+pnBNpLg9kOUwzQ7cLATpO6V98zmIS7V +sMVLFlafMGd4z5H+IrPwchK8EsWDW/JoJXuuWTnyWhYXyCZ3QXE4ozk7zZEytj67fcJ NqpU6tL+kiaLpogH57DYAPZ1L1lUv1FokeHIY4Q3rt3o0XJOUr6iAJrrpIPgNlHuuo16 qFqCrc+RQV+8nNr7vyCHTO9FP5g6+ro6Qtyuf/TuAZ1VKHIh0nFKWXpT7Sg8dGBnov95 5f0Q== X-Gm-Message-State: AOPr4FXN71q3UsOOM8Oee20O8hFb0sLn72SSLGrscKcxn5zb3B4PETlj/3G7+zZRl+lyRg== X-Received: by 10.140.201.143 with SMTP id w137mr23691019qha.27.1460689957908; Thu, 14 Apr 2016 20:12:37 -0700 (PDT) Received: from [192.168.1.143] (cpe-66-108-29-119.nyc.res.rr.com. [66.108.29.119]) by smtp.googlemail.com with ESMTPSA id e62sm19469962qge.44.2016.04.14.20.12.37 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 14 Apr 2016 20:12:37 -0700 (PDT) Subject: Re: Casting an object to another type while debugging? To: Yichao Yu References: <571059D1.3010308@gmail.com> Cc: gdb@sourceware.org From: Thomas Nyberg Message-ID: <57105C27.8090705@gmail.com> Date: Fri, 15 Apr 2016 03:12:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2016-04/txt/msg00021.txt.bz2 Thank you so much! Should have been obvious when reading (PyObject *)... Here it is in action: ``` (gdb) print ((PyUnicodeObject *) name) $15 = (PyUnicodeObject *) 0x7ffff7ee3298 (gdb) print *((PyUnicodeObject *) name) $16 = {_base = {_base = {ob_base = {ob_refcnt = 2, ob_type = 0x8a1a00 }, length = 4, hash = -1762387814536268224, state = {interned = 1, kind = 1, compact = 1, ascii = 1, ready = 1}, wstr = 0x0}, utf8_length = 92360930452852, utf8 = 0x73 , wstr_length = 9050624}, data = {any = 0x2, latin1 = 0x2 , ucs2 = 0x2, ucs4 = 0x2}} ``` On 04/14/2016 11:09 PM, Yichao Yu wrote: > On Thu, Apr 14, 2016 at 11:02 PM, Thomas Nyberg wrote: >> Hello, >> >> Please yell and scream if I'm posting this to the wrong list (and then >> point me in the right direction :) ). >> >> To make my situation specific, I'm stepping through the cpython >> implementation of the python interpreter using gdb. The implementation >> tends to pass around all objects as type (PyObject *). I know that a >> certain object can be cast to (PyUnicode_Type *) and so I've tried to do >> this to print out the structure, but without success. Here are some >> examples: >> >> ``` >> (gdb) print name >> $10 = (PyObject *) 0x7ffff7ee3298 >> (gdb) print *name >> $11 = {ob_refcnt = 2, ob_type = 0x8a1a00 } >> (gdb) print name >> $12 = (PyObject *) 0x7ffff7ee3298 >> (gdb) print *name >> $13 = {ob_refcnt = 2, ob_type = 0x8a1a00 } >> (gdb) print (void *) name >> $14 = (void *) 0x7ffff7ee3298 >> (gdb) print * (void *) name >> Attempt to dereference a generic pointer. >> (gdb) print * (PyUnicode_Type *) name >> A syntax error in expression, near `) name'. >> (gdb) print * (PyUnicode_Typ *) name >> No symbol "PyUnicode_Typ" in current context. >> (gdb) print (PyUnicode_Type *) name >> A syntax error in expression, near `) name'. >> (gdb) print ((PyUnicode_Type *) name) >> A syntax error in expression, near `) name)'. >> (gdb) print ((PyUnicode_Type *) name); >> A syntax error in expression, near `) name);'. > > PyUnicode_Type is not the name of the C type it's a variable/address > that's holding the python type/vtable. I believe PyUnicodObject is the > C type name. > > >> ``` >> >> So from this it seems to me like it's recognizing the PyUnicode_Type >> fine, but I just get this weird syntax error. My google-foo hasn't >> worked out that well so far in resolving this. What am I doing wrong? Am >> I misunderstanding how casting works? >> >> Thanks for any help! >> >> Cheers, >> Thomas