From patchwork Tue Apr 1 22:54:31 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siva Chandra Reddy X-Patchwork-Id: 382 Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx21.g.dreamhost.com (peon2454.g.dreamhost.com [208.113.200.127]) by wilcox.dreamhost.com (Postfix) with ESMTP id 534923600D4 for ; Tue, 1 Apr 2014 15:54:44 -0700 (PDT) Received: by homiemail-mx21.g.dreamhost.com (Postfix, from userid 14314964) id F0F16FD8CAF; Tue, 1 Apr 2014 15:54:43 -0700 (PDT) X-Original-To: gdb@patchwork.siddhesh.in Delivered-To: x14314964@homiemail-mx21.g.dreamhost.com Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by homiemail-mx21.g.dreamhost.com (Postfix) with ESMTPS id BBCBE1017120 for ; Tue, 1 Apr 2014 15:54:43 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:date:message-id:subject:from:to :content-type; q=dns; s=default; b=plsvmZOmcRbasIcPDdUKHi+OonjDV kVmYX9ymAz8fAWi3wepmm+OiwKalT5ZorFHb+CjoG8czUFHyTEzocyVtfZDzFy2U TCVNc8wwhgUgbrVKCC4H7i+nF8/M7urKiuZ9p7KnrHKTYPeblUvtscn0ayXO4Ru4 VXUNLfudWJElRw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:date:message-id:subject:from:to :content-type; s=default; bh=7st0LHDc3YU5X6R+D6ixxmVkVcc=; b=YJ5 K0J6bMEEhrypHhS0AIiRVOHfgkJ54QgYsss5pFpXoGFsI/oGHqa0bCXHvc9OHE3V W76qiB+DLEVgv0KceYS+RLdzzvAoRBOCDob0kB0VO00wrKPZyCGpDKgrRx+D8L21 o1qy2W0rJ8bY2P8zKoZOGvnLUqH1EAgZe1aATDyI= Received: (qmail 648 invoked by alias); 1 Apr 2014 22:54:41 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 635 invoked by uid 89); 1 Apr 2014 22:54:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-we0-f178.google.com Received: from mail-we0-f178.google.com (HELO mail-we0-f178.google.com) (74.125.82.178) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 01 Apr 2014 22:54:34 +0000 Received: by mail-we0-f178.google.com with SMTP id u56so6823326wes.23 for ; Tue, 01 Apr 2014 15:54:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to :content-type; bh=LmwnjbAA9h36Qg3KagcJ0lS2DNcJt1F9FdqC71c+oeM=; b=kk+C2bkkInQvJ8lC8yCgG0c+1Orw+sadJgYA+FQCK+8XNa9a3IEj5JrKCGrTU93Qen h7FrrIxh1hkrQmiv+vN64bed+GL95DRrEx8yNIU45I+7dx1mQWKsQKpLrIL689gvKdSt eT5HmE8+I9LBLViwVcYbbmJ6E5lrp6xjE8iS2ljUUg9iJG6B+5gxiH1PO5U6e9fob4+B cnjyc/EzIUE8REsQu9p41FWfyblE8efKK5SFReoITwA79XflKEig76xnkEkMqxJ62O0q Z3UqJ4M48/B03DgXCSX5EnXTVyN4W/6US8T4c8xpY6bJLTffguyKotsk/5P3hf6c72yX iUKg== X-Gm-Message-State: ALoCoQlW7PqRXerZMEXbF4Jp3fFsYm3/6znQj3W6joFvNHj/J20vtuBpMvYn46sU4YrDLdI8tH7c5l9kjaJpKUaUcQ8VoCWzR5QQfSTFHpCCzM1h9wM+pvNP4Bl/fMYxkgXbQJGr0ygFBc99chzT/lu5n+0VpQrta3F9T9OR28Vg2utmqqgkpH4yhgC6+rVWpWvyzuZUT/XQZDLV3ftjk20wJI2rhb2Btw== MIME-Version: 1.0 X-Received: by 10.194.242.4 with SMTP id wm4mr7319102wjc.88.1396392871371; Tue, 01 Apr 2014 15:54:31 -0700 (PDT) Received: by 10.15.48.194 with HTTP; Tue, 1 Apr 2014 15:54:31 -0700 (PDT) Date: Tue, 1 Apr 2014 15:54:31 -0700 Message-ID: Subject: [Patch v12 4/4] Add debug method support to the Python API From: Siva Chandra To: gdb-patches X-IsSubscribed: yes X-DH-Original-To: gdb@patchwork.siddhesh.in This part adds the debug method support to the Python API. 2014-04-01 Siva Chandra Reddy * 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 . + +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: (, + , + ) + """ + 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. + 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), + 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. + + 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. + + 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 . + +"""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. + 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'") + 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 + + +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 . */ + +#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; + } + + /* 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); + 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; + + 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. */ + 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"); diff --git a/gdb/testsuite/gdb.python/py-debugmethods.cc b/gdb/testsuite/gdb.python/py-debugmethods.cc new file mode 100644 index 0000000..fa6ea7a --- /dev/null +++ b/gdb/testsuite/gdb.python/py-debugmethods.cc @@ -0,0 +1,194 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 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 . */ + +#include + +using namespace std; + +namespace dop +{ + +class A +{ + public: + int a; + int array [10]; + virtual ~A (); + int operator+ (const A &obj); + virtual int operator- (const A &obj); + virtual int geta (); +}; + +A::~A () { } + +int +A::operator+ (const A &obj) +{ + cout << "From CC :" << endl; + return a + obj.a; +} + +int A::operator- (const A &obj) +{ + cout << "From CC :" << endl; + return a - obj.a; +} + +int A::geta (void) +{ + cout << "From CC geta:" << endl; + return a; +} + +class B : public A +{ + public: + virtual int operator* (const B &obj); +}; + +int +B::operator* (const B &obj) +{ + cout << "From CC :" << endl; + return a * obj.a; +} + +typedef B Bt; + +typedef Bt Btt; + +class C : public Bt +{ + public: + virtual ~C(); +}; + +C::~C () { } + +class D : public B +{ + public: + /* This class overrides the virtual operator* defined in B. The + associated Python script replaces B::operator* but not D::operator*. + Hence, if we have a reference pointing to an instance of D, the C++ + version of D::operator* should be invoked and not the Python version + B::operator* even after loading the python script. */ + virtual int operator* (const B &obj); +}; + +int +D::operator* (const B &obj) +{ + cout << "From CC :" << endl; + return a * obj.a; +} + +class E : public A +{ + public: + /* This class has a member named 'a', while the base class also has a + member named 'a'. When one invokes A::geta(), A::a should be + returned and not E::a as the 'geta' method is defined on class 'A'. + This class tests this aspect of debug methods. */ + int a; +}; + +template +class G +{ + public: + template + int size_diff (); + + template + int size_mul (); + + template + T mul(const T1 t1); + + public: + T t; +}; + +template +template +int +G::size_diff () +{ + cout << "From CC G<>::size_diff:" << endl; + return sizeof (T1) - sizeof (T); +} + +template +template +int +G::size_mul () +{ + cout << "From CC G<>::size_mul:" << endl; + return M * sizeof (T); +} + +template +template +T +G::mul (const T1 t1) +{ + cout << "From CC G<>::mul:" << endl; + return t1 * t; +} + +} + +using namespace dop; + +int main(void) +{ + A a1, a2; + a1.a = 5; + a2.a = 10; + C c1; + c1.a = 20; + B b1; + b1.a = 30; + D d1; + d1.a = 50; + Bt bt; + bt.a = 40; + A &ref_c = c1; + B &ref_d = d1; + Btt btt; + btt.a = -5; + G g, *g_ptr; + g.t = 5; + g_ptr = &g; + E e; + e.a = 1000; + e.A::a = 100; + E *e_ptr = &e; + E &e_ref = e; + + int diff = g.size_diff (); + int smul = g.size_mul<2> (); + int mul = g.mul (1.0); + + for (int i = 0; i < 10; i++) + { + a1.array[i] = a2.array[i] = i; + } + + return 0; /* Break here. */ +} diff --git a/gdb/testsuite/gdb.python/py-debugmethods.exp b/gdb/testsuite/gdb.python/py-debugmethods.exp new file mode 100644 index 0000000..27f63d9 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-debugmethods.exp @@ -0,0 +1,133 @@ +# Copyright 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 . + +# This file is part of the GDB testsuite. It tests the debug methods +# feature in the Python extension language. + +load_lib gdb-python.exp + +if { [skip_cplus_tests] } { continue } + +standard_testfile py-debugmethods.cc + +if {[prepare_for_testing $testfile.exp $testfile $srcfile {debug c++}]} { + return -1 +} + +# Skip all tests if Python scripting is not enabled. +if { [skip_python_tests] } { continue } + +if ![runto_main] { + return -1 +} + +set debug_methods_script [gdb_remote_download host \ + ${srcdir}/${subdir}/${testfile}.py] + +gdb_breakpoint [gdb_get_line_number "Break here."] +gdb_continue_to_breakpoint "Break here" ".*Break here.*" + +# Tests before loading the debug methods. +gdb_test "p a1 + a2" "From CC .*15" "Before: a1 + a2" +gdb_test "p a2 - a1" "From CC .*5" "Before: a1 - a2" +gdb_test "p b1 - a1" "From CC .*25" "Before: b1 - a1" +gdb_test "p c1 - a1" "From CC .*15" "Before: c1 - a1" +gdb_test "p c1 - c1" "From CC .*0" "Before: c1 - c1" +gdb_test "p a1.geta()" "From CC geta.*5" "Before: a1.geta()" +gdb_test "p ref_c - a1" "From CC .*15" "Before: ref_c - a1" +gdb_test "p ref_c - c1" "From CC .*0" "Before: ref_c - c1" +gdb_test "p b1 * b1" "From CC .*900" "Before: b1 * b1" +gdb_test "p ref_c * b1" "No symbol.*" "Before: ref_c * b1" +gdb_test "p ref_d * b1" "From CC .*1500" "Before: ref_d * b1" +gdb_test "p bt * c1" "From CC .*800" "Before: bt * c1" +gdb_test "p ++a1" "No symbol.*" "Before: ++a1" +gdb_test "p a1.getarrayind(5)" "Couldn't find method.*" \ + "Before: a1.getarrayind(5)" +gdb_test "p e.geta()" "From CC geta.*100" "Before: e.geta()" +gdb_test "p g.size_diff()" "From CC G<>::size_diff.*" \ + "Before: g.size_diff()" +gdb_test "p g.size_diff()" "Couldn't find method.*" \ + "Before: g.size_diff()" +gdb_test "p g.size_mul<2>()" "From CC G<>::size_mul.*" \ + "Before: g.size_mul<2>()" +gdb_test "p g.size_mul<5>()" "Couldn't find method.*" \ + "Before: g.size_mul<5>()" +gdb_test "p g.mul(2.0)" "From CC G<>::mul.*" \ + "Before: g.mul(2.0)" +gdb_test "p g.mul('a')" "Couldn't find method.*" \ + "Before: g.mul('a')" + +# Load the script which adds the debug methods. +gdb_test_no_output "source ${debug_methods_script}" "load the script file" + +# Tests after loading debug methods. +gdb_test "p a1 + a2" "From Python .*15" "After: a1 + a2" +gdb_test "p a2 - a1" "From CC .*5" "After: a1 - a2" +gdb_test "p b1 - a1" "From CC .*25" "After: b1 - a2" +gdb_test "p c1 - a1" "From CC .*15" "After: c1 - a1" +gdb_test "p c1 - c1" "From Python .*0" "After: c1 - c1" +gdb_test "p a1.geta()" "From Python .*5" "After: a1.geta()" +gdb_test "p ref_c - a1" "From CC .*15" "After: ref_c - a1" +gdb_test "p ref_c - c1" "From Python .*0" "After: ref_c - c1" +gdb_test "p b1 * b1" "From Python .*900" "After: b1 * b1" +gdb_test "p ref_c * b1" "No symbol.*" "After: ref_c * b1 failure" +gdb_test "p ref_d * b1" "From CC .*1500" "After: ref_d * b1" +gdb_test "p bt * c1" "From Python .*800" "After: bt * c1" +gdb_test "p ++a1" "From Python .*6" "After: ++a1" +gdb_test "p a1.getarrayind(5)" "From Python .*5" \ + "After: a1.getarrayind(5)" +gdb_test "p e.geta()" "From Python .*100" "After: e.geta()" +gdb_test "p e_ptr->geta()" "From Python .*100" "After: e_ptr->geta()" +gdb_test "p e_ref.geta()" "From Python .*100" "After: e_ref.geta()" +gdb_test "p e.method(10)" "From Python .*" "After: e.method(10)" +gdb_test "p e.method('a')" "From Python .*" \ + "After: e.method('a')" +gdb_test "p g.size_diff ()" "From Python G<>::size_diff.*" \ + "After: g.size_diff()" +gdb_test "p g.size_diff< unsigned long >()" "From Python G<>::size_diff.*" \ + "After: g.size_diff()" +gdb_test "p g.size_mul<2>()" "From Python G<>::size_mul.*" \ + "After: g.size_mul<2>()" +gdb_test "p g.size_mul< 5 >()" "From Python G<>::size_mul.*" \ + "After: g.size_mul< 5 >()" +gdb_test "p g.mul(2.0)" "From Python G<>::mul.*" \ + "After: g.mul(2.0)" +gdb_test "p g.mul('a')" "From Python G<>::mul.*" \ +gdb_test "p g_ptr->mul('a')" "From Python G<>::mul.*" \ + "After: g_ptr->mul('a')" + +# Tests for 'disable/enable debug-method' command. +gdb_test_no_output "disable debug-method .*debugmethods G_methods" \ + "Disable G_methods" +gdb_test "p g.mul('a')" "Couldn't find method.*" \ + "g.mul('a') after disabling G_methods" +gdb_test_no_output "enable debug-method .*debugmethods G_methods" \ + "Enable G_methods" +gdb_test "p g.mul('a')" "From Python G<>::mul.*" \ + "After enabling G_methods" +gdb_test_no_output "disable debug-method .*debugmethods G_methods;mul" \ + "Disable G_methods;mul" +gdb_test "p g.mul('a')" "Couldn't find method.*" \ + "g.mul('a') after disabling G_methods;mul" +gdb_test_no_output "enable debug-method .*debugmethods G_methods;mul" \ + "Enable G_methods;mul" +gdb_test "p g.mul('a')" "From Python G<>::mul.*" \ + "After enabling G_methods;mul" + +# Test for 'info debug-methods' command +gdb_test "info debug-method global plus" "global.*plus_plus_A" \ + "info debug-method global plus" + +remote_file host delete ${debug_methods_script} diff --git a/gdb/testsuite/gdb.python/py-debugmethods.py b/gdb/testsuite/gdb.python/py-debugmethods.py new file mode 100644 index 0000000..76c0385 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-debugmethods.py @@ -0,0 +1,233 @@ +# Copyright 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 . + +# This file is part of the GDB testsuite. It implements debug methods +# in the Python extension language. + +import gdb +import re + +from gdb.debug_method import DebugMethod +from gdb.debug_method import DebugMethodMatcher, DebugMethodWorker +from gdb.debug_method import SimpleDebugMethodMatcher + +def A_plus_A(obj, opr): + print ('From Python :') + return obj['a'] + opr['a'] + +def plus_plus_A(obj): + print ('From Python :') + return obj['a'] + 1 + +def C_minus_C(obj, opr): + # This function is not defined for objects of class C in the associated C++ + # file. However, C is derived from A which has a virtual operator- method. + # Hence, if an operator '-' is used on a reference pointing to 'C' after + # loading this script, then this Python version should be invoked. + print ('From Python :') + return obj['a'] - opr['a'] + +def B_star_B(obj, opr): + print ('From Python :') + return obj['a'] * opr['a'] + +def A_geta(obj): + print ('From Python :') + return obj['a'] + +def A_getarrayind(obj, index): + print 'From Python :' + return obj['array'][index] + + +type_A = gdb.parse_and_eval('(dop::A *) 0').type.target() +type_B = gdb.parse_and_eval('(dop::B *) 0').type.target() +type_C = gdb.parse_and_eval('(dop::C *) 0').type.target() +type_int = gdb.parse_and_eval('(int *) 0').type.target() + + +# The E class matcher and worker test two things: +# 1. debug method returning None. +# 2. Matcher returning a list of workers. + +class E_method_worker1(DebugMethodWorker): + def __init__(self): + pass + + def get_arg_types(self): + return gdb.lookup_type('char') + + def invoke(self, obj, args): + print 'From Python ' + return None + + +class E_method_worker2(DebugMethodWorker): + def __init__(self): + pass + + def get_arg_types(self): + return gdb.lookup_type('int') + + def invoke(self, obj, args): + print 'From Python ' + return None + + +class E_method_matcher(DebugMethodMatcher): + def __init__(self): + DebugMethodMatcher.__init__(self, 'E_methods') + + def match(self, class_type, method_name): + class_tag = class_type.unqualified().tag + if not re.match('^dop::E$', class_tag): + return None + if not re.match('^method$', method_name): + return None + return [E_method_worker1(), E_method_worker2()] + + +# The G class method matcher and worker illustrate how to write +# debug method matchers and workers for template classes and template +# methods. + +class G_size_diff_worker(DebugMethodWorker): + def __init__(self, class_template_type, method_template_type): + self._class_template_type = class_template_type + self._method_template_type = method_template_type + + def get_arg_types(self): + pass + + def invoke(self, obj, args): + print ('From Python G<>::size_diff()') + return (self._method_template_type.sizeof - + self._class_template_type.sizeof) + + +class G_size_mul_worker(DebugMethodWorker): + def __init__(self, class_template_type, method_template_val): + self._class_template_type = class_template_type + self._method_template_val = method_template_val + + def get_arg_types(self): + pass + + def invoke(self, obj, args): + print ('From Python G<>::size_mul()') + return self._class_template_type.sizeof * self._method_template_val + + +class G_mul_worker(DebugMethodWorker): + def __init__(self, class_template_type, method_template_type): + self._class_template_type = class_template_type + self._method_template_type = method_template_type + + def get_arg_types(self): + return self._method_template_type + + def invoke(self, obj, args): + print ('From Python G<>::mul()') + return obj['t'] * args[0] + + +class G_methods_matcher(DebugMethodMatcher): + def __init__(self): + DebugMethodMatcher.__init__(self, 'G_methods') + self.methods = [DebugMethod('size_diff'), + DebugMethod('size_mul'), + DebugMethod('mul')] + + def _is_enabled(self, name): + for method in self.methods: + if method.name == name and method.enabled: + return True + + def match(self, class_type, method_name): + class_tag = class_type.unqualified().tag + if not re.match('^dop::G<[ ]*[_a-zA-Z][ _a-zA-Z0-9]*>$', + class_tag): + return None + t_name = class_tag[7:-1] + try: + t_type = gdb.lookup_type(t_name) + except gdb.error: + return None + if re.match('^size_diff<[ ]*[_a-zA-Z][ _a-zA-Z0-9]*>$', method_name): + if not self._is_enabled('size_diff'): + return None + t1_name = method_name[10:-1] + try: + t1_type = gdb.lookup_type(t1_name) + return G_size_diff_worker(t_type, t1_type) + except gdb.error: + return None + if re.match('^size_mul<[ ]*[0-9]+[ ]*>$', method_name): + if not self._is_enabled('size_mul'): + return None + m_val = int(method_name[9:-1]) + return G_size_mul_worker(t_type, m_val) + if re.match('^mul<[ ]*[_a-zA-Z][ _a-zA-Z0-9]*>$', method_name): + if not self._is_enabled('mul'): + return None + t1_name = method_name[4:-1] + try: + t1_type = gdb.lookup_type(t1_name) + return G_mul_worker(t_type, t1_type) + except gdb.error: + return None + + +global_dm_list = [ + SimpleDebugMethodMatcher('A_plus_A', + '^dop::A$', + 'operator\+', + A_plus_A, + # This is a replacement, hence match the arg type + # exactly! + type_A.const().reference()), + SimpleDebugMethodMatcher('plus_plus_A', + '^dop::A$', + 'operator\+\+', + plus_plus_A), + SimpleDebugMethodMatcher('C_minus_C', + '^dop::C$', + 'operator\-', + C_minus_C, + type_C), + SimpleDebugMethodMatcher('B_star_B', + '^dop::B$', + 'operator\*', + B_star_B, + # This is a replacement, hence match the arg type + # exactly! + type_B.const().reference()), + SimpleDebugMethodMatcher('A_geta', + '^dop::A$', + '^geta$', + A_geta), + SimpleDebugMethodMatcher('A_getarrayind', + '^dop::A$', + '^getarrayind$', + A_getarrayind, + type_int), +] + +for matcher in global_dm_list: + gdb.debug_method.register_debug_method_matcher(gdb, matcher) +gdb.debug_method.register_debug_method_matcher(gdb.current_progspace(), + G_methods_matcher()) +gdb.debug_method.register_debug_method_matcher(gdb.current_objfile(), + E_method_matcher())