From patchwork Thu Apr 19 19:15:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 26862 Received: (qmail 46902 invoked by alias); 19 Apr 2018 19:16:07 -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 46456 invoked by uid 89); 19 Apr 2018 19:15:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=1978, CATCH, separated, 2411 X-HELO: gateway20.websitewelcome.com Received: from gateway20.websitewelcome.com (HELO gateway20.websitewelcome.com) (192.185.62.46) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 19 Apr 2018 19:15:52 +0000 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway20.websitewelcome.com (Postfix) with ESMTP id 2EF2C400E6892 for ; Thu, 19 Apr 2018 14:15:50 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 9F26fF3FZ5CKD9F26fnzH9; Thu, 19 Apr 2018 14:15:50 -0500 X-Authority-Reason: nr=8 Received: from 97-122-176-117.hlrn.qwest.net ([97.122.176.117]:44916 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89_1) (envelope-from ) id 1f9F25-0041O4-PL; Thu, 19 Apr 2018 14:15:50 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 7/8] Allow breakpoint commands to be set from Python Date: Thu, 19 Apr 2018 13:15:38 -0600 Message-Id: <20180419191539.661-8-tom@tromey.com> In-Reply-To: <20180419191539.661-1-tom@tromey.com> References: <20180419191539.661-1-tom@tromey.com> X-BWhitelist: no X-Source-L: No X-Exim-ID: 1f9F25-0041O4-PL X-Source-Sender: 97-122-176-117.hlrn.qwest.net (bapiya.Home) [97.122.176.117]:44916 X-Source-Auth: tom+tromey.com X-Email-Count: 8 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes This changes the Python API so that breakpoint commands can be set by writing to the "commands" attribute. 2018-04-19 Tom Tromey PR python/22731: * NEWS: Mention that breakpoint commands are writable. * python/py-breakpoint.c (bppy_set_commands): New function. (breakpoint_object_getset) <"commands">: Use it. doc/ChangeLog 2018-04-19 Tom Tromey PR python/22731: * python.texi (Breakpoints In Python): Mention that "commands" is writable. testsuite/ChangeLog 2018-04-19 Tom Tromey PR python/22731: * gdb.python/py-breakpoint.exp: Test setting breakpoint commands. --- gdb/ChangeLog | 7 +++++ gdb/NEWS | 5 ++++ gdb/doc/ChangeLog | 6 ++++ gdb/doc/python.texi | 2 +- gdb/python/py-breakpoint.c | 45 +++++++++++++++++++++++++++++- gdb/testsuite/ChangeLog | 5 ++++ gdb/testsuite/gdb.python/py-breakpoint.exp | 10 ++++++- 7 files changed, 77 insertions(+), 3 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 63fe30d175..14d5fbb7c0 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -24,6 +24,11 @@ set|show record btrace cpu Controls the processor to be used for enabling errata workarounds for branch trace decode. +* Python Scripting + + ** The commands attached to a breakpoint can be set by assigning to + the breakpoint's "commands" field. + * New targets RiscV ELF riscv*-*-elf diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index ebd48fffe7..a2b948a4ca 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -5116,7 +5116,7 @@ value is @code{None}. This attribute is writable. This attribute holds the commands attached to the breakpoint. If there are commands, this attribute's value is a string holding all the commands, separated by newlines. If there are no commands, this -attribute is @code{None}. This attribute is not writable. +attribute is @code{None}. This attribute is writable. @end defvar @node Finish Breakpoints in Python diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c index d654b92a8c..f386661bfb 100644 --- a/gdb/python/py-breakpoint.c +++ b/gdb/python/py-breakpoint.c @@ -510,6 +510,49 @@ bppy_get_commands (PyObject *self, void *closure) return host_string_to_python_string (stb.c_str ()); } +/* Set the commands attached to a breakpoint. Returns 0 on success. + Returns -1 on error, with a python exception set. */ +static int +bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure) +{ + gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; + struct breakpoint *bp = self_bp->bp; + struct gdb_exception except = exception_none; + + BPPY_SET_REQUIRE_VALID (self_bp); + + gdb::unique_xmalloc_ptr commands + (python_string_to_host_string (newvalue)); + if (commands == nullptr) + return -1; + + TRY + { + bool first = true; + char *save_ptr = nullptr; + gdb::function_view reader + = [&] () + { + const char *result = strtok_r (first ? commands.get () : nullptr, + "\n", &save_ptr); + first = false; + return result; + }; + + counted_command_line lines = read_command_lines_1 (reader, 1, nullptr); + breakpoint_set_commands (self_bp->bp, std::move (lines)); + } + CATCH (ex, RETURN_MASK_ALL) + { + except = ex; + } + END_CATCH + + GDB_PY_SET_HANDLE_EXCEPTION (except); + + return 0; +} + /* Python function to get the breakpoint type. */ static PyObject * bppy_get_type (PyObject *self, void *closure) @@ -1185,7 +1228,7 @@ when setting this property.", NULL }, { "condition", bppy_get_condition, bppy_set_condition, "Condition of the breakpoint, as specified by the user,\ or None if no condition set."}, - { "commands", bppy_get_commands, NULL, + { "commands", bppy_get_commands, bppy_set_commands, "Commands of the breakpoint, as specified by the user."}, { "type", bppy_get_type, NULL, "Type of breakpoint."}, diff --git a/gdb/testsuite/gdb.python/py-breakpoint.exp b/gdb/testsuite/gdb.python/py-breakpoint.exp index 6e0ff88f87..3ce0ea11de 100644 --- a/gdb/testsuite/gdb.python/py-breakpoint.exp +++ b/gdb/testsuite/gdb.python/py-breakpoint.exp @@ -197,8 +197,16 @@ proc_with_prefix test_bkpt_cond_and_cmds { } { gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" \ "Get Breakpoint List" 0 - gdb_test "python print (blist\[len(blist)-1\].commands)" \ + gdb_py_test_silent_cmd "python last_bp = blist\[len(blist)-1\]" \ + "Find last breakpoint" 0 + gdb_test "python print (last_bp.commands)" \ "print \"Command for breakpoint has been executed.\".*print result" + + gdb_test_no_output "python last_bp.commands = 'echo hi\\necho there'" \ + "set commands" + # Note the length is 3 because the string ends in a \n. + gdb_test "python print (len(last_bp.commands.split('\\n')))" "3" \ + "check number of lines in commands" } proc_with_prefix test_bkpt_invisible { } {