Update copyright year range in all GDB files.
[deliverable/binutils-gdb.git] / gdb / cli / cli-interp.c
CommitLineData
4a8f6654
AC
1/* CLI Definitions for GDB, the GNU debugger.
2
42a4f53d 3 Copyright (C) 2002-2019 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 28#include "infrun.h"
76727919 29#include "observable.h"
26cde2cc
PA
30#include "gdbthread.h"
31#include "thread-fsm.h"
00431a78 32#include "inferior.h"
4a8f6654 33
d6f9b0fb
PA
34cli_interp_base::cli_interp_base (const char *name)
35 : interp (name)
36{}
37
38cli_interp_base::~cli_interp_base ()
39{}
40
8322445e 41/* The console interpreter. */
d6f9b0fb
PA
42
43class cli_interp final : public cli_interp_base
8322445e 44{
d6f9b0fb
PA
45 public:
46 explicit cli_interp (const char *name);
ba543ca5 47 ~cli_interp ();
d6f9b0fb
PA
48
49 void init (bool top_level) override;
50 void resume () override;
51 void suspend () override;
52 gdb_exception exec (const char *command_str) override;
53 ui_out *interp_ui_out () override;
54
8322445e 55 /* The ui_out for the console interpreter. */
112e8700 56 cli_ui_out *cli_uiout;
8322445e
PA
57};
58
d6f9b0fb
PA
59cli_interp::cli_interp (const char *name)
60 : cli_interp_base (name)
61{
62 /* Create a default uiout builder for the CLI. */
63 this->cli_uiout = cli_out_new (gdb_stdout);
64}
65
ba543ca5
GB
66cli_interp::~cli_interp ()
67{
68 delete cli_uiout;
69}
70
4034d0ff
AT
71/* Suppress notification struct. */
72struct cli_suppress_notification cli_suppress_notification =
73 {
74 0 /* user_selected_context_changed */
75 };
76
73ab01a0
PA
77/* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
78 and returns NULL otherwise. */
79
80static struct cli_interp *
81as_cli_interp (struct interp *interp)
82{
716b8bc5 83 return dynamic_cast<cli_interp *> (interp);
73ab01a0 84}
4a8f6654 85
4791eb66 86/* Longjmp-safe wrapper for "execute_command". */
71fff37b 87static struct gdb_exception safe_execute_command (struct ui_out *uiout,
95a6b0a1 88 const char *command,
ebcd3b23 89 int from_tty);
fd664c91 90
26cde2cc
PA
91/* See cli-interp.h.
92
93 Breakpoint hits should always be mirrored to a console. Deciding
94 what to mirror to a console wrt to breakpoints and random stops
95 gets messy real fast. E.g., say "s" trips on a breakpoint. We'd
96 clearly want to mirror the event to the console in this case. But
97 what about more complicated cases like "s&; thread n; s&", and one
98 of those steps spawning a new thread, and that thread hitting a
99 breakpoint? It's impossible in general to track whether the thread
100 had any relation to the commands that had been executed. So we
101 just simplify and always mirror breakpoints and random events to
102 all consoles.
103
104 OTOH, we should print the source line to the console when stepping
105 or other similar commands, iff the step was started by that console
106 (or in MI's case, by a console command), but not if it was started
107 with MI's -exec-step or similar. */
108
109int
110should_print_stop_to_console (struct interp *console_interp,
111 struct thread_info *tp)
112{
113 if ((bpstat_what (tp->control.stop_bpstat).main_action
114 == BPSTAT_WHAT_STOP_NOISY)
8980e177
PA
115 || tp->thread_fsm == NULL
116 || tp->thread_fsm->command_interp == console_interp
117 || !thread_fsm_finished_p (tp->thread_fsm))
26cde2cc
PA
118 return 1;
119 return 0;
120}
121
fd664c91
PA
122/* Observers for several run control events. If the interpreter is
123 quiet (i.e., another interpreter is being run with
124 interpreter-exec), print nothing. */
125
243a9253
PA
126/* Observer for the normal_stop notification. */
127
128static void
129cli_on_normal_stop (struct bpstats *bs, int print_frame)
130{
eaae60fd
PA
131 if (!print_frame)
132 return;
133
0e454242 134 SWITCH_THRU_ALL_UIS ()
243a9253 135 {
eaae60fd
PA
136 struct interp *interp = top_level_interpreter ();
137 struct cli_interp *cli = as_cli_interp (interp);
138 struct thread_info *thread;
73ab01a0
PA
139
140 if (cli == NULL)
141 continue;
142
eaae60fd
PA
143 thread = inferior_thread ();
144 if (should_print_stop_to_console (interp, thread))
73ab01a0 145 print_stop_event (cli->cli_uiout);
243a9253
PA
146 }
147}
148
fd664c91
PA
149/* Observer for the signal_received notification. */
150
151static void
152cli_on_signal_received (enum gdb_signal siggnal)
153{
0e454242 154 SWITCH_THRU_ALL_UIS ()
73ab01a0
PA
155 {
156 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
157
158 if (cli == NULL)
159 continue;
160
161 print_signal_received_reason (cli->cli_uiout, siggnal);
162 }
fd664c91
PA
163}
164
165/* Observer for the end_stepping_range notification. */
166
167static void
168cli_on_end_stepping_range (void)
169{
0e454242 170 SWITCH_THRU_ALL_UIS ()
73ab01a0
PA
171 {
172 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
173
174 if (cli == NULL)
175 continue;
176
177 print_end_stepping_range_reason (cli->cli_uiout);
178 }
fd664c91
PA
179}
180
181/* Observer for the signalled notification. */
182
183static void
184cli_on_signal_exited (enum gdb_signal siggnal)
185{
0e454242 186 SWITCH_THRU_ALL_UIS ()
73ab01a0
PA
187 {
188 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
189
190 if (cli == NULL)
191 continue;
192
193 print_signal_exited_reason (cli->cli_uiout, siggnal);
194 }
fd664c91
PA
195}
196
197/* Observer for the exited notification. */
198
199static void
200cli_on_exited (int exitstatus)
201{
0e454242 202 SWITCH_THRU_ALL_UIS ()
73ab01a0
PA
203 {
204 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
205
206 if (cli == NULL)
207 continue;
208
209 print_exited_reason (cli->cli_uiout, exitstatus);
210 }
fd664c91
PA
211}
212
213/* Observer for the no_history notification. */
214
215static void
216cli_on_no_history (void)
217{
0e454242 218 SWITCH_THRU_ALL_UIS ()
73ab01a0
PA
219 {
220 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
221
222 if (cli == NULL)
223 continue;
224
225 print_no_history_reason (cli->cli_uiout);
226 }
fd664c91
PA
227}
228
92bcb5f9
PA
229/* Observer for the sync_execution_done notification. */
230
231static void
232cli_on_sync_execution_done (void)
233{
73ab01a0
PA
234 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
235
236 if (cli == NULL)
237 return;
238
239 display_gdb_prompt (NULL);
92bcb5f9
PA
240}
241
242/* Observer for the command_error notification. */
243
244static void
245cli_on_command_error (void)
246{
73ab01a0
PA
247 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
248
249 if (cli == NULL)
250 return;
251
252 display_gdb_prompt (NULL);
92bcb5f9
PA
253}
254
4034d0ff
AT
255/* Observer for the user_selected_context_changed notification. */
256
257static void
258cli_on_user_selected_context_changed (user_selected_what selection)
259{
4034d0ff
AT
260 /* This event is suppressed. */
261 if (cli_suppress_notification.user_selected_context)
262 return;
263
151bb4a5 264 thread_info *tp = inferior_ptid != null_ptid ? inferior_thread () : NULL;
4034d0ff 265
0e454242 266 SWITCH_THRU_ALL_UIS ()
4034d0ff
AT
267 {
268 struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
269
270 if (cli == NULL)
271 continue;
272
273 if (selection & USER_SELECTED_INFERIOR)
274 print_selected_inferior (cli->cli_uiout);
275
276 if (tp != NULL
277 && ((selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME))))
278 print_selected_thread_frame (cli->cli_uiout, selection);
279 }
280}
281
b2d86570
PA
282/* pre_command_loop implementation. */
283
284void
d6f9b0fb 285cli_interp_base::pre_command_loop ()
b2d86570
PA
286{
287 display_gdb_prompt (0);
288}
289
4a8f6654
AC
290/* These implement the cli out interpreter: */
291
d6f9b0fb
PA
292void
293cli_interp::init (bool top_level)
4a8f6654 294{
4a8f6654
AC
295}
296
d6f9b0fb
PA
297void
298cli_interp::resume ()
4a8f6654 299{
3c216924 300 struct ui *ui = current_ui;
d6f9b0fb 301 struct cli_interp *cli = this;
38caaeec
DJ
302 struct ui_file *stream;
303
4a8f6654 304 /*sync_execution = 1; */
38caaeec 305
ebcd3b23
MS
306 /* gdb_setup_readline will change gdb_stdout. If the CLI was
307 previously writing to gdb_stdout, then set it to the new
308 gdb_stdout afterwards. */
38caaeec 309
112e8700 310 stream = cli->cli_uiout->set_stream (gdb_stdout);
38caaeec
DJ
311 if (stream != gdb_stdout)
312 {
112e8700 313 cli->cli_uiout->set_stream (stream);
38caaeec
DJ
314 stream = NULL;
315 }
316
3c216924
PA
317 gdb_setup_readline (1);
318
319 ui->input_handler = command_line_handler;
38caaeec
DJ
320
321 if (stream != NULL)
112e8700 322 cli->cli_uiout->set_stream (gdb_stdout);
4a8f6654
AC
323}
324
d6f9b0fb
PA
325void
326cli_interp::suspend ()
4a8f6654
AC
327{
328 gdb_disable_readline ();
4a8f6654
AC
329}
330
d6f9b0fb
PA
331gdb_exception
332cli_interp::exec (const char *command_str)
4a8f6654 333{
d6f9b0fb 334 struct cli_interp *cli = this;
4a8f6654 335 struct ui_file *old_stream;
71fff37b 336 struct gdb_exception result;
4a8f6654 337
ebcd3b23
MS
338 /* gdb_stdout could change between the time cli_uiout was
339 initialized and now. Since we're probably using a different
340 interpreter which has a new ui_file for gdb_stdout, use that one
341 instead of the default.
4a8f6654 342
ebcd3b23
MS
343 It is important that it gets reset everytime, since the user
344 could set gdb to use a different interpreter. */
112e8700 345 old_stream = cli->cli_uiout->set_stream (gdb_stdout);
95a6b0a1 346 result = safe_execute_command (cli->cli_uiout, command_str, 1);
112e8700 347 cli->cli_uiout->set_stream (old_stream);
4a8f6654
AC
348 return result;
349}
350
d6f9b0fb
PA
351bool
352cli_interp_base::supports_command_editing ()
3c216924 353{
d6f9b0fb 354 return true;
3c216924
PA
355}
356
71fff37b 357static struct gdb_exception
95a6b0a1
TT
358safe_execute_command (struct ui_out *command_uiout, const char *command,
359 int from_tty)
4a8f6654 360{
7556d4a4 361 struct gdb_exception e = exception_none;
f9679975
PA
362
363 /* Save and override the global ``struct ui_out'' builder. */
753ff9bd
TT
364 scoped_restore saved_uiout = make_scoped_restore (&current_uiout,
365 command_uiout);
cdb27c12 366
492d29ea 367 TRY
04bd08de
TT
368 {
369 execute_command (command, from_tty);
370 }
492d29ea 371 CATCH (exception, RETURN_MASK_ALL)
7556d4a4
PA
372 {
373 e = exception;
374 }
492d29ea 375 END_CATCH
f9679975 376
8a076db9
AC
377 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
378 caller should print the exception. */
9cbc821d 379 exception_print (gdb_stderr, e);
8a076db9 380 return e;
4a8f6654
AC
381}
382
d6f9b0fb
PA
383ui_out *
384cli_interp::interp_ui_out ()
4801a9a3 385{
d6f9b0fb 386 struct cli_interp *cli = (struct cli_interp *) this;
8322445e
PA
387
388 return cli->cli_uiout;
389}
390
616268b6
PA
391/* These hold the pushed copies of the gdb output files.
392 If NULL then nothing has yet been pushed. */
393struct saved_output_files
394{
395 ui_file *out;
396 ui_file *err;
397 ui_file *log;
398 ui_file *targ;
399 ui_file *targerr;
400};
401static saved_output_files saved_output;
402
403/* See cli-interp.h. */
404
405void
d6f9b0fb 406cli_interp_base::set_logging (ui_file_up logfile, bool logging_redirect)
616268b6
PA
407{
408 if (logfile != NULL)
409 {
410 saved_output.out = gdb_stdout;
411 saved_output.err = gdb_stderr;
412 saved_output.log = gdb_stdlog;
413 saved_output.targ = gdb_stdtarg;
414 saved_output.targerr = gdb_stdtargerr;
415
416 /* A raw pointer since ownership is transferred to
417 gdb_stdout. */
418 ui_file *output = make_logging_output (gdb_stdout,
419 std::move (logfile),
420 logging_redirect);
421 gdb_stdout = output;
422 gdb_stdlog = output;
423 gdb_stderr = output;
424 gdb_stdtarg = output;
425 gdb_stdtargerr = output;
426 }
427 else
428 {
429 /* Only delete one of the files -- they are all set to the same
430 value. */
431 delete gdb_stdout;
432
433 gdb_stdout = saved_output.out;
434 gdb_stderr = saved_output.err;
435 gdb_stdlog = saved_output.log;
436 gdb_stdtarg = saved_output.targ;
437 gdb_stdtargerr = saved_output.targerr;
438
439 saved_output.out = NULL;
440 saved_output.err = NULL;
441 saved_output.log = NULL;
442 saved_output.targ = NULL;
443 saved_output.targerr = NULL;
444 }
445}
446
8322445e
PA
447/* Factory for CLI interpreters. */
448
449static struct interp *
450cli_interp_factory (const char *name)
451{
d6f9b0fb 452 return new cli_interp (name);
4801a9a3 453}
4a8f6654 454
4791eb66 455/* Standard gdb initialization hook. */
b9362cc7 456
4a8f6654
AC
457void
458_initialize_cli_interp (void)
459{
8322445e 460 interp_factory_register (INTERP_CONSOLE, cli_interp_factory);
73ab01a0
PA
461
462 /* If changing this, remember to update tui-interp.c as well. */
76727919
TT
463 gdb::observers::normal_stop.attach (cli_on_normal_stop);
464 gdb::observers::end_stepping_range.attach (cli_on_end_stepping_range);
465 gdb::observers::signal_received.attach (cli_on_signal_received);
466 gdb::observers::signal_exited.attach (cli_on_signal_exited);
467 gdb::observers::exited.attach (cli_on_exited);
468 gdb::observers::no_history.attach (cli_on_no_history);
469 gdb::observers::sync_execution_done.attach (cli_on_sync_execution_done);
470 gdb::observers::command_error.attach (cli_on_command_error);
471 gdb::observers::user_selected_context_changed.attach
4034d0ff 472 (cli_on_user_selected_context_changed);
4a8f6654 473}
This page took 1.555495 seconds and 4 git commands to generate.