Add an objfile getter to gdb.Type
Commit Message
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(+)
Comments
>>>>> "Christian" == Christian Biesinger via gdb-patches <gdb-patches@sourceware.org> writes:
Christian> This allows users of the Python API to find the objfile where a type
Christian> was defined.
Could you say what your motivation is for adding this?
The reason I ask is that I still harbor some hopes that we can complete
the "objfile splitting" project, and if so, then it wouldn't be possible
to associate a type with a single objfile.
I suppose maybe we could make the Python wrappers per-objfile. So, this
patch maybe wouldn't necessarily limit this.
The patch itself looks fine to me.
Tom
On Tue, May 28, 2019 at 5:07 PM Tom Tromey <tom@tromey.com> wrote:
> >>>>> "Christian" == Christian Biesinger via gdb-patches <gdb-patches@sourceware.org> writes:
> Christian> This allows users of the Python API to find the objfile where a type
> Christian> was defined.
>
> Could you say what your motivation is for adding this?
>
> The reason I ask is that I still harbor some hopes that we can complete
> the "objfile splitting" project, and if so, then it wouldn't be possible
> to associate a type with a single objfile.
>
> I suppose maybe we could make the Python wrappers per-objfile. So, this
> patch maybe wouldn't necessarily limit this.
I'm not sure what the objfile splitting this is about, but the
motivation is as follows. I want to use this API for better supporting
a project called JsDbg, which is basically (for the purpose of this
question) a data structure visualizer for multiple debuggers
(https://github.com/MicrosoftEdge/JsDbg/blob/master/docs/FEATURES.md).
I would like access to the objfile (really I just care about the name
of the shared library/executable) to disambiguate typenames that may
be the same in different solibs. I know that this doesn't handle the
case where the different solibs are all loaded in the same process,
but that should be ok.
Does that sound reasonable?
Christian
>
> The patch itself looks fine to me.
>
> Tom
>>>>> ">" == Christian Biesinger via gdb-patches <gdb-patches@sourceware.org> writes:
Tom> I suppose maybe we could make the Python wrappers per-objfile. So, this
Tom> patch maybe wouldn't necessarily limit this.
>> I'm not sure what the objfile splitting this is about, but the
>> motivation is as follows.
[...]
>> Does that sound reasonable?
Yeah, thanks for clarifying. If you think you will be writing future
gdb changes, we should set you up with write-after-approval access.
Email me off-list to do that. Otherwise, if you'd prefer, I can commit
it on your behalf.
thanks,
Tom
On Mon, Jun 3, 2019 at 2:50 PM Tom Tromey <tom@tromey.com> wrote:
>
> >>>>> ">" == Christian Biesinger via gdb-patches <gdb-patches@sourceware.org> writes:
>
> Tom> I suppose maybe we could make the Python wrappers per-objfile. So, this
> Tom> patch maybe wouldn't necessarily limit this.
>
> >> I'm not sure what the objfile splitting this is about, but the
> >> motivation is as follows.
> [...]
>
> >> Does that sound reasonable?
>
> Yeah, thanks for clarifying. If you think you will be writing future
> gdb changes, we should set you up with write-after-approval access.
> Email me off-list to do that. Otherwise, if you'd prefer, I can commit
> it on your behalf.
I don't think I'll be writing enough patches for that to be worth it,
so it would be great if you could commit this. Thanks!
Christian
>>>>> "Christian" == Christian Biesinger via gdb-patches <gdb-patches@sourceware.org> writes:
Christian> I don't think I'll be writing enough patches for that to be worth it,
Christian> so it would be great if you could commit this. Thanks!
I checked it in.
Tom
@@ -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]
@@ -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 ()
@@ -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 }
};
@@ -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\\\]"