From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31034 invoked by alias); 10 Jun 2009 20:10:54 -0000 Received: (qmail 31026 invoked by uid 22791); 10 Jun 2009 20:10:53 -0000 X-SWARE-Spam-Status: No, hits=-2.1 required=5.0 tests=AWL,BAYES_00,J_CHICKENPOX_33 X-Spam-Check-By: sourceware.org Received: from fmmailgate03.web.de (HELO fmmailgate03.web.de) (217.72.192.234) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 10 Jun 2009 20:10:47 +0000 Received: from smtp08.web.de (fmsmtp08.dlan.cinetic.de [172.20.5.216]) by fmmailgate03.web.de (Postfix) with ESMTP id 31693FF548C1; Wed, 10 Jun 2009 22:10:44 +0200 (CEST) Received: from [88.64.26.191] (helo=[192.168.1.10]) by smtp08.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #277) id 1MEU8B-0006m1-00; Wed, 10 Jun 2009 22:10:43 +0200 Message-ID: <4A30133F.9000909@web.de> Date: Wed, 10 Jun 2009 20:10:00 -0000 From: Jan Kiszka User-Agent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); de; rv:1.8.1.12) Gecko/20080226 SUSE/2.0.0.12-1.1 Thunderbird/2.0.0.12 Mnenhy/0.7.5.666 MIME-Version: 1.0 To: tromey@redhat.com CC: gdb@sourceware.org Subject: Re: container_of equivalent in gdb-python script References: <4A2FE5AB.4060707@siemens.com> In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig4D8BF75A9B2CC4F735C47A2C" X-Sender: jan.kiszka@web.de 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 X-SW-Source: 2009-06/txt/msg00094.txt.bz2 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig4D8BF75A9B2CC4F735C47A2C Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-length: 2082 Tom Tromey wrote: >>>>>> "Jan" =3D=3D Jan Kiszka writes: >=20 > Jan> I'm at the point where I would apply a standard container_of() > Jan> macro in C: convert the list entry into a gdb.Value that > Jan> describes the containing object. >=20 > Jan> Of course, I can implement container_of as a gdb expression, print t= he > Jan> result and drag it in via gdb.history(). Done that already, basically > Jan> works. But it is fairly ugly as the print output will flood the scre= en. >=20 > I would say that the usual approach would be to reimplement the macro > in Python. The Python Value API is reasonably robust and can usually > be used for this. If you post the macro definition maybe I could help > with that. More details wouldn't hurt, either... are you writing a > pretty-printer? A new command? A convenience function? I want to automate 'add-symbol-file linux_module.ko 0xff...' that you have to run for loading the symbols of dynamically loaded kernel modules. Before that you also have to look up the module base address, typically by cat'ing /proc/modules on the target. With a proper python script, this will be trivial to do automatically. You just have to walk the module list of the kernel you are attached to, extract names and base addresses, search for the corresponding module binaries (also easy with python) and issue the proper add-symbol-file commands. But now back to the core problems, starting with the exercise to implement offset_of(type, field): def offset_of(type, field): container_type =3D gdb.lookup_type(type) dummy_obj =3D gdb.selected_frame().read_var('modules') container_obj =3D dummy_obj.cast(container_type) field_obj =3D container_obj[field] return int(str(field_obj.address), 16) - \ int(str(container_obj.address), 16) I meanwhile discovered (reading testcases and python-*.c) gdb.lookup_type() and Value.cast() to make this real. But you see, I still need an ugly synthetic Value object which must have a non-'None' address to do this calculation. Is there a cleaner, more generic way? Jan --------------enig4D8BF75A9B2CC4F735C47A2C Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" Content-length: 257 -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iEYEARECAAYFAkowE0MACgkQniDOoMHTA+kGjQCeNk/+HRb+MLUBo/mdk+OItK9C CPkAnA4zGtY8QXL63QO7rna0R82bvLJX =DK7l -----END PGP SIGNATURE----- --------------enig4D8BF75A9B2CC4F735C47A2C--