Automatic date update in version.in
[binutils-gdb.git] / gdb / interps.c
blob3a9c590b8c87a5f7bb6aa2f3880a7b3e59068a23
1 /* Manages interpreters for GDB, the GNU debugger.
3 Copyright (C) 2000-2022 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. */
32 #include "defs.h"
33 #include "gdbcmd.h"
34 #include "ui-out.h"
35 #include "gdbsupport/event-loop.h"
36 #include "event-top.h"
37 #include "interps.h"
38 #include "completer.h"
39 #include "top.h" /* For command_loop. */
40 #include "main.h"
41 #include "gdbsupport/buildargv.h"
43 /* Each UI has its own independent set of interpreters. */
45 struct ui_interp_info
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;
52 /* The interpreter that is active while `interp_exec' is active, NULL
53 at all other times. */
54 struct interp *command_interpreter;
57 /* Get UI's ui_interp_info object. Never returns NULL. */
59 static struct ui_interp_info *
60 get_interp_info (struct ui *ui)
62 if (ui->interp_info == NULL)
63 ui->interp_info = XCNEW (struct ui_interp_info);
64 return ui->interp_info;
67 /* Get the current UI's ui_interp_info object. Never returns
68 NULL. */
70 static struct ui_interp_info *
71 get_current_interp_info (void)
73 return get_interp_info (current_ui);
76 /* The magic initialization routine for this module. */
78 static struct interp *interp_lookup_existing (struct ui *ui,
79 const char *name);
81 interp::interp (const char *name)
82 : m_name (make_unique_xstrdup (name))
86 interp::~interp ()
90 /* An interpreter factory. Maps an interpreter name to the factory
91 function that instantiates an interpreter by that name. */
93 struct interp_factory
95 interp_factory (const char *name_, interp_factory_func func_)
96 : name (name_), func (func_)
99 /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */
100 const char *name;
102 /* The function that creates the interpreter. */
103 interp_factory_func func;
106 /* The registered interpreter factories. */
107 static std::vector<interp_factory> interpreter_factories;
109 /* See interps.h. */
111 void
112 interp_factory_register (const char *name, interp_factory_func func)
114 /* Assert that no factory for NAME is already registered. */
115 for (const interp_factory &f : interpreter_factories)
116 if (strcmp (f.name, name) == 0)
118 internal_error (__FILE__, __LINE__,
119 _("interpreter factory already registered: \"%s\"\n"),
120 name);
123 interpreter_factories.emplace_back (name, func);
126 /* Add interpreter INTERP to the gdb interpreter list. The
127 interpreter must not have previously been added. */
128 static void
129 interp_add (struct ui *ui, struct interp *interp)
131 struct ui_interp_info *ui_interp = get_interp_info (ui);
133 gdb_assert (interp_lookup_existing (ui, interp->name ()) == NULL);
135 interp->next = ui_interp->interp_list;
136 ui_interp->interp_list = interp;
139 /* This sets the current interpreter to be INTERP. If INTERP has not
140 been initialized, then this will also run the init method.
142 The TOP_LEVEL parameter tells if this new interpreter is
143 the top-level one. The top-level is what is requested
144 on the command line, and is responsible for reporting general
145 notification about target state changes. For example, if
146 MI is the top-level interpreter, then it will always report
147 events such as target stops and new thread creation, even if they
148 are caused by CLI commands. */
150 static void
151 interp_set (struct interp *interp, bool top_level)
153 struct ui_interp_info *ui_interp = get_current_interp_info ();
154 struct interp *old_interp = ui_interp->current_interpreter;
156 /* If we already have an interpreter, then trying to
157 set top level interpreter is kinda pointless. */
158 gdb_assert (!top_level || !ui_interp->current_interpreter);
159 gdb_assert (!top_level || !ui_interp->top_level_interpreter);
161 if (old_interp != NULL)
163 current_uiout->flush ();
164 old_interp->suspend ();
167 ui_interp->current_interpreter = interp;
168 if (top_level)
169 ui_interp->top_level_interpreter = interp;
171 if (interpreter_p != interp->name ())
172 interpreter_p = interp->name ();
174 /* Run the init proc. */
175 if (!interp->inited)
177 interp->init (top_level);
178 interp->inited = true;
181 /* Do this only after the interpreter is initialized. */
182 current_uiout = interp->interp_ui_out ();
184 /* Clear out any installed interpreter hooks/event handlers. */
185 clear_interpreter_hooks ();
187 interp->resume ();
190 /* Look up the interpreter for NAME. If no such interpreter exists,
191 return NULL, otherwise return a pointer to the interpreter. */
193 static struct interp *
194 interp_lookup_existing (struct ui *ui, const char *name)
196 struct ui_interp_info *ui_interp = get_interp_info (ui);
197 struct interp *interp;
199 for (interp = ui_interp->interp_list;
200 interp != NULL;
201 interp = interp->next)
203 if (strcmp (interp->name (), name) == 0)
204 return interp;
207 return NULL;
210 /* See interps.h. */
212 struct interp *
213 interp_lookup (struct ui *ui, const char *name)
215 if (name == NULL || strlen (name) == 0)
216 return NULL;
218 /* Only create each interpreter once per top level. */
219 struct interp *interp = interp_lookup_existing (ui, name);
220 if (interp != NULL)
221 return interp;
223 for (const interp_factory &factory : interpreter_factories)
224 if (strcmp (factory.name, name) == 0)
226 interp = factory.func (name);
227 interp_add (ui, interp);
228 return interp;
231 return NULL;
234 /* See interps.h. */
236 void
237 set_top_level_interpreter (const char *name)
239 /* Find it. */
240 struct interp *interp = interp_lookup (current_ui, name);
242 if (interp == NULL)
243 error (_("Interpreter `%s' unrecognized"), name);
244 /* Install it. */
245 interp_set (interp, true);
248 void
249 current_interp_set_logging (ui_file_up logfile, bool logging_redirect,
250 bool debug_redirect)
252 struct ui_interp_info *ui_interp = get_current_interp_info ();
253 struct interp *interp = ui_interp->current_interpreter;
255 interp->set_logging (std::move (logfile), logging_redirect, debug_redirect);
258 /* Temporarily overrides the current interpreter. */
259 struct interp *
260 scoped_restore_interp::set_interp (const char *name)
262 struct ui_interp_info *ui_interp = get_current_interp_info ();
263 struct interp *interp = interp_lookup (current_ui, name);
264 struct interp *old_interp = ui_interp->current_interpreter;
266 if (interp)
267 ui_interp->current_interpreter = interp;
268 return old_interp;
271 /* Returns true if the current interp is the passed in name. */
273 current_interp_named_p (const char *interp_name)
275 struct ui_interp_info *ui_interp = get_current_interp_info ();
276 struct interp *interp = ui_interp->current_interpreter;
278 if (interp != NULL)
279 return (strcmp (interp->name (), interp_name) == 0);
281 return 0;
284 /* The interpreter that was active when a command was executed.
285 Normally that'd always be CURRENT_INTERPRETER, except that MI's
286 -interpreter-exec command doesn't actually flip the current
287 interpreter when running its sub-command. The
288 `command_interpreter' global tracks when interp_exec is called
289 (IOW, when -interpreter-exec is called). If that is set, it is
290 INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
291 INTERP "CMD". Otherwise, interp_exec isn't active, and so the
292 interpreter running the command is the current interpreter. */
294 struct interp *
295 command_interp (void)
297 struct ui_interp_info *ui_interp = get_current_interp_info ();
299 if (ui_interp->command_interpreter != NULL)
300 return ui_interp->command_interpreter;
301 else
302 return ui_interp->current_interpreter;
305 /* See interps.h. */
307 void
308 interp_pre_command_loop (struct interp *interp)
310 gdb_assert (interp != NULL);
312 interp->pre_command_loop ();
315 /* See interp.h */
318 interp_supports_command_editing (struct interp *interp)
320 return interp->supports_command_editing ();
323 /* interp_exec - This executes COMMAND_STR in the current
324 interpreter. */
326 struct gdb_exception
327 interp_exec (struct interp *interp, const char *command_str)
329 struct ui_interp_info *ui_interp = get_current_interp_info ();
331 /* See `command_interp' for why we do this. */
332 scoped_restore save_command_interp
333 = make_scoped_restore (&ui_interp->command_interpreter, interp);
335 return interp->exec (command_str);
338 /* A convenience routine that nulls out all the common command hooks.
339 Use it when removing your interpreter in its suspend proc. */
340 void
341 clear_interpreter_hooks (void)
343 deprecated_print_frame_info_listing_hook = 0;
344 /*print_frame_more_info_hook = 0; */
345 deprecated_query_hook = 0;
346 deprecated_warning_hook = 0;
347 deprecated_readline_begin_hook = 0;
348 deprecated_readline_hook = 0;
349 deprecated_readline_end_hook = 0;
350 deprecated_context_hook = 0;
351 deprecated_call_command_hook = 0;
352 deprecated_error_begin_hook = 0;
355 static void
356 interpreter_exec_cmd (const char *args, int from_tty)
358 struct ui_interp_info *ui_interp = get_current_interp_info ();
359 struct interp *old_interp, *interp_to_use;
360 unsigned int nrules;
361 unsigned int i;
363 /* Interpreters may clobber stdout/stderr (e.g. in mi_interp::resume at time
364 of writing), preserve their state here. */
365 scoped_restore save_stdout = make_scoped_restore (&gdb_stdout);
366 scoped_restore save_stderr = make_scoped_restore (&gdb_stderr);
367 scoped_restore save_stdlog = make_scoped_restore (&gdb_stdlog);
368 scoped_restore save_stdtarg = make_scoped_restore (&gdb_stdtarg);
369 scoped_restore save_stdtargerr = make_scoped_restore (&gdb_stdtargerr);
371 if (args == NULL)
372 error_no_arg (_("interpreter-exec command"));
374 gdb_argv prules (args);
375 nrules = prules.count ();
377 if (nrules < 2)
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]);
392 if (e.reason < 0)
394 interp_set (old_interp, 0);
395 error (_("error in command: \"%s\"."), prules[i]);
399 interp_set (old_interp, 0);
402 /* See interps.h. */
404 void
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));
421 struct interp *
422 top_level_interpreter (void)
424 struct ui_interp_info *ui_interp = get_current_interp_info ();
426 return ui_interp->top_level_interpreter;
429 /* See interps.h. */
431 struct interp *
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. */
440 void _initialize_interpreter ();
441 void
442 _initialize_interpreter ()
444 struct cmd_list_element *c;
446 c = add_cmd ("interpreter-exec", class_support,
447 interpreter_exec_cmd, _("\
448 Execute a command in an interpreter.\n\
449 Usage: interpreter-exec INTERPRETER COMMAND...\n\
450 The first argument is the name of the interpreter to use.\n\
451 The following arguments are the commands to execute.\n\
452 A command can have arguments, separated by spaces.\n\
453 These spaces must be escaped using \\ or the command\n\
454 and its arguments must be enclosed in double quotes."), &cmdlist);
455 set_cmd_completer (c, interpreter_completer);