[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
This commit is contained in:
Tom de Vries
2025-06-28 13:03:14 +02:00
parent d62eaecf2e
commit f9e9e263f5
4 changed files with 91 additions and 0 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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.