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