Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / cli / cli-interp.c
CommitLineData
4a8f6654
AC
1/* CLI Definitions for GDB, the GNU debugger.
2
618f726f 3 Copyright (C) 2002-2016 Free Software Foundation, Inc.
4a8f6654
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
4a8f6654
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/>. */
4a8f6654
AC
19
20#include "defs.h"
3c216924 21#include "cli-interp.h"
4a8f6654 22#include "interps.h"
4a8f6654
AC
23#include "event-top.h"
24#include "ui-out.h"
25#include "cli-out.h"
26#include "top.h" /* for "execute_command" */
3c216924 27#include "event-top.h"
fd664c91
PA
28#include "infrun.h"
29#include "observer.h"
26cde2cc
PA
30#include "gdbthread.h"
31#include "thread-fsm.h"
4a8f6654 32
8322445e
PA
33/* The console interpreter. */
34struct cli_interp
35{
36 /* The ui_out for the console interpreter. */
37 struct ui_out *cli_uiout;
38};
39
73ab01a0
PA
40/* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
41 and returns NULL otherwise. */
42
43static struct cli_interp *
44as_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}
4a8f6654 50
4791eb66 51/* Longjmp-safe wrapper for "execute_command". */
71fff37b 52static struct gdb_exception safe_execute_command (struct ui_out *uiout,
ebcd3b23
MS
53 char *command,
54 int from_tty);
fd664c91 55
26cde2cc
PA
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
74int
75should_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)
8980e177
PA
80 || tp->thread_fsm == NULL
81 || tp->thread_fsm->command_interp == console_interp
82 || !thread_fsm_finished_p (tp->thread_fsm))
26cde2cc
PA
83 return 1;
84 return 0;
85}
86
fd664c91
PA
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
243a9253
PA
91/* Observer for the normal_stop notification. */
92
93static void
94cli_on_normal_stop (struct bpstats *bs, int print_frame)
95{
73ab01a0
PA
96 struct switch_thru_all_uis state;
97
eaae60fd
PA
98 if (!print_frame)
99 return;
100
73ab01a0 101 SWITCH_THRU_ALL_UIS (state)
243a9253 102 {
eaae60fd
PA
103 struct interp *interp = top_level_interpreter ();
104 struct cli_interp *cli = as_cli_interp (interp);
105 struct thread_info *thread;
73ab01a0
PA
106
107 if (cli == NULL)
108 continue;
109
eaae60fd
PA
110 thread = inferior_thread ();
111 if (should_print_stop_to_console (interp, thread))
73ab01a0 112 print_stop_event (cli->cli_uiout);
243a9253
PA
113 }
114}
115
fd664c91
PA
116/* Observer for the signal_received notification. */
117
118static void
119cli_on_signal_received (enum gdb_signal siggnal)
120{
73ab01a0
PA
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 }
fd664c91
PA
132}
133
134/* Observer for the end_stepping_range notification. */
135
136static void
137cli_on_end_stepping_range (void)
138{
73ab01a0
PA
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 }
fd664c91
PA
150}
151
152/* Observer for the signalled notification. */
153
154static void
155cli_on_signal_exited (enum gdb_signal siggnal)
156{
73ab01a0
PA
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 }
fd664c91
PA
168}
169
170/* Observer for the exited notification. */
171
172static void
173cli_on_exited (int exitstatus)
174{
73ab01a0
PA
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 }
fd664c91
PA
186}
187
188/* Observer for the no_history notification. */
189
190static void
191cli_on_no_history (void)
192{
73ab01a0
PA
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 }
fd664c91
PA
204}
205
92bcb5f9
PA
206/* Observer for the sync_execution_done notification. */
207
208static void
209cli_on_sync_execution_done (void)
210{
73ab01a0
PA
211 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
212
213 if (cli == NULL)
214 return;
215
216 display_gdb_prompt (NULL);
92bcb5f9
PA
217}
218
219/* Observer for the command_error notification. */
220
221static void
222cli_on_command_error (void)
223{
73ab01a0
PA
224 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
225
226 if (cli == NULL)
227 return;
228
229 display_gdb_prompt (NULL);
92bcb5f9
PA
230}
231
b2d86570
PA
232/* pre_command_loop implementation. */
233
234void
235cli_interpreter_pre_command_loop (struct interp *self)
236{
237 display_gdb_prompt (0);
238}
239
4a8f6654
AC
240/* These implement the cli out interpreter: */
241
242static void *
4801a9a3 243cli_interpreter_init (struct interp *self, int top_level)
4a8f6654 244{
8322445e 245 return interp_data (self);
4a8f6654
AC
246}
247
248static int
249cli_interpreter_resume (void *data)
250{
3c216924 251 struct ui *ui = current_ui;
8322445e 252 struct cli_interp *cli = (struct cli_interp *) data;
38caaeec
DJ
253 struct ui_file *stream;
254
4a8f6654 255 /*sync_execution = 1; */
38caaeec 256
ebcd3b23
MS
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. */
38caaeec 260
8322445e 261 stream = cli_out_set_stream (cli->cli_uiout, gdb_stdout);
38caaeec
DJ
262 if (stream != gdb_stdout)
263 {
8322445e 264 cli_out_set_stream (cli->cli_uiout, stream);
38caaeec
DJ
265 stream = NULL;
266 }
267
3c216924
PA
268 gdb_setup_readline (1);
269
270 ui->input_handler = command_line_handler;
38caaeec
DJ
271
272 if (stream != NULL)
8322445e 273 cli_out_set_stream (cli->cli_uiout, gdb_stdout);
38caaeec 274
4a8f6654
AC
275 return 1;
276}
277
278static int
279cli_interpreter_suspend (void *data)
280{
281 gdb_disable_readline ();
282 return 1;
283}
284
71fff37b 285static struct gdb_exception
4a8f6654
AC
286cli_interpreter_exec (void *data, const char *command_str)
287{
8322445e 288 struct cli_interp *cli = (struct cli_interp *) data;
4a8f6654 289 struct ui_file *old_stream;
71fff37b 290 struct gdb_exception result;
4a8f6654
AC
291
292 /* FIXME: cagney/2003-02-01: Need to const char *propogate
293 safe_execute_command. */
224c3ddb
SM
294 char *str = (char *) alloca (strlen (command_str) + 1);
295 strcpy (str, command_str);
4a8f6654 296
ebcd3b23
MS
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.
4a8f6654 301
ebcd3b23
MS
302 It is important that it gets reset everytime, since the user
303 could set gdb to use a different interpreter. */
8322445e
PA
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);
4a8f6654
AC
307 return result;
308}
309
3c216924
PA
310int
311cli_interpreter_supports_command_editing (struct interp *interp)
312{
313 return 1;
314}
315
71fff37b 316static struct gdb_exception
f9679975 317safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
4a8f6654 318{
7556d4a4 319 struct gdb_exception e = exception_none;
f9679975
PA
320 struct ui_out *saved_uiout;
321
322 /* Save and override the global ``struct ui_out'' builder. */
79a45e25
PA
323 saved_uiout = current_uiout;
324 current_uiout = command_uiout;
cdb27c12 325
492d29ea 326 TRY
04bd08de
TT
327 {
328 execute_command (command, from_tty);
329 }
492d29ea 330 CATCH (exception, RETURN_MASK_ALL)
7556d4a4
PA
331 {
332 e = exception;
333 }
492d29ea 334 END_CATCH
f9679975
PA
335
336 /* Restore the global builder. */
79a45e25 337 current_uiout = saved_uiout;
f9679975 338
8a076db9
AC
339 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
340 caller should print the exception. */
9cbc821d 341 exception_print (gdb_stderr, e);
8a076db9 342 return e;
4a8f6654
AC
343}
344
4801a9a3
PA
345static struct ui_out *
346cli_ui_out (struct interp *self)
347{
8322445e
PA
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
355static 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 */
b2d86570 362 cli_interpreter_pre_command_loop, /* pre_command_loop_proc */
3c216924 363 cli_interpreter_supports_command_editing, /* supports_command_editing_proc */
8322445e
PA
364};
365
366/* Factory for CLI interpreters. */
367
368static struct interp *
369cli_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);
4801a9a3 377}
4a8f6654 378
4791eb66 379/* Standard gdb initialization hook. */
b9362cc7
AC
380extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
381
4a8f6654
AC
382void
383_initialize_cli_interp (void)
384{
8322445e 385 interp_factory_register (INTERP_CONSOLE, cli_interp_factory);
73ab01a0
PA
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);
4a8f6654 396}
This page took 0.828328 seconds and 4 git commands to generate.