From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1086 invoked by alias); 18 Jan 2010 07:57:25 -0000 Mailing-List: contact archer-help@sourceware.org; run by ezmlm Sender: Precedence: bulk List-Post: List-Help: List-Subscribe: List-Id: Received: (qmail 1073 invoked by uid 22791); 18 Jan 2010 07:57:24 -0000 X-SWARE-Spam-Status: No, hits=-2.4 required=5.0 tests=AWL,BAYES_00,SPF_PASS X-Spam-Check-By: sourceware.org From: Matt McCormick Subject: [PATCH 3/3] [python] Create pretty-printer lookup_function classes. To: archer@sourceware.org Date: Mon, 18 Jan 2010 07:57:00 -0000 Message-ID: <20100118075716.2095.84917.stgit@localhost> In-Reply-To: References: User-Agent: StGIT/0.14.3 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-SW-Source: 2010-q1/txt/msg00020.txt.bz2 These prevent repeated code and are extensible. gdb/ChangeLog 2009-31-12 Matt McCormick * 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 * 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 . + +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)