Add an objfile getter to gdb.Type

Message ID 20190523183935.209380-1-cbiesinger@google.com
State New, archived
Headers

Commit Message

Terekhov, Mikhail via Gdb-patches May 23, 2019, 6:39 p.m. UTC
  This allows users of the Python API to find the objfile where a type
was defined.

gdb/ChangeLog:

2019-05-22  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-22  Christian Biesinger  <cbiesinger@google.com>

	* gdb/doc/python.texi: Document new objfile property

---
 gdb/NEWS             |  3 +++
 gdb/doc/python.texi  |  5 +++++
 gdb/python/py-type.c | 16 ++++++++++++++++
 3 files changed, 24 insertions(+)
  

Comments

Eli Zaretskii May 23, 2019, 7:01 p.m. UTC | #1
> Date: Thu, 23 May 2019 13:39:35 -0500
> From: "Christian Biesinger via gdb-patches" <gdb-patches@sourceware.org>
> Cc: Christian Biesinger <cbiesinger@google.com>
> 
> This allows users of the Python API to find the objfile where a type
> was defined.
> 
> gdb/ChangeLog:
> 
> 2019-05-22  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-22  Christian Biesinger  <cbiesinger@google.com>
> 
> 	* gdb/doc/python.texi: Document new objfile property

OK for the documentation parts, thanks.
  
Simon Marchi May 23, 2019, 7:34 p.m. UTC | #2
Hi Christian,

On 2019-05-23 2:39 p.m., Christian Biesinger via gdb-patches wrote:
> 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..722960e032 100644
> --- a/gdb/python/py-type.c
> +++ b/gdb/python/py-type.c
> @@ -413,6 +413,20 @@ typy_get_tag (PyObject *self, void *closure)
>    return PyString_FromString (tagname);
>  }
>  
> +/* Return the type's tag, or None.  */

copy-pasto: "type's objfile"

> +static PyObject *
> +typy_get_objfile (PyObject *self, void *closure)
> +{
> +  struct type *type = ((type_object *) self)->type;
> +  struct objfile *objfile = nullptr;
> +
> +  objfile = TYPE_OBJFILE(type);

You can write it as

  struct objfile *objfile = objfile = TYPE_OBJFILE (type);

directly.

We would need a corresponding test though.  It should be a relatively easy addition
to testsuite/gdb.python/py-type.exp.  See here for info about how to run just one
test:

https://sourceware.org/gdb/wiki/TestingGDB#Running_specific_tests

Thanks!

Simon
  
Terekhov, Mikhail via Gdb-patches May 23, 2019, 8:36 p.m. UTC | #3
On Thu, May 23, 2019 at 2:34 PM Simon Marchi <simark@simark.ca> wrote:
> > +/* Return the type's tag, or None.  */
>
> copy-pasto: "type's objfile"

Done

> > +static PyObject *
> > +typy_get_objfile (PyObject *self, void *closure)
> > +{
> > +  struct type *type = ((type_object *) self)->type;
> > +  struct objfile *objfile = nullptr;
> > +
> > +  objfile = TYPE_OBJFILE(type);
>
> You can write it as
>
>   struct objfile *objfile = objfile = TYPE_OBJFILE (type);
>
> directly.
>
> We would need a corresponding test though.  It should be a relatively easy addition
> to testsuite/gdb.python/py-type.exp.  See here for info about how to run just one
> test:

Done. I sent the updated patch as a separate email, I hope I did it right.

Thanks,
Christian
  

Patch

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..722960e032 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -413,6 +413,20 @@  typy_get_tag (PyObject *self, void *closure)
   return PyString_FromString (tagname);
 }
 
+/* Return the type's tag, or None.  */
+static PyObject *
+typy_get_objfile (PyObject *self, void *closure)
+{
+  struct type *type = ((type_object *) self)->type;
+  struct objfile *objfile = nullptr;
+
+  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 +1433,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 }
 };