From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by sourceware.org (Postfix) with ESMTPS id 5AEA83857C44 for ; Tue, 22 Sep 2020 10:23:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 5AEA83857C44 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=undo.io Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=grees@undo.io Received: by mail-wr1-x444.google.com with SMTP id g4so16427638wrs.5 for ; Tue, 22 Sep 2020 03:23:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=undo-io.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=j0AX534ixmKHBB6lK+hNUu2AQCaEaP5jzQ8jia9vCXs=; b=qLM70KgZVOicy1lspcqK50X6Np9Pw8WSOZ7g7ZzBJBwEvmP30w0EAU8TeEkBPrSGFa zq56CWTKsRwepnfmvQmkSO4z3H3nlOAzfJt4POIaImGwyBIChoCMd9IHoHRlrsVymkhm hKrrcGAzFPedyBlqF6AT/puTDWinbfoC9uCyRA1+Lz2tnV9t2T50X8vso6XrBPSyUlfT aX2ekm6k+DctmpM5LAIPoTX3CFEXhNLRLQND5tmAf64JewcbjISJ5SHc5VFTTq1IApAb H8aE4xYMPMYKPIGxUgZMqravWokbDj3g0McpkDdAJZ/5kf/4vQU1zMfIEnTh0eTHPenu R9aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=j0AX534ixmKHBB6lK+hNUu2AQCaEaP5jzQ8jia9vCXs=; b=CSRRnPJe7kG8o9iL0srcsp0be2bEDhMWgz564gbOM3bYRo8kDKoX0TZBbSmnq/jhUj HKssIupMfGDD0D+OdKoPWSX+VflIuONW52DmVEs27/+DIqbtg9Y/+Lk8DpCKVgXX7s1n gDn1afcHbKcNoBqLwlAf4kSzv7d4cNTuv3cYATYPk8PEmYNc6x/m9pzvQp2r+1y7kpZu 9b4Av9LXq4FT9Dvina/zH8Q8nyuzv+MfG1vE/v5xLZCKdpwXrY6NgsGlAdS42WlK1y8O oqpeLbeLYxqy1NGPOYPZxv1QdBcFzA2zObGxmqcOnJ3AQNUe/ZB14micIcVKn3RLvowQ 6zTg== X-Gm-Message-State: AOAM532hTBMLwYDkZxfJVQbhAQi3dc6STlRcLDUIwDyJI19/CDcf3Uoj Oa9gn7UESciZoN1lrGvQygeB+NOOXXhyfngVkw45Sld+q3kJFnX4Lnjh0SBAH8jNnjG+tNMpS0C UWT7CkfEP+SwLX9mSs9cQQKwzlLOCR09PvvGnpK2tSwWhotsSd5pYRcl8mNo2It5/qXI= X-Google-Smtp-Source: ABdhPJzYYx7E0mT1lCFrLIofxDqd9w0EReUJqn3Ggr/sz9cJzGkBZQWswkGLoS6rCOsso0KpcpDSbQ== X-Received: by 2002:adf:f808:: with SMTP id s8mr4459398wrp.268.1600770231891; Tue, 22 Sep 2020 03:23:51 -0700 (PDT) Received: from tilsit-thinkpad.undoers.io (cpc91190-cmbg18-2-0-cust123.5-4.cable.virginm.net. [80.6.99.124]) by smtp.gmail.com with ESMTPSA id q18sm26941771wre.78.2020.09.22.03.23.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Sep 2020 03:23:51 -0700 (PDT) From: Gareth Rees To: gdb-patches@sourceware.org Subject: [PATCH] gdb: Fix from_tty argument to gdb.execute in Python. Date: Tue, 22 Sep 2020 11:23:43 +0100 Message-Id: <20200922102343.8440-1-grees@undo.io> X-Mailer: git-send-email 2.26.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Sep 2020 10:23:55 -0000 Prior to commit 56bcdbea2b, the from_tty keyword argument to the Python function gdb.execute controlled whether the command took input from the terminal. When from_tty=True, the "starti" command prompted the user: (gdb) python gdb.execute("starti", from_tty=True) The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /bin/true Program stopped. When from_tty=False, it did not prompt the user, and "yes" was assumed: (gdb) python gdb.execute('starti', from_tty=False) Program stopped. However, after commit 56bcdbea2b, the from_tty keyword argument no longer had this effect. For example, as of commit 7ade7fba75: (gdb) python gdb.execute('starti', from_tty=True) The program being debugged has been started already. Start it from the beginning? (y or n) [answered Y; input not from terminal] Starting program: /bin/true Program stopped. Note the "[answered Y; input not from terminal]" in the output even though from_tty=True was requested. Looking at commit 56bcdbea2b, it seems that the behaviour of the from_tty argument was changed accidentally. The commit message said: Let gdb.execute handle multi-line commands This changes the Python API so that gdb.execute can now handle multi-line commands, like "commands" or "define". and there was no mention of changing the effect of the from_tty argument. It looks as though the code for setting the instream to 0 was accidentally moved from execute_user_command() to execute_control_commands() along with the code for iterating over a series of command lines. Accordingly, the simplest way to fix this is to partially reverse commit 56bcdbea2b by moving the code for setting the instream to 0 back to execute_user_command() where it was to begin with. Additionally, add a test case to reduce the risk of similar breakage in future. gdb/ChangeLog: * cli/cli-script.c (execute_control_commands): don't set instream to 0 here as this breaks the from_tty argument to gdb.execute in Python. (execute_user_command): set instream to 0 here instead. gdb/testsuite/ChangeLog: * gdb.python/python.exp: add test cases for the from_tty argument to gdb.execute. --- gdb/cli/cli-script.c | 16 ++++++++-------- gdb/testsuite/gdb.python/python.exp | 13 +++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c index da4a41023a..4adcda85e6 100644 --- a/gdb/cli/cli-script.c +++ b/gdb/cli/cli-script.c @@ -392,14 +392,6 @@ execute_cmd_post_hook (struct cmd_list_element *c) void execute_control_commands (struct command_line *cmdlines, int from_tty) { - /* Set the instream to 0, indicating execution of a - user-defined function. */ - scoped_restore restore_instream - = make_scoped_restore (¤t_ui->instream, nullptr); - scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0); - scoped_restore save_nesting - = make_scoped_restore (&command_nest_depth, command_nest_depth + 1); - while (cmdlines) { enum command_control_type ret = execute_control_command (cmdlines, @@ -464,6 +456,14 @@ execute_user_command (struct cmd_list_element *c, const char *args) if (user_args_stack.size () > max_user_call_depth) error (_("Max user call depth exceeded -- command aborted.")); + /* Set the instream to 0, indicating execution of a + user-defined function. */ + scoped_restore restore_instream + = make_scoped_restore (¤t_ui->instream, nullptr); + scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0); + scoped_restore save_nesting + = make_scoped_restore (&command_nest_depth, command_nest_depth + 1); + execute_control_commands (cmdlines, 0); } diff --git a/gdb/testsuite/gdb.python/python.exp b/gdb/testsuite/gdb.python/python.exp index a031ea5a18..017f33afe5 100644 --- a/gdb/testsuite/gdb.python/python.exp +++ b/gdb/testsuite/gdb.python/python.exp @@ -526,3 +526,16 @@ gdb_test "print \$cvar3" "= void" \ # Test PR 23669, the following would invoke the "commands" command instead of # "show commands". gdb_test "python gdb.execute(\"show commands\")" "$decimal print \\\$cvar3.*" + +# Test that the from_tty argument to gdb.execute is effective. If +# False, the user is not prompted for decisions such as restarting the +# program, and "yes" is assumed. If True, the user is prompted. +gdb_test "python gdb.execute('starti', from_tty=False)" \ + "Program stopped.*" \ + "starti via gdb.execute, not from tty" +gdb_test_multiple "python gdb.execute('starti', from_tty=True)" \ + "starti via gdb.execute, from tty" { + -re {The program being debugged has been started already\.\r\nStart it from the beginning\? \(y or n\) $} { + gdb_test "y" "Starting program:.*" "starti via interactive input" + } +} -- 2.26.0