]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[python] Add gdb.Type.name attribute.
authorJoel Brobecker <brobecker@adacore.com>
Mon, 23 Dec 2013 03:18:51 +0000 (07:18 +0400)
committerJoel Brobecker <brobecker@adacore.com>
Tue, 7 Jan 2014 03:11:17 +0000 (07:11 +0400)
Consider the following declarations:

    typedef long our_time_t;
    our_time_t current_time = 1384395743;

The purpose of this patch is to allow the use of a pretty-printer
for variables of type our_time_t.  Normally, pretty-printing sniffers
use the tag name in order to determine which, if any, pretty-printer
should be used. But in the case above, the tag name is not set, since
it does not apply to integral types.

This patch extends the gdb.Type list of attributes to also include
the name of the type, thus allowing the sniffer to match against
that name. With that change, I was able to write a pretty-printer
which displays our variable as follow:

    (gdb) print current_time
    $1 = Thu Nov 14 02:22:23 2013 (1384395743)

gdb/ChangeLog:

        * python/py-type.c (typy_get_name): New function.
        (type_object_getset): Add entry for attribute "name".
        * NEWS: Add entry mentioning this new attribute.

gdb/doc/ChangeLog:

        * gdb.texinfo (Types In Python): Document new attribute Types.name.

gdb/testsuite:

        * gdb.python/py-pp-integral.c: New file.
        * gdb.python/py-pp-integral.py: New file.
        * gdb.python/py-pp-integral.exp: New file.

Tested on x86_64-linux.

gdb/ChangeLog
gdb/NEWS
gdb/doc/ChangeLog
gdb/doc/gdb.texinfo
gdb/python/py-type.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-pp-integral.c [new file with mode: 0644]
gdb/testsuite/gdb.python/py-pp-integral.exp [new file with mode: 0644]
gdb/testsuite/gdb.python/py-pp-integral.py [new file with mode: 0644]

index cf1d1b95f8b6934c93110d40a93348bad0b9aba3..13d2e165c6c97e13b36064daa16554776532a80b 100644 (file)
@@ -1,3 +1,9 @@
+2014-01-07  Joel Brobecker  <brobecker@adacore.com>
+
+       * python/py-type.c (typy_get_name): New function.
+       (type_object_getset): Add entry for attribute "name".
+       * NEWS: Add entry mentioning this new attribute.
+
 2014-01-07  Yao Qi  <yao@codesourcery.com>
 
        * gnu-nat.c (set_exceptions_cmd): Remove an empty body 'if'
index b64967eaedb4bcc520364b357c5a797c1698b2d0..d408f90b2f6cc383066b2e5efa0428e1170c45f6 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -44,6 +44,7 @@
   ** Line tables representation has been added.
   ** New attribute 'parent_type' for gdb.Field objects.
   ** gdb.Field objects can be used as subscripts on gdb.Value objects.
+  ** New attribute 'name' for gdb.Type objects.
 
 * New targets
 
index a7e1c2f06d3517966fa0fd377b2ed837d2ddaa94..afcfbd981f36bf9e9bb6e7b0abcaa0110d666941 100644 (file)
@@ -1,3 +1,7 @@
+2014-01-07  Joel Brobecker  <brobecker@adacore.com>
+
+       * gdb.texinfo (Types In Python): Document new attribute Types.name.
+
 2013-12-30  Joel Brobecker  <brobecker@adacore.com>
 
        * gdb.texinfo (Types In Python): Clarify the documentation
index 010bb6bdfff908aaa3a3e9f5586aab848da106c3..74ddcf5cee450d99bac3cb4fe37348748709046d 100644 (file)
@@ -24311,6 +24311,11 @@ The type code for this type.  The type code will be one of the
 @code{TYPE_CODE_} constants defined below.
 @end defvar
 
+@defvar Type.name
+The name of this type.  If this type has no name, then @code{None}
+is returned.
+@end defvar
+
 @defvar Type.sizeof
 The size of this type, in target @code{char} units.  Usually, a
 target's @code{char} type will be an 8-bit byte.  However, on some
index 3941a112468d227774aa88157ccab78e7dcc6268..16bb4421eedb1a138320f4cdadc3c6a2bd4d10eb 100644 (file)
@@ -414,6 +414,18 @@ typy_items (PyObject *self, PyObject *args)
   return typy_fields_items (self, iter_items);
 }
 
+/* Return the type's name, or None.  */
+
+static PyObject *
+typy_get_name (PyObject *self, void *closure)
+{
+  struct type *type = ((type_object *) self)->type;
+
+  if (TYPE_NAME (type) == NULL)
+    Py_RETURN_NONE;
+  return PyString_FromString (TYPE_NAME (type));
+}
+
 /* Return the type's tag, or None.  */
 static PyObject *
 typy_get_tag (PyObject *self, void *closure)
@@ -1395,6 +1407,8 @@ static PyGetSetDef type_object_getset[] =
 {
   { "code", typy_get_code, NULL,
     "The code for this type.", NULL },
+  { "name", typy_get_name, NULL,
+    "The name for this type, or None.", NULL },
   { "sizeof", typy_get_sizeof, NULL,
     "The size of this type, in bytes.", NULL },
   { "tag", typy_get_tag, NULL,
index 662d1617157e2680e9e80abd0da8f765ac042e32..67e5cc127f7a4892fe8014d140a6e5ca4d86defb 100644 (file)
@@ -1,3 +1,9 @@
+2014-01-07  Joel Brobecker  <brobecker@adacore.com>
+
+       * gdb.python/py-pp-integral.c: New file.
+       * gdb.python/py-pp-integral.py: New file.
+       * gdb.python/py-pp-integral.exp: New file.
+
 2013-12-19  Sergio Durigan Junior  <sergiodj@redhat.com>
 
        PR breakpoints/16297
diff --git a/gdb/testsuite/gdb.python/py-pp-integral.c b/gdb/testsuite/gdb.python/py-pp-integral.c
new file mode 100644 (file)
index 0000000..a5fe8c3
--- /dev/null
@@ -0,0 +1,33 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2013-2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see  <http://www.gnu.org/licenses/>.  */
+
+typedef long time_t;
+
+static void
+tick_tock (time_t *t)
+{
+  *t++;
+}
+
+int
+main (void)
+{
+  time_t current_time = 1384395743;
+
+  tick_tock (&current_time);
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.python/py-pp-integral.exp b/gdb/testsuite/gdb.python/py-pp-integral.exp
new file mode 100644 (file)
index 0000000..1262c3f
--- /dev/null
@@ -0,0 +1,38 @@
+# Copyright (C) 2013-2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile
+
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
+    return -1
+}
+
+# Skip all tests if Python scripting is not enabled.
+if { [skip_python_tests] } { continue }
+
+if ![runto tick_tock] {
+    return -1
+}
+
+set remote_python_file [gdb_remote_download host \
+                           ${srcdir}/${subdir}/${testfile}.py]
+
+gdb_test_no_output "source ${remote_python_file}"
+
+gdb_test "print *t" " = Thu Nov 14 02:22:23 2013 \\(1384395743\\)"
+
+gdb_test "print /r *t" "= 1384395743"
+
+remote_file host delete ${remote_python_file}
diff --git a/gdb/testsuite/gdb.python/py-pp-integral.py b/gdb/testsuite/gdb.python/py-pp-integral.py
new file mode 100644 (file)
index 0000000..34f41a6
--- /dev/null
@@ -0,0 +1,35 @@
+# Copyright (C) 2013-2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from time import asctime, gmtime
+import gdb  # silence pyflakes
+
+
+class TimePrinter:
+    def __init__(self, val):
+        self.val = val
+
+    def to_string(self):
+        secs = int(self.val)
+        return "%s (%d)" % (asctime(gmtime(secs)), secs)
+
+
+def time_sniffer(val):
+    if hasattr(val.type, 'name') and val.type.name == "time_t":
+        return TimePrinter(val)
+    return None
+
+
+gdb.pretty_printers.append(time_sniffer)