binutils-gdb/gdb/cli/cli-interp.h
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

49 lines
1.7 KiB
C++

/* CLI Definitions for GDB, the GNU debugger.
Copyright (C) 2016-2026 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/>. */
#ifndef GDB_CLI_CLI_INTERP_H
#define GDB_CLI_CLI_INTERP_H
#include "interps.h"
/* A console-like interpreter. Implements functionality common to the
CLI and the TUI. */
class cli_interp_base : public interp
{
public:
explicit cli_interp_base (const char *name);
virtual ~cli_interp_base () = 0;
void pre_command_loop () override;
bool supports_command_editing () override;
void on_signal_received (gdb_signal sig) override;
void on_signal_exited (gdb_signal sig) override;
void on_normal_stop (bpstat *bs, int print_frame) override;
void on_exited (int status) override;
void on_no_history () override;
void on_sync_execution_done () override;
void on_command_error () override;
void on_user_selected_context_changed (user_selected_what selection) override;
};
/* Returns true if the current stop should be printed to
CONSOLE_INTERP. */
extern int should_print_stop_to_console (struct interp *interp,
struct thread_info *tp);
#endif /* GDB_CLI_CLI_INTERP_H */