aboutsummaryrefslogtreecommitdiff
path: root/gdb
diff options
context:
space:
mode:
authorPeter Waller <p@pwaller.net>2020-12-21 09:38:07 -0500
committerSimon Marchi <simon.marchi@polymtl.ca>2020-12-21 09:38:22 -0500
commite1ff6226d8e36896edbdd651753a3e1292381ba0 (patch)
treec30ef5fba5cf64bd5d6ae951132b0b2880643623 /gdb
parentca01f1bf3d9e5f5bcddf72bd83f574395e859d1c (diff)
downloadfsf-binutils-gdb-e1ff6226d8e36896edbdd651753a3e1292381ba0.zip
fsf-binutils-gdb-e1ff6226d8e36896edbdd651753a3e1292381ba0.tar.gz
fsf-binutils-gdb-e1ff6226d8e36896edbdd651753a3e1292381ba0.tar.bz2
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 <p@pwaller.net> Change-Id: Id87423b262d058857ea9dca5866ca6471741e512
Diffstat (limited to 'gdb')
-rw-r--r--gdb/ChangeLog4
-rw-r--r--gdb/interps.c8
-rw-r--r--gdb/testsuite/ChangeLog5
-rw-r--r--gdb/testsuite/gdb.base/style-interp-exec-mi.c22
-rw-r--r--gdb/testsuite/gdb.base/style-interp-exec-mi.exp46
5 files changed, 85 insertions, 0 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 6e0c8be..a3aabb5 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -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.
diff --git a/gdb/interps.c b/gdb/interps.c
index 4b2e3fd..e186a73 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 3e08608..da1f22d 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -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
index 0000000..9d7b2f1
--- /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 <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
index 0000000..f3f010e
--- /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 <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"
+}