From: Tom de Vries Date: Sat, 28 Jun 2025 11:03:14 +0000 (+0200) Subject: [gdb/tdep] Add "maint set console-translation-mode " command X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f9e9e263f5d8aa60f34207c056fd3c6d8bd5d8ff;p=thirdparty%2Fbinutils-gdb.git [gdb/tdep] Add "maint set console-translation-mode " 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 " 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 Reviewed-By: Eli Zaretskii [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 --- diff --git a/gdb/NEWS b/gdb/NEWS index 6c8a008d39d..2abe376a583 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 +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 4ef640698bd..b6d626b4031 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 84a7b9f7a52..481bd41e152 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 #include @@ -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 . */ + +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 3f1cd55d727..777d64d14d1 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.