From patchwork Thu Nov 21 12:47:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Vrany X-Patchwork-Id: 101654 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C4746385701A for ; Thu, 21 Nov 2024 12:52:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C4746385701A X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-114.mimecast.com (us-smtp-delivery-114.mimecast.com [170.10.129.114]) by sourceware.org (Postfix) with ESMTP id B5DFC3857BB6 for ; Thu, 21 Nov 2024 12:48:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B5DFC3857BB6 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 B5DFC3857BB6 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.114 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732193300; cv=none; b=XpitW/qIpDGgLA2R+A3zyAMVEbt9ObKCBahvYfmsOugytWAtb4/p96oeI+vClTRmrp/fT2TKrKjZP7N3lZGRFJxYhWkRQN5pEgGsQMjMhy6ohn3gwcIpWmIV2DEg2tO4mBS8+0l64K/JRQJSyJTpoCpbHf5/XDFpm/zTZUGSV+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732193300; c=relaxed/simple; bh=dtzHfeIZU//MaZJUI3Wmsy73gLb1WB3TIn2UQ8yvBgk=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=iCH4YLXn7CHTVqFJZ82oS3bR030EOY0mAhE3znHO/1BJkvSfkHLBsmBIxRoP0WL2AeExAKxJGgjzvtjLBLjNM3vcyjV2qHvwpzN+0M3gKmj0XrE+Idy7w9mRjPBabV2PF9H77hHsIRDsiCn4HsJPHPuf64mb4k35qvYmmgszP8M= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B5DFC3857BB6 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2176.outbound.protection.outlook.com [104.47.59.176]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-255-K7xh7gTeMcOriJWmUQK__A-1; Thu, 21 Nov 2024 07:48:19 -0500 X-MC-Unique: K7xh7gTeMcOriJWmUQK__A-1 X-Mimecast-MFC-AGG-ID: K7xh7gTeMcOriJWmUQK__A Received: from SA0PR17MB4314.namprd17.prod.outlook.com (2603:10b6:806:e7::16) by SA1PR17MB6504.namprd17.prod.outlook.com (2603:10b6:806:337::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.22; Thu, 21 Nov 2024 12:48:17 +0000 Received: from SA0PR17MB4314.namprd17.prod.outlook.com ([fe80::38a7:a6f2:3b95:bc26]) by SA0PR17MB4314.namprd17.prod.outlook.com ([fe80::38a7:a6f2:3b95:bc26%5]) with mapi id 15.20.8158.023; Thu, 21 Nov 2024 12:48:17 +0000 From: Jan Vrany To: gdb-patches@sourceware.org CC: Jan Vrany , Eli Zaretskii Subject: [RFC v2 13/21] gdb/python: allow instantiation of gdb.Compunit from Python Date: Thu, 21 Nov 2024 12:47:06 +0000 Message-ID: <20241121124714.419946-14-jan.vrany@labware.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241121124714.419946-1-jan.vrany@labware.com> References: <20241121124714.419946-1-jan.vrany@labware.com> X-ClientProxiedBy: LO4P265CA0297.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:38f::6) To SA0PR17MB4314.namprd17.prod.outlook.com (2603:10b6:806:e7::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA0PR17MB4314:EE_|SA1PR17MB6504:EE_ X-MS-Office365-Filtering-Correlation-Id: ed285c87-7967-46dc-f7b6-08dd0a2ac569 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014 X-Microsoft-Antispam-Message-Info: cxYOfNV6hCVSW7Fiy2W8nZnEJyg7kPXYP8f/2086weD+8fYo0aYvUdemQ+jassBPpCvcPv8ZCsI41lbQY4UYDphivt1AHxXdAsGiHUP/aMNQFBayTlF0BOxjuxYTNs02+c0EmJGMr2I/Wt/oXWBP9SHCpCmCrxoYrltcqK1Ts4Ba6HzCSd/gYQ1uxnemj5kwzZPDYfyDzo0mxkfqSd9uN+qzrzYhBicGw/U/Vwqu1CJLiu4PVRd1TIMzc69Jm2jXoIF5HwYP98IHcfGE5qDeyMWeMIyJsYIiANSoIPflAHAtJXt7fe7VTPKH+kT2uq6X6WjvWjMCeezJ85D/dt01sXZAOwFpB5dmXspTN8gyGrD0CukKcRyG+z1EFdFUXtfQfXByKg2rZAJq6G+Qg7bryfuttg1UhglmUFkGEBUPvQMGlaEylm6HpZqGghcW5D2uvlRY2GshYFGAZH4m0gIp+uqVra9W1nFGBJUlxDAmduHp/iyn3EAuZHmnWtpUeQU1fMKCvLz0KhdSy0pGco9UNg/RAgLi99Gzt37eCWlvClZnAk0/4H37d1AdbKYF8wuA0NEC4/mVid6m1ZQIn5dtI6O7/eYvvgOE//seBB64GlBppzDFqzsu0sN9TdxMmVx9gEXpxHknTrN/FNAkhMlHjIOeXy2oI8cXiJJ8S9dAIoTSpXt4xYCU3IJlCuy0sDjbkWfsZytT/a3LWzRLy13WI7OdU/MKTcRyHj+nmx6/3fPSez04iWect+1Ia1Isa7QMJGLAM/DwBk9/zu33yO3Qf5/srOVhluyWdY9LCe5X2gPSKR40L3iJQv8WvMtLN1Cdj/ghU1APgPtww3byHekJFfaZlvJdyJBb2JNUYl+RcU9i8bihVqAZgozyhHS5GVlar2BiLXj1wcKyjMoAC3+wtQwo+Bs4uMAixbWqEZDLR85fVqmHWDEkCR5m6BeN5bgnVFi86KHsDfGyRRS+L1rWeOD6aEg5nnwj7zmLkHrSgonF8RiOA3dwEAQrxsE45QtW09xT3bC1fPY9tERWTtAmdZmmlxZ0Gyzb8iEvMGJnXORWkfsANudiD3k/hsqN8ADFVdEbNsjAxtyejFEN+Pmzi0ixDuUZH8RxXB8xiJW5r311TUHEMbo1lxW9aRb2jsZpqIxxz6CiNiUP9gCyPb+YqOX1fFXbd/UzIeoXN6BY6C4tq0qwrTX5MDwt51j/yW2u6nSXFzpevnYxtWfOC26MZg/56y3B+WKhleYHgd4bmbwVpqWGjwuVULoz8WV6YnVJ X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SA0PR17MB4314.namprd17.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(366016)(376014); DIR:OUT; SFP:1102 X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: rPhk5OEEY/K1+iw0Zpzh6SpPO3lMBVb/D7pccLRsUCyi+n8JR+aUoop2okqAQS8ZHxHw2OGoIcbLdFr9YSWtIBX7Rs61ODfgagHdG8iATUsob5uuCNolADXl1hPv9o5eeAQk+AybAaX+k8O+Gp6DZ+bbv68JP0to7pw5QokFjRX5VR1kIjT22MSux5y8XIlMWGVS3CvuEP04fVf9NayZdO7UV3rk2A0JQy+b02Dung1feHt5zuDxaAA9gpoYiIxJD6aeD/pmh233iPZp1sBbX9QIi1vTdWNniqDRTfsgsmxHFW1UBf9EhEOkkeU8VfSMihugS/ddhR5+nuzai1kvhggLk/7TXSb563oZi/mHs3GzkMYhUATazbcYgVeBQ1fNSau+U+4itXX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63gOzjkY070O4x1u032LTT9EI29wcUsUqZy8461pab9f+m/aH+AZ8hQG12tyAOWxjbn3Y+KBrBEjkhpQt+36lQn7AEEIBzn/3pFSI9WCiSBIF4Wo5Epe5nteLHJLPU/OZWM4Bye X-OriginatorOrg: labware.com X-MS-Exchange-CrossTenant-Network-Message-Id: ed285c87-7967-46dc-f7b6-08dd0a2ac569 X-MS-Exchange-CrossTenant-AuthSource: SA0PR17MB4314.namprd17.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Nov 2024 12:48:17.5486 (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: ZgKvkfHEjcROOLKLGD6JgLYy50tkgvXxVNIOwiWsy14HyV6A7mOSKpNe120IoVVvlIHs4z/wVBNe2O6pfOqPJg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR17MB6504 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Sqi4cVd8yqaRpZK3EQbxk9VTK-i5TPD9LHKGNX5Id6I_1732193298 X-Mimecast-Originator: labware.com X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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~patchwork=sourceware.org@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 Reviewed-By: Eli Zaretskii --- gdb/doc/python.texi | 18 ++++- gdb/python/py-compunit.c | 97 +++++++++++++++++++++++- gdb/python/py-objfile.c | 12 +++ gdb/python/python-internal.h | 1 + gdb/testsuite/gdb.python/py-compunit.exp | 42 +++++++++- 5 files changed, 167 insertions(+), 3 deletions(-) diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index f48803d08b4..115d5f04165 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -6551,7 +6551,7 @@ that objfile. @xref{Objfiles In Python}. A @code{gdb.Compunit} object has the following attributes: @defvar Compunit.objfile -The compunits' backing object file. @xref{Objfiles In Python}. +The compunit's backing object file. @xref{Objfiles In Python}. This attribute is not writable. @end defvar @@ -6570,6 +6570,22 @@ The list of symbol tables associated with this compunit. A @code{gdb.Compunit} object has the following methods: +@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 global +block and empty static block (@pxref{Blocks In Python}). + +The @var{start} and @var{end} arguments specifies the start and end address +of compunit's global and static blocks. It must not overlap with any existing +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 (including +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 829746cc92d..c7e89643abc 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 . */ +#include #include "charset.h" #include "symtab.h" #include "source.h" @@ -217,6 +218,100 @@ set_compunit (compunit_object *obj, struct compunit_symtab *compunit) obj->next = nullptr; } +/* 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 = (struct compunit_object*) zelf; + + if (self->compunit) + { + PyErr_Format (PyExc_RuntimeError, + _("Compunit object already initialized.")); + return -1; + } + + static const char *keywords[] = { "filename", "objfile", "start", "end", + "capacity", nullptr }; + const char *filename; + PyObject *objf_obj = nullptr; + uint64_t start = 0; + uint64_t end = 0; + uint64_t capacity = 8; + + + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "sOKK|K", keywords, + &filename, &objf_obj, &start, &end, + &capacity)) + return -1; + + auto objf = objfile_object_to_objfile (objf_obj); + if (! objf) + { + PyErr_Format (PyExc_TypeError, + _("The objfile argument is not valid gdb.Objfile object")); + return -1; + } + + /* Check that start-end range is valid. */ + if (! (start <= end)) + { + PyErr_Format (PyExc_ValueError, + _("The start argument must be less or equal to the end " + "argument")); + return -1; + + } + + /* Check that to-be created compunits' global block does not overlap + with existing compunit. */ + + for (struct objfile *of : objf->pspace ()->objfiles_safe ()) + { + for (compunit_symtab *cu : of->compunits ()) + { + global_block *gb = cu->blockvector ()->global_block (); + if (std::max (start, gb->start ()) < std::min(end, gb->end ())) + { + PyErr_Format (PyExc_ValueError, + _("The start-end range overlaps with existing compunit")); + return -1; + } + } + } + + blockvector *bv = allocate_blockvector (&objf->objfile_obstack, + FIRST_LOCAL_BLOCK, capacity); + compunit_symtab *cu = allocate_compunit_symtab (objf, filename); + cu->set_dirname (nullptr); + cu->set_blockvector (bv); + + /* Allocate global block. */ + global_block *gb = 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 = 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. */ gdbpy_ref<> @@ -313,7 +408,7 @@ PyTypeObject compunit_object_type = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - 0, /* tp_init */ + cupy_init, /* tp_init */ 0, /* tp_alloc */ PyType_GenericNew /* tp_new */ }; diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index f8b29ae1f47..21f46260e74 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -824,6 +824,18 @@ objfile_to_objfile_object (struct objfile *objfile) return gdbpy_ref<>::new_reference (result); } +/* 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. */ diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index a5bcfedf79b..88432e04f47 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -575,6 +575,7 @@ frame_info_ptr frame_object_to_frame_info (PyObject *frame_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); extern PyObject *gdbpy_execute_mi_command (PyObject *self, PyObject *args, PyObject *kw); diff --git a/gdb/testsuite/gdb.python/py-compunit.exp b/gdb/testsuite/gdb.python/py-compunit.exp index 450cd6c9f1b..cf9197988d7 100644 --- a/gdb/testsuite/gdb.python/py-compunit.exp +++ b/gdb/testsuite/gdb.python/py-compunit.exp @@ -54,9 +54,49 @@ gdb_test "python print (objfile.compunits()\[0\].symtabs)" \ "Compunit symtabs return a sequence of gdb.Symtab" +# Test creation of compunits +gdb_py_test_silent_cmd "python cu = 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 = gdb.Compunit(\"dynamic2\", objfile, 400, 500, 24)" \ + "Create compunit2 with capacity fo 24 blocks" 1 + +gdb_test "python cu3 = gdb.Compunit(\"dynamic3\", gdb, 600, 700)" \ + "TypeError.*:.*" \ + "Create compunit3 passing non-objfile" + +gdb_test "python cu4 = gdb.Compunit(\"dynamic4\", objfile, 900, 800)" \ + "ValueError.*:.*" \ + "Create compunit4 passing invalid global block range" + +gdb_test "python cu5 = gdb.Compunit(\"dynamic5\", objfile, 225, 325)" \ + "ValueError.*:.*" \ + "Create compunit4 passing overlapping global block range" + + gdb_unload "unload 1" gdb_test "python print (objfile.is_valid())" "False" \ "Get objfile validity after unload" gdb_test "python print (objfile.compunits())" "RuntimeError.*: Objfile no longer exists.*" \ -"Get objfile compunits after unload" \ No newline at end of file +"Get objfile compunits after unload" +gdb_py_test_silent_cmd "python cu6 = gdb.Compunit(\"dynamic6\", objfile)" \ + "Create compunit4 passing invalid objfile" 1 \ No newline at end of file