]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: Fix from_tty argument to gdb.execute in Python.
authorGareth Rees <grees@undo.io>
Sat, 26 Sep 2020 18:01:45 +0000 (11:01 -0700)
committerJoel Brobecker <brobecker@adacore.com>
Mon, 28 Sep 2020 20:15:59 +0000 (13:15 -0700)
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, "starti" and similar commands
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, these commands 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
nullptr was accidentally moved from execute_user_command() to
execute_control_commands() along with the other scoped restores.

Accordingly, the simplest way to fix this is to partially reverse
commit 56bcdbea2b by moving the code for setting the instream to
nullptr 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:

PR python/26586
* cli/cli-script.c (execute_control_commands): don't set
instream to nullptr here as this breaks the from_tty argument
to gdb.execute in Python.
(execute_user_command): set instream to nullptr here instead.

gdb/testsuite/ChangeLog:

PR python/26586
* gdb.python/python.exp: add test cases for the from_tty
argument to gdb.execute.

(cherry picked from commit 8f9929bb97dc0f0fdf60269ac8c9a7d50746646f)

gdb/ChangeLog
gdb/cli/cli-script.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/python.exp

index 74720981cf83ff0ac7626c2cb4cfb0dcc02165d7..72ebf68cfa077214a37258bea6bd0b79c8380662 100644 (file)
@@ -1,3 +1,11 @@
+2020-09-28  Gareth Rees <grees@undo.io>  (tiny change)
+
+       PR python/26586
+       * cli/cli-script.c (execute_control_commands): don't set
+       instream to nullptr here as this breaks the from_tty argument
+       to gdb.execute in Python.
+       (execute_user_command): set instream to nullptr here instead.
+
 2020-09-25  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>
 
        * breakpoint.c (commands_command_1): Make a copy of the 'arg'
index da4a41023ad28554218e0649ca5ca611541200fa..f8ac610d4d6d3505350e1dba868b20891b652999 100644 (file)
@@ -392,10 +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 (&current_ui->instream, nullptr);
   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
   scoped_restore save_nesting
     = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
@@ -464,6 +460,11 @@ 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 nullptr, indicating execution of a
+     user-defined function.  */
+  scoped_restore restore_instream
+    = make_scoped_restore (&current_ui->instream, nullptr);
+
   execute_control_commands (cmdlines, 0);
 }
 
index 62b9baf8ea2e20549b42822d9d57f0f989f9e5e9..5ef2bf3ea138770defce4ee63aeaf66084e1ec45 100644 (file)
@@ -1,3 +1,9 @@
+2020-09-28  Gareth Rees <grees@undo.io>  (tiny change)
+
+       PR python/26586
+       * gdb.python/python.exp: add test cases for the from_tty
+       argument to gdb.execute.
+
 2020-09-25  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>
 
        * gdb.base/bp-cmds-sourced-script.c: New file.
index a031ea5a189a1db5131c02f876d1af94fcdb9033..017f33afe52457f630a462197459d7f8a6a40094 100644 (file)
@@ -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"
+    }
+}