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