Emit inferior, thread and frame selection events to all UIs
[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 /* Observer for the user_selected_context_changed notification. */
210
211 static void
212 tui_on_user_selected_context_changed (user_selected_what selection)
213 {
214 struct switch_thru_all_uis state;
215 struct thread_info *tp;
216
217 /* This event is suppressed. */
218 if (cli_suppress_notification.user_selected_context)
219 return;
220
221 tp = find_thread_ptid (inferior_ptid);
222
223 SWITCH_THRU_ALL_UIS (state)
224 {
225 struct interp *tui = as_tui_interp (top_level_interpreter ());
226
227 if (tui == NULL)
228 continue;
229
230 if (selection & USER_SELECTED_INFERIOR)
231 print_selected_inferior (tui_ui_out (tui));
232
233 if (tp != NULL
234 && ((selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME))))
235 print_selected_thread_frame (tui_ui_out (tui), selection);
236
237 }
238 }
239
240 /* These implement the TUI interpreter. */
241
242 static void *
243 tui_init (struct interp *self, int top_level)
244 {
245 /* Install exit handler to leave the screen in a good shape. */
246 atexit (tui_exit);
247
248 tui_initialize_static_data ();
249
250 tui_initialize_io ();
251 tui_initialize_win ();
252 if (ui_file_isatty (gdb_stdout))
253 tui_initialize_readline ();
254
255 return NULL;
256 }
257
258 static int
259 tui_resume (void *data)
260 {
261 struct ui *ui = current_ui;
262 struct ui_file *stream;
263
264 /* gdb_setup_readline will change gdb_stdout. If the TUI was
265 previously writing to gdb_stdout, then set it to the new
266 gdb_stdout afterwards. */
267
268 stream = cli_out_set_stream (tui_old_uiout, gdb_stdout);
269 if (stream != gdb_stdout)
270 {
271 cli_out_set_stream (tui_old_uiout, stream);
272 stream = NULL;
273 }
274
275 gdb_setup_readline (1);
276
277 ui->input_handler = command_line_handler;
278
279 if (stream != NULL)
280 cli_out_set_stream (tui_old_uiout, gdb_stdout);
281
282 if (tui_start_enabled)
283 tui_enable ();
284 return 1;
285 }
286
287 static int
288 tui_suspend (void *data)
289 {
290 tui_start_enabled = tui_active;
291 tui_disable ();
292 return 1;
293 }
294
295 static struct ui_out *
296 tui_ui_out (struct interp *self)
297 {
298 if (tui_active)
299 return tui_out;
300 else
301 return tui_old_uiout;
302 }
303
304 static struct gdb_exception
305 tui_exec (void *data, const char *command_str)
306 {
307 internal_error (__FILE__, __LINE__, _("tui_exec called"));
308 }
309
310 /* The TUI interpreter's vtable. */
311
312 static const struct interp_procs tui_interp_procs = {
313 tui_init,
314 tui_resume,
315 tui_suspend,
316 tui_exec,
317 tui_ui_out,
318 NULL,
319 cli_interpreter_pre_command_loop,
320 cli_interpreter_supports_command_editing,
321 };
322
323 /* Factory for TUI interpreters. */
324
325 static struct interp *
326 tui_interp_factory (const char *name)
327 {
328 return interp_new (name, &tui_interp_procs, NULL);
329 }
330
331 /* Provide a prototype to silence -Wmissing-prototypes. */
332 extern initialize_file_ftype _initialize_tui_interp;
333
334 void
335 _initialize_tui_interp (void)
336 {
337 interp_factory_register (INTERP_TUI, tui_interp_factory);
338
339 if (interpreter_p && strcmp (interpreter_p, INTERP_TUI) == 0)
340 tui_start_enabled = 1;
341
342 if (interpreter_p && strcmp (interpreter_p, INTERP_CONSOLE) == 0)
343 {
344 xfree (interpreter_p);
345 interpreter_p = xstrdup (INTERP_TUI);
346 }
347
348 /* If changing this, remember to update cli-interp.c as well. */
349 observer_attach_normal_stop (tui_on_normal_stop);
350 observer_attach_signal_received (tui_on_signal_received);
351 observer_attach_end_stepping_range (tui_on_end_stepping_range);
352 observer_attach_signal_exited (tui_on_signal_exited);
353 observer_attach_exited (tui_on_exited);
354 observer_attach_no_history (tui_on_no_history);
355 observer_attach_sync_execution_done (tui_on_sync_execution_done);
356 observer_attach_command_error (tui_on_command_error);
357 observer_attach_user_selected_context_changed
358 (tui_on_user_selected_context_changed);
359 }
This page took 0.046098 seconds and 5 git commands to generate.