Commit | Line | Data |
---|---|---|
4a8f6654 AC |
1 | /* Manages interpreters for GDB, the GNU debugger. |
2 | ||
b811d2c2 | 3 | Copyright (C) 2000-2020 Free Software Foundation, Inc. |
4a8f6654 AC |
4 | |
5 | Written by Jim Ingham <jingham@apple.com> of Apple Computer, Inc. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
4a8f6654 AC |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
1777feb0 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4a8f6654 AC |
21 | |
22 | /* This is just a first cut at separating out the "interpreter" | |
23 | functions of gdb into self-contained modules. There are a couple | |
24 | of open areas that need to be sorted out: | |
25 | ||
26 | 1) The interpreter explicitly contains a UI_OUT, and can insert itself | |
27 | into the event loop, but it doesn't explicitly contain hooks for readline. | |
28 | I did this because it seems to me many interpreters won't want to use | |
29 | the readline command interface, and it is probably simpler to just let | |
30 | them take over the input in their resume proc. */ | |
31 | ||
32 | #include "defs.h" | |
33 | #include "gdbcmd.h" | |
34 | #include "ui-out.h" | |
35 | #include "event-loop.h" | |
36 | #include "event-top.h" | |
37 | #include "interps.h" | |
38 | #include "completer.h" | |
4a8f6654 | 39 | #include "top.h" /* For command_loop. */ |
be34f849 | 40 | #include "continuations.h" |
b0be6c91 | 41 | #include "main.h" |
4a8f6654 | 42 | |
cb814510 PA |
43 | /* Each UI has its own independent set of interpreters. */ |
44 | ||
45 | struct ui_interp_info | |
46 | { | |
47 | /* Each top level has its own independent set of interpreters. */ | |
48 | struct interp *interp_list; | |
49 | struct interp *current_interpreter; | |
50 | struct interp *top_level_interpreter; | |
51 | ||
52 | /* The interpreter that is active while `interp_exec' is active, NULL | |
53 | at all other times. */ | |
54 | struct interp *command_interpreter; | |
55 | }; | |
56 | ||
8322445e | 57 | /* Get UI's ui_interp_info object. Never returns NULL. */ |
cb814510 PA |
58 | |
59 | static struct ui_interp_info * | |
8322445e | 60 | get_interp_info (struct ui *ui) |
cb814510 | 61 | { |
cb814510 PA |
62 | if (ui->interp_info == NULL) |
63 | ui->interp_info = XCNEW (struct ui_interp_info); | |
64 | return ui->interp_info; | |
65 | } | |
b4a14fd0 | 66 | |
8322445e PA |
67 | /* Get the current UI's ui_interp_info object. Never returns |
68 | NULL. */ | |
69 | ||
70 | static struct ui_interp_info * | |
71 | get_current_interp_info (void) | |
72 | { | |
73 | return get_interp_info (current_ui); | |
74 | } | |
75 | ||
1777feb0 | 76 | /* The magic initialization routine for this module. */ |
4a8f6654 | 77 | |
8322445e PA |
78 | static struct interp *interp_lookup_existing (struct ui *ui, |
79 | const char *name); | |
80 | ||
d6f9b0fb | 81 | interp::interp (const char *name) |
d525a99b | 82 | : m_name (xstrdup (name)) |
4a8f6654 | 83 | { |
d6f9b0fb | 84 | this->inited = false; |
4a8f6654 AC |
85 | } |
86 | ||
d6f9b0fb | 87 | interp::~interp () |
fbe61a36 GB |
88 | { |
89 | xfree (m_name); | |
90 | } | |
d6f9b0fb | 91 | |
8322445e PA |
92 | /* An interpreter factory. Maps an interpreter name to the factory |
93 | function that instantiates an interpreter by that name. */ | |
94 | ||
95 | struct interp_factory | |
96 | { | |
4c2287b0 SM |
97 | interp_factory (const char *name_, interp_factory_func func_) |
98 | : name (name_), func (func_) | |
99 | {} | |
100 | ||
8322445e PA |
101 | /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */ |
102 | const char *name; | |
103 | ||
104 | /* The function that creates the interpreter. */ | |
105 | interp_factory_func func; | |
106 | }; | |
107 | ||
8322445e | 108 | /* The registered interpreter factories. */ |
4c2287b0 | 109 | static std::vector<interp_factory> interpreter_factories; |
8322445e PA |
110 | |
111 | /* See interps.h. */ | |
112 | ||
113 | void | |
114 | interp_factory_register (const char *name, interp_factory_func func) | |
115 | { | |
8322445e | 116 | /* Assert that no factory for NAME is already registered. */ |
4c2287b0 SM |
117 | for (const interp_factory &f : interpreter_factories) |
118 | if (strcmp (f.name, name) == 0) | |
8322445e PA |
119 | { |
120 | internal_error (__FILE__, __LINE__, | |
121 | _("interpreter factory already registered: \"%s\"\n"), | |
122 | name); | |
123 | } | |
124 | ||
4c2287b0 | 125 | interpreter_factories.emplace_back (name, func); |
8322445e PA |
126 | } |
127 | ||
4a8f6654 AC |
128 | /* Add interpreter INTERP to the gdb interpreter list. The |
129 | interpreter must not have previously been added. */ | |
130 | void | |
8322445e | 131 | interp_add (struct ui *ui, struct interp *interp) |
4a8f6654 | 132 | { |
8322445e | 133 | struct ui_interp_info *ui_interp = get_interp_info (ui); |
cb814510 | 134 | |
d525a99b | 135 | gdb_assert (interp_lookup_existing (ui, interp->name ()) == NULL); |
4a8f6654 | 136 | |
cb814510 PA |
137 | interp->next = ui_interp->interp_list; |
138 | ui_interp->interp_list = interp; | |
4a8f6654 AC |
139 | } |
140 | ||
141 | /* This sets the current interpreter to be INTERP. If INTERP has not | |
d6f9b0fb | 142 | been initialized, then this will also run the init method. |
683f2885 VP |
143 | |
144 | The TOP_LEVEL parameter tells if this new interpreter is | |
145 | the top-level one. The top-level is what is requested | |
146 | on the command line, and is responsible for reporting general | |
147 | notification about target state changes. For example, if | |
148 | MI is the top-level interpreter, then it will always report | |
149 | events such as target stops and new thread creation, even if they | |
150 | are caused by CLI commands. */ | |
d6f9b0fb | 151 | |
a474bd8e | 152 | static void |
d6f9b0fb | 153 | interp_set (struct interp *interp, bool top_level) |
4a8f6654 | 154 | { |
cb814510 PA |
155 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
156 | struct interp *old_interp = ui_interp->current_interpreter; | |
4a8f6654 | 157 | |
683f2885 VP |
158 | /* If we already have an interpreter, then trying to |
159 | set top level interpreter is kinda pointless. */ | |
cb814510 PA |
160 | gdb_assert (!top_level || !ui_interp->current_interpreter); |
161 | gdb_assert (!top_level || !ui_interp->top_level_interpreter); | |
683f2885 | 162 | |
cb814510 | 163 | if (old_interp != NULL) |
4a8f6654 | 164 | { |
112e8700 | 165 | current_uiout->flush (); |
d6f9b0fb | 166 | old_interp->suspend (); |
4a8f6654 | 167 | } |
4a8f6654 | 168 | |
cb814510 | 169 | ui_interp->current_interpreter = interp; |
683f2885 | 170 | if (top_level) |
cb814510 | 171 | ui_interp->top_level_interpreter = interp; |
4a8f6654 AC |
172 | |
173 | /* We use interpreter_p for the "set interpreter" variable, so we need | |
1777feb0 | 174 | to make sure we have a malloc'ed copy for the set command to free. */ |
4a8f6654 | 175 | if (interpreter_p != NULL |
d525a99b | 176 | && strcmp (interp->name (), interpreter_p) != 0) |
4a8f6654 AC |
177 | { |
178 | xfree (interpreter_p); | |
179 | ||
d525a99b | 180 | interpreter_p = xstrdup (interp->name ()); |
4a8f6654 AC |
181 | } |
182 | ||
d6f9b0fb | 183 | /* Run the init proc. */ |
4a8f6654 AC |
184 | if (!interp->inited) |
185 | { | |
d6f9b0fb PA |
186 | interp->init (top_level); |
187 | interp->inited = true; | |
4a8f6654 AC |
188 | } |
189 | ||
4801a9a3 | 190 | /* Do this only after the interpreter is initialized. */ |
d6f9b0fb | 191 | current_uiout = interp->interp_ui_out (); |
4801a9a3 | 192 | |
907d819a | 193 | /* Clear out any installed interpreter hooks/event handlers. */ |
4a8f6654 AC |
194 | clear_interpreter_hooks (); |
195 | ||
d6f9b0fb | 196 | interp->resume (); |
4a8f6654 AC |
197 | } |
198 | ||
8322445e PA |
199 | /* Look up the interpreter for NAME. If no such interpreter exists, |
200 | return NULL, otherwise return a pointer to the interpreter. */ | |
201 | ||
202 | static struct interp * | |
203 | interp_lookup_existing (struct ui *ui, const char *name) | |
4a8f6654 | 204 | { |
8322445e | 205 | struct ui_interp_info *ui_interp = get_interp_info (ui); |
4a8f6654 AC |
206 | struct interp *interp; |
207 | ||
cb814510 PA |
208 | for (interp = ui_interp->interp_list; |
209 | interp != NULL; | |
210 | interp = interp->next) | |
4a8f6654 | 211 | { |
d525a99b | 212 | if (strcmp (interp->name (), name) == 0) |
4a8f6654 AC |
213 | return interp; |
214 | } | |
215 | ||
216 | return NULL; | |
217 | } | |
218 | ||
8322445e PA |
219 | /* See interps.h. */ |
220 | ||
221 | struct interp * | |
222 | interp_lookup (struct ui *ui, const char *name) | |
223 | { | |
8322445e PA |
224 | if (name == NULL || strlen (name) == 0) |
225 | return NULL; | |
226 | ||
227 | /* Only create each interpreter once per top level. */ | |
4c2287b0 | 228 | struct interp *interp = interp_lookup_existing (ui, name); |
8322445e PA |
229 | if (interp != NULL) |
230 | return interp; | |
231 | ||
4c2287b0 SM |
232 | for (const interp_factory &factory : interpreter_factories) |
233 | if (strcmp (factory.name, name) == 0) | |
8322445e | 234 | { |
4c2287b0 | 235 | interp = factory.func (name); |
8322445e PA |
236 | interp_add (ui, interp); |
237 | return interp; | |
238 | } | |
239 | ||
240 | return NULL; | |
241 | } | |
242 | ||
60eb5395 PA |
243 | /* See interps.h. */ |
244 | ||
245 | void | |
246 | set_top_level_interpreter (const char *name) | |
247 | { | |
248 | /* Find it. */ | |
249 | struct interp *interp = interp_lookup (current_ui, name); | |
250 | ||
251 | if (interp == NULL) | |
252 | error (_("Interpreter `%s' unrecognized"), name); | |
253 | /* Install it. */ | |
d6f9b0fb | 254 | interp_set (interp, true); |
60eb5395 PA |
255 | } |
256 | ||
616268b6 | 257 | void |
ca1285d1 AH |
258 | current_interp_set_logging (ui_file_up logfile, bool logging_redirect, |
259 | bool debug_redirect) | |
37ce89eb | 260 | { |
cb814510 PA |
261 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
262 | struct interp *interp = ui_interp->current_interpreter; | |
263 | ||
ca1285d1 | 264 | interp->set_logging (std::move (logfile), logging_redirect, debug_redirect); |
37ce89eb SS |
265 | } |
266 | ||
c41535fd EZ |
267 | /* Temporarily overrides the current interpreter. */ |
268 | struct interp * | |
be0d7abb | 269 | scoped_restore_interp::set_interp (const char *name) |
c41535fd | 270 | { |
cb814510 | 271 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
8322445e | 272 | struct interp *interp = interp_lookup (current_ui, name); |
cb814510 | 273 | struct interp *old_interp = ui_interp->current_interpreter; |
c41535fd EZ |
274 | |
275 | if (interp) | |
cb814510 | 276 | ui_interp->current_interpreter = interp; |
c41535fd EZ |
277 | return old_interp; |
278 | } | |
279 | ||
1777feb0 | 280 | /* Returns true if the current interp is the passed in name. */ |
4a8f6654 AC |
281 | int |
282 | current_interp_named_p (const char *interp_name) | |
283 | { | |
cb814510 PA |
284 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
285 | struct interp *interp = ui_interp->current_interpreter; | |
286 | ||
287 | if (interp != NULL) | |
d525a99b | 288 | return (strcmp (interp->name (), interp_name) == 0); |
4a8f6654 AC |
289 | |
290 | return 0; | |
291 | } | |
292 | ||
17b2616c PA |
293 | /* The interpreter that was active when a command was executed. |
294 | Normally that'd always be CURRENT_INTERPRETER, except that MI's | |
295 | -interpreter-exec command doesn't actually flip the current | |
296 | interpreter when running its sub-command. The | |
297 | `command_interpreter' global tracks when interp_exec is called | |
298 | (IOW, when -interpreter-exec is called). If that is set, it is | |
299 | INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec | |
300 | INTERP "CMD". Otherwise, interp_exec isn't active, and so the | |
301 | interpreter running the command is the current interpreter. */ | |
302 | ||
303 | struct interp * | |
304 | command_interp (void) | |
305 | { | |
cb814510 PA |
306 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
307 | ||
308 | if (ui_interp->command_interpreter != NULL) | |
309 | return ui_interp->command_interpreter; | |
17b2616c | 310 | else |
cb814510 | 311 | return ui_interp->current_interpreter; |
17b2616c PA |
312 | } |
313 | ||
b2d86570 PA |
314 | /* See interps.h. */ |
315 | ||
4a8f6654 | 316 | void |
b2d86570 | 317 | interp_pre_command_loop (struct interp *interp) |
4a8f6654 | 318 | { |
b2d86570 | 319 | gdb_assert (interp != NULL); |
4d09c5b4 | 320 | |
d6f9b0fb | 321 | interp->pre_command_loop (); |
4a8f6654 AC |
322 | } |
323 | ||
3c216924 PA |
324 | /* See interp.h */ |
325 | ||
326 | int | |
327 | interp_supports_command_editing (struct interp *interp) | |
328 | { | |
d6f9b0fb | 329 | return interp->supports_command_editing (); |
3c216924 PA |
330 | } |
331 | ||
4a8f6654 | 332 | /* interp_exec - This executes COMMAND_STR in the current |
1777feb0 | 333 | interpreter. */ |
4a8f6654 | 334 | |
71fff37b | 335 | struct gdb_exception |
4a8f6654 AC |
336 | interp_exec (struct interp *interp, const char *command_str) |
337 | { | |
cb814510 PA |
338 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
339 | ||
17b2616c | 340 | /* See `command_interp' for why we do this. */ |
753ff9bd TT |
341 | scoped_restore save_command_interp |
342 | = make_scoped_restore (&ui_interp->command_interpreter, interp); | |
17b2616c | 343 | |
753ff9bd | 344 | return interp->exec (command_str); |
4a8f6654 AC |
345 | } |
346 | ||
907d819a MS |
347 | /* A convenience routine that nulls out all the common command hooks. |
348 | Use it when removing your interpreter in its suspend proc. */ | |
4a8f6654 | 349 | void |
11308a41 | 350 | clear_interpreter_hooks (void) |
4a8f6654 | 351 | { |
9a4105ab | 352 | deprecated_print_frame_info_listing_hook = 0; |
4a8f6654 | 353 | /*print_frame_more_info_hook = 0; */ |
9a4105ab AC |
354 | deprecated_query_hook = 0; |
355 | deprecated_warning_hook = 0; | |
9a4105ab AC |
356 | deprecated_readline_begin_hook = 0; |
357 | deprecated_readline_hook = 0; | |
358 | deprecated_readline_end_hook = 0; | |
9a4105ab AC |
359 | deprecated_context_hook = 0; |
360 | deprecated_target_wait_hook = 0; | |
361 | deprecated_call_command_hook = 0; | |
9a4105ab | 362 | deprecated_error_begin_hook = 0; |
4a8f6654 AC |
363 | } |
364 | ||
b9362cc7 | 365 | static void |
1970a12f | 366 | interpreter_exec_cmd (const char *args, int from_tty) |
4a8f6654 | 367 | { |
cb814510 | 368 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
4a8f6654 | 369 | struct interp *old_interp, *interp_to_use; |
4a8f6654 AC |
370 | unsigned int nrules; |
371 | unsigned int i; | |
4a8f6654 | 372 | |
d1a41061 PP |
373 | if (args == NULL) |
374 | error_no_arg (_("interpreter-exec command")); | |
375 | ||
773a1edc TT |
376 | gdb_argv prules (args); |
377 | nrules = prules.count (); | |
4a8f6654 AC |
378 | |
379 | if (nrules < 2) | |
590042fc | 380 | error (_("Usage: interpreter-exec INTERPRETER COMMAND...")); |
4a8f6654 | 381 | |
cb814510 | 382 | old_interp = ui_interp->current_interpreter; |
4a8f6654 | 383 | |
8322445e | 384 | interp_to_use = interp_lookup (current_ui, prules[0]); |
4a8f6654 | 385 | if (interp_to_use == NULL) |
8a3fe4f8 | 386 | error (_("Could not find interpreter \"%s\"."), prules[0]); |
4a8f6654 | 387 | |
d6f9b0fb | 388 | interp_set (interp_to_use, false); |
4a8f6654 AC |
389 | |
390 | for (i = 1; i < nrules; i++) | |
391 | { | |
71fff37b | 392 | struct gdb_exception e = interp_exec (interp_to_use, prules[i]); |
abbb1732 | 393 | |
a7479e7e | 394 | if (e.reason < 0) |
4a8f6654 | 395 | { |
683f2885 | 396 | interp_set (old_interp, 0); |
8a3fe4f8 | 397 | error (_("error in command: \"%s\"."), prules[i]); |
4a8f6654 AC |
398 | } |
399 | } | |
400 | ||
683f2885 | 401 | interp_set (old_interp, 0); |
4a8f6654 AC |
402 | } |
403 | ||
60eb5395 PA |
404 | /* See interps.h. */ |
405 | ||
eb3ff9a5 | 406 | void |
6f937416 | 407 | interpreter_completer (struct cmd_list_element *ignore, |
eb3ff9a5 | 408 | completion_tracker &tracker, |
6f937416 | 409 | const char *text, const char *word) |
4a8f6654 | 410 | { |
4c2287b0 SM |
411 | int textlen = strlen (text); |
412 | ||
413 | for (const interp_factory &interp : interpreter_factories) | |
4a8f6654 | 414 | { |
4c2287b0 | 415 | if (strncmp (interp.name, text, textlen) == 0) |
4a8f6654 | 416 | { |
60a20c19 PA |
417 | tracker.add_completion |
418 | (make_completion_match_str (interp.name, text, word)); | |
4a8f6654 AC |
419 | } |
420 | } | |
4a8f6654 AC |
421 | } |
422 | ||
eb18d289 NR |
423 | struct interp * |
424 | top_level_interpreter (void) | |
425 | { | |
cb814510 PA |
426 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
427 | ||
428 | return ui_interp->top_level_interpreter; | |
eb18d289 NR |
429 | } |
430 | ||
9204d692 PA |
431 | /* See interps.h. */ |
432 | ||
433 | struct interp * | |
434 | current_interpreter (void) | |
435 | { | |
436 | struct ui_interp_info *ui_interp = get_interp_info (current_ui); | |
437 | ||
438 | return ui_interp->current_interpreter; | |
439 | } | |
440 | ||
4a8f6654 AC |
441 | /* This just adds the "interpreter-exec" command. */ |
442 | void | |
443 | _initialize_interpreter (void) | |
444 | { | |
445 | struct cmd_list_element *c; | |
446 | ||
447 | c = add_cmd ("interpreter-exec", class_support, | |
1a966eab | 448 | interpreter_exec_cmd, _("\ |
89549d7f | 449 | Execute a command in an interpreter.\n\ |
590042fc | 450 | Usage: interpreter-exec INTERPRETER COMMAND...\n\ |
4a8f6654 | 451 | The first argument is the name of the interpreter to use.\n\ |
590042fc PW |
452 | The following arguments are the commands to execute.\n\ |
453 | A command can have arguments, separated by spaces.\n\ | |
454 | These spaces must be escaped using \\ or the command\n\ | |
455 | and its arguments must be enclosed in double quotes."), &cmdlist); | |
4a8f6654 AC |
456 | set_cmd_completer (c, interpreter_completer); |
457 | } |