aboutsummaryrefslogtreecommitdiff
path: root/gdb
diff options
context:
space:
mode:
authorTom de Vries <tdevries@suse.de>2025-06-28 13:03:14 +0200
committerTom de Vries <tdevries@suse.de>2025-06-28 13:03:14 +0200
commitf9e9e263f5d8aa60f34207c056fd3c6d8bd5d8ff (patch)
tree07b57b1af87a2ed109ee40cc7732a9f15c54461a /gdb
parentd62eaecf2e6fc2bce3a0d3a711666c700e79a51a (diff)
downloadbinutils-f9e9e263f5d8aa60f34207c056fd3c6d8bd5d8ff.zip
binutils-f9e9e263f5d8aa60f34207c056fd3c6d8bd5d8ff.tar.gz
binutils-f9e9e263f5d8aa60f34207c056fd3c6d8bd5d8ff.tar.bz2
[gdb/tdep] Add "maint set console-translation-mode <binary|text>" command
On MSYS2, say we record a brief gdb session using TERM=dumb script: ... $ gdb -q (gdb) print 1 $1 = 1 (gdb) q ... When looking at the resulting typescript, we notice something odd: ... $ gdb -q^M (gdb) print 1^M $1 = 1^M^M (gdb) q^M ... For some reason, we have "$1 = 1\r\r\n(gdb) ". Looking at the documentation of _setmode [1], it mentions translation mode _O_TEXT as a mode in which "\n" is translated into "\r\n" on output. So, it looks like this translation happens twice. Add a command "maint set console-translation-mode <binary|text>" command that allows us to set the translation mode of stdout/stderr to binary, such that we get instead: ... $ gdb -q -ex "maint set console-translation-mode binary"^M (gdb) print 1^M $1 = 1^M (gdb) q^M ... Since we run into this in the testsuite, add "maint set console-translation-mode binary" to INTERNAL_GDBFLAGS. Based on "maint set testsuite-mode on/off" from these patches [2][3] by Pierre Muller. Compared to that proposal, I dropped the name testsuite-mode, because the behaviour is not specific to the testsuite. Also I chose values binary/text instead of on/off because eventually there may be other translation mode values that we need [4]. Co-Authored-By: Pierre Muller <muller@sourceware.org> Reviewed-By: Eli Zaretskii <eliz@gnu.org> [1] https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode [2] https://sourceware.org/legacy-ml/gdb-patches/2013-09/msg00939.html [3] https://sourceware.org/legacy-ml/gdb-patches/2013-09/msg00940.html [4] https://learn.microsoft.com/en-us/cpp/c-runtime-library/translation-mode-constants
Diffstat (limited to 'gdb')
-rw-r--r--gdb/NEWS6
-rw-r--r--gdb/doc/gdb.texinfo17
-rw-r--r--gdb/mingw-hdep.c64
-rw-r--r--gdb/testsuite/lib/gdb.exp4
4 files changed, 91 insertions, 0 deletions
diff --git a/gdb/NEWS b/gdb/NEWS
index 6c8a008..2abe376 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -59,6 +59,12 @@ maintenance check symtabs
maintenance canonicalize
Show the canonical form of a C++ name.
+maintenance set console-translation-mode <binary|text>
+maintenance show console-translation-mode
+ Controls the translation mode of GDB stdout/stderr. MS-Windows only. In
+ binary mode, no translation is done. In text mode, a Line Feed is
+ translated into a Carriage Return-Line Feed combination.
+
set riscv numeric-register-names on|off
show riscv numeric-register-names
Controls whether GDB refers to risc-v registers by their numeric names
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 4ef6406..b6d626b 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -42755,6 +42755,23 @@ reports and error and the command is aborted.
@item show watchdog
Show the current setting of the target wait timeout.
+
+@kindex maint set console-translation-mode
+@kindex maint show console-translation-mode
+@item maint set console-translation-mode @r{[}binary|text@r{]}
+@itemx maint show console-translation-mode
+Controls the translation mode of @value{GDBN} stdout/stderr. MS-Windows
+only. Useful for running the @value{GDBN} testsuite.
+
+The translation mode values are as follows:
+@table @code
+@item binary
+No translation.
+@item text
+Translate @samp{\n} (LF, a single Line Feed) into @samp{\r\n} (CR-LF, a
+Carriage Return-Line Feed combination).
+@end table
+
@end table
@node Remote Protocol
diff --git a/gdb/mingw-hdep.c b/gdb/mingw-hdep.c
index 84a7b9f..481bd41 100644
--- a/gdb/mingw-hdep.c
+++ b/gdb/mingw-hdep.c
@@ -23,6 +23,8 @@
#include "gdbsupport/gdb_select.h"
#include "inferior.h"
#include "cli/cli-style.h"
+#include "command.h"
+#include "cli/cli-cmds.h"
#include <windows.h>
#include <signal.h>
@@ -445,3 +447,65 @@ install_sigint_handler (c_c_handler_ftype *fn)
current_handler = fn;
return result;
}
+
+/* Set stdout and stderr handles to translation mode MODE. */
+
+static void
+set_console_translation_mode (int mode)
+{
+ setmode (fileno (stdout), mode);
+ setmode (fileno (stderr), mode);
+}
+
+/* Arg in "maint set console-translation-mode <arg>. */
+
+static std::string maint_console_translation_mode;
+
+/* Current value of "maint set/show console-translation-mode". */
+
+static std::string console_translation_mode = "unknown";
+
+/* Sets the console translation mode. */
+
+static void
+set_maint_console_translation_mode (const char *args, int from_tty,
+ struct cmd_list_element *c)
+{
+ if (maint_console_translation_mode == "binary")
+ set_console_translation_mode (O_BINARY);
+ else if (maint_console_translation_mode == "text")
+ set_console_translation_mode (O_TEXT);
+ else
+ error (_("Invalid console translation mode: %s"),
+ maint_console_translation_mode.c_str ());
+
+ console_translation_mode = maint_console_translation_mode;
+}
+
+/* Shows the console translation mode. */
+
+static void
+show_maint_console_translation_mode (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c,
+ const char *value)
+{
+ gdb_printf (file, _("Console translation mode is %s.\n"),
+ console_translation_mode.c_str ());
+}
+
+extern void _initialize_mingw_hdep ();
+
+void
+_initialize_mingw_hdep ()
+{
+ add_setshow_string_cmd ("console-translation-mode",
+ class_maintenance,
+ &maint_console_translation_mode, _("\
+Set the translation mode of stdout/stderr."), _("\
+Show the translation mode of stdout/stderr."), _("\
+Use \"binary\", or \"text\""),
+ set_maint_console_translation_mode,
+ show_maint_console_translation_mode,
+ &maintenance_set_cmdlist,
+ &maintenance_show_cmdlist);
+}
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 3f1cd55..777d64d 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -272,6 +272,10 @@ if ![info exists INTERNAL_GDBFLAGS] {
# Handle the case that "interactive-mode auto" reports off.
append INTERNAL_GDBFLAGS { -iex "set interactive-mode on"}
+
+ if { [ishost "*-*-mingw*"] } {
+ append INTERNAL_GDBFLAGS { -iex "maint set console-translation-mode binary"}
+ }
}
# The variable gdb_prompt is a regexp which matches the gdb prompt.