From: "Christian Biesinger via gdb-patches" <gdb-patches@sourceware.org>
To: gdb-patches@sourceware.org
Cc: Christian Biesinger <cbiesinger@google.com>
Subject: [PATCH] Add an objfile getter to gdb.Type
Date: Thu, 23 May 2019 21:37:00 -0000 [thread overview]
Message-ID: <20190523213729.27928-1-cbiesinger@google.com> (raw)
In-Reply-To: <20190523203227.260432-1-cbiesinger@google.com>
This allows users of the Python API to find the objfile where a type
was defined.
gdb/ChangeLog:
2019-05-23 Christian Biesinger <cbiesinger@google.com>
Add objfile property to gdb.Type.
* gdb/NEWS: Mention Python API addition.
* gdb/python/py-type.c (typy_get_objfile): New method.
gdb/doc/ChangeLog:
2019-05-23 Christian Biesinger <cbiesinger@google.com>
* gdb/doc/python.texi: Document new gdb.Type.objfile property.
gdb/testsuite/ChangeLog:
2019-05-23 Christian Biesinger <cbiesinger@google.com>
* gdb/testsuite/gdb.python/py-type.exp: Test for new
gdb.Type.objfile property.
---
gdb/NEWS | 3 +++
gdb/doc/python.texi | 5 +++++
gdb/python/py-type.c | 14 ++++++++++++++
gdb/testsuite/gdb.python/py-type.exp | 4 ++++
4 files changed, 26 insertions(+)
diff --git a/gdb/NEWS b/gdb/NEWS
index 792548139e..c715bd6bca 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -27,6 +27,9 @@
'array_indexes', 'symbols', 'unions', 'deref_refs', 'actual_objects',
'static_members', 'max_elements', 'repeat_threshold', and 'format'.
+ ** gdb.Type has a new property 'objfile' which returns the objfile the
+ type was defined in.
+
* New commands
set may-call-functions [on|off]
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 98e52bb770..f769ad03a2 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -1087,6 +1087,11 @@ languages have this concept. If this type has no tag name, then
@code{None} is returned.
@end defvar
+@defvar Type.objfile
+The @code{gdb.Objfile} that this type was defined in, or @code{None} if
+there is no associated objfile.
+@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 22cc658a8b..7b99beacae 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -413,6 +413,18 @@ typy_get_tag (PyObject *self, void *closure)
return PyString_FromString (tagname);
}
+/* Return the type's objfile, or None. */
+static PyObject *
+typy_get_objfile (PyObject *self, void *closure)
+{
+ struct type *type = ((type_object *) self)->type;
+ struct objfile *objfile = TYPE_OBJFILE (type);
+
+ if (objfile == nullptr)
+ Py_RETURN_NONE;
+ return objfile_to_objfile_object (objfile).release ();
+}
+
/* Return the type, stripped of typedefs. */
static PyObject *
typy_strip_typedefs (PyObject *self, PyObject *args)
@@ -1419,6 +1431,8 @@ static gdb_PyGetSetDef type_object_getset[] =
"The size of this type, in bytes.", NULL },
{ "tag", typy_get_tag, NULL,
"The tag name for this type, or None.", NULL },
+ { "objfile", typy_get_objfile, NULL,
+ "The objfile this type was defined in, or None.", NULL },
{ NULL }
};
diff --git a/gdb/testsuite/gdb.python/py-type.exp b/gdb/testsuite/gdb.python/py-type.exp
index 734f9b40fd..da4d4a7228 100644
--- a/gdb/testsuite/gdb.python/py-type.exp
+++ b/gdb/testsuite/gdb.python/py-type.exp
@@ -98,6 +98,8 @@ proc test_fields {lang} {
gdb_py_test_silent_cmd "print (st)" "print value (st)" 1
gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value (st) from history" 1
gdb_py_test_silent_cmd "python fields = st.type.fields()" "get fields from st.type" 1
+ gdb_test "python print (st.type.objfile.filename == gdb.current_progspace ().filename)" "True" \
+ "check type.objfile"
gdb_test "python print (len(fields))" "2" "check number of fields (st)"
gdb_test "python print (fields\[0\].name)" "a" "check structure field a name"
gdb_test "python print (fields\[1\].name)" "b" "check structure field b name"
@@ -269,6 +271,8 @@ if { [build_inferior "${binfile}" "c"] == 0 } {
# Skip all tests if Python scripting is not enabled.
if { [skip_python_tests] } { continue }
+ gdb_test "python print (gdb.lookup_type ('char').objfile)" "None"
+
gdb_test "python print(gdb.lookup_type('char').array(1, 0))" \
"char \\\[0\\\]"
--
2.22.0.rc1.257.g3120a18244-goog
next prev parent reply other threads:[~2019-05-23 21:37 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-05-23 18:40 Christian Biesinger via gdb-patches
2019-05-23 19:01 ` Eli Zaretskii
2019-05-23 19:34 ` Simon Marchi
2019-05-23 20:36 ` Christian Biesinger via gdb-patches
2019-05-23 20:32 ` Christian Biesinger via gdb-patches
2019-05-23 21:06 ` Simon Marchi
2019-05-23 21:25 ` Christian Biesinger via gdb-patches
2019-05-23 21:37 ` Christian Biesinger via gdb-patches [this message]
2019-05-28 22:07 ` Tom Tromey
2019-05-30 17:01 ` Christian Biesinger via gdb-patches
2019-06-03 19:50 ` Tom Tromey
2019-06-03 20:28 ` Christian Biesinger via gdb-patches
2019-06-04 15:47 ` Tom Tromey
2019-06-04 21:48 ` Pedro Alves
2019-06-04 22:07 ` Christian Biesinger via gdb-patches
2019-06-05 1:51 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190523213729.27928-1-cbiesinger@google.com \
--to=gdb-patches@sourceware.org \
--cc=cbiesinger@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).