]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/interps.c
internal_error: remove need to pass __FILE__/__LINE__
[thirdparty/binutils-gdb.git] / gdb / interps.c
CommitLineData
4a8f6654
AC
1/* Manages interpreters for GDB, the GNU debugger.
2
4a94e368 3 Copyright (C) 2000-2022 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"
400b5eca 35#include "gdbsupport/event-loop.h"
4a8f6654
AC
36#include "event-top.h"
37#include "interps.h"
38#include "completer.h"
4a8f6654 39#include "top.h" /* For command_loop. */
b0be6c91 40#include "main.h"
7904e961 41#include "gdbsupport/buildargv.h"
4a8f6654 42
cb814510
PA
43/* Each UI has its own independent set of interpreters. */
44
45struct 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
59static struct ui_interp_info *
8322445e 60get_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
70static struct ui_interp_info *
71get_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
78static struct interp *interp_lookup_existing (struct ui *ui,
79 const char *name);
80
d6f9b0fb 81interp::interp (const char *name)
3af607d9 82 : m_name (make_unique_xstrdup (name))
4a8f6654 83{
4a8f6654
AC
84}
85
d6f9b0fb 86interp::~interp ()
fbe61a36 87{
fbe61a36 88}
d6f9b0fb 89
8322445e
PA
90/* An interpreter factory. Maps an interpreter name to the factory
91 function that instantiates an interpreter by that name. */
92
93struct interp_factory
94{
4c2287b0
SM
95 interp_factory (const char *name_, interp_factory_func func_)
96 : name (name_), func (func_)
97 {}
98
8322445e
PA
99 /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */
100 const char *name;
101
102 /* The function that creates the interpreter. */
103 interp_factory_func func;
104};
105
8322445e 106/* The registered interpreter factories. */
4c2287b0 107static std::vector<interp_factory> interpreter_factories;
8322445e
PA
108
109/* See interps.h. */
110
111void
112interp_factory_register (const char *name, interp_factory_func func)
113{
8322445e 114 /* Assert that no factory for NAME is already registered. */
4c2287b0
SM
115 for (const interp_factory &f : interpreter_factories)
116 if (strcmp (f.name, name) == 0)
8322445e 117 {
f34652de 118 internal_error (_("interpreter factory already registered: \"%s\"\n"),
8322445e
PA
119 name);
120 }
121
4c2287b0 122 interpreter_factories.emplace_back (name, func);
8322445e
PA
123}
124
4a8f6654
AC
125/* Add interpreter INTERP to the gdb interpreter list. The
126 interpreter must not have previously been added. */
9716aa0a 127static void
8322445e 128interp_add (struct ui *ui, struct interp *interp)
4a8f6654 129{
8322445e 130 struct ui_interp_info *ui_interp = get_interp_info (ui);
cb814510 131
d525a99b 132 gdb_assert (interp_lookup_existing (ui, interp->name ()) == NULL);
4a8f6654 133
cb814510
PA
134 interp->next = ui_interp->interp_list;
135 ui_interp->interp_list = interp;
4a8f6654
AC
136}
137
138/* This sets the current interpreter to be INTERP. If INTERP has not
d6f9b0fb 139 been initialized, then this will also run the init method.
683f2885
VP
140
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. */
d6f9b0fb 148
a474bd8e 149static void
d6f9b0fb 150interp_set (struct interp *interp, bool top_level)
4a8f6654 151{
cb814510
PA
152 struct ui_interp_info *ui_interp = get_current_interp_info ();
153 struct interp *old_interp = ui_interp->current_interpreter;
4a8f6654 154
683f2885
VP
155 /* If we already have an interpreter, then trying to
156 set top level interpreter is kinda pointless. */
cb814510
PA
157 gdb_assert (!top_level || !ui_interp->current_interpreter);
158 gdb_assert (!top_level || !ui_interp->top_level_interpreter);
683f2885 159
cb814510 160 if (old_interp != NULL)
4a8f6654 161 {
112e8700 162 current_uiout->flush ();
d6f9b0fb 163 old_interp->suspend ();
4a8f6654 164 }
4a8f6654 165
cb814510 166 ui_interp->current_interpreter = interp;
683f2885 167 if (top_level)
cb814510 168 ui_interp->top_level_interpreter = interp;
4a8f6654 169
b2a696a8
TT
170 if (interpreter_p != interp->name ())
171 interpreter_p = interp->name ();
4a8f6654 172
d6f9b0fb 173 /* Run the init proc. */
4a8f6654
AC
174 if (!interp->inited)
175 {
d6f9b0fb
PA
176 interp->init (top_level);
177 interp->inited = true;
4a8f6654
AC
178 }
179
4801a9a3 180 /* Do this only after the interpreter is initialized. */
d6f9b0fb 181 current_uiout = interp->interp_ui_out ();
4801a9a3 182
907d819a 183 /* Clear out any installed interpreter hooks/event handlers. */
4a8f6654
AC
184 clear_interpreter_hooks ();
185
d6f9b0fb 186 interp->resume ();
4a8f6654
AC
187}
188
8322445e
PA
189/* Look up the interpreter for NAME. If no such interpreter exists,
190 return NULL, otherwise return a pointer to the interpreter. */
191
192static struct interp *
193interp_lookup_existing (struct ui *ui, const char *name)
4a8f6654 194{
8322445e 195 struct ui_interp_info *ui_interp = get_interp_info (ui);
4a8f6654
AC
196 struct interp *interp;
197
cb814510
PA
198 for (interp = ui_interp->interp_list;
199 interp != NULL;
200 interp = interp->next)
4a8f6654 201 {
d525a99b 202 if (strcmp (interp->name (), name) == 0)
4a8f6654
AC
203 return interp;
204 }
205
206 return NULL;
207}
208
8322445e
PA
209/* See interps.h. */
210
211struct interp *
212interp_lookup (struct ui *ui, const char *name)
213{
8322445e
PA
214 if (name == NULL || strlen (name) == 0)
215 return NULL;
216
217 /* Only create each interpreter once per top level. */
4c2287b0 218 struct interp *interp = interp_lookup_existing (ui, name);
8322445e
PA
219 if (interp != NULL)
220 return interp;
221
4c2287b0
SM
222 for (const interp_factory &factory : interpreter_factories)
223 if (strcmp (factory.name, name) == 0)
8322445e 224 {
4c2287b0 225 interp = factory.func (name);
8322445e
PA
226 interp_add (ui, interp);
227 return interp;
228 }
229
230 return NULL;
231}
232
60eb5395
PA
233/* See interps.h. */
234
235void
236set_top_level_interpreter (const char *name)
237{
238 /* Find it. */
239 struct interp *interp = interp_lookup (current_ui, name);
240
241 if (interp == NULL)
242 error (_("Interpreter `%s' unrecognized"), name);
243 /* Install it. */
d6f9b0fb 244 interp_set (interp, true);
60eb5395
PA
245}
246
616268b6 247void
ca1285d1
AH
248current_interp_set_logging (ui_file_up logfile, bool logging_redirect,
249 bool debug_redirect)
37ce89eb 250{
cb814510
PA
251 struct ui_interp_info *ui_interp = get_current_interp_info ();
252 struct interp *interp = ui_interp->current_interpreter;
253
ca1285d1 254 interp->set_logging (std::move (logfile), logging_redirect, debug_redirect);
37ce89eb
SS
255}
256
c41535fd
EZ
257/* Temporarily overrides the current interpreter. */
258struct interp *
be0d7abb 259scoped_restore_interp::set_interp (const char *name)
c41535fd 260{
cb814510 261 struct ui_interp_info *ui_interp = get_current_interp_info ();
8322445e 262 struct interp *interp = interp_lookup (current_ui, name);
cb814510 263 struct interp *old_interp = ui_interp->current_interpreter;
c41535fd
EZ
264
265 if (interp)
cb814510 266 ui_interp->current_interpreter = interp;
c41535fd
EZ
267 return old_interp;
268}
269
1777feb0 270/* Returns true if the current interp is the passed in name. */
4a8f6654
AC
271int
272current_interp_named_p (const char *interp_name)
273{
cb814510
PA
274 struct ui_interp_info *ui_interp = get_current_interp_info ();
275 struct interp *interp = ui_interp->current_interpreter;
276
277 if (interp != NULL)
d525a99b 278 return (strcmp (interp->name (), interp_name) == 0);
4a8f6654
AC
279
280 return 0;
281}
282
17b2616c
PA
283/* The interpreter that was active when a command was executed.
284 Normally that'd always be CURRENT_INTERPRETER, except that MI's
285 -interpreter-exec command doesn't actually flip the current
286 interpreter when running its sub-command. The
287 `command_interpreter' global tracks when interp_exec is called
288 (IOW, when -interpreter-exec is called). If that is set, it is
289 INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
290 INTERP "CMD". Otherwise, interp_exec isn't active, and so the
291 interpreter running the command is the current interpreter. */
292
293struct interp *
294command_interp (void)
295{
cb814510
PA
296 struct ui_interp_info *ui_interp = get_current_interp_info ();
297
298 if (ui_interp->command_interpreter != NULL)
299 return ui_interp->command_interpreter;
17b2616c 300 else
cb814510 301 return ui_interp->current_interpreter;
17b2616c
PA
302}
303
b2d86570
PA
304/* See interps.h. */
305
4a8f6654 306void
b2d86570 307interp_pre_command_loop (struct interp *interp)
4a8f6654 308{
b2d86570 309 gdb_assert (interp != NULL);
4d09c5b4 310
d6f9b0fb 311 interp->pre_command_loop ();
4a8f6654
AC
312}
313
3c216924
PA
314/* See interp.h */
315
316int
317interp_supports_command_editing (struct interp *interp)
318{
d6f9b0fb 319 return interp->supports_command_editing ();
3c216924
PA
320}
321
4a8f6654 322/* interp_exec - This executes COMMAND_STR in the current
1777feb0 323 interpreter. */
4a8f6654 324
71fff37b 325struct gdb_exception
4a8f6654
AC
326interp_exec (struct interp *interp, const char *command_str)
327{
cb814510
PA
328 struct ui_interp_info *ui_interp = get_current_interp_info ();
329
17b2616c 330 /* See `command_interp' for why we do this. */
753ff9bd
TT
331 scoped_restore save_command_interp
332 = make_scoped_restore (&ui_interp->command_interpreter, interp);
17b2616c 333
753ff9bd 334 return interp->exec (command_str);
4a8f6654
AC
335}
336
907d819a
MS
337/* A convenience routine that nulls out all the common command hooks.
338 Use it when removing your interpreter in its suspend proc. */
4a8f6654 339void
11308a41 340clear_interpreter_hooks (void)
4a8f6654 341{
9a4105ab 342 deprecated_print_frame_info_listing_hook = 0;
4a8f6654 343 /*print_frame_more_info_hook = 0; */
9a4105ab
AC
344 deprecated_query_hook = 0;
345 deprecated_warning_hook = 0;
9a4105ab
AC
346 deprecated_readline_begin_hook = 0;
347 deprecated_readline_hook = 0;
348 deprecated_readline_end_hook = 0;
9a4105ab 349 deprecated_context_hook = 0;
9a4105ab 350 deprecated_call_command_hook = 0;
9a4105ab 351 deprecated_error_begin_hook = 0;
4a8f6654
AC
352}
353
b9362cc7 354static void
1970a12f 355interpreter_exec_cmd (const char *args, int from_tty)
4a8f6654 356{
cb814510 357 struct ui_interp_info *ui_interp = get_current_interp_info ();
4a8f6654 358 struct interp *old_interp, *interp_to_use;
4a8f6654
AC
359 unsigned int nrules;
360 unsigned int i;
4a8f6654 361
e1ff6226
PW
362 /* Interpreters may clobber stdout/stderr (e.g. in mi_interp::resume at time
363 of writing), preserve their state here. */
364 scoped_restore save_stdout = make_scoped_restore (&gdb_stdout);
365 scoped_restore save_stderr = make_scoped_restore (&gdb_stderr);
366 scoped_restore save_stdlog = make_scoped_restore (&gdb_stdlog);
367 scoped_restore save_stdtarg = make_scoped_restore (&gdb_stdtarg);
368 scoped_restore save_stdtargerr = make_scoped_restore (&gdb_stdtargerr);
369
d1a41061
PP
370 if (args == NULL)
371 error_no_arg (_("interpreter-exec command"));
372
773a1edc
TT
373 gdb_argv prules (args);
374 nrules = prules.count ();
4a8f6654
AC
375
376 if (nrules < 2)
590042fc 377 error (_("Usage: interpreter-exec INTERPRETER COMMAND..."));
4a8f6654 378
cb814510 379 old_interp = ui_interp->current_interpreter;
4a8f6654 380
8322445e 381 interp_to_use = interp_lookup (current_ui, prules[0]);
4a8f6654 382 if (interp_to_use == NULL)
8a3fe4f8 383 error (_("Could not find interpreter \"%s\"."), prules[0]);
4a8f6654 384
d6f9b0fb 385 interp_set (interp_to_use, false);
4a8f6654
AC
386
387 for (i = 1; i < nrules; i++)
388 {
71fff37b 389 struct gdb_exception e = interp_exec (interp_to_use, prules[i]);
abbb1732 390
a7479e7e 391 if (e.reason < 0)
4a8f6654 392 {
683f2885 393 interp_set (old_interp, 0);
8a3fe4f8 394 error (_("error in command: \"%s\"."), prules[i]);
4a8f6654
AC
395 }
396 }
397
683f2885 398 interp_set (old_interp, 0);
4a8f6654
AC
399}
400
60eb5395
PA
401/* See interps.h. */
402
eb3ff9a5 403void
6f937416 404interpreter_completer (struct cmd_list_element *ignore,
eb3ff9a5 405 completion_tracker &tracker,
6f937416 406 const char *text, const char *word)
4a8f6654 407{
4c2287b0
SM
408 int textlen = strlen (text);
409
410 for (const interp_factory &interp : interpreter_factories)
4a8f6654 411 {
4c2287b0 412 if (strncmp (interp.name, text, textlen) == 0)
4a8f6654 413 {
60a20c19
PA
414 tracker.add_completion
415 (make_completion_match_str (interp.name, text, word));
4a8f6654
AC
416 }
417 }
4a8f6654
AC
418}
419
eb18d289
NR
420struct interp *
421top_level_interpreter (void)
422{
cb814510
PA
423 struct ui_interp_info *ui_interp = get_current_interp_info ();
424
425 return ui_interp->top_level_interpreter;
eb18d289
NR
426}
427
9204d692
PA
428/* See interps.h. */
429
430struct interp *
431current_interpreter (void)
432{
433 struct ui_interp_info *ui_interp = get_interp_info (current_ui);
434
435 return ui_interp->current_interpreter;
436}
437
4a8f6654 438/* This just adds the "interpreter-exec" command. */
6c265988 439void _initialize_interpreter ();
4a8f6654 440void
6c265988 441_initialize_interpreter ()
4a8f6654
AC
442{
443 struct cmd_list_element *c;
444
445 c = add_cmd ("interpreter-exec", class_support,
1a966eab 446 interpreter_exec_cmd, _("\
89549d7f 447Execute a command in an interpreter.\n\
590042fc 448Usage: interpreter-exec INTERPRETER COMMAND...\n\
4a8f6654 449The first argument is the name of the interpreter to use.\n\
590042fc
PW
450The following arguments are the commands to execute.\n\
451A command can have arguments, separated by spaces.\n\
452These spaces must be escaped using \\ or the command\n\
453and its arguments must be enclosed in double quotes."), &cmdlist);
4a8f6654
AC
454 set_cmd_completer (c, interpreter_completer);
455}