From e1ff6226d8e36896edbdd651753a3e1292381ba0 Mon Sep 17 00:00:00 2001
From: Peter Waller
Date: Mon, 21 Dec 2020 09:38:07 -0500
Subject: [PATCH] Preserve gdb_std{out, err, log, targ, targerr} across
interpreter_exec_cmd
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
Change-Id: Id87423b262d058857ea9dca5866ca6471741e512
---
gdb/ChangeLog | 4 ++
gdb/interps.c | 8 ++++
gdb/testsuite/ChangeLog | 5 ++
gdb/testsuite/gdb.base/style-interp-exec-mi.c | 22 +++++++++
.../gdb.base/style-interp-exec-mi.exp | 46 +++++++++++++++++++
5 files changed, 85 insertions(+)
create mode 100644 gdb/testsuite/gdb.base/style-interp-exec-mi.c
create mode 100644 gdb/testsuite/gdb.base/style-interp-exec-mi.exp
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 6e0c8be81bc..a3aabb50682 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,7 @@
+2020-12-21 Peter Waller
+
+ * interps.c (interpreter_exec_cmd): Restore streams pointers.
+
2020-12-21 Markus Metzger
* record.c (require_record_target): Rephrase error message.
diff --git a/gdb/interps.c b/gdb/interps.c
index 4b2e3fd37b0..e186a736786 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -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"));
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 3e08608edda..da1f22dfd38 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2020-12-21 Peter Waller
+
+ * gdb.base/style-interp-exec-mi.exp: New.
+ * gdb.base/style-interp-exec-mi.c: New.
+
2020-12-21 Simon Marchi
* 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
index 00000000000..9d7b2f1a4c2
--- /dev/null
+++ b/gdb/testsuite/gdb.base/style-interp-exec-mi.c
@@ -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 . */
+
+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
index 00000000000..f3f010e24a4
--- /dev/null
+++ b/gdb/testsuite/gdb.base/style-interp-exec-mi.exp
@@ -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 .
+
+# 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"
+}
--
2.30.2