public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [RFA/python+doco/NEWS] Add gdb.Type.name attribute.
@ 2013-12-23  3:37 Joel Brobecker
  2013-12-23  3:54 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Joel Brobecker @ 2013-12-23  3:37 UTC (permalink / raw)
  To: gdb-patches

Hello,

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.
        * py-pp-integral.py: New file.
        * py-pp-integral.exp: New file.

Tested on x86_64-linux. OK to commit?

Thank you!
-- 
Joel

---
 gdb/NEWS                                    |  1 +
 gdb/doc/gdb.texinfo                         |  5 ++++
 gdb/python/py-type.c                        | 14 +++++++++++
 gdb/testsuite/gdb.python/py-pp-integral.c   | 33 +++++++++++++++++++++++++
 gdb/testsuite/gdb.python/py-pp-integral.exp | 38 +++++++++++++++++++++++++++++
 gdb/testsuite/gdb.python/py-pp-integral.py  | 35 ++++++++++++++++++++++++++
 6 files changed, 126 insertions(+)
 create mode 100644 gdb/testsuite/gdb.python/py-pp-integral.c
 create mode 100644 gdb/testsuite/gdb.python/py-pp-integral.exp
 create mode 100644 gdb/testsuite/gdb.python/py-pp-integral.py

diff --git a/gdb/NEWS b/gdb/NEWS
index e4baf50..5fac44a 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -42,6 +42,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
 
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 29f1cfc..2c8e4bc 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -24324,6 +24324,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
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index 4ad1e12..a70dcfd 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -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,
diff --git a/gdb/testsuite/gdb.python/py-pp-integral.c b/gdb/testsuite/gdb.python/py-pp-integral.c
new file mode 100644
index 0000000..39763e1
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-pp-integral.c
@@ -0,0 +1,33 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2013 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
index 0000000..4b49d99
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-pp-integral.exp
@@ -0,0 +1,38 @@
+# Copyright (C) 2013 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
index 0000000..9f4dd25
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-pp-integral.py
@@ -0,0 +1,35 @@
+# Copyright (C) 2013 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)
-- 
1.8.3.2

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFA/python+doco/NEWS] Add gdb.Type.name attribute.
  2013-12-23  3:37 [RFA/python+doco/NEWS] Add gdb.Type.name attribute Joel Brobecker
@ 2013-12-23  3:54 ` Eli Zaretskii
  2014-01-06 21:44 ` Tom Tromey
  2014-01-09 18:55 ` Tom Tromey
  2 siblings, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2013-12-23  3:54 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: gdb-patches

> From: Joel Brobecker <brobecker@adacore.com>
> Date: Mon, 23 Dec 2013 07:37:25 +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.
>         * py-pp-integral.py: New file.
>         * py-pp-integral.exp: New file.
> 
> Tested on x86_64-linux. OK to commit?

OK for the documentation parts.

Thanks.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFA/python+doco/NEWS] Add gdb.Type.name attribute.
  2013-12-23  3:37 [RFA/python+doco/NEWS] Add gdb.Type.name attribute Joel Brobecker
  2013-12-23  3:54 ` Eli Zaretskii
@ 2014-01-06 21:44 ` Tom Tromey
  2014-01-07  3:14   ` Joel Brobecker
  2014-01-09 18:55 ` Tom Tromey
  2 siblings, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2014-01-06 21:44 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: gdb-patches

>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:

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

Joel>         * py-pp-integral.c: New file.
Joel>         * py-pp-integral.py: New file.
Joel>         * py-pp-integral.exp: New file.

Joel> Tested on x86_64-linux. OK to commit?

Thanks Joel.  This is ok.

Note that scalar types are often lost by gdb, so if you want to try
"print (our_time_t) value", it will probably not work.  This is fallout
from check_typedef.  There's a bug in bugzilla about this.

Tom

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFA/python+doco/NEWS] Add gdb.Type.name attribute.
  2014-01-06 21:44 ` Tom Tromey
@ 2014-01-07  3:14   ` Joel Brobecker
  0 siblings, 0 replies; 6+ messages in thread
From: Joel Brobecker @ 2014-01-07  3:14 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> >>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:
> 
> Joel>         * python/py-type.c (typy_get_name): New function.
> Joel>         (type_object_getset): Add entry for attribute "name".
> Joel>         * NEWS: Add entry mentioning this new attribute.
> 
> Joel>         * py-pp-integral.c: New file.
> Joel>         * py-pp-integral.py: New file.
> Joel>         * py-pp-integral.exp: New file.
> 
> Joel> Tested on x86_64-linux. OK to commit?
> 
> Thanks Joel.  This is ok.

Thanks, Tom and Eli for the reviews. The patch is now in.

> Note that scalar types are often lost by gdb, so if you want to try
> "print (our_time_t) value", it will probably not work.  This is fallout
> from check_typedef.  There's a bug in bugzilla about this.

I see. I've noticed the same problem with Ada as well, but during
internal transformations (what we call type "fix"-ing), where we lose
information as we go - Eg, we lose the type's name, or the type of
the array index, etc.  I've tried to fix things when they become
needed as well.

-- 
Joel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFA/python+doco/NEWS] Add gdb.Type.name attribute.
  2013-12-23  3:37 [RFA/python+doco/NEWS] Add gdb.Type.name attribute Joel Brobecker
  2013-12-23  3:54 ` Eli Zaretskii
  2014-01-06 21:44 ` Tom Tromey
@ 2014-01-09 18:55 ` Tom Tromey
  2014-01-10  4:02   ` Joel Brobecker
  2 siblings, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2014-01-09 18:55 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: gdb-patches

>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:

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

Hi Joel.  Today I happened to notice:

Joel> +gdb_test_no_output "source ${remote_python_file}"

... this puts the full path to the .py file into the test name.

I think the test should specify a name that doesn't include a path,
instead.

This same problem affects another new test,
gdb.ada/pp-rec-component.exp.

thanks,
Tom

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFA/python+doco/NEWS] Add gdb.Type.name attribute.
  2014-01-09 18:55 ` Tom Tromey
@ 2014-01-10  4:02   ` Joel Brobecker
  0 siblings, 0 replies; 6+ messages in thread
From: Joel Brobecker @ 2014-01-10  4:02 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 817 bytes --]

> Hi Joel.  Today I happened to notice:
> 
> Joel> +gdb_test_no_output "source ${remote_python_file}"
> 
> ... this puts the full path to the .py file into the test name.
> 
> I think the test should specify a name that doesn't include a path,
> instead.
> 
> This same problem affects another new test,
> gdb.ada/pp-rec-component.exp.

Indeed, thanks for pointing it out. I fixed both instances with
the attached patches.

gdb/testsuite:

2014-01-10  Joel Brobecker  <brobecker@adacore.com>

        * gdb.ada/pp-rec-component.exp: Remove path from "source" test.

2014-01-10  Joel Brobecker  <brobecker@adacore.com>

        * gdb.python/py-pp-integral.exp: Remove path from "source" test.

Tested on x86_64-linux, after verifying that the gdb.sum no longer
shows the path for the "source" tests.

Pushed.
-- 
Joel

[-- Attachment #2: 0001-Remove-path-from-gdb.python-py-pp-integral.exp-sourc.patch --]
[-- Type: text/x-diff, Size: 1474 bytes --]

From 4e23fced8108c99ddc9d52fb2c02af8cac4cc7bb Mon Sep 17 00:00:00 2001
From: Joel Brobecker <brobecker@adacore.com>
Date: Fri, 10 Jan 2014 07:50:29 +0400
Subject: [PATCH 1/2] Remove path from gdb.python/py-pp-integral.exp "source"
 test.

gdb/testsuite/ChangeLog:

	* gdb.python/py-pp-integral.exp: Remove path from "source" test.
---
 gdb/testsuite/ChangeLog                     | 4 ++++
 gdb/testsuite/gdb.python/py-pp-integral.exp | 3 ++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 4c4dcec..283f1f2 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2014-01-10  Joel Brobecker  <brobecker@adacore.com>
+
+	* gdb.python/py-pp-integral.exp: Remove path from "source" test.
+
 2014-01-09  Maciej W. Rozycki  <macro@codesourcery.com>
 	    Pedro Alves  <palves@redhat.com>
 
diff --git a/gdb/testsuite/gdb.python/py-pp-integral.exp b/gdb/testsuite/gdb.python/py-pp-integral.exp
index 1262c3f..8149bde 100644
--- a/gdb/testsuite/gdb.python/py-pp-integral.exp
+++ b/gdb/testsuite/gdb.python/py-pp-integral.exp
@@ -29,7 +29,8 @@ if ![runto tick_tock] {
 set remote_python_file [gdb_remote_download host \
 			    ${srcdir}/${subdir}/${testfile}.py]
 
-gdb_test_no_output "source ${remote_python_file}"
+gdb_test_no_output "source ${remote_python_file}" \
+    "source ${testfile}.py"
 
 gdb_test "print *t" " = Thu Nov 14 02:22:23 2013 \\(1384395743\\)"
 
-- 
1.8.3.2


[-- Attachment #3: 0002-Remove-path-from-gdb.ada-pp-rec-component.exp-source.patch --]
[-- Type: text/x-diff, Size: 1606 bytes --]

From a2cd8cfed14491303eb8338f90e206034c5a3fe2 Mon Sep 17 00:00:00 2001
From: Joel Brobecker <brobecker@adacore.com>
Date: Fri, 10 Jan 2014 07:54:23 +0400
Subject: [PATCH 2/2] Remove path from gdb.ada/pp-rec-component.exp "source"
 test

gdb/testsuite/ChangeLog:

	* gdb.ada/pp-rec-component.exp: Remove path from "source" test.
---
 gdb/testsuite/ChangeLog                    | 4 ++++
 gdb/testsuite/gdb.ada/pp-rec-component.exp | 3 ++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 283f1f2..2bfeec1 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,5 +1,9 @@
 2014-01-10  Joel Brobecker  <brobecker@adacore.com>
 
+	* gdb.ada/pp-rec-component.exp: Remove path from "source" test.
+
+2014-01-10  Joel Brobecker  <brobecker@adacore.com>
+
 	* gdb.python/py-pp-integral.exp: Remove path from "source" test.
 
 2014-01-09  Maciej W. Rozycki  <macro@codesourcery.com>
diff --git a/gdb/testsuite/gdb.ada/pp-rec-component.exp b/gdb/testsuite/gdb.ada/pp-rec-component.exp
index faa7a0f..f2e4ab2 100644
--- a/gdb/testsuite/gdb.ada/pp-rec-component.exp
+++ b/gdb/testsuite/gdb.ada/pp-rec-component.exp
@@ -28,7 +28,8 @@ if { [skip_python_tests] } { continue }
 
 set remote_python_file \
     [gdb_remote_download host ${srcdir}/${subdir}/${gdb_test_file_name}.py]
-gdb_test_no_output "source ${remote_python_file}"
+gdb_test_no_output "source ${remote_python_file}" \
+    "source ${gdb_test_file_name}.py"
 
 set bp_location [gdb_get_line_number "BREAK" ${testdir}/foo.adb]
 runto "foo.adb:$bp_location"
-- 
1.8.3.2


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-01-10  4:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-23  3:37 [RFA/python+doco/NEWS] Add gdb.Type.name attribute Joel Brobecker
2013-12-23  3:54 ` Eli Zaretskii
2014-01-06 21:44 ` Tom Tromey
2014-01-07  3:14   ` Joel Brobecker
2014-01-09 18:55 ` Tom Tromey
2014-01-10  4:02   ` Joel Brobecker

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).