[Patch v12 4/4] Add debug method support to the Python API

Doug Evans dje@google.com
Fri Apr 11 18:15:00 GMT 2014


Siva Chandra writes:
 > This part adds the debug method support to the Python API.
 > 
 > 2014-04-01  Siva Chandra Reddy  <sivachandra@google.com>
 > 
 >         * python/py-debugmethods.c: New file.
 >         * python/py-objfile.c (objfile_object): New field
 >         'debug_methods'.
 >         (objfpy_dealloc): XDECREF on the new debug_methods field.
 >         (objfpy_new, objfile_to_objfile_object): Initialize
 >         debug_methods field.
 >         (objfpy_get_debug_methods): New function.
 >         (objfile_getset): New entry 'debug_methods'.
 >         * python/py-progspace.c (pspace_object): New field
 >         'debug_methods'.
 >         (pspy_dealloc): XDECREF on the new debug_methods field.
 >         (pspy_new, pspace_to_pspace_object): Initialize
 >         debug_methods field.
 >         (pspy_get_debug_methods): New function.
 >         (pspace_getset): New entry 'debug_methods'.
 >         * python/python-internal.h: Add declarations for new functions.
 >         * python/python.c (_initialize_python): Invoke
 >         gdbpy_initialize_debug_methods.
 >         * python/lib/gdb/__init__.py (debug_methods): New
 >         attribute.
 >         * python/lib/gdb/debug_method.py: New file.
 >         * python/lib/gdb/command/debug_methods.py: New file.
 > 
 >         testuite/
 >         * gdb.python/py-debugmethods.cc: New testcase to test debug
 >         methods.
 >         * gdb.python/py-debugmethods.exp: New tests to test debug
 >         methods.
 >         * gdb.python/py-debugmethods.py: Python script supporting the
 >         new testcase and tests.
 > diff --git a/gdb/Makefile.in b/gdb/Makefile.in
 > index 3efedc8..be58814 100644
 > --- a/gdb/Makefile.in
 > +++ b/gdb/Makefile.in
 > @@ -344,6 +344,7 @@ SUBDIR_PYTHON_OBS = \
 >  	py-breakpoint.o \
 >  	py-cmd.o \
 >  	py-continueevent.o \
 > +	py-debugmethods.o \
 >  	py-event.o \
 >  	py-evtregistry.o \
 >  	py-evts.o \
 > @@ -380,6 +381,7 @@ SUBDIR_PYTHON_SRCS = \
 >  	python/py-breakpoint.c \
 >  	python/py-cmd.c \
 >  	python/py-continueevent.c \
 > +	python/py-debugmethods.c \
 >  	python/py-event.c \
 >  	python/py-evtregistry.c \
 >  	python/py-evts.c \
 > @@ -2378,6 +2380,10 @@ py-continueevent.o: $(srcdir)/python/py-continueevent.c
 >  	$(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-continueevent.c
 >  	$(POSTCOMPILE)
 >  
 > +py-debugmethods.o: $(srcdir)/python/py-debugmethods.c
 > +	$(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-debugmethods.c
 > +	$(POSTCOMPILE)
 > +
 >  py-event.o: $(srcdir)/python/py-event.c
 >  	$(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-event.c
 >  	$(POSTCOMPILE)
 > diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in
 > index 3288e50..a69aec6 100644
 > --- a/gdb/data-directory/Makefile.in
 > +++ b/gdb/data-directory/Makefile.in
 > @@ -63,8 +63,10 @@ PYTHON_FILES = \
 >  	gdb/types.py \
 >  	gdb/printing.py \
 >  	gdb/prompt.py \
 > +	gdb/debug_method.py \
 >  	gdb/command/bound_registers.py \
 >  	gdb/command/__init__.py \
 > +	gdb/command/debug_methods.py \
 >  	gdb/command/frame_filters.py \
 >  	gdb/command/type_printers.py \
 >  	gdb/command/pretty_printers.py \
 > diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
 > index 95a76c2..88fbba0 100644
 > --- a/gdb/python/lib/gdb/__init__.py
 > +++ b/gdb/python/lib/gdb/__init__.py
 > @@ -67,6 +67,8 @@ pretty_printers = []
 >  
 >  # Initial type printers.
 >  type_printers = []
 > +# Initial debug method matchers.
 > +debug_methods = []
 >  # Initial frame filters.
 >  frame_filters = {}
 >  
 > diff --git a/gdb/python/lib/gdb/command/debug_methods.py b/gdb/python/lib/gdb/command/debug_methods.py
 > new file mode 100644
 > index 0000000..a60c98d
 > --- /dev/null
 > +++ b/gdb/python/lib/gdb/command/debug_methods.py
 > @@ -0,0 +1,264 @@
 > +# Debug method commands.
 > +# 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/>.
 > +
 > +import gdb
 > +import re
 > +
 > +"""GDB commands for working with debug-methods."""
 > +
 > +
 > +def validate_dm_regexp(part_name, regexp):
 > +    try:
 > +        return re.compile(regexp)
 > +    except SyntaxError:
 > +        raise SyntaxError("Invalid %s regexp: %s", part_name, regexp)
 > +
 > +
 > +def parse_dm_command_args(arg):
 > +    """Parses the arguments passed to a debug method command.
 > +
 > +    Arguments:
 > +        arg: The argument string passed to a debug method command.
 > +
 > +    Returns:
 > +        A 3-tuple: (<locus matching regular expression>,
 > +                    <matcher matching regular expression>,
 > +                    <name matching regular experssion>)

expression

 > +    """
 > +    argv = gdb.string_to_argv(arg)
 > +    argc = len(argv)
 > +    if argc > 2:
 > +        raise SyntaxError("Too many arguments to command.")
 > +    locus_regexp = ""
 > +    matcher_name_regexp = ""
 > +    dm_name_regexp = None
 > +    if argc >= 1:
 > +        locus_regexp = argv[0]
 > +    if argc == 2:
 > +        parts = argv[1].split(";", 1)
 > +        matcher_name_regexp = parts[0]
 > +        if len(parts) > 1:
 > +            dm_name_regexp = parts[1]
 > +    if dm_name_regexp:
 > +        name_re = validate_dm_regexp("debug method name", dm_name_regexp)
 > +    else:
 > +        name_re = None
 > +    return (validate_dm_regexp("locus", locus_regexp),
 > +            validate_dm_regexp("matcher name", matcher_name_regexp),
 > +            name_re)
 > +
 > +
 > +def get_global_method_matchers(locus_re, matcher_re):
 > +    """Returns a dict of matching globally registered debug methods.
 > +
 > +    Arguments:
 > +        locus_re: Even though only globally registered debug methods are
 > +                  looked up, they will be looked up only if 'global' matches
 > +                  LOCUS_RE.
 > +        matcher_re: The regular expression matching the names of debug methods.
 > +
 > +    Returns:
 > +        A dict of matching globally registered debug method matchers.  The only
 > +        key in the dict will be 'global'.
 > +    """
 > +    locus_str = "global"
 > +    dm_dict = { locus_str: [] }
 > +    if locus_re.match("global"):
 > +        dm_dict[locus_str].extend(
 > +            [m for m in gdb.debug_methods if matcher_re.match(m.name)])
 > +    return dm_dict
 > +
 > +
 > +def get_method_matchers_in_loci(loci, locus_re, matcher_re):
 > +    """Returns a dict of matching registered debug methods in the LOCI.
 > +
 > +    Arguments:
 > +        loci: The list of loci to lookup matching debug methods in.
 > +        locus_re: Debug method matchers will be looked up in a particular locus
 > +                  only if its filename matches the regular expression LOCUS_RE.
 > +        matcher_re: The regular expression to match the debug method matcher
 > +                    names.
 > +
 > +    Returns:
 > +        A dict of matching debug method matchers.  The keys of the dict are the
 > +        filenames of the loci the debug method matchers belong to.
 > +    """
 > +    dm_dict = {}
 > +    for locus in loci:
 > +        if not locus_re.match(locus.filename):
 > +            continue
 > +        locus_type = "objfile"
 > +        if isinstance(locus, gdb.Progspace):
 > +            locus_type = "progspace"
 > +        locus_str = "registered with %s %s" % (locus_type, locus.filename)
 > +        dm_dict[locus_str] = [
 > +            m for m in locus.debug_methods if matcher_re.match(m.name)]
 > +    return dm_dict
 > +
 > +
 > +def print_dm_info(dm_dict, name_re):
 > +    """Print a dictionary of debug methods."""
 > +    def get_status_string(method):
 > +        if not m.enabled:
 > +            return " [disabled]"
 > +        else:
 > +          return ""
 > +
 > +    if not dm_dict:
 > +        return
 > +    for locus_str in dm_dict:
 > +        if not dm_dict[locus_str]:
 > +            continue
 > +        print ("Debug methods in %s:" % locus_str)
 > +        for matcher in dm_dict[locus_str]:
 > +            print ("  %s" % matcher.name)
 > +            if not matcher.methods:
 > +                continue
 > +            for m in matcher.methods:
 > +                if name_re is None or name_re.match(m.name):
 > +                    print ("    %s%s" % (m.name, get_status_string(m)))
 > +
 > +
 > +def set_dm_status1(dm_dict, name_re, status):
 > +    """Set the status (enabled/disabled) of a dictionary of debug methods."""
 > +    for locus_str, matchers in dm_dict.iteritems():
 > +        for matcher in matchers:
 > +            if not name_re:
 > +                # If the name regex is missing, then set the status of the
 > +                # matcher and move on.
 > +                matcher.enabled = status
 > +                continue
 > +            if not matcher.methods:
 > +                # The methods attribute could be None. Move on.

Two spaces after period.

 > +                continue
 > +            for m in matcher.methods:
 > +                if name_re.match(m.name):
 > +                    m.enabled = status
 > +
 > +
 > +def set_dm_status(arg, status):
 > +    """Set the status (enabled/disabled) of debug methods matching ARG.
 > +    This is a helper function for enable/disable commands.  ARG is the
 > +    argument string passed to the commands.
 > +    """
 > +    locus_re, matcher_re, name_re = parse_dm_command_args(arg)
 > +    set_dm_status1(get_global_method_matchers(locus_re, matcher_re), name_re,
 > +                   status)
 > +    set_dm_status1(
 > +        get_method_matchers_in_loci(gdb.progspaces(), locus_re, matcher_re),

The pretty-printer enable/disable support only looks at the current progspace.
It would be wrong for debug-methods to work differently.
Maybe the pretty-printer support should work differently,
but I don't think so.  Note that gdb.objfiles() only returns the
objfiles of current program space.

 > +        name_re,
 > +        status)
 > +    set_dm_status1(
 > +        get_method_matchers_in_loci(gdb.objfiles(), locus_re, matcher_re),
 > +        name_re,
 > +        status)
 > +
 > +
 > +class InfoDebugMethod(gdb.Command):
 > +    """GDB command to list registered debug method matchers.
 > +
 > +    Usage: info debug-method [locus-regexp [name-regexp]]
 > +
 > +    LOCUS-REGEXP is a regular expression matching the location of the debug
 > +    method matchers.  If it is omitted, all registered debug method matchers
 > +    from all loci are listed.  A locus could be 'global', a regular expression
 > +    matching filenames of program spaces, or a regular expression matching
 > +    filenames of objfiles.
 > +

Ditto.  The pretty-printer "info" command only looks at the current program
space.  Also, to specify the program space in LOCUS-REGEXP the pretty-printers
use "progspace".  e.g. info pretty-printer progspace
This should work identically.
Looking at the doc string in pretty-printers.py I see it's a big confused.
I'll submit a patch to fix the doc string.

 > +    NAME-REGEXP is a regular expression matching the names of debug method
 > +    matchers.  If this omitted for a specified locus, then all registered debug
 > +    methods in the locus are listed.  To list only a certain debug methods
 > +    managed by a single matcher, the name regexp can be specified as
 > +    matcher-name-regexp;debug-method-name-regexp.
 > +    """
 > +
 > +    def __init__(self):
 > +        super(InfoDebugMethod, self).__init__("info debug-method",
 > +                                               gdb.COMMAND_DATA)
 > +
 > +    def invoke(self, arg, from_tty):
 > +        locus_re, matcher_re, name_re = parse_dm_command_args(arg)
 > +        print_dm_info(get_global_method_matchers(locus_re, matcher_re),
 > +                      name_re)
 > +        print_dm_info(
 > +            get_method_matchers_in_loci(
 > +                gdb.progspaces(), locus_re, matcher_re),
 > +            name_re)
 > +        print_dm_info(
 > +            get_method_matchers_in_loci(gdb.objfiles(), locus_re, matcher_re),
 > +            name_re)
 > +
 > +
 > +class EnableDebugMethod(gdb.Command):
 > +    """GDB command to enable a specified (group of) debug method(s).
 > +
 > +    Usage: enable debug-method [locus-regexp [name-regexp]]
 > +
 > +    LOCUS-REGEXP is a regular expression matching the location of the debug
 > +    methods.  If it is omitted, all registered debug methods from all loci
 > +    are enabled.  A locus could be 'global', a regular expression matching
 > +    filenames of program spaces or a regular expression matching filenames of
 > +    objfiles.

Ditto.  E.g.,
"A locus could be 'global', 'progspace', or a regular expression matching
filenames of objfiles."

 > +
 > +    NAME-REGEXP is a regular expression matching the names of debug methods
 > +    within a given locus.  If this omitted for a specified locus, then all
 > +    registered debug method matchers in the locus are enabled.  To enable only
 > +    a certain debug methods managed by a single matcher, the name regexp can be
 > +    specified as matcher-name-regexp;debug-method-name-regexp.
 > +    """
 > +
 > +    def __init__(self):
 > +        super(EnableDebugMethod, self).__init__("enable debug-method",
 > +                                                gdb.COMMAND_DATA)
 > +
 > +    def invoke(self, arg, from_tty):
 > +        set_dm_status(arg, True)
 > +
 > +
 > +class DisableDebugMethod(gdb.Command):
 > +    """GDB command to disable a specified (group of) debug method(s).
 > +
 > +    Usage: disable debug-method [locus-regexp [name-regexp]]
 > +
 > +    LOCUS-REGEXP is a regular expression matching the location of the debug
 > +    methods.  If it is omitted, all registered debug methods from all loci
 > +    are disabled.  A locus could be 'global', a regular expression matching
 > +    filenames of program spaces or a regular expression matching filenames of
 > +    objfiles.
 > +
 > +    NAME-REGEXP is a regular expression matching the names of debug methods
 > +    within a given locus.  If this omitted for a specified locus, then all
 > +    registered debug method matchers in the locus are disabled.  To disable
 > +    only a certain debug methods managed by a single matcher, the name regexp
 > +    can be specified as matcher-name-regexp;debug-method-name-regexp.
 > +    """
 > +
 > +    def __init__(self):
 > +        super(DisableDebugMethod, self).__init__("disable debug-method",
 > +                                                gdb.COMMAND_DATA)
 > +
 > +    def invoke(self, arg, from_tty):
 > +        set_dm_status(arg, False)
 > +
 > +
 > +def register_debug_method_commands():
 > +    """Installs the debug method commands."""
 > +    InfoDebugMethod()
 > +    EnableDebugMethod()
 > +    DisableDebugMethod()
 > +
 > +
 > +register_debug_method_commands()
 > diff --git a/gdb/python/lib/gdb/debug_method.py b/gdb/python/lib/gdb/debug_method.py
 > new file mode 100644
 > index 0000000..2f6a5b4
 > --- /dev/null
 > +++ b/gdb/python/lib/gdb/debug_method.py
 > @@ -0,0 +1,255 @@
 > +# Python side of the support for debug methods.
 > +# 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/>.
 > +
 > +"""Utilities for defining debug methods"""
 > +
 > +import gdb
 > +import re
 > +import sys
 > +
 > +
 > +if sys.version_info[0] > 2:
 > +    # Python 3 removed basestring and long
 > +    basestring = str
 > +    long = int
 > +
 > +
 > +class DebugMethod(object):
 > +    """Base class (or a prototype) for debug method description.
 > +
 > +    Currently, the description only requires only 'name' and 'enabled'
 > +    attributes.  Description objects are managed by 'DebugMethodMatcher'
 > +    objects (see below).
 > +
 > +    Attributes:
 > +        name: The name of the debug method.
 > +        enabled: A boolean indicating if the debug method is enabled.
 > +    """
 > +
 > +    def __init__(self, name):
 > +        self.name = name
 > +        self.enabled = True
 > +
 > +
 > +class DebugMethodMatcher(object):
 > +    """Abstract base class for matching a debug method.
 > +
 > +    When looking for debug methods, GDB invokes the `match' method of a
 > +    registered debug method matcher to match the object type and method name.
 > +    The `match' method in concrete classes derived from this class should
 > +    return a `DebugMethodWorker' object, or a list of `DebugMethodWorker'
 > +    objects if there is a match (see below for 'DebugMethodWorker' class).
 > +
 > +    Attributes:
 > +        name: The name of the matcher.
 > +        enabled: A boolean indicating if the matcher is enabled.
 > +        debug_methods: A sequence of objects of type 'DebugMethod', or objects
 > +            which have at least the attributes of a 'DebugMethod' object.
 > +            This list is used by the 'enable'/'disable'/'info' commands to
 > +            enable/disable/list the debug methods registered with GDB.  See
 > +            the 'match' method below to know how this sequence is used.
 > +    """
 > +
 > +    def __init__(self, name):
 > +        """
 > +        Args:
 > +            name: An identifying name for the debug method or the group of
 > +                  debug methods returned by the `match' method.
 > +        """
 > +        self.name = name
 > +        self.enabled = True
 > +        self.methods = None
 > +
 > +    def match(self, class_type, method_name):
 > +        """Match class type and method name.
 > +
 > +        In derived classes, it should return a DebugMethodWorker object, or a
 > +        sequence of 'DebugMethodWorker' objects.  Only those debug method
 > +        workers whose corresponding 'DebugMethod' descriptor object is enabled
 > +        should be returned.
 > +
 > +        Args:
 > +            class_type: The class type (gdb.Type object) to match.
 > +            method_name: The name (string) of the method to match.
 > +        """
 > +        raise NotImplementedError("DebugMethodMatcher match")
 > +
 > +
 > +class DebugMethodWorker(object):
 > +    """Base class for all debug method workers defined in Python.
 > +
 > +    A debug method worker is an object which matches the method arguments, and
 > +    invokes the method when GDB wants it to.  Internally, GDB first invokes the
 > +    'get_arg_types' method to perform overload resolution.  If GDB selects to
 > +    invoke this Python debug method, then it invokes it via the overridden
 > +    'invoke' method.
 > +
 > +    Derived classes should override the 'get_arg_types' and 'invoke' methods.
 > +    """
 > +
 > +    def get_arg_types(self):
 > +        """Return arguments types of a debug method.
 > +
 > +        A sequence of gdb.Type objects corresponding to the arguments of the
 > +        debug method are returned.  If the debug method takes no arguments,
 > +        then returns 'None' or an empty sequence.  If the debug method takes
 > +        only a single argument, then a gdb.Type object or a sequence with a
 > +        single gdb.Type element is returned.
 > +        """
 > +        raise NotImplementedError("DebugMethod get_arg_types")
 > +
 > +    def invoke(self, obj, args):
 > +        """Invoke the debug method.
 > +
 > +        Args:
 > +            obj: The gdb.Value of the object on which the method is to be
 > +                 invoked.

Hmmm, what if the method is a static method?

 > +            args: The tuple of arguments to the method.  Each element of the
 > +                  tuple is a gdb.Value object.
 > +
 > +        Returns:
 > +            A gdb.Value corresponding to the value returned by the debug
 > +            method.  Returns 'None' if the method does not return anything.
 > +        """
 > +        raise NotImplementedError("DebugMethod invoke")
 > +
 > +
 > +class SimpleDebugMethodMatcher(DebugMethodMatcher):
 > +    """A utility class to implement simple debug method mathers and workers.
 > +
 > +    See the __init__ method below for information on how instances of this
 > +    class can be used.
 > +
 > +    For simple classes and methods, one can choose to use this class.  For
 > +    complex debug methods, which need to replace/implement template methods on
 > +    possibly template classes, one should implement their own debug method
 > +    matchers and workers.  See py-debugmethods.py in testsuite/gdb.python
 > +    directory of the GDB source tree for examples.
 > +    """
 > +
 > +    class SimpleDebugMethodWorker(DebugMethodWorker):
 > +        def __init__(self, method_function, arg_types):
 > +            self._arg_types = arg_types
 > +            self._method_function = method_function
 > +
 > +        def get_arg_types(self):
 > +            return self._arg_types
 > +
 > +        def invoke(self, obj, args):
 > +            return self._method_function(obj, *args)
 > +
 > +
 > +    def __init__(self, name, class_matcher, method_matcher, method_function,
 > +                 *arg_types):
 > +        """
 > +        Args:
 > +            name: Name of the debug method matcher.
 > +            class_matcher: A regular expression used to match the name of the
 > +                class whose method this debug method is implementing/replacing.
 > +            method_matcher: A regular expression used to match the name of the
 > +                method this debug method is implementing/replacing.
 > +            method_function: A Python callable which would be called via the
 > +                'invoke' method of the worker returned by the objects of this
 > +                class.  This callable should accept the object (*this) as the
 > +                first argument followed by the rest of the arguments to the
 > +                method. All arguments to this function should be gdb.Value
 > +                objects.
 > +            arg_types: The gdb.Type objects corresponding to the arguments that
 > +                this debug method takes. It can be None, or an empty sequence,
 > +                or a single gdb.Type object, or a sequence of gdb.Type objects.
 > +        """
 > +        DebugMethodMatcher.__init__(self, name)
 > +        assert callable(method_function), (
 > +            "The 'method_function' argument to 'SimpleDebugMethodMatcher' "
 > +            "__init__ method should be a callable.")
 > +        self._method_function = method_function
 > +        self._class_matcher = class_matcher
 > +        self._method_matcher = method_matcher
 > +        self._arg_types = arg_types
 > +
 > +    def match(self, class_type, method_name):
 > +        cm = re.match(self._class_matcher, str(class_type.unqualified().tag))
 > +        mm = re.match(self._method_matcher, method_name)
 > +        if cm and mm:
 > +            return SimpleDebugMethodMatcher.SimpleDebugMethodWorker(
 > +                self._method_function, self._arg_types)
 > +
 > +
 > +# A helper function for register_debug_method_matcher which returns an error
 > +# object if MATCHER is not having the requisite attributes in the proper
 > +# format.
 > +
 > +def validate_debug_method_matcher(matcher):
 > +    if not isinstance(matcher, DebugMethodMatcher):
 > +        return TypeError("Debug method matcher is not an instance of "
 > +                         "'DebugMethodMatcher'")

I think(!) Python's "duck typing" would argue against having this check.
It doesn't have to be an instance of DebugMethodMatcher, it just has to
look and behave like one.

 > +    if not hasattr(matcher, "name"):
 > +        return TypeError("Debug method matcher is missing attribute: name")
 > +    if not hasattr(matcher, "enabled"):
 > +        return TypeError("Debug method matcher is missing attribute: enabled")
 > +    if not isinstance(matcher.name, basestring):
 > +        return TypeError("Attribute 'name' of debug method matcher is not a "
 > +                         "string")
 > +    if matcher.name.find(";") >= 0:
 > +        return ValueError("Debug method matcher name cannot contain ';' in it")
 > +
 > +
 > +# A helper function for register_debug_method_matcher which looks up a debug
 > +# method matcher with NAME in LOCUS.  Returns the index of the debug method
 > +# matcher in 'debug_methods' sequence attribute of the LOCUS.
 > +
 > +def lookup_debug_method_matcher(locus, name):
 > +    i = 0
 > +    for m in locus.debug_methods:
 > +        if m.name == name:
 > +            return i
 > +        i = i + 1

What if not found?

 > +
 > +
 > +def register_debug_method_matcher(locus, matcher, replace=False):
 > +    """Registers a debug method matcher MATCHER with a LOCUS.
 > +
 > +    Arguments:
 > +        locus: The locus in which the debug methods should be registered.  It
 > +               can be 'None' to indicate that the debug methods should be
 > +               registered globally. Or, it could be a gdb.Objfile or a
 > +               gdb.Progspace object in which the debug methods should be
 > +               registered.
 > +        matcher: The debug method matcher to register with the LOCUS.  It
 > +            should be an instance of 'DebugMethodMatcher' class.
 > +        replace: If True, replace any existing debug method matcher with the
 > +            same name in the locus.  Otherwise, if a matcher with the same name
 > +            exists in the locus, raise an exception.
 > +    """
 > +    err = validate_debug_method_matcher(matcher)
 > +    if err:
 > +        raise err
 > +    if not locus:
 > +        locus = gdb
 > +    if locus == gdb:
 > +        locus_name = "global"
 > +    else:
 > +        locus_name = locus.filename
 > +    index = lookup_debug_method_matcher(locus, matcher.name)
 > +    if index:
 > +        if replace:
 > +            del locus.debug_methods[index]
 > +        else:
 > +            raise RuntimeError("Debug method matcher already registered with "
 > +                               "%s: %s" % (locus_name, new_method.name))
 > +    if gdb.parameter("verbose"):
 > +        gdb.write("Registering debug method matcher '%s' with %s' ...\n")
 > +    locus.debug_methods.insert(0, matcher)
 > diff --git a/gdb/python/py-debugmethods.c b/gdb/python/py-debugmethods.c
 > new file mode 100644
 > index 0000000..37d2714
 > --- /dev/null
 > +++ b/gdb/python/py-debugmethods.c
 > @@ -0,0 +1,648 @@
 > +/* Support for debug methods in Python.
 > +
 > +   Copyright (C) 2013-2014 Free Software Foundation, Inc.
 > +
 > +   This file is part of GDB.
 > +
 > +   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/>.  */
 > +
 > +#include "defs.h"
 > +#include "arch-utils.h"
 > +#include "extension-priv.h"
 > +#include "objfiles.h"
 > +#include "value.h"
 > +#include "language.h"
 > +
 > +#include "python.h"
 > +#include "python-internal.h"
 > +
 > +static const char enabled_field_name[] = "enabled";
 > +static const char match_method_name[] = "match";
 > +static const char get_arg_types_method_name[] = "get_arg_types";
 > +static const char invoke_method_name[] = "invoke";
 > +static const char matchers_attr_str[] = "debug_methods";
 > +
 > +static PyObject *py_match_method_name = NULL;
 > +static PyObject *py_get_arg_types_method_name = NULL;
 > +static PyObject *py_invoke_method_name = NULL;
 > +
 > +struct gdbpy_worker_data
 > +{
 > +  PyObject *worker;
 > +  PyObject *this_type;
 > +};
 > +
 > +static struct debug_method_worker *new_python_debug_method_worker
 > +  (PyObject *item, PyObject *py_obj_type);
 > +
 > +/* Implementation of free_debug_method_worker_data for Python.  */
 > +
 > +void
 > +gdbpy_free_debug_method_worker_data
 > +  (const struct extension_language_defn *extlang, void *data)
 > +{
 > +  struct gdbpy_worker_data *worker_data = data;
 > +
 > +  gdb_assert (worker_data->worker != NULL && worker_data->this_type != NULL);
 > +
 > +  Py_DECREF (worker_data->worker);
 > +  Py_DECREF (worker_data->this_type);
 > +  xfree (worker_data);
 > +}
 > +
 > +/* Implementation of clone_debug_method_worker_data for Python.  */
 > +
 > +void *
 > +gdbpy_clone_debug_method_worker_data
 > +  (const struct extension_language_defn *extlang, void *data)
 > +{
 > +  struct gdbpy_worker_data *worker_data = data, *new_data;
 > +
 > +  gdb_assert (worker_data->worker != NULL && worker_data->this_type != NULL);
 > +
 > +  new_data = XCNEW (struct gdbpy_worker_data);
 > +  new_data->worker = worker_data->worker;
 > +  new_data->this_type = worker_data->this_type;
 > +  Py_INCREF (new_data->worker);
 > +  Py_INCREF (new_data->this_type);
 > +
 > +  return new_data;
 > +}
 > +
 > +/* Invoke the "match" method of the MATCHER and return a new reference
 > +   to the result.  Returns NULL on error.  */
 > +
 > +static PyObject *
 > +invoke_match_method (PyObject *matcher, PyObject *py_obj_type,
 > +		     const char *debug_method_name)
 > +{
 > +  PyObject *py_debug_method_name;
 > +  PyObject *match_method, *enabled_field, *match_result;
 > +  struct cleanup *cleanups;
 > +  int enabled;
 > +
 > +  cleanups = make_cleanup (null_cleanup, NULL);
 > +
 > +  enabled_field = PyObject_GetAttrString (matcher, enabled_field_name);
 > +  if (enabled_field == NULL)
 > +    {
 > +      do_cleanups (cleanups);
 > +      return NULL;
 > +    }
 > +  make_cleanup_py_decref (enabled_field);
 > +
 > +  enabled = PyObject_IsTrue (enabled_field);
 > +  if (enabled == -1)
 > +    {
 > +      do_cleanups (cleanups);
 > +      return NULL;
 > +    }
 > +  if (enabled == 0)
 > +    {
 > +      /* Return 'None' if the matcher is not enabled.  */
 > +      do_cleanups (cleanups);
 > +      Py_RETURN_NONE;
 > +    }
 > +
 > +  match_method = PyObject_GetAttrString (matcher, match_method_name);
 > +  if (match_method == NULL)
 > +    {
 > +      do_cleanups (cleanups);
 > +      return NULL;
 > +    }
 > +  make_cleanup_py_decref (match_method);
 > +
 > +  py_debug_method_name = PyString_FromString (debug_method_name);
 > +  if (py_debug_method_name == NULL)
 > +    {
 > +      do_cleanups (cleanups);
 > +      return NULL;
 > +    }
 > +  make_cleanup_py_decref (py_debug_method_name);
 > +
 > +  match_result = PyObject_CallMethodObjArgs (matcher,
 > +					     py_match_method_name,
 > +					     py_obj_type,
 > +					     py_debug_method_name,
 > +					     NULL);
 > +
 > +  do_cleanups (cleanups);
 > +
 > +  return match_result;
 > +}
 > +
 > +/* Implementation of get_matching_debug_method_workers for Python.  */
 > +
 > +enum ext_lang_rc
 > +gdbpy_get_matching_debug_method_workers
 > +  (const struct extension_language_defn *extlang,
 > +   struct type *obj_type, const char *method_name,
 > +   debug_method_worker_vec **dm_vec)
 > +{
 > +  struct cleanup *cleanups;
 > +  struct objfile *objfile;
 > +  VEC (debug_method_worker_ptr) *worker_vec = NULL;
 > +  PyObject *py_type, *py_progspace;
 > +  PyObject *py_debug_method_matcher_list = NULL, *list_iter, *matcher;
 > +
 > +  gdb_assert (obj_type != NULL && method_name != NULL);
 > +
 > +  cleanups = ensure_python_env (get_current_arch (), current_language);
 > +
 > +  py_type = type_to_type_object (obj_type);
 > +  if (py_type == NULL)
 > +    {
 > +      gdbpy_print_stack ();
 > +      do_cleanups (cleanups);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }
 > +  make_cleanup_py_decref (py_type);
 > +
 > +  /* Create an empty list of debug methods.  */
 > +  py_debug_method_matcher_list = PyList_New (0);
 > +  if (py_debug_method_matcher_list == NULL)
 > +    {
 > +      gdbpy_print_stack ();
 > +      do_cleanups (cleanups);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }

IWBN to tweak the following code so that we can register a cleanup for
py_debug_method_matcher_list here.  But it's not critical.

 > +
 > +  /* Gather debug method matchers registered with the object files.  */
 > +  ALL_OBJFILES (objfile)
 > +    {
 > +      PyObject *py_objfile = objfile_to_objfile_object (objfile);
 > +      PyObject *objfile_matchers, *temp = py_debug_method_matcher_list;
 > +
 > +      if (py_objfile == NULL)
 > +	{
 > +	  gdbpy_print_stack ();
 > +	  Py_DECREF (py_debug_method_matcher_list);
 > +	  do_cleanups (cleanups);
 > +
 > +	  return EXT_LANG_RC_ERROR;
 > +	}
 > +
 > +      objfile_matchers = objfpy_get_debug_methods (py_objfile, NULL);
 > +      py_debug_method_matcher_list = PySequence_Concat (temp,
 > +							objfile_matchers);
 > +      Py_DECREF (temp);
 > +      Py_DECREF (objfile_matchers);
 > +      if (py_debug_method_matcher_list == NULL)
 > +	{
 > +	  gdbpy_print_stack ();
 > +	  do_cleanups (cleanups);
 > +
 > +	  return EXT_LANG_RC_ERROR;
 > +	}
 > +    }
 > +
 > +  /* Gather debug methods matchers registered with the current program
 > +     space.  */
 > +  py_progspace = pspace_to_pspace_object (current_program_space);
 > +  if (py_progspace != NULL)
 > +    {
 > +      PyObject *temp = py_debug_method_matcher_list;
 > +      PyObject *pspace_matchers = pspy_get_debug_methods (py_progspace, NULL);
 > +
 > +      py_debug_method_matcher_list = PySequence_Concat (temp, pspace_matchers);
 > +      Py_DECREF (temp);
 > +      Py_DECREF (pspace_matchers);
 > +      if (py_debug_method_matcher_list == NULL)
 > +	{
 > +	  gdbpy_print_stack ();
 > +	  do_cleanups (cleanups);
 > +
 > +	  return EXT_LANG_RC_ERROR;
 > +	}
 > +    }
 > +  else
 > +    {
 > +      gdbpy_print_stack ();
 > +      Py_DECREF (py_debug_method_matcher_list);
 > +      do_cleanups (cleanups);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }
 > +
 > +  /* Gather debug method matchers registered globally.  */
 > +  if (gdb_python_module != NULL
 > +      && PyObject_HasAttrString (gdb_python_module, matchers_attr_str))
 > +    {
 > +      PyObject *gdb_matchers;
 > +      PyObject *temp = py_debug_method_matcher_list;
 > +
 > +      gdb_matchers = PyObject_GetAttrString (gdb_python_module,
 > +					     matchers_attr_str);
 > +      if (gdb_matchers != NULL)
 > +	{
 > +	  py_debug_method_matcher_list = PySequence_Concat (temp,
 > +							   gdb_matchers);
 > +	  Py_DECREF (temp);
 > +	  Py_DECREF (gdb_matchers);
 > +	  if (py_debug_method_matcher_list == NULL)
 > +	    {
 > +	      gdbpy_print_stack ();
 > +	      do_cleanups (cleanups);
 > +
 > +	      return EXT_LANG_RC_ERROR;
 > +	    }
 > +	}
 > +      else
 > +	{
 > +	  gdbpy_print_stack ();
 > +	  Py_DECREF (py_debug_method_matcher_list);
 > +	  do_cleanups (cleanups);
 > +
 > +	  return EXT_LANG_RC_ERROR;
 > +	}
 > +    }
 > +
 > +  /* Safe to make a cleanup for py_debug_method_matcher_list now as it
 > +     will not change any more.  */
 > +  make_cleanup_py_decref (py_debug_method_matcher_list);
 > +
 > +  list_iter = PyObject_GetIter (py_debug_method_matcher_list);
 > +  if (list_iter == NULL)
 > +    {
 > +      gdbpy_print_stack ();
 > +      do_cleanups (cleanups);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }
 > +  while ((matcher = PyIter_Next (list_iter)) != NULL)
 > +    {
 > +      PyObject *match_result = invoke_match_method (matcher, py_type,
 > +						    method_name);
 > +
 > +      if (match_result == NULL)
 > +	{
 > +	  gdbpy_print_stack ();
 > +	  Py_DECREF (matcher);
 > +	  do_cleanups (cleanups);
 > +
 > +	  return EXT_LANG_RC_ERROR;
 > +	}
 > +      if (match_result == Py_None)
 > +	; /* This means there was no match.  */
 > +      else if (PySequence_Check (match_result))
 > +	{
 > +	  PyObject *iter = PyObject_GetIter (match_result);
 > +	  PyObject *py_worker;
 > +
 > +	  if (iter == NULL)
 > +	    {
 > +	      gdbpy_print_stack ();
 > +	      Py_DECREF (matcher);
 > +	      Py_DECREF (match_result);
 > +	      do_cleanups (cleanups);
 > +
 > +	      return EXT_LANG_RC_ERROR;
 > +	    }
 > +	  while ((py_worker = PyIter_Next (iter)) != NULL)
 > +	    {
 > +	      struct debug_method_worker *worker;
 > +
 > +	      worker = new_python_debug_method_worker (py_worker, py_type);
 > +	      VEC_safe_push (debug_method_worker_ptr, worker_vec, worker);
 > +	      Py_DECREF (py_worker);
 > +	    }
 > +	  Py_DECREF (iter);
 > +	  /* Report any error that could have occurred while iterating.  */
 > +	  if (PyErr_Occurred ())
 > +	    {
 > +	      gdbpy_print_stack ();
 > +	      Py_DECREF (matcher);
 > +	      Py_DECREF (match_result);
 > +	      do_cleanups (cleanups);
 > +
 > +	      return EXT_LANG_RC_ERROR;
 > +	    }
 > +	}
 > +      else
 > +	{
 > +	  struct debug_method_worker *worker;
 > +
 > +	  worker = new_python_debug_method_worker (match_result, py_type);
 > +	  VEC_safe_push (debug_method_worker_ptr, worker_vec, worker);
 > +	}
 > +
 > +      Py_XDECREF (match_result);

The code catches match_result == NULL above, so can we use Py_DECREF here?

 > +      Py_DECREF (matcher);
 > +    }
 > +  Py_DECREF (list_iter);
 > +  /* Report any error that could have occurred while iterating.  */
 > +  if (PyErr_Occurred ())
 > +    {
 > +      gdbpy_print_stack ();
 > +      do_cleanups (cleanups);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }
 > +
 > +  do_cleanups (cleanups);
 > +  *dm_vec = worker_vec;
 > +
 > +  return EXT_LANG_RC_OK;
 > +}
 > +
 > +/* Implementation of get_debug_method_arg_types for Python.  */
 > +
 > +enum ext_lang_rc
 > +gdbpy_get_debug_method_arg_types (const struct extension_language_defn *extlang,
 > +				  struct debug_method_worker *worker,
 > +				  int *nargs, struct type ***arg_types)
 > +{
 > +  struct gdbpy_worker_data *worker_data = worker->data;
 > +  PyObject *py_worker = worker_data->worker;
 > +  PyObject *get_arg_types_method;
 > +  PyObject *py_argtype_list, *list_iter = NULL, *item;
 > +  struct cleanup *cleanups;
 > +  struct type **type_array, *obj_type;
 > +  int i = 1, arg_count;
 > +
 > +  /* Set nargs to 0 so that any premature return from this function returns
 > +     0 arg types.  */
 > +  *nargs = 0;

How about a static method taking zero args?
Does the API for this function say an error return has *nargs = 0?
I'd expect not so how about just leaving *nargs unset at this point?
[Or initializing it to -1 is fine, I just don't want the reader to think
the value of *nargs is usable if there's an error.]

 > +
 > +  cleanups = ensure_python_env (get_current_arch (), current_language);
 > +
 > +  get_arg_types_method =  PyObject_GetAttrString (py_worker,
 > +						  get_arg_types_method_name);
 > +  if (get_arg_types_method == NULL)
 > +    {
 > +      gdbpy_print_stack ();
 > +      do_cleanups (cleanups);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }
 > +  make_cleanup_py_decref (get_arg_types_method);
 > +
 > +  py_argtype_list = PyObject_CallMethodObjArgs (py_worker,
 > +						py_get_arg_types_method_name,
 > +						NULL);
 > +  if (py_argtype_list == NULL)
 > +    {
 > +      gdbpy_print_stack ();
 > +      do_cleanups (cleanups);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }
 > +  make_cleanup_py_decref (py_argtype_list);
 > +  if (py_argtype_list == Py_None)
 > +    arg_count = 0;
 > +  else if (PySequence_Check (py_argtype_list))
 > +    {
 > +      arg_count = PySequence_Size (py_argtype_list);
 > +      if (arg_count == -1)
 > +	{
 > +	  gdbpy_print_stack ();
 > +	  do_cleanups (cleanups);
 > +
 > +	  return EXT_LANG_RC_ERROR;
 > +	}
 > +
 > +      list_iter = PyObject_GetIter (py_argtype_list);
 > +      if (list_iter == NULL)
 > +	{
 > +	  gdbpy_print_stack ();
 > +	  do_cleanups (cleanups);
 > +
 > +	  return EXT_LANG_RC_ERROR;
 > +	}
 > +      make_cleanup_py_decref (list_iter);
 > +    }
 > +  else
 > +    arg_count = 1;
 > +
 > +  /* Include the 'this' argument in the size.  */
 > +  type_array = XCNEWVEC (struct type *, arg_count + 1);
 > +  i = 1;
 > +  if (list_iter != NULL)
 > +    {
 > +      while ((item = PyIter_Next (list_iter)) != NULL)
 > +	{
 > +	  struct type *arg_type = type_object_to_type (item);
 > +
 > +	  Py_DECREF (item);
 > +	  if (arg_type == NULL)
 > +	    {
 > +	      PyErr_SetString (PyExc_TypeError,
 > +			       _("Arg type returned by the get_arg_types "
 > +				 "method of a debug method worker object is "
 > +				 "not a gdb.Type object."));
 > +	      break;
 > +	    }
 > +
 > +	  type_array[i] = arg_type;
 > +	  i++;
 > +	}
 > +    }
 > +  else if (arg_count == 1)
 > +    {
 > +      /* py_argtype_list is not actually a list but a single gdb.Type
 > +	 object.  */
 > +      struct type *arg_type = type_object_to_type (py_argtype_list);
 > +
 > +      if (arg_type == NULL)
 > +	PyErr_SetString (PyExc_TypeError,
 > +			 _("Arg type returned by the get_arg_types method "
 > +			   "of a debug method worker object is not a gdb.Type "
 > +			   "object."));
 > +      else
 > +	{
 > +	  type_array[1] = arg_type;
 > +	  i++;
 > +	}
 > +    }
 > +  if (PyErr_Occurred ())
 > +    {
 > +      gdbpy_print_stack ();
 > +      do_cleanups (cleanups);
 > +      xfree (type_array);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }
 > +
 > +  /* Add the type of 'this' as the first argument.  */

Ah, so the current patch doesn't support static methods?
I think that's ok for a first pass, as long as the API does not in any
way preclude (or make hard) adding support later.

 > +  obj_type = type_object_to_type (worker_data->this_type);
 > +  type_array[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type), NULL);
 > +  *nargs = i;
 > +  *arg_types = type_array;
 > +  do_cleanups (cleanups);
 > +
 > +  return EXT_LANG_RC_OK;
 > +}
 > +
 > +/* Implementation of invoke_debug_method for Python.  */
 > +
 > +enum ext_lang_rc
 > +gdbpy_invoke_debug_method (const struct extension_language_defn *extlang,
 > +			   struct debug_method_worker *worker,
 > +			   struct value *obj, struct value **args, int nargs,
 > +			   struct value **result)
 > +{
 > +  int i;
 > +  struct cleanup *cleanups;
 > +  PyObject *py_value_obj, *py_arg_tuple, *py_result;
 > +  PyObject *invoke_method;
 > +  struct type *obj_type, *this_type;
 > +  struct value *res = NULL;
 > +  struct gdbpy_worker_data *worker_data = worker->data;
 > +  PyObject *debug_method_worker = worker_data->worker;
 > +
 > +  cleanups = ensure_python_env (get_current_arch (), current_language);
 > +
 > +  invoke_method =  PyObject_GetAttrString (debug_method_worker,
 > +					   invoke_method_name);
 > +  if (invoke_method == NULL)
 > +    {
 > +      gdbpy_print_stack ();
 > +      do_cleanups (cleanups);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }
 > +  make_cleanup_py_decref (invoke_method);
 > +
 > +  obj_type = check_typedef (value_type (obj));
 > +  this_type = check_typedef (type_object_to_type (worker_data->this_type));
 > +  if (TYPE_CODE (obj_type) == TYPE_CODE_PTR)
 > +    {
 > +      struct type *this_ptr = lookup_pointer_type (this_type);
 > +
 > +      if (!types_equal (obj_type, this_ptr))
 > +	obj = value_cast (this_ptr, obj);
 > +    }
 > +  else if (TYPE_CODE (obj_type) == TYPE_CODE_REF)
 > +    {
 > +      struct type *this_ref = lookup_reference_type (this_type);
 > +
 > +      if (!types_equal (obj_type, this_ref))
 > +	obj = value_cast (this_ref, obj);
 > +    }
 > +  else
 > +    {
 > +      if (!types_equal (obj_type, this_type))
 > +	obj = value_cast (this_type, obj);
 > +    }
 > +  py_value_obj = value_to_value_object (obj);
 > +  if (py_value_obj == NULL)
 > +    {
 > +      gdbpy_print_stack ();
 > +      do_cleanups (cleanups);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }
 > +  make_cleanup_py_decref (py_value_obj);
 > +
 > +  py_arg_tuple = PyTuple_New (nargs);
 > +  if (py_arg_tuple == NULL)
 > +    {
 > +      gdbpy_print_stack ();
 > +      do_cleanups (cleanups);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }
 > +  make_cleanup_py_decref (py_arg_tuple);
 > +
 > +  for (i = 0; i < nargs; i++)
 > +    {
 > +      PyObject *py_value_arg = value_to_value_object (args[i]);
 > +
 > +      if (py_value_arg == NULL)
 > +	{
 > +	  gdbpy_print_stack ();
 > +	  do_cleanups (cleanups);
 > +
 > +	  return EXT_LANG_RC_ERROR;
 > +	}
 > +
 > +      PyTuple_SET_ITEM (py_arg_tuple, i, py_value_arg);
 > +    }
 > +
 > +  py_result = PyObject_CallMethodObjArgs (debug_method_worker,
 > +					  py_invoke_method_name,
 > +					  py_value_obj,
 > +					  py_arg_tuple, NULL);
 > +  if (py_result == NULL)
 > +    {
 > +      gdbpy_print_stack ();
 > +      do_cleanups (cleanups);
 > +
 > +      return EXT_LANG_RC_ERROR;
 > +    }
 > +  make_cleanup_py_decref (py_result);
 > +
 > +  if (py_result != Py_None)
 > +    {
 > +      res = convert_value_from_python (py_result);
 > +      if (res == NULL)
 > +	{
 > +	  gdbpy_print_stack ();
 > +	  do_cleanups (cleanups);
 > +
 > +	  return EXT_LANG_RC_ERROR;
 > +	}
 > +    }
 > +  else
 > +    {
 > +      res = allocate_value (lookup_typename (python_language, python_gdbarch,
 > +					     "void", NULL, 0));
 > +    }
 > +
 > +  *result = res;
 > +  do_cleanups (cleanups);
 > +
 > +  return EXT_LANG_RC_OK;
 > +}
 > +
 > +/* Creates a new Python debug_method_worker object.
 > +   The new object has data of type 'struct gdbpy_worker_data' composed
 > +   with the components PY_WORKER and THIS_TYPE.  */
 > +
 > +static struct debug_method_worker *
 > +new_python_debug_method_worker (PyObject *py_worker, PyObject *this_type)
 > +{
 > +  struct gdbpy_worker_data *data;
 > +
 > +  gdb_assert (py_worker != NULL && this_type != NULL);
 > +
 > +  data = XCNEW (struct gdbpy_worker_data);
 > +  data->worker = py_worker;
 > +  data->this_type = this_type;
 > +  Py_INCREF (py_worker);
 > +  Py_INCREF (this_type);
 > +
 > +  return new_debug_method_worker (&extension_language_python, data);
 > +}
 > +
 > +int
 > +gdbpy_initialize_debug_methods (void)
 > +{
 > +  py_match_method_name = PyString_FromString (match_method_name);
 > +  if (py_match_method_name == NULL)
 > +    return -1;
 > +
 > +  py_invoke_method_name = PyString_FromString (invoke_method_name);
 > +  if (py_invoke_method_name == NULL)
 > +    return -1;
 > +
 > +  py_get_arg_types_method_name
 > +    = PyString_FromString (get_arg_types_method_name);
 > +  if (py_get_arg_types_method_name == NULL)
 > +    return -1;
 > +
 > +  return 1;
 > +}
 > diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
 > index 97fb0be..91b88a9 100644
 > --- a/gdb/python/py-objfile.c
 > +++ b/gdb/python/py-objfile.c
 > @@ -37,6 +37,9 @@ typedef struct
 >    PyObject *frame_filters;
 >    /* The type-printer list.  */
 >    PyObject *type_printers;
 > +
 > +  /* The debug method matcher list.  */
 > +  PyObject *debug_methods;
 >  } objfile_object;
 >  
 >  static PyTypeObject objfile_object_type
 > @@ -67,6 +70,7 @@ objfpy_dealloc (PyObject *o)
 >    Py_XDECREF (self->printers);
 >    Py_XDECREF (self->frame_filters);
 >    Py_XDECREF (self->type_printers);
 > +  Py_XDECREF (self->debug_methods);
 >    Py_TYPE (self)->tp_free (self);
 >  }
 >  
 > @@ -99,6 +103,13 @@ objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
 >  	  Py_DECREF (self);
 >  	  return NULL;
 >  	}
 > +
 > +      self->debug_methods = PyList_New (0);
 > +      if (self->debug_methods == NULL)
 > +	{
 > +	  Py_DECREF (self);
 > +	  return NULL;
 > +	}
 >      }
 >    return (PyObject *) self;
 >  }
 > @@ -193,6 +204,17 @@ objfpy_get_type_printers (PyObject *o, void *ignore)
 >    return self->type_printers;
 >  }
 >  
 > +/* Get the 'debug_methods' attribute.  */
 > +
 > +PyObject *
 > +objfpy_get_debug_methods (PyObject *o, void *ignore)
 > +{
 > +  objfile_object *self = (objfile_object *) o;
 > +
 > +  Py_INCREF (self->debug_methods);
 > +  return self->debug_methods;
 > +}
 > +
 >  /* Set the 'type_printers' attribute.  */
 >  
 >  static int
 > @@ -292,6 +314,13 @@ objfile_to_objfile_object (struct objfile *objfile)
 >  	      return NULL;
 >  	    }
 >  
 > +	  object->debug_methods = PyList_New (0);
 > +	  if (object->debug_methods == NULL)
 > +	    {
 > +	      Py_DECREF (object);
 > +	      return NULL;
 > +	    }
 > +
 >  	  set_objfile_data (objfile, objfpy_objfile_data_key, object);
 >  	}
 >      }
 > @@ -333,6 +362,8 @@ static PyGetSetDef objfile_getset[] =
 >      objfpy_set_frame_filters, "Frame Filters.", NULL },
 >    { "type_printers", objfpy_get_type_printers, objfpy_set_type_printers,
 >      "Type printers.", NULL },
 > +  { "debug_methods", objfpy_get_debug_methods, NULL,
 > +    "Debug methods.", NULL },
 >    { NULL }
 >  };
 >  
 > diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
 > index cda5a86..0afd7b7 100644
 > --- a/gdb/python/py-progspace.c
 > +++ b/gdb/python/py-progspace.c
 > @@ -39,6 +39,9 @@ typedef struct
 >    PyObject *frame_filters;
 >    /* The type-printer list.  */
 >    PyObject *type_printers;
 > +
 > +  /* The debug method list.  */
 > +  PyObject *debug_methods;
 >  } pspace_object;
 >  
 >  static PyTypeObject pspace_object_type
 > @@ -75,6 +78,7 @@ pspy_dealloc (PyObject *self)
 >    Py_XDECREF (ps_self->printers);
 >    Py_XDECREF (ps_self->frame_filters);
 >    Py_XDECREF (ps_self->type_printers);
 > +  Py_XDECREF (ps_self->debug_methods);
 >    Py_TYPE (self)->tp_free (self);
 >  }
 >  
 > @@ -107,6 +111,13 @@ pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
 >  	  Py_DECREF (self);
 >  	  return NULL;
 >  	}
 > +
 > +      self->debug_methods = PyList_New (0);
 > +      if (self->debug_methods == NULL)
 > +	{
 > +	  Py_DECREF (self);
 > +	  return NULL;
 > +	}
 >      }
 >    return (PyObject *) self;
 >  }
 > @@ -201,6 +212,17 @@ pspy_get_type_printers (PyObject *o, void *ignore)
 >    return self->type_printers;
 >  }
 >  
 > +/* Get the 'debug_methods' attribute.  */
 > +
 > +PyObject *
 > +pspy_get_debug_methods (PyObject *o, void *ignore)
 > +{
 > +  pspace_object *self = (pspace_object *) o;
 > +
 > +  Py_INCREF (self->debug_methods);
 > +  return self->debug_methods;
 > +}
 > +
 >  /* Set the 'type_printers' attribute.  */
 >  
 >  static int
 > @@ -288,6 +310,13 @@ pspace_to_pspace_object (struct program_space *pspace)
 >  	      return NULL;
 >  	    }
 >  
 > +	  object->debug_methods = PyList_New (0);
 > +	  if (object->debug_methods == NULL)
 > +	    {
 > +	      Py_DECREF (object);
 > +	      return NULL;
 > +	    }
 > +
 >  	  set_program_space_data (pspace, pspy_pspace_data_key, object);
 >  	}
 >      }
 > @@ -320,6 +349,8 @@ static PyGetSetDef pspace_getset[] =
 >      "Frame filters.", NULL },
 >    { "type_printers", pspy_get_type_printers, pspy_set_type_printers,
 >      "Type printers.", NULL },
 > +  { "debug_methods", pspy_get_debug_methods, NULL,
 > +    "Debug methods.", NULL },
 >    { NULL }
 >  };
 >  
 > diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
 > index 07650f7..2357d53 100644
 > --- a/gdb/python/python-internal.h
 > +++ b/gdb/python/python-internal.h
 > @@ -307,6 +307,25 @@ extern enum ext_lang_bp_stop gdbpy_breakpoint_cond_says_stop
 >    (const struct extension_language_defn *, struct breakpoint *);
 >  extern int gdbpy_breakpoint_has_cond (const struct extension_language_defn *,
 >  				      struct breakpoint *b);
 > +
 > +extern void *gdbpy_clone_debug_method_worker_data
 > +  (const struct extension_language_defn *extlang, void *data);
 > +extern void gdbpy_free_debug_method_worker_data
 > +  (const struct extension_language_defn *extlang, void *data);
 > +extern enum ext_lang_rc gdbpy_get_matching_debug_method_workers
 > +  (const struct extension_language_defn *extlang,
 > +   struct type *obj_type, const char *method_name,
 > +   debug_method_worker_vec **dm_vec);
 > +extern enum ext_lang_rc gdbpy_get_debug_method_arg_types
 > +  (const struct extension_language_defn *extlang,
 > +   struct debug_method_worker *worker,
 > +   int *nargs,
 > +   struct type ***arg_types);
 > +extern enum ext_lang_rc gdbpy_invoke_debug_method
 > +  (const struct extension_language_defn *extlang,
 > +   struct debug_method_worker *worker,
 > +   struct value *obj, struct value **args, int nargs,
 > +   struct value **result);
 >  
 >  PyObject *gdbpy_history (PyObject *self, PyObject *args);
 >  PyObject *gdbpy_breakpoints (PyObject *, PyObject *);
 > @@ -345,11 +364,13 @@ PyObject *pspace_to_pspace_object (struct program_space *)
 >      CPYCHECKER_RETURNS_BORROWED_REF;
 >  PyObject *pspy_get_printers (PyObject *, void *);
 >  PyObject *pspy_get_frame_filters (PyObject *, void *);
 > +PyObject *pspy_get_debug_methods (PyObject *, void *);
 >  
 >  PyObject *objfile_to_objfile_object (struct objfile *)
 >      CPYCHECKER_RETURNS_BORROWED_REF;
 >  PyObject *objfpy_get_printers (PyObject *, void *);
 >  PyObject *objfpy_get_frame_filters (PyObject *, void *);
 > +PyObject *objfpy_get_debug_methods (PyObject *, void *);
 >  
 >  PyObject *gdbarch_to_arch_object (struct gdbarch *gdbarch);
 >  
 > @@ -430,6 +451,8 @@ int gdbpy_initialize_new_objfile_event (void)
 >    CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 >  int gdbpy_initialize_arch (void)
 >    CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 > +int gdbpy_initialize_debug_methods (void)
 > +  CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 >  
 >  struct cleanup *make_cleanup_py_decref (PyObject *py);
 >  struct cleanup *make_cleanup_py_xdecref (PyObject *py);
 > diff --git a/gdb/python/python.c b/gdb/python/python.c
 > index cbfa73a..aeffe93 100644
 > --- a/gdb/python/python.c
 > +++ b/gdb/python/python.c
 > @@ -186,7 +186,13 @@ static const struct extension_language_ops python_extension_ops =
 >    gdbpy_set_quit_flag,
 >    gdbpy_check_quit_flag,
 >  
 > -  gdbpy_before_prompt_hook
 > +  gdbpy_before_prompt_hook,
 > +
 > +  gdbpy_clone_debug_method_worker_data,
 > +  gdbpy_free_debug_method_worker_data,
 > +  gdbpy_get_matching_debug_method_workers,
 > +  gdbpy_get_debug_method_arg_types,
 > +  gdbpy_invoke_debug_method
 >  };
 >  
 >  /* Architecture and language to be used in callbacks from
 > @@ -1752,7 +1758,8 @@ message == an error message without a stack will be printed."),
 >        || gdbpy_initialize_exited_event () < 0
 >        || gdbpy_initialize_thread_event () < 0
 >        || gdbpy_initialize_new_objfile_event ()  < 0
 > -      || gdbpy_initialize_arch () < 0)
 > +      || gdbpy_initialize_arch () < 0
 > +      || gdbpy_initialize_debug_methods () < 0)
 >      goto fail;
 >  
 >    gdbpy_to_string_cst = PyString_FromString ("to_string");

[testsuite elided as LGTM for that part]



More information about the Gdb-patches mailing list