Only send sync execution command output to the UI that ran the command
[deliverable/binutils-gdb.git] / gdb / tui / tui-interp.c
1 /* TUI Interpreter definitions for GDB, the GNU debugger.
2
3 Copyright (C) 2003-2016 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "cli/cli-interp.h"
22 #include "interps.h"
23 #include "top.h"
24 #include "event-top.h"
25 #include "event-loop.h"
26 #include "ui-out.h"
27 #include "cli-out.h"
28 #include "tui/tui-data.h"
29 #include "readline/readline.h"
30 #include "tui/tui-win.h"
31 #include "tui/tui.h"
32 #include "tui/tui-io.h"
33 #include "infrun.h"
34 #include "observer.h"
35 #include "gdbthread.h"
36
37 static struct ui_out *tui_ui_out (struct interp *self);
38
39 /* Set to 1 when the TUI mode must be activated when we first start
40 gdb. */
41 static int tui_start_enabled = 0;
42
43 /* Returns the INTERP if the INTERP is a TUI, and returns NULL
44 otherwise. */
45
46 static struct interp *
47 as_tui_interp (struct interp *interp)
48 {
49 if (strcmp (interp_name (interp), INTERP_TUI) == 0)
50 return interp;
51 return NULL;
52 }
53
54 /* Cleanup the tui before exiting. */
55
56 static void
57 tui_exit (void)
58 {
59 /* Disable the tui. Curses mode is left leaving the screen in a
60 clean state (see endwin()). */
61 tui_disable ();
62 }
63
64 /* Observers for several run control events. If the interpreter is
65 quiet (i.e., another interpreter is being run with
66 interpreter-exec), print nothing. */
67
68 /* Observer for the normal_stop notification. */
69
70 static void
71 tui_on_normal_stop (struct bpstats *bs, int print_frame)
72 {
73 struct switch_thru_all_uis state;
74
75 if (!print_frame)
76 return;
77
78 SWITCH_THRU_ALL_UIS (state)
79 {
80 struct interp *interp = top_level_interpreter ();
81 struct interp *tui = as_tui_interp (interp);
82 struct thread_info *thread;
83
84 if (tui == NULL)
85 continue;
86
87 thread = inferior_thread ();
88 if (should_print_stop_to_console (interp, thread))
89 print_stop_event (tui_ui_out (tui));
90 }
91 }
92
93 /* Observer for the signal_received notification. */
94
95 static void
96 tui_on_signal_received (enum gdb_signal siggnal)
97 {
98 struct switch_thru_all_uis state;
99
100 SWITCH_THRU_ALL_UIS (state)
101 {
102 struct interp *tui = as_tui_interp (top_level_interpreter ());
103
104 if (tui == NULL)
105 continue;
106
107 print_signal_received_reason (tui_ui_out (tui), siggnal);
108 }
109 }
110
111 /* Observer for the end_stepping_range notification. */
112
113 static void
114 tui_on_end_stepping_range (void)
115 {
116 struct switch_thru_all_uis state;
117
118 SWITCH_THRU_ALL_UIS (state)
119 {
120 struct interp *tui = as_tui_interp (top_level_interpreter ());
121
122 if (tui == NULL)
123 continue;
124
125 print_end_stepping_range_reason (tui_ui_out (tui));
126 }
127 }
128
129 /* Observer for the signal_exited notification. */
130
131 static void
132 tui_on_signal_exited (enum gdb_signal siggnal)
133 {
134 struct switch_thru_all_uis state;
135
136 SWITCH_THRU_ALL_UIS (state)
137 {
138 struct interp *tui = as_tui_interp (top_level_interpreter ());
139
140 if (tui == NULL)
141 continue;
142
143 print_signal_exited_reason (tui_ui_out (tui), siggnal);
144 }
145 }
146
147 /* Observer for the exited notification. */
148
149 static void
150 tui_on_exited (int exitstatus)
151 {
152 struct switch_thru_all_uis state;
153
154 SWITCH_THRU_ALL_UIS (state)
155 {
156 struct interp *tui = as_tui_interp (top_level_interpreter ());
157
158 if (tui == NULL)
159 continue;
160
161 print_exited_reason (tui_ui_out (tui), exitstatus);
162 }
163 }
164
165 /* Observer for the no_history notification. */
166
167 static void
168 tui_on_no_history (void)
169 {
170 struct switch_thru_all_uis state;
171
172 SWITCH_THRU_ALL_UIS (state)
173 {
174 struct interp *tui = as_tui_interp (top_level_interpreter ());
175
176 if (tui == NULL)
177 continue;
178
179 print_no_history_reason (tui_ui_out (tui));
180 }
181 }
182
183 /* Observer for the sync_execution_done notification. */
184
185 static void
186 tui_on_sync_execution_done (void)
187 {
188 struct interp *tui = as_tui_interp (top_level_interpreter ());
189
190 if (tui == NULL)
191 return;
192
193 display_gdb_prompt (NULL);
194 }
195
196 /* Observer for the command_error notification. */
197
198 static void
199 tui_on_command_error (void)
200 {
201 struct interp *tui = as_tui_interp (top_level_interpreter ());
202
203 if (tui == NULL)
204 return;
205
206 display_gdb_prompt (NULL);
207 }
208
209 /* These implement the TUI interpreter. */
210
211 static void *
212 tui_init (struct interp *self, int top_level)
213 {
214 /* Install exit handler to leave the screen in a good shape. */
215 atexit (tui_exit);
216
217 tui_initialize_static_data ();
218
219 tui_initialize_io ();
220 tui_initialize_win ();
221 if (ui_file_isatty (gdb_stdout))
222 tui_initialize_readline ();
223
224 return NULL;
225 }
226
227 static int
228 tui_resume (void *data)
229 {
230 struct ui *ui = current_ui;
231 struct ui_file *stream;
232
233 /* gdb_setup_readline will change gdb_stdout. If the TUI was
234 previously writing to gdb_stdout, then set it to the new
235 gdb_stdout afterwards. */
236
237 stream = cli_out_set_stream (tui_old_uiout, gdb_stdout);
238 if (stream != gdb_stdout)
239 {
240 cli_out_set_stream (tui_old_uiout, stream);
241 stream = NULL;
242 }
243
244 gdb_setup_readline (1);
245
246 ui->input_handler = command_line_handler;
247
248 if (stream != NULL)
249 cli_out_set_stream (tui_old_uiout, gdb_stdout);
250
251 if (tui_start_enabled)
252 tui_enable ();
253 return 1;
254 }
255
256 static int
257 tui_suspend (void *data)
258 {
259 tui_start_enabled = tui_active;
260 tui_disable ();
261 return 1;
262 }
263
264 static struct ui_out *
265 tui_ui_out (struct interp *self)
266 {
267 if (tui_active)
268 return tui_out;
269 else
270 return tui_old_uiout;
271 }
272
273 static struct gdb_exception
274 tui_exec (void *data, const char *command_str)
275 {
276 internal_error (__FILE__, __LINE__, _("tui_exec called"));
277 }
278
279 /* The TUI interpreter's vtable. */
280
281 static const struct interp_procs tui_interp_procs = {
282 tui_init,
283 tui_resume,
284 tui_suspend,
285 tui_exec,
286 tui_ui_out,
287 NULL,
288 cli_interpreter_pre_command_loop,
289 cli_interpreter_supports_command_editing,
290 };
291
292 /* Factory for TUI interpreters. */
293
294 static struct interp *
295 tui_interp_factory (const char *name)
296 {
297 return interp_new (name, &tui_interp_procs, NULL);
298 }
299
300 /* Provide a prototype to silence -Wmissing-prototypes. */
301 extern initialize_file_ftype _initialize_tui_interp;
302
303 void
304 _initialize_tui_interp (void)
305 {
306 interp_factory_register (INTERP_TUI, tui_interp_factory);
307
308 if (interpreter_p && strcmp (interpreter_p, INTERP_TUI) == 0)
309 tui_start_enabled = 1;
310
311 if (interpreter_p && strcmp (interpreter_p, INTERP_CONSOLE) == 0)
312 {
313 xfree (interpreter_p);
314 interpreter_p = xstrdup (INTERP_TUI);
315 }
316
317 /* If changing this, remember to update cli-interp.c as well. */
318 observer_attach_normal_stop (tui_on_normal_stop);
319 observer_attach_signal_received (tui_on_signal_received);
320 observer_attach_end_stepping_range (tui_on_end_stepping_range);
321 observer_attach_signal_exited (tui_on_signal_exited);
322 observer_attach_exited (tui_on_exited);
323 observer_attach_no_history (tui_on_no_history);
324 observer_attach_sync_execution_done (tui_on_sync_execution_done);
325 observer_attach_command_error (tui_on_command_error);
326 }
This page took 0.039597 seconds and 5 git commands to generate.