GDB (xrefs)
Loading...
Searching...
No Matches
ui.c
Go to the documentation of this file.
1/* Copyright (C) 2023 Free Software Foundation, Inc.
2
3 This file is part of GDB.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18#include "defs.h"
19#include "ui.h"
20
21#include "cli/cli-cmds.h"
22#include "event-top.h"
23#include "gdbsupport/buildargv.h"
24#include "gdbsupport/filestuff.h"
25#include "gdbsupport/gdb_file.h"
26#include "gdbsupport/scoped_fd.h"
27#include "interps.h"
28#include "pager.h"
29#include "main.h"
30#include "top.h"
31
32/* See top.h. */
33
34struct ui *main_ui;
35struct ui *current_ui;
36struct ui *ui_list;
37
38/* The highest UI number ever assigned. */
39
40static int highest_ui_num;
41
42/* See top.h. */
43
44ui::ui (FILE *instream_, FILE *outstream_, FILE *errstream_)
45 : num (++highest_ui_num),
46 stdin_stream (instream_),
47 instream (instream_),
48 outstream (outstream_),
49 errstream (errstream_),
50 input_fd (fileno (instream)),
56{
57 unbuffer_stream (instream_);
58
59 if (ui_list == NULL)
60 ui_list = this;
61 else
62 {
63 struct ui *last;
64
65 for (last = ui_list; last->next != NULL; last = last->next)
66 ;
67 last->next = this;
68 }
69}
70
72{
73 struct ui *ui, *uiprev;
74
75 uiprev = NULL;
76
77 for (ui = ui_list; ui != NULL; uiprev = ui, ui = ui->next)
78 if (ui == this)
79 break;
80
81 gdb_assert (ui != NULL);
82
83 if (uiprev != NULL)
84 uiprev->next = next;
85 else
86 ui_list = next;
87
88 delete m_gdb_stdin;
89 delete m_gdb_stdout;
90 delete m_gdb_stderr;
91}
92
93
94/* Returns whether GDB is running on an interactive terminal. */
95
96bool
98{
99 if (batch_flag)
100 return false;
101
104
106}
107
108
109/* When there is an event ready on the stdin file descriptor, instead
110 of calling readline directly throught the callback function, or
111 instead of calling gdb_readline_no_editing_callback, give gdb a
112 chance to detect errors and do something. */
113
114static void
115stdin_event_handler (int error, gdb_client_data client_data)
116{
117 struct ui *ui = (struct ui *) client_data;
118
119 if (error)
120 {
121 /* Switch to the main UI, so diagnostics always go there. */
123
125 if (main_ui == ui)
126 {
127 /* If stdin died, we may as well kill gdb. */
128 gdb_printf (gdb_stderr, _("error detected on stdin\n"));
129 quit_command ((char *) 0, 0);
130 }
131 else
132 {
133 /* Simply delete the UI. */
134 delete ui;
135 }
136 }
137 else
138 {
139 /* Switch to the UI whose input descriptor woke up the event
140 loop. */
141 current_ui = ui;
142
143 /* This makes sure a ^C immediately followed by further input is
144 always processed in that order. E.g,. with input like
145 "^Cprint 1\n", the SIGINT handler runs, marks the async
146 signal handler, and then select/poll may return with stdin
147 ready, instead of -1/EINTR. The
148 gdb.base/double-prompt-target-event-error.exp test exercises
149 this. */
150 QUIT;
151
152 do
153 {
155 ui->call_readline (client_data);
156 }
158 }
159}
160
161/* See top.h. */
162
163void
165{
166 if (input_fd != -1)
167 add_file_handler (input_fd, stdin_event_handler, this,
168 string_printf ("ui-%d", num), true);
169}
170
171/* See top.h. */
172
173void
175{
176 if (input_fd != -1)
177 delete_file_handler (input_fd);
178}
179
180/* Open file named NAME for read/write, making sure not to make it the
181 controlling terminal. */
182
183static gdb_file_up
185{
186 scoped_fd fd = gdb_open_cloexec (name, O_RDWR | O_NOCTTY, 0);
187 if (fd.get () < 0)
188 perror_with_name (_("opening terminal failed"));
189
190 return fd.to_file ("w+");
191}
192
193/* Implementation of the "new-ui" command. */
194
195static void
196new_ui_command (const char *args, int from_tty)
197{
198 int argc;
199 const char *interpreter_name;
200 const char *tty_name;
201
202 dont_repeat ();
203
204 gdb_argv argv (args);
205 argc = argv.count ();
206
207 if (argc < 2)
208 error (_("Usage: new-ui INTERPRETER TTY"));
209
210 interpreter_name = argv[0];
211 tty_name = argv[1];
212
213 {
214 scoped_restore save_ui = make_scoped_restore (&current_ui);
215
216 /* Open specified terminal. Note: we used to open it three times,
217 once for each of stdin/stdout/stderr, but that does not work
218 with Windows named pipes. */
219 gdb_file_up stream = open_terminal_stream (tty_name);
220
221 std::unique_ptr<ui> ui
222 (new struct ui (stream.get (), stream.get (), stream.get ()));
223
224 ui->async = 1;
225
226 current_ui = ui.get ();
227
228 set_top_level_interpreter (interpreter_name);
229
231
232 /* Make sure the file is not closed. */
233 stream.release ();
234
235 ui.release ();
236 }
237
238 gdb_printf ("New UI allocated\n");
239}
240
241void _initialize_ui ();
242void
244{
246Create a new UI.\n\
247Usage: new-ui INTERPRETER TTY\n\
248The first argument is the name of the interpreter to run.\n\
249The second argument is the terminal the UI runs on."), &cmdlist);
251}
const char *const name
virtual void pre_command_loop()
Definition interps.h:78
struct cmd_list_element * cmdlist
Definition cli-cmds.c:87
void quit_command(const char *args, int from_tty)
Definition cli-cmds.c:477
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition cli-decode.c:233
void set_cmd_completer(struct cmd_list_element *cmd, completer_ftype *completer)
Definition cli-decode.c:117
void dont_repeat()
Definition top.c:696
@ class_support
Definition command.h:58
#define ISATTY(FP)
Definition defs.h:570
@ AUTO_BOOLEAN_TRUE
Definition defs.h:248
@ AUTO_BOOLEAN_AUTO
Definition defs.h:250
#define QUIT
Definition defs.h:187
int call_stdin_event_handler_again_p
Definition event-top.c:103
#define O_NOCTTY
Definition inflow.c:44
struct interp * top_level_interpreter(void)
Definition interps.c:345
void set_top_level_interpreter(const char *name)
Definition interps.c:191
void interpreter_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition interps.c:328
int batch_flag
Definition main.c:84
Definition ui.h:55
struct ui_file * m_gdb_stdin
Definition ui.h:146
struct ui_file * m_gdb_stdlog
Definition ui.h:151
int num
Definition ui.h:66
struct ui_file * m_gdb_stdout
Definition ui.h:144
int async
Definition ui.h:106
struct ui_file * m_gdb_stderr
Definition ui.h:148
bool input_interactive_p() const
Definition ui.c:97
ui(FILE *instream, FILE *outstream, FILE *errstream)
Definition ui.c:44
FILE * outstream
Definition ui.h:121
int input_fd
Definition ui.h:128
struct ui * next
Definition ui.h:63
bool m_input_interactive_p
Definition ui.h:133
FILE * stdin_stream
Definition ui.h:113
~ui()
Definition ui.c:71
void unregister_file_handler()
Definition ui.c:174
void(* call_readline)(gdb_client_data)
Definition ui.h:78
void register_file_handler()
Definition ui.c:164
FILE * errstream
Definition ui.h:123
FILE * instream
Definition ui.h:119
void unbuffer_stream(FILE *stream)
Definition top.c:261
auto_boolean interactive_mode
Definition top.c:1831
struct ui * ui_list
Definition ui.c:36
static gdb_file_up open_terminal_stream(const char *name)
Definition ui.c:184
struct ui * main_ui
Definition ui.c:34
static void stdin_event_handler(int error, gdb_client_data client_data)
Definition ui.c:115
struct ui * current_ui
Definition ui.c:35
static void new_ui_command(const char *args, int from_tty)
Definition ui.c:196
static int highest_ui_num
Definition ui.c:40
void _initialize_ui()
Definition ui.c:243
struct ui * ui_list
Definition ui.c:36
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define gdb_stderr
Definition utils.h:187