From: Matt McCormick <matt@mmmccormick.com>
To: archer@sourceware.org
Subject: [PATCH 3/3] [python] Create pretty-printer lookup_function classes.
Date: Mon, 18 Jan 2010 07:57:00 -0000 [thread overview]
Message-ID: <20100118075716.2095.84917.stgit@localhost> (raw)
In-Reply-To: <f23d1cbc1001171943y447c46f1wa7d6f6c1bfc52a68@mail.gmail.com>
These prevent repeated code and are extensible.
gdb/ChangeLog
2009-31-12 Matt McCormick <matt@mmmccormick.com>
* Makefile.in: Install gdb/pretty/lookup_function.py.
* python/lib/gdb/pretty/lookup_function.py)
(RELookupFunction, RELookupFunctionTag): New lookup_function classes.
gdb/testsuite/ChangeLog
2009-31-12 Matt McCormick <matt@mmmccormick.com>
* gdb.python/py-prettyprint.py (lookup_function):
Use RELookupFunctionTag to create the lookup_function.
---
gdb/Makefile.in | 2 -
gdb/python/lib/gdb/pretty/lookup_function.py | 75 ++++++++++++++++++++++++++
gdb/testsuite/gdb.python/py-prettyprint.py | 35 +-----------
3 files changed, 79 insertions(+), 33 deletions(-)
create mode 100644 gdb/python/lib/gdb/pretty/lookup_function.py
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index e9055b1..d1510c2 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -2075,7 +2075,7 @@ PY_FILES = gdb/FrameIterator.py gdb/FrameWrapper.py gdb/command/alias.py \
gdb/command/ignore_errors.py gdb/command/save_breakpoints.py \
gdb/function/caller_is.py gdb/function/in_scope.py \
gdb/function/__init__.py gdb/backtrace.py gdb/__init__.py \
- gdb/pretty/__init__.py
+ gdb/pretty/__init__.py gdb/pretty/lookup_function.py
# Install the Python library. Python library files go under
# $(pythondir).
diff --git a/gdb/python/lib/gdb/pretty/lookup_function.py b/gdb/python/lib/gdb/pretty/lookup_function.py
new file mode 100644
index 0000000..f6699bd
--- /dev/null
+++ b/gdb/python/lib/gdb/pretty/lookup_function.py
@@ -0,0 +1,75 @@
+# Pretty-printer lookup_function classes.
+
+# Copyright (C) 2009 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/>.
+
+import gdb
+
+class RELookupFunction (object):
+ """Returns a pretty-printer based on some type filtering followed by a search
+ in a dictionary of key:value of typename_regex:pretty-printer. If it cannot
+ find a pretty-printer to print the given type, returns None.
+
+ Arguments
+ ---------
+ pretty_printers_dict: dict
+ typename_regex : pretty-pretter-function
+ """
+
+ def __init__ (self, pretty_printers_dict, type_attribute=''):
+ self.pretty_printers_dict = pretty_printers_dict
+
+ def _canonical_type (self, gdb_type):
+# If it points to a reference, get the reference.
+ if gdb_type.code == gdb.TYPE_CODE_REF:
+ gdb_type = gdb_type.target ()
+
+# Get the unqualified type, stripped of typedefs.
+ gdb_type = gdb_type.unqualified ().strip_typedefs ()
+
+ return gdb_type
+
+ def _filter_types (self, gdb_type):
+ """Filters the given type and returns a name to pass to the
+ pretty_printers_dict or None."""
+ gdb_type = self._canonical_type (gdb_type)
+
+ return gdb_type
+
+ def __call__ (self, val):
+ "Returns a pretty-printer that can print val or None."
+ typename = self._filter_types (val.type)
+ if typename == None:
+ return None
+
+ for regex in self.pretty_printers_dict:
+ if regex.match (str (typename)):
+ return self.pretty_printers_dict[regex] (val)
+
+ return None
+
+class RELookupFunctionTag (RELookupFunction):
+ """Similar to RELookupFunction, but when trying to match to type.tag.
+ Useful for ``struct``, ``union``, or ``enum`` in C or C++."""
+
+ def __init__ (self, pretty_printers_dict):
+ super (RELookupFunctionTag, self).__init__ (pretty_printers_dict)
+
+ def _filter_types (self, gdb_type):
+ """Filters the given type and returns a name to pass to the
+ pretty_printers_dict or None."""
+ gdb_type = self._canonical_type (gdb_type)
+
+ return gdb_type.tag
diff --git a/gdb/testsuite/gdb.python/py-prettyprint.py b/gdb/testsuite/gdb.python/py-prettyprint.py
index 9a0d107..84682a8 100644
--- a/gdb/testsuite/gdb.python/py-prettyprint.py
+++ b/gdb/testsuite/gdb.python/py-prettyprint.py
@@ -18,6 +18,8 @@
import re
+from gdb.pretty.lookup_function import RELookupFunctionTag
+
# Test returning a Value from a printer.
class string_print:
def __init__(self, val):
@@ -137,37 +139,6 @@ class pp_outer:
yield 's', self.val['s']
yield 'x', self.val['x']
-def lookup_function (val):
- "Look-up and return a pretty-printer that can print val."
-
- # Get the type.
- type = val.type
-
- # If it points to a reference, get the reference.
- if type.code == gdb.TYPE_CODE_REF:
- type = type.target ()
-
- # Get the unqualified type, stripped of typedefs.
- type = type.unqualified ().strip_typedefs ()
-
- # Get the type name.
- typename = type.tag
-
- if typename == None:
- return None
-
- # Iterate over local dictionary of types to determine
- # if a printer is registered for that type. Return an
- # instantiation of the printer if found.
- for function in pretty_printers_dict:
- if function.match (typename):
- return pretty_printers_dict[function] (val)
-
- # Cannot find a pretty printer. Return None.
-
- return None
-
-
def register_pretty_printers ():
pretty_printers_dict[re.compile ('^struct s$')] = pp_s
pretty_printers_dict[re.compile ('^s$')] = pp_s
@@ -203,6 +174,6 @@ def register_pretty_printers ():
pretty_printers_dict[re.compile ('^outerstruct$')] = pp_outer
pretty_printers_dict = {}
-
register_pretty_printers ()
+lookup_function = RELookupFunctionTag (pretty_printers_dict)
gdb.pretty_printers.append (lookup_function)
next prev parent reply other threads:[~2010-01-18 7:57 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-12-31 15:00 [PATCH 1/3] [rfc][python] Create gdb.python package Matt McCormick
2009-12-31 15:00 ` [PATCH 2/3] [python] Create gdb.pretty.register Matt McCormick
2009-12-31 15:00 ` [PATCH 3/3] [python] Create pretty-printer lookup_function classes Matt McCormick
2010-01-18 3:43 ` [PATCH 2/3] [python] Create gdb.pretty.register Matthew McCormick (thewtex)
2010-01-18 7:57 ` Matt McCormick [this message]
2010-01-18 7:57 ` Matt McCormick
2010-01-18 7:57 ` [PATCH 1/3] [python] Create gdb.pretty package Matt McCormick
2010-01-18 8:05 ` [Archer] " Joel Brobecker
2010-01-18 16:20 ` Matthew McCormick (thewtex)
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=20100118075716.2095.84917.stgit@localhost \
--to=matt@mmmccormick.com \
--cc=archer@sourceware.org \
/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).