Preserve gdb_std{out, err, log, targ, targerr} across interpreter_exec_cmd
authorPeter Waller <p@pwaller.net>
Mon, 21 Dec 2020 14:38:07 +0000 (09:38 -0500)
committerSimon Marchi <simon.marchi@polymtl.ca>
Mon, 21 Dec 2020 14:38:22 +0000 (09:38 -0500)
Calls through interpreter_exec_cmd can cause the output state to be modified in
a way which doesn't get back after the execution.

It looks like the intent is that interp::resume should put things back how they
should be, however, mi_interp::resume modifies gdb_stdout and nothing currently
restores it to the previous state.

To see the broken behaviour:

  gdb -ex starti -ex bt -ex 'interpreter-exec mi echo' -ex bt -ex q echo <<<''

Prior to this patch, on a terminal environment, the first backtrace is
coloured, and the second backtrace is not. The reason is that
stdio_file::can_emit_style_escape becomes false, because the gdb_stdout gets
overwritten in mi_interp::resume and not replaced.

gdb/ChangeLog:

* interps.c (interpreter_exec_cmd): Restore streams pointers.

gdb/testsuite/ChangeLog:

* gdb.base/style-interp-exec-mi.exp: New.
* gdb.base/style-interp-exec-mi.c: New.

Signed-off-by: Peter Waller <p@pwaller.net>
Change-Id: Id87423b262d058857ea9dca5866ca6471741e512

gdb/ChangeLog
gdb/interps.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/style-interp-exec-mi.c [new file with mode: 0644]
gdb/testsuite/gdb.base/style-interp-exec-mi.exp [new file with mode: 0644]

index 6e0c8be81bcc6380c34f7262729e6ae1704488db..a3aabb506826159ec3a1b1eae30de47aa9e2c1d9 100644 (file)
@@ -1,3 +1,7 @@
+2020-12-21  Peter Waller  <p@pwaller.net>
+
+       * interps.c (interpreter_exec_cmd): Restore streams pointers.
+
 2020-12-21  Markus Metzger  <markus.t.metzger@intel.com>
 
        * record.c (require_record_target): Rephrase error message.
index 4b2e3fd37b0e30c6cf85f7a3e3d58e8d7bbdb8e2..e186a73678631a1711956c4893c52544349d8857 100644 (file)
@@ -370,6 +370,14 @@ interpreter_exec_cmd (const char *args, int from_tty)
   unsigned int nrules;
   unsigned int i;
 
+  /* Interpreters may clobber stdout/stderr (e.g.  in mi_interp::resume at time
+     of writing), preserve their state here.  */
+  scoped_restore save_stdout = make_scoped_restore (&gdb_stdout);
+  scoped_restore save_stderr = make_scoped_restore (&gdb_stderr);
+  scoped_restore save_stdlog = make_scoped_restore (&gdb_stdlog);
+  scoped_restore save_stdtarg = make_scoped_restore (&gdb_stdtarg);
+  scoped_restore save_stdtargerr = make_scoped_restore (&gdb_stdtargerr);
+
   if (args == NULL)
     error_no_arg (_("interpreter-exec command"));
 
index 3e08608edda8fefe2d1d2c8b8d5691d575e3f61e..da1f22dfd382b3ac70d7672d3169c0085d6c7e20 100644 (file)
@@ -1,3 +1,8 @@
+2020-12-21  Peter Waller  <p@pwaller.net>
+
+       * gdb.base/style-interp-exec-mi.exp: New.
+       * gdb.base/style-interp-exec-mi.c: New.
+
 2020-12-21  Simon Marchi  <simon.marchi@polymtl.ca>
 
        * gdb.base/list.exp: Replace send_gdb + gdb_expect with
diff --git a/gdb/testsuite/gdb.base/style-interp-exec-mi.c b/gdb/testsuite/gdb.base/style-interp-exec-mi.c
new file mode 100644 (file)
index 0000000..9d7b2f1
--- /dev/null
@@ -0,0 +1,22 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 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 <http://www.gnu.org/licenses/>.  */
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/style-interp-exec-mi.exp b/gdb/testsuite/gdb.base/style-interp-exec-mi.exp
new file mode 100644 (file)
index 0000000..f3f010e
--- /dev/null
@@ -0,0 +1,46 @@
+# Copyright 2020 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 <http://www.gnu.org/licenses/>.
+
+# It was observed that when doing:
+#
+#   - backtrace
+#   - an "interpreter-exec mi" command
+#   - backtrace again
+#
+# The second backtrace would not be styled.  This test tests that.
+
+standard_testfile
+
+save_vars { env(TERM) } {
+    # We need an ANSI-capable terminal to get the output.
+    setenv TERM ansi
+
+    if { [prepare_for_testing "failed to prepare" \
+           ${testfile} ${srcfile}] } {
+       return
+    }
+
+    if { ![runto_main] } {
+       untested "could not run to main"
+       return
+    }
+
+    gdb_test_no_output "set style enabled on"
+    set main_expr [style main function]
+    gdb_test "backtrace" ".* ${main_expr} .*" "backtrace before"
+    gdb_test "interpreter-exec mi \"-data-evaluate-expression 1\"" \
+       "\\^done,value=\"1\""
+    gdb_test "backtrace" ".* ${main_expr} .*" "backtrace after"
+}
This page took 0.043208 seconds and 4 git commands to generate.