From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 4EpxHZ99WWgt1hoAWB0awg (envelope-from ) for ; Mon, 23 Jun 2025 12:15:27 -0400 Received: by simark.ca (Postfix, from userid 112) id 6F67C1E11C; Mon, 23 Jun 2025 12:15:27 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-9.0 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 3BE9E1E0C2 for ; Mon, 23 Jun 2025 12:15:26 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DF8A0384671B for ; Mon, 23 Jun 2025 16:15:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DF8A0384671B Received: from us-smtp-delivery-114.mimecast.com (us-smtp-delivery-114.mimecast.com [170.10.133.114]) by sourceware.org (Postfix) with ESMTP id 2C5893846703 for ; Mon, 23 Jun 2025 16:11:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2C5893846703 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=labware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=labware.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2C5893846703 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.114 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750695074; cv=none; b=kVmAOFrMUN+kkgVeTwOIhweRyy7tulYy5bQD5DJTTwrWvYXPiWOHOwoARG7D/IR0ydfhcVSPJBfm1mrnWF3/MgJcxJFCmYxYKQAyp62fqhkQIJWaihsYIwCTmOHlScxRo2CxwabYnCpwonXF4RRFCe3Y8FORXs1yk/1Au9yndXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750695074; c=relaxed/simple; bh=V2BiZ3emB0Rj/K/JcUzTPSE0sBQD3si1RJV7V9tz7nY=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=MGIbY5j0grTTyb4e6cl6pPMiNZG6hR7tiy+0UqveXjy68F8PBZ6gGwwiS3MomG/5LwrsGTjNd0toxoXTQRJL0MLl2bH4G5Od6O5CzZbGG2pP8NnMue1p/cYMQ/K7waS8W1XCAGBjLPbXMN0hEx9OjelO2I08rR8aa8kPHZvtMzo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C5893846703 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2121.outbound.protection.outlook.com [40.107.243.121]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-48-E2R0OBhGPc2LfRR5iCT0MQ-1; Mon, 23 Jun 2025 12:11:12 -0400 X-MC-Unique: E2R0OBhGPc2LfRR5iCT0MQ-1 X-Mimecast-MFC-AGG-ID: E2R0OBhGPc2LfRR5iCT0MQ_1750695071 Received: from SA1PR17MB5365.namprd17.prod.outlook.com (2603:10b6:806:1d8::11) by PH0PR17MB4637.namprd17.prod.outlook.com (2603:10b6:510:81::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8857.29; Mon, 23 Jun 2025 16:11:10 +0000 Received: from SA1PR17MB5365.namprd17.prod.outlook.com ([fe80::6398:7dc0:7d88:5a4d]) by SA1PR17MB5365.namprd17.prod.outlook.com ([fe80::6398:7dc0:7d88:5a4d%5]) with mapi id 15.20.8857.026; Mon, 23 Jun 2025 16:11:10 +0000 From: Jan Vrany To: gdb-patches@sourceware.org CC: Jan Vrany , Eli Zaretskii Subject: [RFC v5 10/18] gdb/python: allow instantiation of gdb.Compunit from Python Date: Mon, 23 Jun 2025 17:10:05 +0100 Message-ID: <20250623161013.650814-11-jan.vrany@labware.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250623161013.650814-1-jan.vrany@labware.com> References: <20250623161013.650814-1-jan.vrany@labware.com> X-ClientProxiedBy: LO4P123CA0295.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:196::12) To SA1PR17MB5365.namprd17.prod.outlook.com (2603:10b6:806:1d8::11) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA1PR17MB5365:EE_|PH0PR17MB4637:EE_ X-MS-Office365-Filtering-Correlation-Id: 5627f160-f3a3-486a-ed67-08ddb2709123 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|1800799024 X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?3dbEsmGwo8A+/odgewCBXKK47eaG2F6s946Kv4AXnMsZQrQs3A9Ss+Uqh58Y?= =?us-ascii?Q?pFW9fi8bjNDhrdMH6hxTZjCPA67LoPcMDNhPXwcbz4cb48eR1QLzkjNCU8Bq?= =?us-ascii?Q?1DKMfvowSyN+5Y/P72ez3Wh47xsrf6chJqBUthypnJo7/Mp/5ixFKRU7jge+?= =?us-ascii?Q?7dAijCxgNhmAcv58ULi3K9bt1px/aSKVXCjweYkTmV9vmfeywUBdpYH4KuiV?= =?us-ascii?Q?0GB6FpEbLmYext29X0z3ceK6uOU50tbHpU2eZFLmfpJfh4AQHgxR2/8sxYqO?= =?us-ascii?Q?EHj8pAIJPA4gpD4jlZ7N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OxY9E?= =?us-ascii?Q?Yp/qbFoWFTQOKwVdjVDyt7LBdniS0NQV0/RfoQEDqaKg2QrWSUoUbiFQLkC7?= =?us-ascii?Q?P6Qev5D+R2B5KyBUbNWn5mQZXSK8OCzlJlmXCNw3bFWxK6UJq9msatxBS7T6?= =?us-ascii?Q?V0mG0jHG7zM0nXBWDbfqhPxxnU2EbHZ/uUnjQWMnu3Y2zxc49Mo8yyvvUQej?= =?us-ascii?Q?dAXZft8KMU1CeNqAqR7HromC+NJAIuZTtW2G6x8v/BNIpYeTYhQO9tSJb7Vz?= =?us-ascii?Q?43O9eIqyiZw=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SA1PR17MB5365.namprd17.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(376014)(1800799024); DIR:OUT; SFP:1102 X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?IdsI2AL601DenGdtLqliAtnpsHfHnLbEFGrVQayYPJiq2X633Epbz5VC4dVz?= =?us-ascii?Q?XTXmP6RaVLSxUPTD7lqx3jmTaQgk3nubdfYMRKxY0x+vWJTxii9CBQuXtGYx?= =?us-ascii?Q?XYGNGI9XQ8UGXKS+Ls9ivN6sdmSE1YfUnszUvqqn5kY5ZJHCZTdNLNDItqK3?= =?us-ascii?Q?2sS1fdmbK7Exm5SUq849TbSY2Ffe5iZqQknlCJZD43BdXVg7aHiMNazhF+Q+?= =?us-ascii?Q?hU7dT5vqq1wQyzvLBPgRycg06GwzCcizjSbFgnpRgiTDgoNiFRisxgnt7IB0?= =?us-ascii?Q?dwHfNPEIWszFS3U8ynq6HDBTFz1geCeJLZMVWZBLz9RhLySrCbZNXldJF4hZ?= =?us-ascii?Q?M0brSLpXUMc3Y935upu+JITLHDoS2a9DldhiGuRDSEKbsJt7Cughrx/o1gbI?= =?us-ascii?Q?GfR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vzlLTNpbfdYOsMPTXwtjFkJlc5H?= =?us-ascii?Q?pYWn8k5sxT9NBjsy7M08kuBZWkRUK+Ic5EHETgir0bNl5N9XRRBKtUdyHEs6?= =?us-ascii?Q?V48IVsQqff4GbgHt+6/ivmon80pBXzlwYnFeT43D4ltMWHZ+WNvnru6H7RWn?= =?us-ascii?Q?bOZkvzDBS1iwre+kmOCZ6wktEq6KV7Daes3dfpjYUCr9icc5g787qdzbXuep?= =?us-ascii?Q?RY9O+eQLujSqw0bpSg5EEeTfmndi6/87i22LtK5FKspHreRMea+8zDQTEjvD?= =?us-ascii?Q?h3b6X9pkY4D44Ud/aS81ipOpgu9luXJewxxPmdA4?= X-OriginatorOrg: labware.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5627f160-f3a3-486a-ed67-08ddb2709123 X-MS-Exchange-CrossTenant-AuthSource: SA1PR17MB5365.namprd17.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jun 2025 16:11:09.9823 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b5db0322-1aa0-4c0a-859c-ad0f96966f4c X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: +5tYAWqqMesXhla7ztyFKej+U3mjx30bK5Eyll/+EKDVIhLL3Wf4sFXAQvAMfW//5qr42CWcc315ip/BE5gQfg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR17MB4637 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: NODxo3G2LM9TmauATnkq66lr-EtMMp5CwGz6djrJbYw_1750695071 X-Mimecast-Originator: labware.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=WINDOWS-1252 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org This commit adds code to allow user extension to instantiate gdb.Compunit. This is a step towards a Python support for dynamically generated code (JIT) in GDB. Reviewed-By: Eli Zaretskii --- gdb/block.h | 9 ++ gdb/doc/python.texi | 16 ++++ gdb/python/py-compunit.c | 102 ++++++++++++++++++++++- gdb/python/py-objfile.c | 12 +++ gdb/python/python-internal.h | 1 + gdb/symtab.c | 64 ++++++++++++++ gdb/symtab.h | 6 ++ gdb/testsuite/gdb.python/py-compunit.exp | 47 +++++++++++ 8 files changed, 256 insertions(+), 1 deletion(-) diff --git a/gdb/block.h b/gdb/block.h index 89ceeb4268b..3ea8878481e 100644 --- a/gdb/block.h +++ b/gdb/block.h @@ -24,6 +24,7 @@ #include "dictionary.h" #include "gdbsupport/array-view.h" #include "gdbsupport/next-iterator.h" +#include "gdbsupport/range.h" =20 /* Opaque declarations. */ =20 @@ -312,6 +313,14 @@ struct block : public allocate_on_obstack =20 bool contains (const struct block *a, bool allow_nested =3D false) const= ; =20 + /* Return true if this block's range overlap with [L, H) range. Return + false otherwise. */ + + bool overlaps (CORE_ADDR l, CORE_ADDR h) const + { + return ranges_overlap (l, h, start (), end ()); + } + private: =20 /* Return the default entry-pc of this block. The default is the addres= s diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index eac12bec4d3..ffa78c0d859 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -6716,6 +6716,22 @@ The sequence of @code{gdb.Symtab} objects associated= with this compunit. =20 A @code{gdb.Compunit} object has the following methods: =20 +@defun Compunit.__init__ (filename, objfile, start, end @r{[}, capacity @r= {]}) +Create a new compunit with given @var{filename} in given @var{objfile} +(@pxref{Objfiles In Python}). The newly created compunit has an empty glo= bal +block and empty static block (@pxref{Blocks In Python}). + +The @var{start} and @var{end} arguments specifies the start and end addres= s +of compunit's global and static blocks. It must not overlap with any exis= ting +compunit belonging to the same program space +(@pxref{Progspaces In Python}). + +The optional @var{capacity} argument sets the initial capacity of the +internal block vector. More blocks than @var{capacity} can still be added +to the compunit however. If not specified, defaults to 8 blocks (includin= g +global and static blocks). +@end defun + @defun Compunit.is_valid () Returns @code{True} if the @code{gdb.Compunit} object is valid, @code{False} if not. A @code{gdb.Compunit} object can become invalid if diff --git a/gdb/python/py-compunit.c b/gdb/python/py-compunit.c index a2d7620f483..5346663af19 100644 --- a/gdb/python/py-compunit.c +++ b/gdb/python/py-compunit.c @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . = */ =20 +#include #include "charset.h" #include "symtab.h" #include "source.h" @@ -198,6 +199,105 @@ set_compunit (compunit_object *obj, struct compunit_s= ymtab *compunit) Py_INCREF (obj); } =20 +/* Object initializer; creates a new compunit. + + Use: __init__(FILENAME, OBJFILE, START, END [, CAPACITY]). */ + +static int +cupy_init (PyObject *zelf, PyObject *args, PyObject *kw) +{ + struct compunit_object *self =3D (struct compunit_object*) zelf; + + if (self->compunit) + { + PyErr_Format (PyExc_RuntimeError, +=09=09 _("Compunit object already initialized.")); + return -1; + } + + static const char *keywords[] =3D { "filename", "objfile", "start", "end= ", +=09=09=09=09 "capacity", nullptr }; + const char *filename; + PyObject *objf_obj =3D nullptr; + uint64_t start =3D 0; + uint64_t end =3D 0; + uint64_t capacity =3D 8; + + + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "sOKK|K", keywords, +=09=09=09=09=09&filename, &objf_obj, &start, &end, +=09=09=09=09=09&capacity)) + return -1; + + auto objf =3D objfile_object_to_objfile (objf_obj); + if (! objf) + { + PyErr_Format (PyExc_TypeError, +=09=09 _("The objfile argument is not a valid gdb.Objfile " +=09=09 "object")); + return -1; + } + + /* Check that start-end range is valid. */ + if (! (start <=3D end)) + { + PyErr_Format (PyExc_ValueError, +=09=09 _("The start argument must be less or equal to the end " +=09=09 "argument")); + return -1; + + } + + /* Check that to-be created compunit does not overlap any other existing + existing compunit. We have to make sure that all possibly overlappin= g + compunits are fully expanded before, though. */ + + for (struct objfile *of : objf->pspace ()->objfiles_safe ()) + { + if (of->has_unexpanded_symtabs ()) +=09of->expand_symtabs_maybe_overlapping (start, end); + + for (const compunit_symtab *cu : of->compunits ()) +=09{ +=09 if (cu->maybe_overlaps (start, end)) +=09 { +=09 PyErr_Format (PyExc_ValueError, +=09=09 _("The start-end range may overlap with existing " +=09=09 "compunit")); +=09 return -1; +=09 } +=09} + } + + blockvector *bv =3D allocate_blockvector (&objf->objfile_obstack, +=09=09=09=09=09 FIRST_LOCAL_BLOCK, capacity); + compunit_symtab *cu =3D allocate_compunit_symtab (objf, filename); + cu->set_dirname (nullptr); + cu->set_blockvector (bv); + + /* Allocate global block. */ + global_block *gb =3D new (&objf->objfile_obstack) global_block (); + gb->set_multidict (mdict_create_linear_expandable (language_minimal)); + gb->set_start ((CORE_ADDR) start); + gb->set_end ((CORE_ADDR) end); + gb->set_compunit (cu); + bv->set_block (GLOBAL_BLOCK, gb); + + /* Allocate static block. */ + struct block *sb =3D new (&objf->objfile_obstack) block (); + sb->set_multidict (mdict_create_linear_expandable (language_minimal)); + sb->set_start ((CORE_ADDR) start); + sb->set_end ((CORE_ADDR) end); + sb->set_superblock (gb); + bv->set_block (STATIC_BLOCK, sb); + + add_compunit_symtab_to_objfile (cu); + + set_compunit(self, cu); + + return 0; +} + /* Return a new reference to gdb.Compunit Python object representing COMPUNIT. Return NULL and set the Python error on failure. */ PyObject * @@ -305,7 +405,7 @@ PyTypeObject compunit_object_type =3D { 0,=09=09=09=09 /* tp_descr_get */ 0,=09=09=09=09 /* tp_descr_set */ 0,=09=09=09=09 /* tp_dictoffset */ - 0,=09=09=09=09 /* tp_init */ + cupy_init, /* tp_init */ 0,=09=09=09=09 /* tp_alloc */ PyType_GenericNew=09=09 /* tp_new */ }; diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index dc43e2c9e7d..170a440a0dd 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -826,6 +826,18 @@ objfile_to_objfile_object (struct objfile *objfile) return gdbpy_ref<>::new_reference (result); } =20 +/* Returns the struct objfile value corresponding to the given Python + objfile object OBJ. Returns NULL if OBJ is not an objfile object. */ + +struct objfile * +objfile_object_to_objfile (PyObject *obj) +{ + if (! PyObject_TypeCheck (obj, &objfile_object_type)) + return nullptr; + + return ((objfile_object *)obj)->objfile; +} + /* This function remove any dynamic objfiles left over when the inferior exits. */ =20 diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 18b78a7d235..7daa58516f1 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -568,6 +568,7 @@ frame_info_ptr frame_object_to_frame_info (PyObject *fr= ame_obj); struct gdbarch *arch_object_to_gdbarch (PyObject *obj); struct compunit_symtab *compunit_object_to_compunit (PyObject *obj); inferior *inferior_object_to_inferior(PyObject *obj); +struct objfile *objfile_object_to_objfile(PyObject *obj); =20 extern PyObject *gdbpy_execute_mi_command (PyObject *self, PyObject *args, =09=09=09=09=09 PyObject *kw); diff --git a/gdb/symtab.c b/gdb/symtab.c index 506e21eeac4..ba77c89573e 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -75,6 +75,7 @@ #include "gdbsupport/common-utils.h" #include #include "gdbsupport/unordered_set.h" +#include "gdbsupport/range.h" =20 /* Forward declarations for local functions. */ =20 @@ -537,6 +538,69 @@ compunit_symtab::maybe_contains (CORE_ADDR addr) const =20 /* See symtab.h. */ =20 +bool +compunit_symtab::maybe_overlaps (CORE_ADDR start, CORE_ADDR end) const +{ + if (blockvector ()->global_block ()->overlaps (start, end)) + { + const addrmap_fixed *map =3D blockvector ()->map (); + if (map !=3D nullptr) +=09{ +=09 CORE_ADDR range_start =3D 0; +=09 auto fn =3D [&](CORE_ADDR addr, const void* b) -> int +=09 { +=09 if (range_start !=3D 0) +=09 { +=09=09/* We're currently "inside" a range. This transition means +=09=09 that either: + +=09=09 (i) The current range ends (b =3D=3D nullptr). In that case +=09=09=09 check for overlap and if there's an overlap, return 1 +=09=09=09 and finish. + +=09=09 (ii) Or the range continues into another block. In that +=09=09=09 case, just continue. */ + +=09=09if (b =3D=3D nullptr) +=09=09 { +=09=09 CORE_ADDR range_end =3D addr; +=09=09 bool overlaps =3D ranges_overlap (start, end, +=09=09=09=09=09=09 range_start, range_end); +=09=09 range_start =3D 0; +=09=09 return overlaps; +=09=09 } +=09=09else +=09=09 { +=09=09 return 0; /* continue iterating */ +=09=09 } +=09 } +=09 else +=09 { +=09=09/* We're "outside" the range. This transition means that +=09=09 either: + +=09=09 (i) This is a start of a new range (b !=3D nullptr). In +=09=09=09 this case, just note the start address (which also +=09=09=09 indicates we're "inside" a range from now on). +=09=09 (ii) This the very a beggining of address space. In tha +=09=09=09 case do nothing. */ +=09=09if (b !=3D nullptr) +=09=09 { +=09=09 range_start =3D addr; +=09=09 } +=09=09return 0; /* continue iterating */ +=09 } + +=09 }; +=09 return map->foreach (fn); +=09} + return true; + } + return false; +} + +/* See symtab.h. */ + void compunit_symtab::finalize () { diff --git a/gdb/symtab.h b/gdb/symtab.h index 7f084cef691..e55b06048e3 100644 --- a/gdb/symtab.h +++ b/gdb/symtab.h @@ -1967,6 +1967,12 @@ struct compunit_symtab definitely is not. */ bool maybe_contains (CORE_ADDR addr) const; =20 + /* True, if given address range [START, END) may overlap with + addresses covered by this compunit. Return false if given + range definitely does not overlap. */ + + bool maybe_overlaps (CORE_ADDR start, CORE_ADDR end) const; + /* Unordered chain of all compunit symtabs of this objfile. */ struct compunit_symtab *next; =20 diff --git a/gdb/testsuite/gdb.python/py-compunit.exp b/gdb/testsuite/gdb.p= ython/py-compunit.exp index 9e5c4dc6e03..9f8b4cbe453 100644 --- a/gdb/testsuite/gdb.python/py-compunit.exp +++ b/gdb/testsuite/gdb.python/py-compunit.exp @@ -71,6 +71,50 @@ gdb_test "python print (objfile.compunits()\[0\].symtabs= )" \ "Compunit symtabs return a sequence of gdb.Symtab" =20 =20 +# Test creation of compunits +gdb_py_test_silent_cmd "python cu =3D gdb.Compunit(\"compunit1\", objfile,= 200, 300)" \ + "Create compunit1" 1 +gdb_test "python print(cu)" \ + "" \ + "Print created compunit1" +gdb_test "python print(cu in objfile.compunits())" \ + "True" \ + "Test compunit1 is in objfile.compunits()" +gdb_test "python print(cu.global_block().start)" \ + "0" \ + "Test compunit1.global_block().start is 0" +gdb_test "python print(cu.global_block().end)" \ + "0" \ + "Test compunit1.global_block().end is 0" +gdb_test "python print(cu.static_block().start)" \ + "0" \ + "Test compunit1.static_block().start is 0" +gdb_test "python print(cu.static_block().end)" \ + "0" \ + "Test compunit1.static_block().end is 0" + +gdb_py_test_silent_cmd "python cu2 =3D gdb.Compunit(\"dynamic2\", objfile,= 400, 500, 24)" \ + "Create compunit2 with capacity fo 24 blocks" 1 + +gdb_test "python cu3 =3D gdb.Compunit(\"dynamic3\", gdb, 600, 700)" \ + "TypeError.*:.*" \ + "Create compunit3 passing non-objfile" + +gdb_test "python cu4 =3D gdb.Compunit(\"dynamic4\", objfile, 900, 800)" \ + "ValueError.*:.*" \ + "Create compunit4 passing invalid global block range" + +gdb_test "python cu5 =3D gdb.Compunit(\"dynamic5\", objfile, 225, 325)" \ + "ValueError.*:.*" \ + "Create compunit5 passing overlapping global block range" + +gdb_py_test_silent_cmd "python cu6 =3D gdb.Compunit(\"dynamic6\", objfile,= 0x00EFFF00, 0x00EFFF0A)" \ + "Create compunit6 'inside a hole' of existing compunit spanning over m= ultiple disjoint ranges" 1 + +gdb_test "python cu7 =3D gdb.Compunit(\"dynamic7\", objfile, 0x00F00000, 0= x00F0000F)" \ + "ValueError.*:.*" \ + "Create compunit7 overlapping one of the existing compunit ranges" + gdb_unload "unload 1" =20 gdb_test "python print (objfile.is_valid())" "False" \ @@ -81,3 +125,6 @@ gdb_py_test_silent_cmd "python compunit =3D None" \ "Test compunit deallocation" 1 gdb_test "python print (objfile.compunits())" "RuntimeError.*: Objfile no = longer exists.*" \ "Get objfile compunits after unload" +gdb_test "python cu8 =3D gdb.Compunit(\"dynamic8\", objfile, 1000, 1100)" = \ + "TypeError.*: The objfile argument is not a valid gdb.Objfile object.*= " \ + "Create compunit8 passing invalid objfile" --=20 2.47.2