From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id kLaQBRl9WWgt1hoAWB0awg (envelope-from ) for ; Mon, 23 Jun 2025 12:13:13 -0400 Received: by simark.ca (Postfix, from userid 112) id 11A081E11C; Mon, 23 Jun 2025 12:13:13 -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 18ED11E0C2 for ; Mon, 23 Jun 2025 12:13:12 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B68A73846711 for ; Mon, 23 Jun 2025 16:13:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B68A73846711 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 7C8903846E41 for ; Mon, 23 Jun 2025 16:10:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7C8903846E41 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 7C8903846E41 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=1750695051; cv=none; b=tBhvKiA9sXqcWM33XJyCe0R6B6wXOv73v5u5cU1E3Huaoc+nW41I58CvJuO2qTPt1P89aJ70CMKxdFAUcZB4EgW+hoUieR0mCtojlNIAMd6dJ+fZCTelaks1yYNB0DhbMVo6lHDfnaczxOk1ffH2YyIbrUML30eYLK7i7OW32gE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750695051; c=relaxed/simple; bh=0CoenkgSxl169/vSR7FfeclNtLBMHh/IY8fCOhaWQ2o=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=JHr1u4Kg+MR7bjBBv3YTIsIF9vXb5anlC5vj6lWhMqrnA+kEkPAVxmW+LeZLySzF/pCbpST8M7TMAc59SuW9Bg9ITmtDLVLG4VYAtGw2O4Q+IkuTcwik+Rb5lwXBnoaUeNXJPLWlqGINp62Pf5kGOAq1grsflX+BIebyJ2vT6aA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7C8903846E41 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2096.outbound.protection.outlook.com [40.107.244.96]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-517-USvKjHLUMQSQ6KZMTgpalA-1; Mon, 23 Jun 2025 12:10:49 -0400 X-MC-Unique: USvKjHLUMQSQ6KZMTgpalA-1 X-Mimecast-MFC-AGG-ID: USvKjHLUMQSQ6KZMTgpalA_1750695048 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:10:47 +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:10:47 +0000 From: Jan Vrany To: gdb-patches@sourceware.org CC: Jan Vrany , Eli Zaretskii Subject: [RFC v5 05/18] gdb/python: add function () method to gdb.Type object Date: Mon, 23 Jun 2025 17:10:00 +0100 Message-ID: <20250623161013.650814-6-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: 5fcf6e87-4374-43d6-45f4-08ddb27083e1 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?ibHcRpgLTV2IqJaDLHuMMTXPi3GtqCvAIwggyr/sEKnbHKCchre08O9CRpev?= =?us-ascii?Q?voDMvTMsoz7kR87h0x0GymZ2vYJGOZpjbwCJj7agOY4cVXrSpypGeoac2QUj?= =?us-ascii?Q?dsJx37n/DZjaXRRD/7Ce9SziQpW5TSlsw79kP9yjLPsGJ9oaoNJD6PQWJPQG?= =?us-ascii?Q?nC+cCMBTAXkMXI3jdR1TpBKlihGj+NI7PiLlK3ghBQ2fnLhlRgOSBw3giaAz?= =?us-ascii?Q?l/XQVf4u9dstTO5FczbS+V0lXRJftllG7sF0v6WG2nxXJIirrJySWdx0KIE6?= =?us-ascii?Q?UuZnP0BQ7a/dDyPwKOAtOPQAvKTFDsjd+AZkuTh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q?dultJHKcodxRM5dorbqxdgru1AHDvBZ2A6CKUy9tjbx12kp5mV4Tt/zaKLlx?= =?us-ascii?Q?iulGco/OtKzoaUE1nU73vdpRB9373NrO+O9ps5/jlaZIcMEVvwi655ZBAcqr?= =?us-ascii?Q?zwAJGuKdktpRxY1NMgToyZheKNc8/LIe4lPtAQIUgHzFoXkGGM2Q7VDN/A6S?= =?us-ascii?Q?/9Kl60ch/09xW5ykoDejLhL2kKGcnGDHFLwtgpMyhccPiuPq4gyIKwJfU4MI?= =?us-ascii?Q?hR7kqIDNCFk=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?TFkeRkl5a7dCIzHzsZENG49jNOI6G3kLGmTYLzDACrK4rMdNA0LcGnT44wku?= =?us-ascii?Q?QZYsmwsq4v+ZrQ1d3Eq+GtaPKU4iJyYY0jtm5RRX+OajJBaJL4dhU1+YJAC+?= =?us-ascii?Q?gMP94vOVR0VukH8q8DWRvkXYi7gNvR9e44+0Gvvj4dvdl/Pvt+HnqEkrDiHZ?= =?us-ascii?Q?qZBUFQv5Bn/xod5hxNZNA9gq2edkbnzUQoC6B7KCiStYWInCUL47VAjcwzj6?= =?us-ascii?Q?mkn2TJjWtWe8kqNgZvLCahivqd7aMbcqS8bT4yRDOl0r4eA84BWjM3ylWK9u?= =?us-ascii?Q?HC09vCbg51Q8aDEBZaWL57MlXcrZ3bbGWgZAi8BhcrT2XrQLXBjXwhlVGQnE?= =?us-ascii?Q?/QGZ15omtDL9A8LlFt2rajePS7KJzIMMaYtCDwia8Rm256SXRc9RMczj9EF/?= =?us-ascii?Q?X44uflOzWKOVsAG0DPw6jfTAob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ascii?Q?fYip7UR4fwV9C5artYqEpYDxMVYG0I3WcwQLeK/AadEKAvooQGjMQrtVgvC3?= =?us-ascii?Q?A/biIH6IuuxondrF6p+WamBHGB459R8P1MswbHL+Wo85JgyyfQbMNVlcsF1L?= =?us-ascii?Q?rLFbYnlzdM4Ivb8DKWckGpZU6UcMcGY8orxDTaXqjaWrJDzoQltudbgDzroC?= =?us-ascii?Q?epSxpRS2v+4KWt5EvDfBJHfWe5LM6hwCqQycAgig0ALglkMVvN7PjgQwvaQi?= =?us-ascii?Q?F/tB+31nrJcjnh3bBbWO5ALbfiGm2EHm77THHruS?= X-OriginatorOrg: labware.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5fcf6e87-4374-43d6-45f4-08ddb27083e1 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:10:47.6264 (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: MB6ddCTRTa6nt+WtjnV2t9+2bOcfSiV68IsR0qff2uI7EohF0w217bfJ/UXif8KUvEEkSyToSudxwi0ZCjFbnA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR17MB4637 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: B1BpJ4uFM75BYgK0ILPf_bWbAh94rcfodT_DvSgaujs_1750695048 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 a new method to Python type objects that returns possibly new function type returning that type. Parameter types can be specified too. This will be useful later to create types for function symbols created using Python extension code. Unlike previous version, this commit copies all objfile-owned types passed to gdb.Type.function to make them arch-owned. This way we do not have to expose implementation details of type ownership to user. This commit also prompted creation of type::printable_name in order to produce nicer error message when user wants use types from different architectures to create a new function type. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 3 + gdb/doc/python.texi | 11 +++ gdb/gdbtypes.c | 20 ++++ gdb/gdbtypes.h | 6 ++ gdb/python/py-type.c | 132 +++++++++++++++++++++++++++ gdb/testsuite/gdb.python/py-type.exp | 36 ++++++++ 6 files changed, 208 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index 6c8a008d39d..e8e32a5dd5c 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -330,6 +330,9 @@ vFile:stat ** Added gdb.Architecture.void_type. Returns a gdb.Type representing "vo= id" type for that architecture. =20 + ** Added gdb.Type.function. Returns a new gdb.Type representing a funct= ion + returning that type. Parameter types can be specified too. + * Debugger Adapter Protocol changes =20 ** The "scopes" request will now return a scope holding global diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 6fa22851f6a..633526dc875 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -1594,6 +1594,17 @@ Return a new @code{gdb.Type} object which represents= a pointer to this type. @end defun =20 +@defun Type.function (@r{[}param_type@dots{}@r{]}, @r{[}varargs@r{]}) +Return a new @code{gdb.Type} object which represents a type of function +returning this type. Returned function type is always marked as prototype= d. + +@var{param_type@dots{}} positional-only arguments specify parameter types. +Passing @code{void} type is not allowed. + +The optional @var{varargs} keyword-only argument specifies whether to crea= te +vararg function type. Defaults to @code{False}. +@end defun + @defun Type.strip_typedefs () Return a new @code{gdb.Type} that represents the real type, after removing all layers of typedefs. diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index 591a3cc4323..2dc5b7ff417 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -45,6 +45,7 @@ #include "rust-lang.h" #include "ada-lang.h" #include "extract-store-integer.h" +#include "typeprint.h" =20 /* The value of an invalid conversion badness. */ #define INVALID_CONVERSION 100 @@ -5975,6 +5976,25 @@ type::is_array_like () return defn->is_array_like (this); } =20 +/* See gdbtypes.h. */ + +bool +type::is_void () +{ + return check_typedef (this)->code () =3D=3D TYPE_CODE_VOID; +} + +const std::string +type::printable_name () const +{ + string_file thetype; + + current_language->print_type (const_cast(this), "", +=09=09=09=09&thetype, -1, 0, +=09=09=09=09&type_print_raw_options); + + return thetype.release (); +} =0C =20 static const registry::key gdbtypes_data; diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h index ba216c6db54..b736d366fda 100644 --- a/gdb/gdbtypes.h +++ b/gdb/gdbtypes.h @@ -1054,6 +1054,9 @@ struct type this->main_type->name =3D name; } =20 + /* Get the name of this type suitable for the user. */ + const std::string printable_name () const; + /* Note that if thistype is a TYPEDEF type, you have to call check_typed= ef. But check_typedef does set the TYPE_LENGTH of the TYPEDEF type, so you only have to call check_typedef once. Since value::allocate @@ -1526,6 +1529,9 @@ struct type representations of arrays by the type's language. */ bool is_array_like (); =20 + /* Return true if this type is "void". Follows typedefs. */ + bool is_void (); + /* Return the language that this type came from. */ enum language language () const { return main_type->m_lang; } diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index c546aa7536a..3d6db708f8d 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -768,6 +768,135 @@ typy_unqualified (PyObject *self, PyObject *args) return type_to_type_object (type); } =20 +/* Return a function type. */ + +static PyObject * +typy_function (PyObject *self, PyObject *args, PyObject *kw) +{ + struct type *return_type =3D ((type_object *) self)->type; + + /* Process arguments. We cannot simply use PyArg_ParseTupleAndKeywords + because this method take variable number of arguments. */ + if (args =3D=3D nullptr || !PyTuple_Check (args)) + { + PyErr_Format (PyExc_ValueError, +=09=09 _("Arguments is %s, not a tuple."), +=09=09 Py_TYPE (args)->tp_name); + return nullptr; + } + int nparam_types =3D PyTuple_Size (args); + if (nparam_types =3D=3D -1) + { + /* Can this really happen? At this point we _know_ that +=09 ARGS is a tuple (or subclass). */ + PyErr_Format (PyExc_ValueError, +=09=09 _("Failed retrieve number of parameters.")); + return nullptr; + } + std::vector param_types (nparam_types); + for (int i =3D 0; i < nparam_types; i++) + { + PyObject *param_type_obj =3D PySequence_GetItem (args, i); + + if (param_type_obj =3D=3D nullptr) +=09{ +=09 PyErr_Format (PyExc_ValueError, +=09=09=09_("Failed to retrieve parameter at index %d."), i); +=09 return nullptr; +=09} + else +=09{ +=09 param_types[i] =3D type_object_to_type (param_type_obj); +=09 if (param_types[i] =3D=3D nullptr) +=09 { +=09 PyErr_Format (PyExc_TypeError, +=09=09=09 _("Argument at index %d is %s, not a gdb.Type " +=09=09=09 "object."), +=09=09=09 i, Py_TYPE (param_type_obj)->tp_name); +=09 return nullptr; +=09 } +=09 if (param_types[i]->is_void ()) +=09 { +=09=09PyErr_Format (PyExc_ValueError, +=09=09=09 _("Argument at index %d is a void type but " +=09=09=09=09"void as parameter type is not allowed."), +=09=09=09=09 i); +=09=09return nullptr; +=09 } +=09} + } + + if (kw !=3D nullptr) + { + if (!PyDict_Check (kw)) +=09{ +=09 PyErr_Format (PyExc_ValueError, +=09=09=09_("Arguments is %s, not a dict."), +=09=09=09Py_TYPE (args)->tp_name); +=09 return nullptr; +=09} + + PyObject *key, *value; + Py_ssize_t pos =3D 0; + while (PyDict_Next (kw, &pos, &key, &value)) +=09{ +=09 if (!PyUnicode_Check (key)) +=09 { +=09 PyErr_Format (PyExc_ValueError, +=09=09=09 _("Keyword argument key is %s, not a string."), +=09=09=09 Py_TYPE (key)->tp_name); +=09 return nullptr; +=09 } +=09 if (PyUnicode_CompareWithASCIIString (key, "varargs") !=3D 0) +=09 { +=09 PyErr_Format (PyExc_ValueError, +=09=09=09 _("Invalid keyword argument \"%U\"."), +=09=09=09 key); +=09 return nullptr; +=09 } +=09 if (!PyBool_Check (value)) +=09 { +=09 PyErr_Format (PyExc_ValueError, +=09=09=09 _("Value of \"varargs\" argument is \"%s\", " +=09=09=09 "not a bool."), +=09=09=09 Py_TYPE (value)->tp_name); +=09 return nullptr; +=09 } +=09 if (value =3D=3D Py_True) +=09 { +=09 param_types.push_back (nullptr); +=09 } +=09} + } + + /* Copy all objfile-owned types to arch. This way user does not + need to care about mixing types from different objfiles and + architectures and we do not need to expose this implementation + detail to the user. */ + + copied_types_hash_t copied_types; + return_type =3D copy_type_recursive (return_type, copied_types); + for (int i =3D 0; i < param_types.size (); i++) + { + if (param_types[i] !=3D nullptr) +=09param_types[i] =3D copy_type_recursive (param_types[i], copied_types); + } + + struct type *function_type =3D nullptr; + try + { +=09function_type =3D lookup_function_type_with_arguments +=09 (return_type, param_types.size (), param_types.data ()); +=09function_type->set_is_prototyped (true); + } + catch (const gdb_exception &except) + { + return gdbpy_handle_gdb_exception (nullptr, except); + } + + return type_to_type_object (function_type); +} + /* Return the size of the type represented by SELF, in bytes. */ static PyObject * typy_get_sizeof (PyObject *self, void *closure) @@ -1630,6 +1759,9 @@ Return the type of a template argument." }, { "unqualified", typy_unqualified, METH_NOARGS, "unqualified () -> Type\n\ Return a variant of this type without const or volatile attributes." }, + { "function", (PyCFunction) typy_function, METH_VARARGS | METH_KEYWORDS, + "function () -> Type\n\ +Return a function type returning value of this type." }, { "values", typy_values, METH_NOARGS, "values () -> list\n\ Return a list holding all the fields of this type.\n\ diff --git a/gdb/testsuite/gdb.python/py-type.exp b/gdb/testsuite/gdb.pytho= n/py-type.exp index 0bc4556c653..876ef474a32 100644 --- a/gdb/testsuite/gdb.python/py-type.exp +++ b/gdb/testsuite/gdb.python/py-type.exp @@ -378,6 +378,42 @@ if { [build_inferior "${binfile}" "c"] =3D=3D 0 } { gdb_test "python print(gdb.lookup_type('int').optimized_out())" \ "" =20 + gdb_test_no_output "python int_t =3D gdb.lookup_type('int')" + gdb_test_no_output "python uu_t =3D gdb.parse_and_eval ('uu').type" + + gdb_test "python print(repr(int_t.function()))" \ + "" + + gdb_test "python print(repr(int_t.function(int_t, int_t, int_t)))" \ + "" + + gdb_test "python print(repr(int_t.function(int_t, varargs=3DTrue)))" \ + "" + + gdb_test "python print(repr(int_t.function(varargs=3DTrue)))" \ + "" + + gdb_test "python print(repr(int_t.function(varargs=3DFalse)))" \ + "" + + gdb_test "python print(repr(int_t.function(123)))" \ + "TypeError.*:.*" + + gdb_test "python print(repr(int_t.function(gdb.lookup_type('void'))))" \ + "ValueError.*:.*" + + gdb_test "python print(repr(int_t.function(int_t, gdb.lookup_type('void'= ))))" \ + "ValueError.*:.*" + + gdb_test "python print(repr(int_t.function(int_t, varargs=3D\[1,2,3\])))= " \ + "ValueError.*:.*" + + gdb_test "python print(repr(int_t.function(int_t, kw=3DFalse)))" \ + "ValueError.*:.*" + + gdb_test "python print(repr(int_t.function(gdb.lookup_type('void'), vara= rgs=3DTrue)))" \ + "ValueError.*:.*" + set sint [get_sizeof int 0] gdb_test "python print(gdb.parse_and_eval('aligncheck').type.alignof)" \ $sint --=20 2.47.2