binutils-gdb/gdb/mi/mi-console.c
Tom Tromey 817003ed46 Rewrite output redirection and logging
This patch changes how gdb output redirection is done.

Currently, output is done via the UI.  gdb_stdout, for example, is a
define the expands to an lvalue referencing a field in the current UI.
When redirecting, this field may temporarily be reset; and when
logging is enabled or disabled, this is also done.

This has lead to bugs where the combination of redirection and logging
results in use-after-free.  Crashes are readily observable; see the
new test cases.

This patch upends this.  Now, gdb_stdout is simply an rvalue, and
refers to the current interpreter.  The interpreter provides ui_files
that do whatever rewriting is needed (mostly for MI); then output is
forward to the current UI via an indirection (see the new
ui::passthrough_file).

The ui provides paging, logging, timestamps, and the final stream that
writes to an actual file descriptor.

Redirection is handled at the ui layer.  Rather than changing the
output pipeline, new ui_files are simply swapped in by rewriting
pointers, hopefully with a scoped_restore.

Redirecting at the ui layer means that interpreter rewriting is still
applied when capturing output.  This fixes one of the reported bugs.

Not changing the pipeline means that the problems with the combination
of redirect and logging simply vanish.  Logging just changes a flag
and doesn't involve object destruction.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=17697
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28620
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28798
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28948
Approved-By: Andrew Burgess <aburgess@redhat.com>
2026-02-09 08:15:44 -07:00

95 lines
2.6 KiB
C

/* MI Console code.
Copyright (C) 2000-2026 Free Software Foundation, Inc.
Contributed by Cygnus Solutions (a Red Hat company).
This file is part of GDB.
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/>. */
/* An MI console is a kind of ui_file stream that sends output to
stdout, but encapsulated and prefixed with a distinctive string;
for instance, error output is normally identified by a leading
"&". */
#include "mi-console.h"
/* Create a console that wraps the given output stream RAW with the
string PREFIX and quoting it with QUOTE. */
mi_console_file::mi_console_file (ui_file *raw, const char *prefix, char quote)
: m_raw (raw),
m_prefix (prefix),
m_quote (quote)
{}
void
mi_console_file::write (const char *buf, long length_buf)
{
size_t prev_size = m_buffer.size ();
/* Append the text to our internal buffer. */
m_buffer.write (buf, length_buf);
/* Flush when an embedded newline is present anywhere in the
buffer. */
if (strchr (m_buffer.c_str () + prev_size, '\n') != NULL)
this->flush ();
}
void
mi_console_file::write_async_safe (const char *buf, long length_buf)
{
m_raw->write_async_safe (m_prefix, strlen (m_prefix));
if (m_quote)
{
m_raw->write_async_safe (&m_quote, 1);
m_raw->putstrn (buf, length_buf, m_quote, true);
m_raw->write_async_safe (&m_quote, 1);
}
else
m_raw->putstrn (buf, length_buf, 0, true);
char nl = '\n';
m_raw->write_async_safe (&nl, 1);
}
void
mi_console_file::flush ()
{
const std::string &str = m_buffer.string ();
/* Transform a byte sequence into a console output packet. */
if (!str.empty ())
{
size_t length_buf = str.size ();
const char *buf = str.data ();
gdb_puts (m_prefix, m_raw);
if (m_quote)
{
gdb_putc (m_quote, m_raw);
m_raw->putstrn (buf, length_buf, m_quote);
gdb_putc (m_quote, m_raw);
gdb_putc ('\n', m_raw);
}
else
{
m_raw->putstrn (buf, length_buf, 0);
gdb_putc ('\n', m_raw);
}
gdb_flush (m_raw);
}
m_buffer.clear ();
}