]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/interps.c
Remove a cleanup in Python
[thirdparty/binutils-gdb.git] / gdb / interps.c
CommitLineData
4a8f6654
AC
1/* Manages interpreters for GDB, the GNU debugger.
2
61baf725 3 Copyright (C) 2000-2017 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"
35#include "event-loop.h"
36#include "event-top.h"
37#include "interps.h"
38#include "completer.h"
4a8f6654 39#include "top.h" /* For command_loop. */
be34f849 40#include "continuations.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
AC
76
77void _initialize_interpreter (void);
78
8322445e
PA
79static struct interp *interp_lookup_existing (struct ui *ui,
80 const char *name);
81
d6f9b0fb 82interp::interp (const char *name)
4a8f6654 83{
d6f9b0fb 84 this->name = xstrdup (name);
d6f9b0fb 85 this->inited = false;
4a8f6654
AC
86}
87
d6f9b0fb
PA
88interp::~interp ()
89{}
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{
96 /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */
97 const char *name;
98
99 /* The function that creates the interpreter. */
100 interp_factory_func func;
101};
102
103typedef struct interp_factory *interp_factory_p;
104DEF_VEC_P(interp_factory_p);
105
106/* The registered interpreter factories. */
107static VEC(interp_factory_p) *interpreter_factories = NULL;
108
109/* See interps.h. */
110
111void
112interp_factory_register (const char *name, interp_factory_func func)
113{
114 struct interp_factory *f;
115 int ix;
116
117 /* Assert that no factory for NAME is already registered. */
118 for (ix = 0;
119 VEC_iterate (interp_factory_p, interpreter_factories, ix, f);
120 ++ix)
121 if (strcmp (f->name, name) == 0)
122 {
123 internal_error (__FILE__, __LINE__,
124 _("interpreter factory already registered: \"%s\"\n"),
125 name);
126 }
127
128 f = XNEW (struct interp_factory);
129 f->name = name;
130 f->func = func;
131 VEC_safe_push (interp_factory_p, interpreter_factories, f);
132}
133
4a8f6654
AC
134/* Add interpreter INTERP to the gdb interpreter list. The
135 interpreter must not have previously been added. */
136void
8322445e 137interp_add (struct ui *ui, struct interp *interp)
4a8f6654 138{
8322445e 139 struct ui_interp_info *ui_interp = get_interp_info (ui);
cb814510 140
8322445e 141 gdb_assert (interp_lookup_existing (ui, interp->name) == NULL);
4a8f6654 142
cb814510
PA
143 interp->next = ui_interp->interp_list;
144 ui_interp->interp_list = interp;
4a8f6654
AC
145}
146
147/* This sets the current interpreter to be INTERP. If INTERP has not
d6f9b0fb 148 been initialized, then this will also run the init method.
683f2885
VP
149
150 The TOP_LEVEL parameter tells if this new interpreter is
151 the top-level one. The top-level is what is requested
152 on the command line, and is responsible for reporting general
153 notification about target state changes. For example, if
154 MI is the top-level interpreter, then it will always report
155 events such as target stops and new thread creation, even if they
156 are caused by CLI commands. */
d6f9b0fb 157
a474bd8e 158static void
d6f9b0fb 159interp_set (struct interp *interp, bool top_level)
4a8f6654 160{
cb814510
PA
161 struct ui_interp_info *ui_interp = get_current_interp_info ();
162 struct interp *old_interp = ui_interp->current_interpreter;
4a8f6654 163
683f2885
VP
164 /* If we already have an interpreter, then trying to
165 set top level interpreter is kinda pointless. */
cb814510
PA
166 gdb_assert (!top_level || !ui_interp->current_interpreter);
167 gdb_assert (!top_level || !ui_interp->top_level_interpreter);
683f2885 168
cb814510 169 if (old_interp != NULL)
4a8f6654 170 {
112e8700 171 current_uiout->flush ();
d6f9b0fb 172 old_interp->suspend ();
4a8f6654 173 }
4a8f6654 174
cb814510 175 ui_interp->current_interpreter = interp;
683f2885 176 if (top_level)
cb814510 177 ui_interp->top_level_interpreter = interp;
4a8f6654
AC
178
179 /* We use interpreter_p for the "set interpreter" variable, so we need
1777feb0 180 to make sure we have a malloc'ed copy for the set command to free. */
4a8f6654 181 if (interpreter_p != NULL
cb814510 182 && strcmp (interp->name, interpreter_p) != 0)
4a8f6654
AC
183 {
184 xfree (interpreter_p);
185
cb814510 186 interpreter_p = xstrdup (interp->name);
4a8f6654
AC
187 }
188
d6f9b0fb 189 /* Run the init proc. */
4a8f6654
AC
190 if (!interp->inited)
191 {
d6f9b0fb
PA
192 interp->init (top_level);
193 interp->inited = true;
4a8f6654
AC
194 }
195
4801a9a3 196 /* Do this only after the interpreter is initialized. */
d6f9b0fb 197 current_uiout = interp->interp_ui_out ();
4801a9a3 198
907d819a 199 /* Clear out any installed interpreter hooks/event handlers. */
4a8f6654
AC
200 clear_interpreter_hooks ();
201
d6f9b0fb 202 interp->resume ();
4a8f6654
AC
203}
204
8322445e
PA
205/* Look up the interpreter for NAME. If no such interpreter exists,
206 return NULL, otherwise return a pointer to the interpreter. */
207
208static struct interp *
209interp_lookup_existing (struct ui *ui, const char *name)
4a8f6654 210{
8322445e 211 struct ui_interp_info *ui_interp = get_interp_info (ui);
4a8f6654
AC
212 struct interp *interp;
213
cb814510
PA
214 for (interp = ui_interp->interp_list;
215 interp != NULL;
216 interp = interp->next)
4a8f6654
AC
217 {
218 if (strcmp (interp->name, name) == 0)
219 return interp;
220 }
221
222 return NULL;
223}
224
8322445e
PA
225/* See interps.h. */
226
227struct interp *
228interp_lookup (struct ui *ui, const char *name)
229{
230 struct interp_factory *factory;
231 struct interp *interp;
232 int ix;
233
234 if (name == NULL || strlen (name) == 0)
235 return NULL;
236
237 /* Only create each interpreter once per top level. */
238 interp = interp_lookup_existing (ui, name);
239 if (interp != NULL)
240 return interp;
241
242 for (ix = 0;
243 VEC_iterate (interp_factory_p, interpreter_factories, ix, factory);
244 ++ix)
245 if (strcmp (factory->name, name) == 0)
246 {
247 interp = factory->func (name);
248 interp_add (ui, interp);
249 return interp;
250 }
251
252 return NULL;
253}
254
60eb5395
PA
255/* See interps.h. */
256
257void
258set_top_level_interpreter (const char *name)
259{
260 /* Find it. */
261 struct interp *interp = interp_lookup (current_ui, name);
262
263 if (interp == NULL)
264 error (_("Interpreter `%s' unrecognized"), name);
265 /* Install it. */
d6f9b0fb 266 interp_set (interp, true);
60eb5395
PA
267}
268
1777feb0 269/* Returns the current interpreter. */
4a8f6654
AC
270
271struct ui_out *
272interp_ui_out (struct interp *interp)
273{
cb814510 274 struct ui_interp_info *ui_interp = get_current_interp_info ();
4801a9a3 275
cb814510
PA
276 if (interp == NULL)
277 interp = ui_interp->current_interpreter;
d6f9b0fb 278 return interp->interp_ui_out ();
4801a9a3
PA
279}
280
616268b6
PA
281void
282current_interp_set_logging (ui_file_up logfile,
283 bool logging_redirect)
37ce89eb 284{
cb814510
PA
285 struct ui_interp_info *ui_interp = get_current_interp_info ();
286 struct interp *interp = ui_interp->current_interpreter;
287
62c14536 288 interp->set_logging (std::move (logfile), logging_redirect);
37ce89eb
SS
289}
290
c41535fd
EZ
291/* Temporarily overrides the current interpreter. */
292struct interp *
293interp_set_temp (const char *name)
294{
cb814510 295 struct ui_interp_info *ui_interp = get_current_interp_info ();
8322445e 296 struct interp *interp = interp_lookup (current_ui, name);
cb814510 297 struct interp *old_interp = ui_interp->current_interpreter;
c41535fd
EZ
298
299 if (interp)
cb814510 300 ui_interp->current_interpreter = interp;
c41535fd
EZ
301 return old_interp;
302}
303
4801a9a3
PA
304/* Returns the interpreter's name. */
305
306const char *
307interp_name (struct interp *interp)
308{
309 return interp->name;
4a8f6654
AC
310}
311
1777feb0 312/* Returns true if the current interp is the passed in name. */
4a8f6654
AC
313int
314current_interp_named_p (const char *interp_name)
315{
cb814510
PA
316 struct ui_interp_info *ui_interp = get_current_interp_info ();
317 struct interp *interp = ui_interp->current_interpreter;
318
319 if (interp != NULL)
320 return (strcmp (interp->name, interp_name) == 0);
4a8f6654
AC
321
322 return 0;
323}
324
17b2616c
PA
325/* The interpreter that was active when a command was executed.
326 Normally that'd always be CURRENT_INTERPRETER, except that MI's
327 -interpreter-exec command doesn't actually flip the current
328 interpreter when running its sub-command. The
329 `command_interpreter' global tracks when interp_exec is called
330 (IOW, when -interpreter-exec is called). If that is set, it is
331 INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
332 INTERP "CMD". Otherwise, interp_exec isn't active, and so the
333 interpreter running the command is the current interpreter. */
334
335struct interp *
336command_interp (void)
337{
cb814510
PA
338 struct ui_interp_info *ui_interp = get_current_interp_info ();
339
340 if (ui_interp->command_interpreter != NULL)
341 return ui_interp->command_interpreter;
17b2616c 342 else
cb814510 343 return ui_interp->current_interpreter;
17b2616c
PA
344}
345
b2d86570
PA
346/* See interps.h. */
347
4a8f6654 348void
b2d86570 349interp_pre_command_loop (struct interp *interp)
4a8f6654 350{
b2d86570 351 gdb_assert (interp != NULL);
4d09c5b4 352
d6f9b0fb 353 interp->pre_command_loop ();
4a8f6654
AC
354}
355
3c216924
PA
356/* See interp.h */
357
358int
359interp_supports_command_editing (struct interp *interp)
360{
d6f9b0fb 361 return interp->supports_command_editing ();
3c216924
PA
362}
363
4a8f6654 364/* interp_exec - This executes COMMAND_STR in the current
1777feb0 365 interpreter. */
4a8f6654 366
71fff37b 367struct gdb_exception
4a8f6654
AC
368interp_exec (struct interp *interp, const char *command_str)
369{
cb814510
PA
370 struct ui_interp_info *ui_interp = get_current_interp_info ();
371
17b2616c
PA
372 struct gdb_exception ex;
373 struct interp *save_command_interp;
374
17b2616c 375 /* See `command_interp' for why we do this. */
cb814510
PA
376 save_command_interp = ui_interp->command_interpreter;
377 ui_interp->command_interpreter = interp;
17b2616c 378
d6f9b0fb 379 ex = interp->exec (command_str);
17b2616c 380
cb814510 381 ui_interp->command_interpreter = save_command_interp;
17b2616c
PA
382
383 return ex;
4a8f6654
AC
384}
385
907d819a
MS
386/* A convenience routine that nulls out all the common command hooks.
387 Use it when removing your interpreter in its suspend proc. */
4a8f6654 388void
11308a41 389clear_interpreter_hooks (void)
4a8f6654 390{
9a4105ab 391 deprecated_print_frame_info_listing_hook = 0;
4a8f6654 392 /*print_frame_more_info_hook = 0; */
9a4105ab
AC
393 deprecated_query_hook = 0;
394 deprecated_warning_hook = 0;
9a4105ab 395 deprecated_interactive_hook = 0;
9a4105ab
AC
396 deprecated_readline_begin_hook = 0;
397 deprecated_readline_hook = 0;
398 deprecated_readline_end_hook = 0;
9a4105ab
AC
399 deprecated_context_hook = 0;
400 deprecated_target_wait_hook = 0;
401 deprecated_call_command_hook = 0;
9a4105ab 402 deprecated_error_begin_hook = 0;
4a8f6654
AC
403}
404
b9362cc7 405static void
4a8f6654
AC
406interpreter_exec_cmd (char *args, int from_tty)
407{
cb814510 408 struct ui_interp_info *ui_interp = get_current_interp_info ();
4a8f6654
AC
409 struct interp *old_interp, *interp_to_use;
410 char **prules = NULL;
411 char **trule = NULL;
412 unsigned int nrules;
413 unsigned int i;
5b3fca71 414 struct cleanup *cleanup;
4a8f6654 415
d1a41061
PP
416 if (args == NULL)
417 error_no_arg (_("interpreter-exec command"));
418
419 prules = gdb_buildargv (args);
5b3fca71 420 cleanup = make_cleanup_freeargv (prules);
4a8f6654
AC
421
422 nrules = 0;
d1a41061
PP
423 for (trule = prules; *trule != NULL; trule++)
424 nrules++;
4a8f6654
AC
425
426 if (nrules < 2)
8a3fe4f8 427 error (_("usage: interpreter-exec <interpreter> [ <command> ... ]"));
4a8f6654 428
cb814510 429 old_interp = ui_interp->current_interpreter;
4a8f6654 430
8322445e 431 interp_to_use = interp_lookup (current_ui, prules[0]);
4a8f6654 432 if (interp_to_use == NULL)
8a3fe4f8 433 error (_("Could not find interpreter \"%s\"."), prules[0]);
4a8f6654 434
d6f9b0fb 435 interp_set (interp_to_use, false);
4a8f6654
AC
436
437 for (i = 1; i < nrules; i++)
438 {
71fff37b 439 struct gdb_exception e = interp_exec (interp_to_use, prules[i]);
abbb1732 440
a7479e7e 441 if (e.reason < 0)
4a8f6654 442 {
683f2885 443 interp_set (old_interp, 0);
8a3fe4f8 444 error (_("error in command: \"%s\"."), prules[i]);
4a8f6654
AC
445 }
446 }
447
683f2885 448 interp_set (old_interp, 0);
5b3fca71
TT
449
450 do_cleanups (cleanup);
4a8f6654
AC
451}
452
60eb5395
PA
453/* See interps.h. */
454
eb3ff9a5 455void
6f937416 456interpreter_completer (struct cmd_list_element *ignore,
eb3ff9a5 457 completion_tracker &tracker,
6f937416 458 const char *text, const char *word)
4a8f6654 459{
8322445e 460 struct interp_factory *interp;
4a8f6654 461 int textlen;
8322445e 462 int ix;
4a8f6654 463
4a8f6654 464 textlen = strlen (text);
8322445e
PA
465 for (ix = 0;
466 VEC_iterate (interp_factory_p, interpreter_factories, ix, interp);
467 ++ix)
4a8f6654
AC
468 {
469 if (strncmp (interp->name, text, textlen) == 0)
470 {
49c4e619
TT
471 char *match;
472
473 match = (char *) xmalloc (strlen (word) + strlen (interp->name) + 1);
4a8f6654 474 if (word == text)
49c4e619 475 strcpy (match, interp->name);
4a8f6654
AC
476 else if (word > text)
477 {
1777feb0 478 /* Return some portion of interp->name. */
49c4e619 479 strcpy (match, interp->name + (word - text));
4a8f6654
AC
480 }
481 else
482 {
1777feb0 483 /* Return some of text plus interp->name. */
49c4e619
TT
484 strncpy (match, word, text - word);
485 match[text - word] = '\0';
486 strcat (match, interp->name);
4a8f6654 487 }
eb3ff9a5 488 tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
4a8f6654
AC
489 }
490 }
4a8f6654
AC
491}
492
eb18d289
NR
493struct interp *
494top_level_interpreter (void)
495{
cb814510
PA
496 struct ui_interp_info *ui_interp = get_current_interp_info ();
497
498 return ui_interp->top_level_interpreter;
eb18d289
NR
499}
500
9204d692
PA
501/* See interps.h. */
502
503struct interp *
504current_interpreter (void)
505{
506 struct ui_interp_info *ui_interp = get_interp_info (current_ui);
507
508 return ui_interp->current_interpreter;
509}
510
4a8f6654
AC
511/* This just adds the "interpreter-exec" command. */
512void
513_initialize_interpreter (void)
514{
515 struct cmd_list_element *c;
516
517 c = add_cmd ("interpreter-exec", class_support,
1a966eab
AC
518 interpreter_exec_cmd, _("\
519Execute a command in an interpreter. It takes two arguments:\n\
4a8f6654 520The first argument is the name of the interpreter to use.\n\
1a966eab 521The second argument is the command to execute.\n"), &cmdlist);
4a8f6654
AC
522 set_cmd_completer (c, interpreter_completer);
523}