1 /* Manages interpreters for GDB, the GNU debugger.
3 Copyright (C) 2000-2018 Free Software Foundation, Inc.
5 Written by Jim Ingham <jingham@apple.com> of Apple Computer, Inc.
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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:
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. */
35 #include "event-loop.h"
36 #include "event-top.h"
38 #include "completer.h"
39 #include "top.h" /* For command_loop. */
40 #include "continuations.h"
42 /* Each UI has its own independent set of interpreters. */
46 /* Each top level has its own independent set of interpreters. */
47 struct interp
*interp_list
;
48 struct interp
*current_interpreter
;
49 struct interp
*top_level_interpreter
;
51 /* The interpreter that is active while `interp_exec' is active, NULL
52 at all other times. */
53 struct interp
*command_interpreter
;
56 /* Get UI's ui_interp_info object. Never returns NULL. */
58 static struct ui_interp_info
*
59 get_interp_info (struct ui
*ui
)
61 if (ui
->interp_info
== NULL
)
62 ui
->interp_info
= XCNEW (struct ui_interp_info
);
63 return ui
->interp_info
;
66 /* Get the current UI's ui_interp_info object. Never returns
69 static struct ui_interp_info
*
70 get_current_interp_info (void)
72 return get_interp_info (current_ui
);
75 /* The magic initialization routine for this module. */
77 static struct interp
*interp_lookup_existing (struct ui
*ui
,
80 interp::interp (const char *name
)
81 : m_name (xstrdup (name
))
89 /* An interpreter factory. Maps an interpreter name to the factory
90 function that instantiates an interpreter by that name. */
94 interp_factory (const char *name_
, interp_factory_func func_
)
95 : name (name_
), func (func_
)
98 /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */
101 /* The function that creates the interpreter. */
102 interp_factory_func func
;
105 /* The registered interpreter factories. */
106 static std::vector
<interp_factory
> interpreter_factories
;
111 interp_factory_register (const char *name
, interp_factory_func func
)
113 /* Assert that no factory for NAME is already registered. */
114 for (const interp_factory
&f
: interpreter_factories
)
115 if (strcmp (f
.name
, name
) == 0)
117 internal_error (__FILE__
, __LINE__
,
118 _("interpreter factory already registered: \"%s\"\n"),
122 interpreter_factories
.emplace_back (name
, func
);
125 /* Add interpreter INTERP to the gdb interpreter list. The
126 interpreter must not have previously been added. */
128 interp_add (struct ui
*ui
, struct interp
*interp
)
130 struct ui_interp_info
*ui_interp
= get_interp_info (ui
);
132 gdb_assert (interp_lookup_existing (ui
, interp
->name ()) == NULL
);
134 interp
->next
= ui_interp
->interp_list
;
135 ui_interp
->interp_list
= interp
;
138 /* This sets the current interpreter to be INTERP. If INTERP has not
139 been initialized, then this will also run the init method.
141 The TOP_LEVEL parameter tells if this new interpreter is
142 the top-level one. The top-level is what is requested
143 on the command line, and is responsible for reporting general
144 notification about target state changes. For example, if
145 MI is the top-level interpreter, then it will always report
146 events such as target stops and new thread creation, even if they
147 are caused by CLI commands. */
150 interp_set (struct interp
*interp
, bool top_level
)
152 struct ui_interp_info
*ui_interp
= get_current_interp_info ();
153 struct interp
*old_interp
= ui_interp
->current_interpreter
;
155 /* If we already have an interpreter, then trying to
156 set top level interpreter is kinda pointless. */
157 gdb_assert (!top_level
|| !ui_interp
->current_interpreter
);
158 gdb_assert (!top_level
|| !ui_interp
->top_level_interpreter
);
160 if (old_interp
!= NULL
)
162 current_uiout
->flush ();
163 old_interp
->suspend ();
166 ui_interp
->current_interpreter
= interp
;
168 ui_interp
->top_level_interpreter
= interp
;
170 /* We use interpreter_p for the "set interpreter" variable, so we need
171 to make sure we have a malloc'ed copy for the set command to free. */
172 if (interpreter_p
!= NULL
173 && strcmp (interp
->name (), interpreter_p
) != 0)
175 xfree (interpreter_p
);
177 interpreter_p
= xstrdup (interp
->name ());
180 /* Run the init proc. */
183 interp
->init (top_level
);
184 interp
->inited
= true;
187 /* Do this only after the interpreter is initialized. */
188 current_uiout
= interp
->interp_ui_out ();
190 /* Clear out any installed interpreter hooks/event handlers. */
191 clear_interpreter_hooks ();
196 /* Look up the interpreter for NAME. If no such interpreter exists,
197 return NULL, otherwise return a pointer to the interpreter. */
199 static struct interp
*
200 interp_lookup_existing (struct ui
*ui
, const char *name
)
202 struct ui_interp_info
*ui_interp
= get_interp_info (ui
);
203 struct interp
*interp
;
205 for (interp
= ui_interp
->interp_list
;
207 interp
= interp
->next
)
209 if (strcmp (interp
->name (), name
) == 0)
219 interp_lookup (struct ui
*ui
, const char *name
)
221 if (name
== NULL
|| strlen (name
) == 0)
224 /* Only create each interpreter once per top level. */
225 struct interp
*interp
= interp_lookup_existing (ui
, name
);
229 for (const interp_factory
&factory
: interpreter_factories
)
230 if (strcmp (factory
.name
, name
) == 0)
232 interp
= factory
.func (name
);
233 interp_add (ui
, interp
);
243 set_top_level_interpreter (const char *name
)
246 struct interp
*interp
= interp_lookup (current_ui
, name
);
249 error (_("Interpreter `%s' unrecognized"), name
);
251 interp_set (interp
, true);
255 current_interp_set_logging (ui_file_up logfile
,
256 bool logging_redirect
)
258 struct ui_interp_info
*ui_interp
= get_current_interp_info ();
259 struct interp
*interp
= ui_interp
->current_interpreter
;
261 interp
->set_logging (std::move (logfile
), logging_redirect
);
264 /* Temporarily overrides the current interpreter. */
266 scoped_restore_interp::set_interp (const char *name
)
268 struct ui_interp_info
*ui_interp
= get_current_interp_info ();
269 struct interp
*interp
= interp_lookup (current_ui
, name
);
270 struct interp
*old_interp
= ui_interp
->current_interpreter
;
273 ui_interp
->current_interpreter
= interp
;
277 /* Returns true if the current interp is the passed in name. */
279 current_interp_named_p (const char *interp_name
)
281 struct ui_interp_info
*ui_interp
= get_current_interp_info ();
282 struct interp
*interp
= ui_interp
->current_interpreter
;
285 return (strcmp (interp
->name (), interp_name
) == 0);
290 /* The interpreter that was active when a command was executed.
291 Normally that'd always be CURRENT_INTERPRETER, except that MI's
292 -interpreter-exec command doesn't actually flip the current
293 interpreter when running its sub-command. The
294 `command_interpreter' global tracks when interp_exec is called
295 (IOW, when -interpreter-exec is called). If that is set, it is
296 INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
297 INTERP "CMD". Otherwise, interp_exec isn't active, and so the
298 interpreter running the command is the current interpreter. */
301 command_interp (void)
303 struct ui_interp_info
*ui_interp
= get_current_interp_info ();
305 if (ui_interp
->command_interpreter
!= NULL
)
306 return ui_interp
->command_interpreter
;
308 return ui_interp
->current_interpreter
;
314 interp_pre_command_loop (struct interp
*interp
)
316 gdb_assert (interp
!= NULL
);
318 interp
->pre_command_loop ();
324 interp_supports_command_editing (struct interp
*interp
)
326 return interp
->supports_command_editing ();
329 /* interp_exec - This executes COMMAND_STR in the current
333 interp_exec (struct interp
*interp
, const char *command_str
)
335 struct ui_interp_info
*ui_interp
= get_current_interp_info ();
337 /* See `command_interp' for why we do this. */
338 scoped_restore save_command_interp
339 = make_scoped_restore (&ui_interp
->command_interpreter
, interp
);
341 return interp
->exec (command_str
);
344 /* A convenience routine that nulls out all the common command hooks.
345 Use it when removing your interpreter in its suspend proc. */
347 clear_interpreter_hooks (void)
349 deprecated_print_frame_info_listing_hook
= 0;
350 /*print_frame_more_info_hook = 0; */
351 deprecated_query_hook
= 0;
352 deprecated_warning_hook
= 0;
353 deprecated_interactive_hook
= 0;
354 deprecated_readline_begin_hook
= 0;
355 deprecated_readline_hook
= 0;
356 deprecated_readline_end_hook
= 0;
357 deprecated_context_hook
= 0;
358 deprecated_target_wait_hook
= 0;
359 deprecated_call_command_hook
= 0;
360 deprecated_error_begin_hook
= 0;
364 interpreter_exec_cmd (const char *args
, int from_tty
)
366 struct ui_interp_info
*ui_interp
= get_current_interp_info ();
367 struct interp
*old_interp
, *interp_to_use
;
372 error_no_arg (_("interpreter-exec command"));
374 gdb_argv
prules (args
);
375 nrules
= prules
.count ();
378 error (_("usage: interpreter-exec <interpreter> [ <command> ... ]"));
380 old_interp
= ui_interp
->current_interpreter
;
382 interp_to_use
= interp_lookup (current_ui
, prules
[0]);
383 if (interp_to_use
== NULL
)
384 error (_("Could not find interpreter \"%s\"."), prules
[0]);
386 interp_set (interp_to_use
, false);
388 for (i
= 1; i
< nrules
; i
++)
390 struct gdb_exception e
= interp_exec (interp_to_use
, prules
[i
]);
394 interp_set (old_interp
, 0);
395 error (_("error in command: \"%s\"."), prules
[i
]);
399 interp_set (old_interp
, 0);
405 interpreter_completer (struct cmd_list_element
*ignore
,
406 completion_tracker
&tracker
,
407 const char *text
, const char *word
)
409 int textlen
= strlen (text
);
411 for (const interp_factory
&interp
: interpreter_factories
)
413 if (strncmp (interp
.name
, text
, textlen
) == 0)
415 tracker
.add_completion
416 (make_completion_match_str (interp
.name
, text
, word
));
422 top_level_interpreter (void)
424 struct ui_interp_info
*ui_interp
= get_current_interp_info ();
426 return ui_interp
->top_level_interpreter
;
432 current_interpreter (void)
434 struct ui_interp_info
*ui_interp
= get_interp_info (current_ui
);
436 return ui_interp
->current_interpreter
;
439 /* This just adds the "interpreter-exec" command. */
441 _initialize_interpreter (void)
443 struct cmd_list_element
*c
;
445 c
= add_cmd ("interpreter-exec", class_support
,
446 interpreter_exec_cmd
, _("\
447 Execute a command in an interpreter. It takes two arguments:\n\
448 The first argument is the name of the interpreter to use.\n\
449 The second argument is the command to execute.\n"), &cmdlist
);
450 set_cmd_completer (c
, interpreter_completer
);