1 /* CLI Definitions for GDB, the GNU debugger.
3 Copyright (C) 2002-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 #include "event-top.h"
25 #include "top.h" /* for "execute_command" */
29 /* The console interpreter. */
32 /* The ui_out for the console interpreter. */
33 struct ui_out
*cli_uiout
;
36 /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
37 and returns NULL otherwise. */
39 static struct cli_interp
*
40 as_cli_interp (struct interp
*interp
)
42 if (strcmp (interp_name (interp
), INTERP_CONSOLE
) == 0)
43 return (struct cli_interp
*) interp_data (interp
);
47 /* Longjmp-safe wrapper for "execute_command". */
48 static struct gdb_exception
safe_execute_command (struct ui_out
*uiout
,
52 /* Observers for several run control events. If the interpreter is
53 quiet (i.e., another interpreter is being run with
54 interpreter-exec), print nothing. */
56 /* Observer for the normal_stop notification. */
59 cli_on_normal_stop (struct bpstats
*bs
, int print_frame
)
61 struct switch_thru_all_uis state
;
63 SWITCH_THRU_ALL_UIS (state
)
65 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
71 print_stop_event (cli
->cli_uiout
);
75 /* Observer for the signal_received notification. */
78 cli_on_signal_received (enum gdb_signal siggnal
)
80 struct switch_thru_all_uis state
;
82 SWITCH_THRU_ALL_UIS (state
)
84 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
89 print_signal_received_reason (cli
->cli_uiout
, siggnal
);
93 /* Observer for the end_stepping_range notification. */
96 cli_on_end_stepping_range (void)
98 struct switch_thru_all_uis state
;
100 SWITCH_THRU_ALL_UIS (state
)
102 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
107 print_end_stepping_range_reason (cli
->cli_uiout
);
111 /* Observer for the signalled notification. */
114 cli_on_signal_exited (enum gdb_signal siggnal
)
116 struct switch_thru_all_uis state
;
118 SWITCH_THRU_ALL_UIS (state
)
120 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
125 print_signal_exited_reason (cli
->cli_uiout
, siggnal
);
129 /* Observer for the exited notification. */
132 cli_on_exited (int exitstatus
)
134 struct switch_thru_all_uis state
;
136 SWITCH_THRU_ALL_UIS (state
)
138 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
143 print_exited_reason (cli
->cli_uiout
, exitstatus
);
147 /* Observer for the no_history notification. */
150 cli_on_no_history (void)
152 struct switch_thru_all_uis state
;
154 SWITCH_THRU_ALL_UIS (state
)
156 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
161 print_no_history_reason (cli
->cli_uiout
);
165 /* Observer for the sync_execution_done notification. */
168 cli_on_sync_execution_done (void)
170 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
175 display_gdb_prompt (NULL
);
178 /* Observer for the command_error notification. */
181 cli_on_command_error (void)
183 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
188 display_gdb_prompt (NULL
);
191 /* These implement the cli out interpreter: */
194 cli_interpreter_init (struct interp
*self
, int top_level
)
196 return interp_data (self
);
200 cli_interpreter_resume (void *data
)
202 struct cli_interp
*cli
= (struct cli_interp
*) data
;
203 struct ui_file
*stream
;
205 /*sync_execution = 1; */
207 /* gdb_setup_readline will change gdb_stdout. If the CLI was
208 previously writing to gdb_stdout, then set it to the new
209 gdb_stdout afterwards. */
211 stream
= cli_out_set_stream (cli
->cli_uiout
, gdb_stdout
);
212 if (stream
!= gdb_stdout
)
214 cli_out_set_stream (cli
->cli_uiout
, stream
);
218 gdb_setup_readline ();
221 cli_out_set_stream (cli
->cli_uiout
, gdb_stdout
);
227 cli_interpreter_suspend (void *data
)
229 gdb_disable_readline ();
233 static struct gdb_exception
234 cli_interpreter_exec (void *data
, const char *command_str
)
236 struct cli_interp
*cli
= (struct cli_interp
*) data
;
237 struct ui_file
*old_stream
;
238 struct gdb_exception result
;
240 /* FIXME: cagney/2003-02-01: Need to const char *propogate
241 safe_execute_command. */
242 char *str
= (char *) alloca (strlen (command_str
) + 1);
243 strcpy (str
, command_str
);
245 /* gdb_stdout could change between the time cli_uiout was
246 initialized and now. Since we're probably using a different
247 interpreter which has a new ui_file for gdb_stdout, use that one
248 instead of the default.
250 It is important that it gets reset everytime, since the user
251 could set gdb to use a different interpreter. */
252 old_stream
= cli_out_set_stream (cli
->cli_uiout
, gdb_stdout
);
253 result
= safe_execute_command (cli
->cli_uiout
, str
, 1);
254 cli_out_set_stream (cli
->cli_uiout
, old_stream
);
258 static struct gdb_exception
259 safe_execute_command (struct ui_out
*command_uiout
, char *command
, int from_tty
)
261 struct gdb_exception e
= exception_none
;
262 struct ui_out
*saved_uiout
;
264 /* Save and override the global ``struct ui_out'' builder. */
265 saved_uiout
= current_uiout
;
266 current_uiout
= command_uiout
;
270 execute_command (command
, from_tty
);
272 CATCH (exception
, RETURN_MASK_ALL
)
278 /* Restore the global builder. */
279 current_uiout
= saved_uiout
;
281 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
282 caller should print the exception. */
283 exception_print (gdb_stderr
, e
);
287 static struct ui_out
*
288 cli_ui_out (struct interp
*self
)
290 struct cli_interp
*cli
= (struct cli_interp
*) interp_data (self
);
292 return cli
->cli_uiout
;
295 /* The CLI interpreter's vtable. */
297 static const struct interp_procs cli_interp_procs
= {
298 cli_interpreter_init
, /* init_proc */
299 cli_interpreter_resume
, /* resume_proc */
300 cli_interpreter_suspend
, /* suspend_proc */
301 cli_interpreter_exec
, /* exec_proc */
302 cli_ui_out
, /* ui_out_proc */
303 NULL
, /* set_logging_proc */
304 cli_command_loop
/* command_loop_proc */
307 /* Factory for CLI interpreters. */
309 static struct interp
*
310 cli_interp_factory (const char *name
)
312 struct cli_interp
*cli
= XNEW (struct cli_interp
);
314 /* Create a default uiout builder for the CLI. */
315 cli
->cli_uiout
= cli_out_new (gdb_stdout
);
317 return interp_new (name
, &cli_interp_procs
, cli
);
320 /* Standard gdb initialization hook. */
321 extern initialize_file_ftype _initialize_cli_interp
; /* -Wmissing-prototypes */
324 _initialize_cli_interp (void)
326 interp_factory_register (INTERP_CONSOLE
, cli_interp_factory
);
328 /* If changing this, remember to update tui-interp.c as well. */
329 observer_attach_normal_stop (cli_on_normal_stop
);
330 observer_attach_end_stepping_range (cli_on_end_stepping_range
);
331 observer_attach_signal_received (cli_on_signal_received
);
332 observer_attach_signal_exited (cli_on_signal_exited
);
333 observer_attach_exited (cli_on_exited
);
334 observer_attach_no_history (cli_on_no_history
);
335 observer_attach_sync_execution_done (cli_on_sync_execution_done
);
336 observer_attach_command_error (cli_on_command_error
);