[testsuite] Unbuffer the output in gdb.base/multi-forks.c
[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"
21#include "interps.h"
4a8f6654
AC
22#include "event-top.h"
23#include "ui-out.h"
24#include "cli-out.h"
25#include "top.h" /* for "execute_command" */
fd664c91
PA
26#include "infrun.h"
27#include "observer.h"
4a8f6654 28
ebcd3b23
MS
29/* These are the ui_out and the interpreter for the console
30 interpreter. */
fd664c91
PA
31struct ui_out *cli_uiout;
32static struct interp *cli_interp;
4a8f6654 33
4791eb66 34/* Longjmp-safe wrapper for "execute_command". */
71fff37b 35static struct gdb_exception safe_execute_command (struct ui_out *uiout,
ebcd3b23
MS
36 char *command,
37 int from_tty);
fd664c91
PA
38
39/* Observers for several run control events. If the interpreter is
40 quiet (i.e., another interpreter is being run with
41 interpreter-exec), print nothing. */
42
243a9253
PA
43/* Observer for the normal_stop notification. */
44
45static void
46cli_on_normal_stop (struct bpstats *bs, int print_frame)
47{
48 if (!interp_quiet_p (cli_interp))
49 {
50 if (print_frame)
51 print_stop_event (cli_uiout);
52 }
53}
54
fd664c91
PA
55/* Observer for the signal_received notification. */
56
57static void
58cli_on_signal_received (enum gdb_signal siggnal)
59{
60 if (!interp_quiet_p (cli_interp))
61 print_signal_received_reason (cli_uiout, siggnal);
62}
63
64/* Observer for the end_stepping_range notification. */
65
66static void
67cli_on_end_stepping_range (void)
68{
69 if (!interp_quiet_p (cli_interp))
70 print_end_stepping_range_reason (cli_uiout);
71}
72
73/* Observer for the signalled notification. */
74
75static void
76cli_on_signal_exited (enum gdb_signal siggnal)
77{
78 if (!interp_quiet_p (cli_interp))
79 print_signal_exited_reason (cli_uiout, siggnal);
80}
81
82/* Observer for the exited notification. */
83
84static void
85cli_on_exited (int exitstatus)
86{
87 if (!interp_quiet_p (cli_interp))
88 print_exited_reason (cli_uiout, exitstatus);
89}
90
91/* Observer for the no_history notification. */
92
93static void
94cli_on_no_history (void)
95{
96 if (!interp_quiet_p (cli_interp))
97 print_no_history_reason (cli_uiout);
98}
99
92bcb5f9
PA
100/* Observer for the sync_execution_done notification. */
101
102static void
103cli_on_sync_execution_done (void)
104{
105 if (!interp_quiet_p (cli_interp))
106 display_gdb_prompt (NULL);
107}
108
109/* Observer for the command_error notification. */
110
111static void
112cli_on_command_error (void)
113{
114 if (!interp_quiet_p (cli_interp))
115 display_gdb_prompt (NULL);
116}
117
4a8f6654
AC
118/* These implement the cli out interpreter: */
119
120static void *
4801a9a3 121cli_interpreter_init (struct interp *self, int top_level)
4a8f6654 122{
fd664c91 123 /* If changing this, remember to update tui-interp.c as well. */
243a9253 124 observer_attach_normal_stop (cli_on_normal_stop);
fd664c91
PA
125 observer_attach_end_stepping_range (cli_on_end_stepping_range);
126 observer_attach_signal_received (cli_on_signal_received);
127 observer_attach_signal_exited (cli_on_signal_exited);
128 observer_attach_exited (cli_on_exited);
129 observer_attach_no_history (cli_on_no_history);
92bcb5f9
PA
130 observer_attach_sync_execution_done (cli_on_sync_execution_done);
131 observer_attach_command_error (cli_on_command_error);
fd664c91 132
4a8f6654
AC
133 return NULL;
134}
135
136static int
137cli_interpreter_resume (void *data)
138{
38caaeec
DJ
139 struct ui_file *stream;
140
4a8f6654 141 /*sync_execution = 1; */
38caaeec 142
ebcd3b23
MS
143 /* gdb_setup_readline will change gdb_stdout. If the CLI was
144 previously writing to gdb_stdout, then set it to the new
145 gdb_stdout afterwards. */
38caaeec
DJ
146
147 stream = cli_out_set_stream (cli_uiout, gdb_stdout);
148 if (stream != gdb_stdout)
149 {
150 cli_out_set_stream (cli_uiout, stream);
151 stream = NULL;
152 }
153
4a8f6654 154 gdb_setup_readline ();
38caaeec
DJ
155
156 if (stream != NULL)
157 cli_out_set_stream (cli_uiout, gdb_stdout);
158
4a8f6654
AC
159 return 1;
160}
161
162static int
163cli_interpreter_suspend (void *data)
164{
165 gdb_disable_readline ();
166 return 1;
167}
168
71fff37b 169static struct gdb_exception
4a8f6654
AC
170cli_interpreter_exec (void *data, const char *command_str)
171{
4a8f6654 172 struct ui_file *old_stream;
71fff37b 173 struct gdb_exception result;
4a8f6654
AC
174
175 /* FIXME: cagney/2003-02-01: Need to const char *propogate
176 safe_execute_command. */
224c3ddb
SM
177 char *str = (char *) alloca (strlen (command_str) + 1);
178 strcpy (str, command_str);
4a8f6654 179
ebcd3b23
MS
180 /* gdb_stdout could change between the time cli_uiout was
181 initialized and now. Since we're probably using a different
182 interpreter which has a new ui_file for gdb_stdout, use that one
183 instead of the default.
4a8f6654 184
ebcd3b23
MS
185 It is important that it gets reset everytime, since the user
186 could set gdb to use a different interpreter. */
4a8f6654
AC
187 old_stream = cli_out_set_stream (cli_uiout, gdb_stdout);
188 result = safe_execute_command (cli_uiout, str, 1);
189 cli_out_set_stream (cli_uiout, old_stream);
190 return result;
191}
192
71fff37b 193static struct gdb_exception
f9679975 194safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
4a8f6654 195{
7556d4a4 196 struct gdb_exception e = exception_none;
f9679975
PA
197 struct ui_out *saved_uiout;
198
199 /* Save and override the global ``struct ui_out'' builder. */
79a45e25
PA
200 saved_uiout = current_uiout;
201 current_uiout = command_uiout;
cdb27c12 202
492d29ea 203 TRY
04bd08de
TT
204 {
205 execute_command (command, from_tty);
206 }
492d29ea 207 CATCH (exception, RETURN_MASK_ALL)
7556d4a4
PA
208 {
209 e = exception;
210 }
492d29ea 211 END_CATCH
f9679975
PA
212
213 /* Restore the global builder. */
79a45e25 214 current_uiout = saved_uiout;
f9679975 215
8a076db9
AC
216 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
217 caller should print the exception. */
9cbc821d 218 exception_print (gdb_stderr, e);
8a076db9 219 return e;
4a8f6654
AC
220}
221
4801a9a3
PA
222static struct ui_out *
223cli_ui_out (struct interp *self)
224{
225 return cli_uiout;
226}
4a8f6654 227
4791eb66 228/* Standard gdb initialization hook. */
b9362cc7
AC
229extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */
230
4a8f6654
AC
231void
232_initialize_cli_interp (void)
233{
234 static const struct interp_procs procs = {
235 cli_interpreter_init, /* init_proc */
236 cli_interpreter_resume, /* resume_proc */
237 cli_interpreter_suspend, /* suspend_proc */
238 cli_interpreter_exec, /* exec_proc */
4d09c5b4
AB
239 cli_ui_out, /* ui_out_proc */
240 NULL, /* set_logging_proc */
241 cli_command_loop /* command_loop_proc */
4a8f6654 242 };
4a8f6654 243
4791eb66 244 /* Create a default uiout builder for the CLI. */
4a8f6654 245 cli_uiout = cli_out_new (gdb_stdout);
4801a9a3 246 cli_interp = interp_new (INTERP_CONSOLE, &procs);
4a8f6654
AC
247
248 interp_add (cli_interp);
249}
This page took 0.803163 seconds and 4 git commands to generate.