Commit | Line | Data |
---|---|---|
021e7609 AC |
1 | /* TUI Interpreter definitions for GDB, the GNU debugger. |
2 | ||
618f726f | 3 | Copyright (C) 2003-2016 Free Software Foundation, Inc. |
021e7609 AC |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
021e7609 AC |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
021e7609 AC |
19 | |
20 | #include "defs.h" | |
3c216924 | 21 | #include "cli/cli-interp.h" |
021e7609 AC |
22 | #include "interps.h" |
23 | #include "top.h" | |
24 | #include "event-top.h" | |
25 | #include "event-loop.h" | |
26 | #include "ui-out.h" | |
95cd630e | 27 | #include "cli-out.h" |
d7b2e967 | 28 | #include "tui/tui-data.h" |
021e7609 | 29 | #include "readline/readline.h" |
d7b2e967 | 30 | #include "tui/tui-win.h" |
021e7609 | 31 | #include "tui/tui.h" |
d7b2e967 | 32 | #include "tui/tui-io.h" |
fd664c91 PA |
33 | #include "infrun.h" |
34 | #include "observer.h" | |
eaae60fd | 35 | #include "gdbthread.h" |
fd664c91 PA |
36 | |
37 | static struct ui_out *tui_ui_out (struct interp *self); | |
021e7609 | 38 | |
1cc6d956 MS |
39 | /* Set to 1 when the TUI mode must be activated when we first start |
40 | gdb. */ | |
63858210 SC |
41 | static int tui_start_enabled = 0; |
42 | ||
73ab01a0 PA |
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 | } | |
fd664c91 | 53 | |
021e7609 AC |
54 | /* Cleanup the tui before exiting. */ |
55 | ||
56 | static void | |
57 | tui_exit (void) | |
58 | { | |
1cc6d956 MS |
59 | /* Disable the tui. Curses mode is left leaving the screen in a |
60 | clean state (see endwin()). */ | |
021e7609 AC |
61 | tui_disable (); |
62 | } | |
63 | ||
fd664c91 PA |
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 | ||
243a9253 PA |
68 | /* Observer for the normal_stop notification. */ |
69 | ||
70 | static void | |
71 | tui_on_normal_stop (struct bpstats *bs, int print_frame) | |
72 | { | |
73ab01a0 PA |
73 | struct switch_thru_all_uis state; |
74 | ||
eaae60fd PA |
75 | if (!print_frame) |
76 | return; | |
77 | ||
73ab01a0 | 78 | SWITCH_THRU_ALL_UIS (state) |
243a9253 | 79 | { |
eaae60fd PA |
80 | struct interp *interp = top_level_interpreter (); |
81 | struct interp *tui = as_tui_interp (interp); | |
82 | struct thread_info *thread; | |
73ab01a0 PA |
83 | |
84 | if (tui == NULL) | |
85 | continue; | |
86 | ||
eaae60fd PA |
87 | thread = inferior_thread (); |
88 | if (should_print_stop_to_console (interp, thread)) | |
73ab01a0 | 89 | print_stop_event (tui_ui_out (tui)); |
243a9253 PA |
90 | } |
91 | } | |
92 | ||
fd664c91 PA |
93 | /* Observer for the signal_received notification. */ |
94 | ||
95 | static void | |
96 | tui_on_signal_received (enum gdb_signal siggnal) | |
97 | { | |
73ab01a0 PA |
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 | } | |
fd664c91 PA |
109 | } |
110 | ||
111 | /* Observer for the end_stepping_range notification. */ | |
112 | ||
113 | static void | |
114 | tui_on_end_stepping_range (void) | |
115 | { | |
73ab01a0 PA |
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 | } | |
fd664c91 PA |
127 | } |
128 | ||
129 | /* Observer for the signal_exited notification. */ | |
130 | ||
131 | static void | |
132 | tui_on_signal_exited (enum gdb_signal siggnal) | |
133 | { | |
73ab01a0 PA |
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 | } | |
fd664c91 PA |
145 | } |
146 | ||
147 | /* Observer for the exited notification. */ | |
148 | ||
149 | static void | |
150 | tui_on_exited (int exitstatus) | |
151 | { | |
73ab01a0 PA |
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 | } | |
fd664c91 PA |
163 | } |
164 | ||
165 | /* Observer for the no_history notification. */ | |
166 | ||
167 | static void | |
168 | tui_on_no_history (void) | |
169 | { | |
73ab01a0 PA |
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 | } | |
fd664c91 PA |
181 | } |
182 | ||
92bcb5f9 PA |
183 | /* Observer for the sync_execution_done notification. */ |
184 | ||
185 | static void | |
186 | tui_on_sync_execution_done (void) | |
187 | { | |
73ab01a0 PA |
188 | struct interp *tui = as_tui_interp (top_level_interpreter ()); |
189 | ||
190 | if (tui == NULL) | |
191 | return; | |
192 | ||
193 | display_gdb_prompt (NULL); | |
92bcb5f9 PA |
194 | } |
195 | ||
196 | /* Observer for the command_error notification. */ | |
197 | ||
198 | static void | |
199 | tui_on_command_error (void) | |
200 | { | |
73ab01a0 PA |
201 | struct interp *tui = as_tui_interp (top_level_interpreter ()); |
202 | ||
203 | if (tui == NULL) | |
204 | return; | |
205 | ||
206 | display_gdb_prompt (NULL); | |
92bcb5f9 PA |
207 | } |
208 | ||
4034d0ff AT |
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 | ||
021e7609 AC |
240 | /* These implement the TUI interpreter. */ |
241 | ||
242 | static void * | |
4801a9a3 | 243 | tui_init (struct interp *self, int top_level) |
021e7609 AC |
244 | { |
245 | /* Install exit handler to leave the screen in a good shape. */ | |
246 | atexit (tui_exit); | |
247 | ||
dd1abb8c | 248 | tui_initialize_static_data (); |
021e7609 AC |
249 | |
250 | tui_initialize_io (); | |
9612b5ec | 251 | tui_initialize_win (); |
1180b2c8 L |
252 | if (ui_file_isatty (gdb_stdout)) |
253 | tui_initialize_readline (); | |
021e7609 AC |
254 | |
255 | return NULL; | |
256 | } | |
257 | ||
258 | static int | |
259 | tui_resume (void *data) | |
260 | { | |
3c216924 | 261 | struct ui *ui = current_ui; |
95cd630e DJ |
262 | struct ui_file *stream; |
263 | ||
1cc6d956 MS |
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. */ | |
95cd630e DJ |
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 | ||
3c216924 PA |
275 | gdb_setup_readline (1); |
276 | ||
277 | ui->input_handler = command_line_handler; | |
95cd630e DJ |
278 | |
279 | if (stream != NULL) | |
280 | cli_out_set_stream (tui_old_uiout, gdb_stdout); | |
281 | ||
63858210 SC |
282 | if (tui_start_enabled) |
283 | tui_enable (); | |
021e7609 AC |
284 | return 1; |
285 | } | |
286 | ||
287 | static int | |
288 | tui_suspend (void *data) | |
289 | { | |
63858210 | 290 | tui_start_enabled = tui_active; |
021e7609 AC |
291 | tui_disable (); |
292 | return 1; | |
293 | } | |
294 | ||
4801a9a3 PA |
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 | ||
71fff37b | 304 | static struct gdb_exception |
021e7609 AC |
305 | tui_exec (void *data, const char *command_str) |
306 | { | |
e2e0b3e5 | 307 | internal_error (__FILE__, __LINE__, _("tui_exec called")); |
021e7609 AC |
308 | } |
309 | ||
8322445e PA |
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, | |
b2d86570 | 319 | cli_interpreter_pre_command_loop, |
3c216924 | 320 | cli_interpreter_supports_command_editing, |
8322445e PA |
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 | ||
2c0b251b PA |
331 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
332 | extern initialize_file_ftype _initialize_tui_interp; | |
333 | ||
021e7609 AC |
334 | void |
335 | _initialize_tui_interp (void) | |
336 | { | |
8322445e PA |
337 | interp_factory_register (INTERP_TUI, tui_interp_factory); |
338 | ||
cc4349ed | 339 | if (interpreter_p && strcmp (interpreter_p, INTERP_TUI) == 0) |
63858210 SC |
340 | tui_start_enabled = 1; |
341 | ||
342 | if (interpreter_p && strcmp (interpreter_p, INTERP_CONSOLE) == 0) | |
343 | { | |
344 | xfree (interpreter_p); | |
cc4349ed | 345 | interpreter_p = xstrdup (INTERP_TUI); |
63858210 | 346 | } |
73ab01a0 PA |
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); | |
4034d0ff AT |
357 | observer_attach_user_selected_context_changed |
358 | (tui_on_user_selected_context_changed); | |
021e7609 | 359 | } |