Updated Swedish translation for the opcodes directory
[binutils-gdb.git] / gdb / interps.c
blobe3f6ee6851238ec7b460250dacde7b1befbb1b5b
1 /* Manages interpreters for GDB, the GNU debugger.
3 Copyright (C) 2000-2023 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 "ui.h"
40 #include "main.h"
41 #include "gdbsupport/buildargv.h"
42 #include "gdbsupport/scope-exit.h"
44 /* The magic initialization routine for this module. */
46 static struct interp *interp_lookup_existing (struct ui *ui,
47 const char *name);
49 interp::interp (const char *name)
50 : m_name (name)
54 interp::~interp () = default;
56 /* An interpreter factory. Maps an interpreter name to the factory
57 function that instantiates an interpreter by that name. */
59 struct interp_factory
61 interp_factory (const char *name_, interp_factory_func func_)
62 : name (name_), func (func_)
65 /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */
66 const char *name;
68 /* The function that creates the interpreter. */
69 interp_factory_func func;
72 /* The registered interpreter factories. */
73 static std::vector<interp_factory> interpreter_factories;
75 /* See interps.h. */
77 void
78 interp_factory_register (const char *name, interp_factory_func func)
80 /* Assert that no factory for NAME is already registered. */
81 for (const interp_factory &f : interpreter_factories)
82 if (strcmp (f.name, name) == 0)
84 internal_error (_("interpreter factory already registered: \"%s\"\n"),
85 name);
88 interpreter_factories.emplace_back (name, func);
91 /* Add interpreter INTERP to the gdb interpreter list. The
92 interpreter must not have previously been added. */
93 static void
94 interp_add (struct ui *ui, struct interp *interp)
96 gdb_assert (interp_lookup_existing (ui, interp->name ()) == NULL);
98 ui->interp_list.push_back (*interp);
101 /* This sets the current interpreter to be INTERP. If INTERP has not
102 been initialized, then this will also run the init method.
104 The TOP_LEVEL parameter tells if this new interpreter is
105 the top-level one. The top-level is what is requested
106 on the command line, and is responsible for reporting general
107 notification about target state changes. For example, if
108 MI is the top-level interpreter, then it will always report
109 events such as target stops and new thread creation, even if they
110 are caused by CLI commands. */
112 static void
113 interp_set (struct interp *interp, bool top_level)
115 struct interp *old_interp = current_ui->current_interpreter;
117 /* If we already have an interpreter, then trying to
118 set top level interpreter is kinda pointless. */
119 gdb_assert (!top_level || !current_ui->current_interpreter);
120 gdb_assert (!top_level || !current_ui->top_level_interpreter);
122 if (old_interp != NULL)
124 current_uiout->flush ();
125 old_interp->suspend ();
128 current_ui->current_interpreter = interp;
129 if (top_level)
130 current_ui->top_level_interpreter = interp;
132 if (interpreter_p != interp->name ())
133 interpreter_p = interp->name ();
135 bool warn_about_mi1 = false;
137 /* Run the init proc. */
138 if (!interp->inited)
140 interp->init (top_level);
141 interp->inited = true;
143 if (streq (interp->name (), "mi1"))
144 warn_about_mi1 = true;
147 /* Do this only after the interpreter is initialized. */
148 current_uiout = interp->interp_ui_out ();
150 /* Clear out any installed interpreter hooks/event handlers. */
151 clear_interpreter_hooks ();
153 interp->resume ();
155 if (warn_about_mi1)
156 warning (_("MI version 1 is deprecated in GDB 13 and "
157 "will be removed in GDB 14. Please upgrade "
158 "to a newer version of MI."));
161 /* Look up the interpreter for NAME. If no such interpreter exists,
162 return NULL, otherwise return a pointer to the interpreter. */
164 static struct interp *
165 interp_lookup_existing (struct ui *ui, const char *name)
167 for (interp &interp : ui->interp_list)
168 if (strcmp (interp.name (), name) == 0)
169 return &interp;
171 return nullptr;
174 /* See interps.h. */
176 struct interp *
177 interp_lookup (struct ui *ui, const char *name)
179 if (name == NULL || strlen (name) == 0)
180 return NULL;
182 /* Only create each interpreter once per top level. */
183 struct interp *interp = interp_lookup_existing (ui, name);
184 if (interp != NULL)
185 return interp;
187 for (const interp_factory &factory : interpreter_factories)
188 if (strcmp (factory.name, name) == 0)
190 interp = factory.func (factory.name);
191 interp_add (ui, interp);
192 return interp;
195 return NULL;
198 /* See interps.h. */
200 void
201 set_top_level_interpreter (const char *name)
203 /* Find it. */
204 struct interp *interp = interp_lookup (current_ui, name);
206 if (interp == NULL)
207 error (_("Interpreter `%s' unrecognized"), name);
208 /* Install it. */
209 interp_set (interp, true);
212 void
213 current_interp_set_logging (ui_file_up logfile, bool logging_redirect,
214 bool debug_redirect)
216 struct interp *interp = current_ui->current_interpreter;
218 interp->set_logging (std::move (logfile), logging_redirect, debug_redirect);
221 /* Temporarily overrides the current interpreter. */
222 struct interp *
223 scoped_restore_interp::set_interp (const char *name)
225 struct interp *interp = interp_lookup (current_ui, name);
226 struct interp *old_interp = current_ui->current_interpreter;
228 if (interp)
229 current_ui->current_interpreter = interp;
231 return old_interp;
234 /* Returns true if the current interp is the passed in name. */
236 current_interp_named_p (const char *interp_name)
238 interp *interp = current_ui->current_interpreter;
240 if (interp != NULL)
241 return (strcmp (interp->name (), interp_name) == 0);
243 return 0;
246 /* The interpreter that was active when a command was executed.
247 Normally that'd always be CURRENT_INTERPRETER, except that MI's
248 -interpreter-exec command doesn't actually flip the current
249 interpreter when running its sub-command. The
250 `command_interpreter' global tracks when interp_exec is called
251 (IOW, when -interpreter-exec is called). If that is set, it is
252 INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
253 INTERP "CMD". Otherwise, interp_exec isn't active, and so the
254 interpreter running the command is the current interpreter. */
256 struct interp *
257 command_interp (void)
259 if (current_ui->command_interpreter != nullptr)
260 return current_ui->command_interpreter;
261 else
262 return current_ui->current_interpreter;
265 /* See interps.h. */
267 void
268 interp_pre_command_loop (struct interp *interp)
270 gdb_assert (interp != NULL);
272 interp->pre_command_loop ();
275 /* See interp.h */
278 interp_supports_command_editing (struct interp *interp)
280 return interp->supports_command_editing ();
283 /* interp_exec - This executes COMMAND_STR in the current
284 interpreter. */
286 void
287 interp_exec (struct interp *interp, const char *command_str)
289 /* See `command_interp' for why we do this. */
290 scoped_restore save_command_interp
291 = make_scoped_restore (&current_ui->command_interpreter, interp);
293 interp->exec (command_str);
296 /* A convenience routine that nulls out all the common command hooks.
297 Use it when removing your interpreter in its suspend proc. */
298 void
299 clear_interpreter_hooks (void)
301 deprecated_print_frame_info_listing_hook = 0;
302 /*print_frame_more_info_hook = 0; */
303 deprecated_query_hook = 0;
304 deprecated_warning_hook = 0;
305 deprecated_readline_begin_hook = 0;
306 deprecated_readline_hook = 0;
307 deprecated_readline_end_hook = 0;
308 deprecated_context_hook = 0;
309 deprecated_call_command_hook = 0;
310 deprecated_error_begin_hook = 0;
313 static void
314 interpreter_exec_cmd (const char *args, int from_tty)
316 struct interp *interp_to_use;
317 unsigned int nrules;
318 unsigned int i;
320 /* Interpreters may clobber stdout/stderr (e.g. in mi_interp::resume at time
321 of writing), preserve their state here. */
322 scoped_restore save_stdout = make_scoped_restore (&gdb_stdout);
323 scoped_restore save_stderr = make_scoped_restore (&gdb_stderr);
324 scoped_restore save_stdlog = make_scoped_restore (&gdb_stdlog);
325 scoped_restore save_stdtarg = make_scoped_restore (&gdb_stdtarg);
326 scoped_restore save_stdtargerr = make_scoped_restore (&gdb_stdtargerr);
328 if (args == NULL)
329 error_no_arg (_("interpreter-exec command"));
331 gdb_argv prules (args);
332 nrules = prules.count ();
334 if (nrules < 2)
335 error (_("Usage: interpreter-exec INTERPRETER COMMAND..."));
337 interp *old_interp = current_ui->current_interpreter;
339 interp_to_use = interp_lookup (current_ui, prules[0]);
340 if (interp_to_use == NULL)
341 error (_("Could not find interpreter \"%s\"."), prules[0]);
343 interp_set (interp_to_use, false);
344 SCOPE_EXIT
346 interp_set (old_interp, false);
349 for (i = 1; i < nrules; i++)
350 interp_exec (interp_to_use, prules[i]);
353 /* See interps.h. */
355 void
356 interpreter_completer (struct cmd_list_element *ignore,
357 completion_tracker &tracker,
358 const char *text, const char *word)
360 int textlen = strlen (text);
362 for (const interp_factory &interp : interpreter_factories)
364 if (strncmp (interp.name, text, textlen) == 0)
366 tracker.add_completion
367 (make_completion_match_str (interp.name, text, word));
372 struct interp *
373 top_level_interpreter (void)
375 return current_ui->top_level_interpreter;
378 /* See interps.h. */
380 struct interp *
381 current_interpreter (void)
383 return current_ui->current_interpreter;
386 /* This just adds the "interpreter-exec" command. */
387 void _initialize_interpreter ();
388 void
389 _initialize_interpreter ()
391 struct cmd_list_element *c;
393 c = add_cmd ("interpreter-exec", class_support,
394 interpreter_exec_cmd, _("\
395 Execute a command in an interpreter.\n\
396 Usage: interpreter-exec INTERPRETER COMMAND...\n\
397 The first argument is the name of the interpreter to use.\n\
398 The following arguments are the commands to execute.\n\
399 A command can have arguments, separated by spaces.\n\
400 These spaces must be escaped using \\ or the command\n\
401 and its arguments must be enclosed in double quotes."), &cmdlist);
402 set_cmd_completer (c, interpreter_completer);