From patchwork Sat Mar 7 14:12:37 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siva Chandra Reddy X-Patchwork-Id: 5516 Received: (qmail 116680 invoked by alias); 7 Mar 2015 14:12:43 -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 116668 invoked by uid 89); 7 Mar 2015 14:12:42 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mail-yk0-f179.google.com Received: from mail-yk0-f179.google.com (HELO mail-yk0-f179.google.com) (209.85.160.179) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Sat, 07 Mar 2015 14:12:40 +0000 Received: by ykq19 with SMTP id 19so28807806ykq.4 for ; Sat, 07 Mar 2015 06:12:38 -0800 (PST) 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=1O4KFWekraLBFuYTehFZtuYEyBrQ+C+E0nDnli7rXQE=; b=DJTueJjyy8BuVcQ6p9aHhqAqwuhfULoNEHR76tgBymT9z4dxDHiT8i331Ks7rzlaFn xpy+Uk3vMeL1mqTK9ntdoV5YrsnpfGxYoin9qlKA4krv94H/bFbxul4K5gVG0PjChznB 9PercErykUSs1bKMcjrEGBhBVoMyDCZBfARUM54NOJXcRK4kk/Mqlomk7tSvRNj7VGo9 TcezGn3aMKS/6cDwND/sGJxBK6nfa++AdqHmnzBVcFtfLYI4X71twudr6Z1x5qTK9Jd8 jfrBsdwPKrEfuBfVv/r1zNjptvvx41OQZdj0iP/HSn5j9Lj/KokyVfr8zDWZSeGA1EJx A9yg== X-Gm-Message-State: ALoCoQn2vrUfZ5evoUTKiun3dMiTcnQiv6G43Xi+CgtmzcDcuGYd79TnQ3e9rVHF1JukDIOi4cIO MIME-Version: 1.0 X-Received: by 10.170.195.144 with SMTP id m138mr20121269yke.50.1425737558071; Sat, 07 Mar 2015 06:12:38 -0800 (PST) Received: by 10.170.196.68 with HTTP; Sat, 7 Mar 2015 06:12:37 -0800 (PST) Date: Sat, 7 Mar 2015 06:12:37 -0800 Message-ID: Subject: [PATCH] [Python] Add a boolean parameter "python resolve-operators" From: Siva Chandra To: gdb-patches X-IsSubscribed: yes The attached patch adds a boolean parameter "python resolve-operators". It is implemented using GDB's Python API. Python operators in GDB Python scripts will be resolved to overloaded C++ operators (if they exist) only if this parameter is "on". gdb/ChangeLog: 2015-03-07 Siva Chandra Reddy * NEWS (New commands): Add entries for "set python resolve-operators" and "show python resolve-operators". * data-directory/Makefile.in (PYTHON_FILE_LIST): Add gdb/parameters/__init__.py and gdb/parameters/resolve_operators.py. * python/lib/gdb/__init__.py (packages): Add 'parameters'. * python/lib/gdb/parameters/__init__.py: New file. * python/lib/gdb/parameters/resolve_operators.py: New file. * python/py-value.c (resolve_to_overloaded_operators): New function. (valpy_binop): Invoke value_x_binop only if resolving Python operators to C++ operators is enabled. gdb/testsuite/ChangeLog: 2015-03-07 Siva Chandra Reddy * gdb.python/py-resolve-operators.cc: New file. * gdb.python/py-resolve-operators.exp: New file. diff --git a/gdb/NEWS b/gdb/NEWS index 49dc0e6..0e1a94a 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -28,6 +28,14 @@ record btrace bts record bts Start branch trace recording using Branch Trace Store (BTS) format. +show python resolve-operators + Show whether resolving Python operators (in Python scripts) to + overloaded C++ operators is enabled/disabled. + +set python resolve-operators + Enable/disable resolving Python operators (in Python scripts) to + overloaded C++ operators. + * New options set max-completions diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in index c01b86d..eda541e 100644 --- a/gdb/data-directory/Makefile.in +++ b/gdb/data-directory/Makefile.in @@ -74,6 +74,8 @@ PYTHON_FILE_LIST = \ gdb/function/__init__.py \ gdb/function/caller_is.py \ gdb/function/strfns.py \ + gdb/parameters/__init__.py \ + gdb/parameters/resolve_operators.py \ gdb/printer/__init__.py \ gdb/printer/bound_registers.py diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py index 92b06f2..3152b00 100644 --- a/gdb/python/lib/gdb/__init__.py +++ b/gdb/python/lib/gdb/__init__.py @@ -82,6 +82,7 @@ PYTHONDIR = os.path.dirname(os.path.dirname(__file__)) packages = [ 'function', 'command', + 'parameters', 'printer' ] diff --git a/gdb/python/lib/gdb/parameters/__init__.py b/gdb/python/lib/gdb/parameters/__init__.py new file mode 100644 index 0000000..97b8f7e --- /dev/null +++ b/gdb/python/lib/gdb/parameters/__init__.py @@ -0,0 +1,14 @@ +# Copyright (C) 2015 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 . diff --git a/gdb/python/lib/gdb/parameters/resolve_operators.py b/gdb/python/lib/gdb/parameters/resolve_operators.py new file mode 100644 index 0000000..7f67458 --- /dev/null +++ b/gdb/python/lib/gdb/parameters/resolve_operators.py @@ -0,0 +1,43 @@ +# Copyright (C) 2015 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +import gdb + + +class ResolveOperators(gdb.Parameter): + def __init__(self): + self.set_doc = ('Enable/disable resolving Python operators to ' + 'overloaded C++ operators.\n\n' + 'Usage:\n' + ' set python resolve-operators \n') + self.show_doc = ('Show whether Python operators will be resolved ' + 'to overloaded C++ operators.\n\n' + 'Usage:\n' + ' show python resolve-operators\n') + gdb.Parameter.__init__(self, 'python resolve-operators', + gdb.COMMAND_NONE, gdb.PARAM_BOOLEAN) + self.value = True + + def get_set_string(self): + return ('Resolving Python operators to overloaded C++ operators is ' + + 'set to "%s".' % ('on' if self.value else 'off')) + + def get_show_string(self, svalue): + return ('Resolving Python operators to overloaded C++ operators is ' + + '"%s".' % ('on' if self.value else 'off')) + + +ResolveOperators() diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index 5a13777..015bf8a 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -27,6 +27,7 @@ #include "expression.h" #include "cp-abi.h" #include "python.h" +#include "cli/cli-decode.h" #include "python-internal.h" @@ -920,6 +921,23 @@ enum valpy_opcode #define STRIP_REFERENCE(TYPE) \ ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE)) +/* */ + +static int +resolve_to_overloaded_operators (void) +{ + const char *show_cmd = "show python resolve-operators"; + struct cmd_list_element *alias, *prefix, *cmd; + int found = -1; + volatile struct gdb_exception except; + + found = lookup_cmd_composition (show_cmd, &alias, &prefix, &cmd); + if (!found || !cmd->var || (cmd->var_type != var_boolean)) + return 0; + + return *((int *) cmd->var); +} + /* Returns a value object which is the result of applying the operation specified by OPCODE to the given arguments. Returns NULL on error, with a python exception set. */ @@ -1038,7 +1056,8 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) if (!handled) { - if (binop_user_defined_p (op, arg1, arg2)) + if (binop_user_defined_p (op, arg1, arg2) + && resolve_to_overloaded_operators ()) res_val = value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL); else res_val = value_binop (arg1, arg2, op); diff --git a/gdb/testsuite/gdb.python/py-resolve-operators.cc b/gdb/testsuite/gdb.python/py-resolve-operators.cc new file mode 100644 index 0000000..b4630a7 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-resolve-operators.cc @@ -0,0 +1,38 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2015 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 . */ + +class A +{ +public: + int operator+ (int i); + + int a; +}; + +int +A::operator+ (int i) +{ + return a + i; +} + +int +main (void) +{ + A a = { 10 }; + + return a + -10; /* Break here. */ +} diff --git a/gdb/testsuite/gdb.python/py-resolve-operators.exp b/gdb/testsuite/gdb.python/py-resolve-operators.exp new file mode 100644 index 0000000..9d03352 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-resolve-operators.exp @@ -0,0 +1,50 @@ +# Copyright 2015 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-resolve-operators.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 +} + +gdb_breakpoint [gdb_get_line_number "Break here."] +gdb_continue_to_breakpoint "Break here" ".*Break here.*" + +gdb_test_no_output "python a = gdb.parse_and_eval('a')" "make_a" + +gdb_test "show python resolve-operators" ".*on.*" "test_show_with_on" +gdb_test "python print(a + 113)" "123" "print_a_with_on" + +gdb_test "set python resolve-operators off" ".*off.*" "test_set_to_off" +gdb_test "python print(a + 113)" \ + ".*gdb\.error: Argument to arithmetic operation not a number or boolean.*" \ + "print_a_with_off" + +gdb_test "set python resolve-operators on" ".*on.*" "test_set_to_on" +gdb_test "python print(a + 113)" "123" "print_a_with_on_again"