Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / cli / cli-interp.c
1 /* CLI Definitions for GDB, the GNU debugger.
2
3 Copyright (C) 2002-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-interp.h"
22 #include "interps.h"
23 #include "event-top.h"
24 #include "ui-out.h"
25 #include "cli-out.h"
26 #include "top.h" /* for "execute_command" */
27 #include "event-top.h"
28 #include "infrun.h"
29 #include "observer.h"
30 #include "gdbthread.h"
31 #include "thread-fsm.h"
32
33 /* The console interpreter. */
34 struct cli_interp
35 {
36 /* The ui_out for the console interpreter. */
37 struct ui_out *cli_uiout;
38 };
39
40 /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
41 and returns NULL otherwise. */
42
43 static struct cli_interp *
44 as_cli_interp (struct interp *interp)
45 {
46 if (strcmp (interp_name (interp), INTERP_CONSOLE) == 0)
47 return (struct cli_interp *) interp_data (interp);
48 return NULL;
49 }
50
51 /* Longjmp-safe wrapper for "execute_command". */
52 static struct gdb_exception safe_execute_command (struct ui_out *uiout,
53 char *command,
54 int from_tty);
55
56 /* See cli-interp.h.
57
58 Breakpoint hits should always be mirrored to a console. Deciding
59 what to mirror to a console wrt to breakpoints and random stops
60 gets messy real fast. E.g., say "s" trips on a breakpoint. We'd
61 clearly want to mirror the event to the console in this case. But
62 what about more complicated cases like "s&; thread n; s&", and one
63 of those steps spawning a new thread, and that thread hitting a
64 breakpoint? It's impossible in general to track whether the thread
65 had any relation to the commands that had been executed. So we
66 just simplify and always mirror breakpoints and random events to
67 all consoles.
68
69 OTOH, we should print the source line to the console when stepping
70 or other similar commands, iff the step was started by that console
71 (or in MI's case, by a console command), but not if it was started
72 with MI's -exec-step or similar. */
73
74 int
75 should_print_stop_to_console (struct interp *console_interp,
76 struct thread_info *tp)
77 {
78 if ((bpstat_what (tp->control.stop_bpstat).main_action
79 == BPSTAT_WHAT_STOP_NOISY)
80 || tp->thread_fsm == NULL
81 || tp->thread_fsm->command_interp == console_interp
82 || !thread_fsm_finished_p (tp->thread_fsm))
83 return 1;
84 return 0;
85 }
86
87 /* Observers for several run control events. If the interpreter is
88 quiet (i.e., another interpreter is being run with
89 interpreter-exec), print nothing. */
90
91 /* Observer for the normal_stop notification. */
92
93 static void
94 cli_on_normal_stop (struct bpstats *bs, int print_frame)
95 {
96 struct switch_thru_all_uis state;
97
98 if (!print_frame)
99 return;
100
101 SWITCH_THRU_ALL_UIS (state)
102 {
103 struct interp *interp = top_level_interpreter ();
104 struct cli_interp *cli = as_cli_interp (interp);
105 struct thread_info *thread;
106
107 if (cli == NULL)
108 continue;
109
110 thread = inferior_thread ();
111 if (should_print_stop_to_console (interp, thread))
112 print_stop_event (cli->cli_uiout);
113 }
114 }
115
116 /* Observer for the signal_received notification. */
117
118 static void
119 cli_on_signal_received (enum gdb_signal siggnal)
120 {
121 struct switch_thru_all_uis state;
122
123 SWITCH_THRU_ALL_UIS (state)
124 {
125 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
126
127 if (cli == NULL)
128 continue;
129
130 print_signal_received_reason (cli->cli_uiout, siggnal);
131 }
132 }
133
134 /* Observer for the end_stepping_range notification. */
135
136 static void
137 cli_on_end_stepping_range (void)
138 {
139 struct switch_thru_all_uis state;
140
141 SWITCH_THRU_ALL_UIS (state)
142 {
143 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
144
145 if (cli == NULL)
146 continue;
147
148 print_end_stepping_range_reason (cli->cli_uiout);
149 }
150 }
151
152 /* Observer for the signalled notification. */
153
154 static void
155 cli_on_signal_exited (enum gdb_signal siggnal)
156 {
157 struct switch_thru_all_uis state;
158
159 SWITCH_THRU_ALL_UIS (state)
160 {
161 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
162
163 if (cli == NULL)
164 continue;
165
166 print_signal_exited_reason (cli->cli_uiout, siggnal);
167 }
168 }
169
170 /* Observer for the exited notification. */
171
172 static void
173 cli_on_exited (int exitstatus)
174 {
175 struct switch_thru_all_uis state;
176
177 SWITCH_THRU_ALL_UIS (state)
178 {
179 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
180
181 if (cli == NULL)
182 continue;
183
184 print_exited_reason (cli->cli_uiout, exitstatus);
185 }
186 }
187
188 /* Observer for the no_history notification. */
189
190 static void
191 cli_on_no_history (void)
192 {
193 struct switch_thru_all_uis state;
194
195 SWITCH_THRU_ALL_UIS (state)
196 {
197 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
198
199 if (cli == NULL)
200 continue;
201
202 print_no_history_reason (cli->cli_uiout);
203 }
204 }
205
206 /* Observer for the sync_execution_done notification. */
207
208 static void
209 cli_on_sync_execution_done (void)
210 {
211 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
212
213 if (cli == NULL)
214 return;
215
216 display_gdb_prompt (NULL);
217 }
218
219 /* Observer for the command_error notification. */
220
221 static void
222 cli_on_command_error (void)
223 {
224 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
225
226 if (cli == NULL)
227 return;
228
229 display_gdb_prompt (NULL);
230 }
231
232 /* pre_command_loop implementation. */
233
234 void
235 cli_interpreter_pre_command_loop (struct interp *self)
236 {
237 display_gdb_prompt (0);
238 }
239
240 /* These implement the cli out interpreter: */
241
242 static void *
243 cli_interpreter_init (struct interp *self, int top_level)
244 {
245 return interp_data (self);
246 }
247
248 static int
249 cli_interpreter_resume (void *data)
250 {
251 struct ui *ui = current_ui;
252 struct cli_interp *cli = (struct cli_interp *) data;
253 struct ui_file *stream;
254
255 /*sync_execution = 1; */
256
257 /* gdb_setup_readline will change gdb_stdout. If the CLI was
258 previously writing to gdb_stdout, then set it to the new
259 gdb_stdout afterwards. */
260
261 stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
262 if (stream != gdb_stdout)
263 {
264 cli_out_set_stream (cli->cli_uiout, stream);
265 stream = NULL;
266 }
267
268 gdb_setup_readline (1);
269
270 ui->input_handler = command_line_handler;
271
272 if (stream != NULL)
273 cli_out_set_stream (cli->cli_uiout, gdb_stdout);
274
275 return 1;
276 }
277
278 static int
279 cli_interpreter_suspend (void *data)
280 {
281 gdb_disable_readline ();
282 return 1;
283 }
284
285 static struct gdb_exception
286 cli_interpreter_exec (void *data, const char *command_str)
287 {
288 struct cli_interp *cli = (struct cli_interp *) data;
289 struct ui_file *old_stream;
290 struct gdb_exception result;
291
292 /* FIXME: cagney/2003-02-01: Need to const char *propogate
293 safe_execute_command. */
294 char *str = (char *) alloca (strlen (command_str) + 1);
295 strcpy (str, command_str);
296
297 /* gdb_stdout could change between the time cli_uiout was
298 initialized and now. Since we're probably using a different
299 interpreter which has a new ui_file for gdb_stdout, use that one
300 instead of the default.
301
302 It is important that it gets reset everytime, since the user
303 could set gdb to use a different interpreter. */
304 old_stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
305 result = safe_execute_command (cli->cli_uiout, str, 1);
306 cli_out_set_stream (cli->cli_uiout, old_stream);
307 return result;
308 }
309
310 int
311 cli_interpreter_supports_command_editing (struct interp *interp)
312 {
313 return 1;
314 }
315
316 static struct gdb_exception
317 safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
318 {
319 struct gdb_exception e = exception_none;
320 struct ui_out *saved_uiout;
321
322 /* Save and override the global ``struct ui_out'' builder. */
323 saved_uiout = current_uiout;
324 current_uiout = command_uiout;
325
326 TRY
327 {
328 execute_command (command, from_tty);
329 }
330 CATCH (exception, RETURN_MASK_ALL)
331 {
332 e = exception;
333 }
334 END_CATCH
335
336 /* Restore the global builder. */
337 current_uiout = saved_uiout;
338
339 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
340 caller should print the exception. */
341 exception_print (gdb_stderr, e);
342 return e;
343 }
344
345 static struct ui_out *
346 cli_ui_out (struct interp *self)
347 {
348 struct cli_interp *cli = (struct cli_interp *) interp_data (self);
349
350 return cli->cli_uiout;
351 }
352
353 /* The CLI interpreter's vtable. */
354
355 static const struct interp_procs cli_interp_procs = {
356 cli_interpreter_init, /* init_proc */
357 cli_interpreter_resume, /* resume_proc */
358 cli_interpreter_suspend, /* suspend_proc */
359 cli_interpreter_exec, /* exec_proc */
360 cli_ui_out, /* ui_out_proc */
361 NULL, /* set_logging_proc */
362 cli_interpreter_pre_command_loop, /* pre_command_loop_proc */
363 cli_interpreter_supports_command_editing, /* supports_command_editing_proc */
364 };
365
366 /* Factory for CLI interpreters. */
367
368 static struct interp *
369 cli_interp_factory (const char *name)
370 {
371 struct cli_interp *cli = XNEW (struct cli_interp);
372
373 /* Create a default uiout builder for the CLI. */
374 cli->cli_uiout = cli_out_new (gdb_stdout);
375
376 return interp_new (name, &cli_interp_procs, cli);
377 }
378
379 /* Standard gdb initialization hook. */
380 extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
381
382 void
383 _initialize_cli_interp (void)
384 {
385 interp_factory_register (INTERP_CONSOLE, cli_interp_factory);
386
387 /* If changing this, remember to update tui-interp.c as well. */
388 observer_attach_normal_stop (cli_on_normal_stop);
389 observer_attach_end_stepping_range (cli_on_end_stepping_range);
390 observer_attach_signal_received (cli_on_signal_received);
391 observer_attach_signal_exited (cli_on_signal_exited);
392 observer_attach_exited (cli_on_exited);
393 observer_attach_no_history (cli_on_no_history);
394 observer_attach_sync_execution_done (cli_on_sync_execution_done);
395 observer_attach_command_error (cli_on_command_error);
396 }
This page took 0.037933 seconds and 5 git commands to generate.