From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x12f.google.com (mail-il1-x12f.google.com [IPv6:2607:f8b0:4864:20::12f]) by sourceware.org (Postfix) with ESMTPS id DE290385C6ED for ; Tue, 22 Aug 2023 15:25:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DE290385C6ED Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-il1-x12f.google.com with SMTP id e9e14a558f8ab-34ca07e6d6cso9059455ab.0 for ; Tue, 22 Aug 2023 08:25:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1692717916; x=1693322716; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=buma4xj0J1U3SuRspx58IxDZATupmTEJqnzjFVGkz7k=; b=SOXW/LT+1lPAuXULet13ZHqqYB26TQSuYpkYwM2DODjZT/HmgKiXX1hzt7Kny2IGXj HOSlOn/BaHe2kc8UPPfCMrMC4keTCEMZ6FkAZ3kIqWvO1v5paZ1ePOcXyBDw1M8BWgiu 0lCd/6haL+J/iGjK55yIJPgkQgDBilZ89xQcX2f1qfmzXY3XjBEi1589Qr/vs1p9DG9N UXSAkxy0lu4iHr5nvcJ/0ft5VA8TvpaWqq9/Iz2nDjpX0nVYkQOwnI/akFcNkioO2782 GbbenLHL9JjzNfjet6a/cJ4/nLQURU+0Lwj3iZ25x14A8ywpf+aC31rJYrFvd6A9tEPR KiCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692717916; x=1693322716; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=buma4xj0J1U3SuRspx58IxDZATupmTEJqnzjFVGkz7k=; b=dclYmZkKfZa5G5gUuo8B5bI48nlBK3mGUWYo6NGKd43NRV2vSZrDzK9N0ISXwH5oHq rE4cpL5P1UpGkj3qNvFMzj56rZvREOz/2DGYmZHG4ytHkB9S/UGTQ6c1bX/IlLjwA0QH d1Ooo7hxhrMt1PqDDL7HP/RRn3EMnVcGFBvWwN3VtE9Nsm6K2xuQLtQXT00uCRUX0Qn2 vm32Ok/mSOZKj58hut5asCxAr2fj8WJcprf8oChuLiFsZV+cIVRM/WsNIps3ItX0U+MV AbVyufvR/yLUb8D5IGrPhvITTxg7ICnBKvXWtlIVyk71RDBf+eqldrfIJaXI05TjBvtv l0iA== X-Gm-Message-State: AOJu0YyfniW0CxpV1DXQgFazQqqsTJTg/KBYUGBZtznAs/gDVdpOHXmU BoBOOBXue9TNnBjGBph1nEz+EjznbuiAGPj1Wrm8KA== X-Google-Smtp-Source: AGHT+IEpjBCMGNYnQcxqOaO5bk3prF6xbBQ9R+9EinPrcuCHa2QYF98HZVaZEnzVQ+43bzLpxyg5CQ== X-Received: by 2002:a05:6e02:1e08:b0:345:6ffa:63c7 with SMTP id g8-20020a056e021e0800b003456ffa63c7mr13777938ila.32.1692717915942; Tue, 22 Aug 2023 08:25:15 -0700 (PDT) Received: from localhost.localdomain (75-166-142-177.hlrn.qwest.net. [75.166.142.177]) by smtp.gmail.com with ESMTPSA id e8-20020a92de48000000b003460b8505easm1721608ilr.19.2023.08.22.08.25.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 08:25:15 -0700 (PDT) From: Tom Tromey Date: Tue, 22 Aug 2023 09:25:13 -0600 Subject: [PATCH 7/8] Add new Python APIs to support DAP value display MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230822-array-and-string-like-v1-7-2dcea29b0567@adacore.com> References: <20230822-array-and-string-like-v1-0-2dcea29b0567@adacore.com> In-Reply-To: <20230822-array-and-string-like-v1-0-2dcea29b0567@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,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 List-Id: gdb's language code may know how to display values specially. For example, the Rust code understands that &str is a string-like type, or Ada knows how to handle unconstrained arrays. This knowledge is exposed via val-print, and via varobj -- but currently not via DAP. This patch adds some support code to let DAP also handle these cases, though in a somewhat more generic way. Type.is_array_like and Value.to_array are added to make Python aware of the cases where gdb knows that a structure type is really "array-like". Type.is_string_like is added to make Python aware of cases where gdb's language code knows that a type is string-like. Unlike Value.string, these cases are handled by the type's language, rather than the current language. --- gdb/NEWS | 8 ++++++++ gdb/doc/python.texi | 24 ++++++++++++++++++++++ gdb/python/py-type.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ gdb/python/py-value.c | 37 +++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index c4b1f7a7e3b..98ff00d5efc 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -269,6 +269,9 @@ info main ** gdb.Value now has the 'assign' method. + ** gdb.Value now has the 'to_array' method. This converts an + array-like Value to an array. + ** gdb.Progspace now has the new method "objfile_for_address". This returns the gdb.Objfile, if any, that covers a given address. @@ -278,6 +281,11 @@ info main inferior specific, then this field contains None. This field can be written too. + ** gdb.Type now has the "is_array_like" and "is_string_like" + methods. These reflect GDB's internal idea of whether a type + might be array- or string-like, even if they do not have the + corresponding type code. + *** Changes in GDB 13 * MI version 1 is deprecated, and will be removed in GDB 14. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 7460d6c8e31..e9936991c49 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -1206,6 +1206,13 @@ print frame-arguments scalars} (@pxref{Print Settings}). @end table @end defun +@defun Value.to_array () +If this value is array-like (@pxref{Type.is_array_like}), then this +method converts it to an array, which is returned. If this value is +already an array, it is simply returned. Otherwise, an exception is +throw. +@end defun + @defun Value.string (@r{[}encoding@r{[}, errors@r{[}, length@r{]]]}) If this @code{gdb.Value} represents a string, then this method converts the contents to a Python string. Otherwise, this method will @@ -1392,6 +1399,23 @@ which @code{Type.is_scalar} is @code{False}), will raise a @code{ValueError}. @end defvar +@defvar Type.is_array_like +@anchor{Type.is_array_like} +A boolean indicating whether this type is array-like. + +Some languages have array-like objects that are represented internally +as structures. For example, this is true for a Rust slice type, or +for an Ada unconstrained array. @value{GDBN} may know about these +types. This determination is done based on the language from which +the type originated. +@end defvar + +@defvar Type.is_string_like +A boolean indicating whether this type is string-like. Like +@code{Type.is_array_like}, this is determined based on the originating +language of the type. +@end defvar + The following methods are provided: @defun Type.fields () diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index b60875c792e..1fc1308af8e 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -442,6 +442,59 @@ typy_is_signed (PyObject *self, void *closure) Py_RETURN_TRUE; } +/* Return true if this type is array-like. */ + +static PyObject * +typy_is_array_like (PyObject *self, void *closure) +{ + struct type *type = ((type_object *) self)->type; + + try + { + type = check_typedef (type); + } + catch (const gdb_exception &except) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + + if (type->is_array_like ()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; +} + +/* Return true if this type is string-like. */ + +static PyObject * +typy_is_string_like (PyObject *self, void *closure) +{ + struct type *type = ((type_object *) self)->type; + bool result = false; + + try + { + type = check_typedef (type); + + const language_defn *lang = nullptr; + if (HAVE_GNAT_AUX_INFO (type)) + lang = language_def (language_ada); + else if (HAVE_RUST_SPECIFIC (type)) + lang = language_def (language_rust); + if (lang != nullptr) + result = lang->is_string_type_p (type); + } + catch (const gdb_exception &except) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + + if (result) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; +} + /* Return the type, stripped of typedefs. */ static PyObject * typy_strip_typedefs (PyObject *self, PyObject *args) @@ -1525,6 +1578,10 @@ static gdb_PyGetSetDef type_object_getset[] = "Is this a scalar type?", nullptr }, { "is_signed", typy_is_signed, nullptr, "Is this a signed type?", nullptr }, + { "is_array_like", typy_is_array_like, nullptr, + "Is this an array-like type?", nullptr }, + { "is_string_like", typy_is_string_like, nullptr, + "Is this a string-like type?", nullptr }, { NULL } }; diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index e1178de89e9..ee492fd9af6 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -330,6 +330,40 @@ valpy_rvalue_reference_value (PyObject *self, PyObject *args) return valpy_reference_value (self, args, TYPE_CODE_RVALUE_REF); } +/* Implement Value.to_array. */ + +static PyObject * +valpy_to_array (PyObject *self, PyObject *args) +{ + PyObject *result = nullptr; + + try + { + struct value *val = ((value_object *) self)->value; + struct type *type = check_typedef (val->type ()); + + if (type->code () == TYPE_CODE_ARRAY) + { + result = self; + Py_INCREF (result); + } + else + { + val = value_to_array (val); + if (val == nullptr) + PyErr_SetString (PyExc_TypeError, _("Value is not array-like.")); + else + result = value_to_value_object (val); + } + } + catch (const gdb_exception &except) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + + return result; +} + /* Return a "const" qualified version of the value. */ static PyObject * @@ -2152,6 +2186,9 @@ formatting options" }, { "assign", (PyCFunction) valpy_assign, METH_VARARGS, "assign (VAL) -> None\n\ Assign VAL to this value." }, + { "to_array", valpy_to_array, METH_NOARGS, + "to_array () -> Value\n\ +Return value as an array, if possible." }, {NULL} /* Sentinel */ }; -- 2.40.1