]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/interps.c
update copyright year range in GDB files
[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
4a8f6654
AC
75struct interp
76{
1777feb0 77 /* This is the name in "-i=" and set interpreter. */
4a8f6654
AC
78 const char *name;
79
80 /* Interpreters are stored in a linked list, this is the next
81 one... */
82 struct interp *next;
83
84 /* This is a cookie that an instance of the interpreter can use.
85 This is a bit confused right now as the exact initialization
86 sequence for it, and how it relates to the interpreter's uiout
87 object is a bit confused. */
88 void *data;
89
1777feb0 90 /* Has the init_proc been run? */
4a8f6654
AC
91 int inited;
92
4a8f6654
AC
93 const struct interp_procs *procs;
94 int quiet_p;
95};
96
1777feb0 97/* The magic initialization routine for this module. */
4a8f6654
AC
98
99void _initialize_interpreter (void);
100
8322445e
PA
101static struct interp *interp_lookup_existing (struct ui *ui,
102 const char *name);
103
4a8f6654
AC
104/* interp_new - This allocates space for a new interpreter,
105 fills the fields from the inputs, and returns a pointer to the
1777feb0 106 interpreter. */
4a8f6654 107struct interp *
8322445e 108interp_new (const char *name, const struct interp_procs *procs, void *data)
4a8f6654
AC
109{
110 struct interp *new_interp;
111
70ba0933 112 new_interp = XNEW (struct interp);
4a8f6654
AC
113
114 new_interp->name = xstrdup (name);
8322445e 115 new_interp->data = data;
4a8f6654
AC
116 new_interp->quiet_p = 0;
117 new_interp->procs = procs;
118 new_interp->inited = 0;
119
120 return new_interp;
121}
122
8322445e
PA
123/* An interpreter factory. Maps an interpreter name to the factory
124 function that instantiates an interpreter by that name. */
125
126struct interp_factory
127{
128 /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */
129 const char *name;
130
131 /* The function that creates the interpreter. */
132 interp_factory_func func;
133};
134
135typedef struct interp_factory *interp_factory_p;
136DEF_VEC_P(interp_factory_p);
137
138/* The registered interpreter factories. */
139static VEC(interp_factory_p) *interpreter_factories = NULL;
140
141/* See interps.h. */
142
143void
144interp_factory_register (const char *name, interp_factory_func func)
145{
146 struct interp_factory *f;
147 int ix;
148
149 /* Assert that no factory for NAME is already registered. */
150 for (ix = 0;
151 VEC_iterate (interp_factory_p, interpreter_factories, ix, f);
152 ++ix)
153 if (strcmp (f->name, name) == 0)
154 {
155 internal_error (__FILE__, __LINE__,
156 _("interpreter factory already registered: \"%s\"\n"),
157 name);
158 }
159
160 f = XNEW (struct interp_factory);
161 f->name = name;
162 f->func = func;
163 VEC_safe_push (interp_factory_p, interpreter_factories, f);
164}
165
4a8f6654
AC
166/* Add interpreter INTERP to the gdb interpreter list. The
167 interpreter must not have previously been added. */
168void
8322445e 169interp_add (struct ui *ui, struct interp *interp)
4a8f6654 170{
8322445e 171 struct ui_interp_info *ui_interp = get_interp_info (ui);
cb814510 172
8322445e 173 gdb_assert (interp_lookup_existing (ui, interp->name) == NULL);
4a8f6654 174
cb814510
PA
175 interp->next = ui_interp->interp_list;
176 ui_interp->interp_list = interp;
4a8f6654
AC
177}
178
179/* This sets the current interpreter to be INTERP. If INTERP has not
180 been initialized, then this will also run the init proc. If the
181 init proc is successful, return 1, if it fails, set the old
182 interpreter back in place and return 0. If we can't restore the
183 old interpreter, then raise an internal error, since we are in
1777feb0 184 pretty bad shape at this point.
683f2885
VP
185
186 The TOP_LEVEL parameter tells if this new interpreter is
187 the top-level one. The top-level is what is requested
188 on the command line, and is responsible for reporting general
189 notification about target state changes. For example, if
190 MI is the top-level interpreter, then it will always report
191 events such as target stops and new thread creation, even if they
192 are caused by CLI commands. */
4a8f6654 193int
683f2885 194interp_set (struct interp *interp, int top_level)
4a8f6654 195{
cb814510
PA
196 struct ui_interp_info *ui_interp = get_current_interp_info ();
197 struct interp *old_interp = ui_interp->current_interpreter;
4a8f6654 198 int first_time = 0;
4a8f6654
AC
199 char buffer[64];
200
683f2885
VP
201 /* If we already have an interpreter, then trying to
202 set top level interpreter is kinda pointless. */
cb814510
PA
203 gdb_assert (!top_level || !ui_interp->current_interpreter);
204 gdb_assert (!top_level || !ui_interp->top_level_interpreter);
683f2885 205
cb814510 206 if (old_interp != NULL)
4a8f6654 207 {
112e8700 208 current_uiout->flush ();
cb814510
PA
209 if (old_interp->procs->suspend_proc
210 && !old_interp->procs->suspend_proc (old_interp->data))
4a8f6654 211 {
8a3fe4f8 212 error (_("Could not suspend interpreter \"%s\"."),
cb814510 213 old_interp->name);
4a8f6654
AC
214 }
215 }
216 else
217 {
218 first_time = 1;
219 }
220
cb814510 221 ui_interp->current_interpreter = interp;
683f2885 222 if (top_level)
cb814510 223 ui_interp->top_level_interpreter = interp;
4a8f6654
AC
224
225 /* We use interpreter_p for the "set interpreter" variable, so we need
1777feb0 226 to make sure we have a malloc'ed copy for the set command to free. */
4a8f6654 227 if (interpreter_p != NULL
cb814510 228 && strcmp (interp->name, interpreter_p) != 0)
4a8f6654
AC
229 {
230 xfree (interpreter_p);
231
cb814510 232 interpreter_p = xstrdup (interp->name);
4a8f6654
AC
233 }
234
1777feb0 235 /* Run the init proc. If it fails, try to restore the old interp. */
4a8f6654
AC
236
237 if (!interp->inited)
238 {
239 if (interp->procs->init_proc != NULL)
240 {
4801a9a3 241 interp->data = interp->procs->init_proc (interp, top_level);
4a8f6654
AC
242 }
243 interp->inited = 1;
244 }
245
4801a9a3
PA
246 /* Do this only after the interpreter is initialized. */
247 current_uiout = interp->procs->ui_out_proc (interp);
248
907d819a 249 /* Clear out any installed interpreter hooks/event handlers. */
4a8f6654
AC
250 clear_interpreter_hooks ();
251
252 if (interp->procs->resume_proc != NULL
253 && (!interp->procs->resume_proc (interp->data)))
254 {
683f2885 255 if (old_interp == NULL || !interp_set (old_interp, 0))
4a8f6654 256 internal_error (__FILE__, __LINE__,
e2e0b3e5 257 _("Failed to initialize new interp \"%s\" %s"),
4a8f6654
AC
258 interp->name, "and could not restore old interp!\n");
259 return 0;
260 }
261
92bcb5f9 262 if (!first_time && !interp_quiet_p (interp))
4a8f6654 263 {
92bcb5f9
PA
264 xsnprintf (buffer, sizeof (buffer),
265 "Switching to interpreter \"%.24s\".\n", interp->name);
112e8700 266 current_uiout->text (buffer);
4a8f6654
AC
267 }
268
269 return 1;
270}
271
8322445e
PA
272/* Look up the interpreter for NAME. If no such interpreter exists,
273 return NULL, otherwise return a pointer to the interpreter. */
274
275static struct interp *
276interp_lookup_existing (struct ui *ui, const char *name)
4a8f6654 277{
8322445e 278 struct ui_interp_info *ui_interp = get_interp_info (ui);
4a8f6654
AC
279 struct interp *interp;
280
cb814510
PA
281 for (interp = ui_interp->interp_list;
282 interp != NULL;
283 interp = interp->next)
4a8f6654
AC
284 {
285 if (strcmp (interp->name, name) == 0)
286 return interp;
287 }
288
289 return NULL;
290}
291
8322445e
PA
292/* See interps.h. */
293
294struct interp *
295interp_lookup (struct ui *ui, const char *name)
296{
297 struct interp_factory *factory;
298 struct interp *interp;
299 int ix;
300
301 if (name == NULL || strlen (name) == 0)
302 return NULL;
303
304 /* Only create each interpreter once per top level. */
305 interp = interp_lookup_existing (ui, name);
306 if (interp != NULL)
307 return interp;
308
309 for (ix = 0;
310 VEC_iterate (interp_factory_p, interpreter_factories, ix, factory);
311 ++ix)
312 if (strcmp (factory->name, name) == 0)
313 {
314 interp = factory->func (name);
315 interp_add (ui, interp);
316 return interp;
317 }
318
319 return NULL;
320}
321
60eb5395
PA
322/* See interps.h. */
323
324void
325set_top_level_interpreter (const char *name)
326{
327 /* Find it. */
328 struct interp *interp = interp_lookup (current_ui, name);
329
330 if (interp == NULL)
331 error (_("Interpreter `%s' unrecognized"), name);
332 /* Install it. */
333 if (!interp_set (interp, 1))
334 error (_("Interpreter `%s' failed to initialize."), name);
335}
336
1777feb0 337/* Returns the current interpreter. */
4a8f6654
AC
338
339struct ui_out *
340interp_ui_out (struct interp *interp)
341{
cb814510 342 struct ui_interp_info *ui_interp = get_current_interp_info ();
4801a9a3 343
cb814510
PA
344 if (interp == NULL)
345 interp = ui_interp->current_interpreter;
346 return interp->procs->ui_out_proc (interp);
4801a9a3
PA
347}
348
37ce89eb
SS
349int
350current_interp_set_logging (int start_log, struct ui_file *out,
351 struct ui_file *logfile)
352{
cb814510
PA
353 struct ui_interp_info *ui_interp = get_current_interp_info ();
354 struct interp *interp = ui_interp->current_interpreter;
355
356 if (interp == NULL
357 || interp->procs->set_logging_proc == NULL)
37ce89eb
SS
358 return 0;
359
cb814510 360 return interp->procs->set_logging_proc (interp, start_log, out, logfile);
37ce89eb
SS
361}
362
c41535fd
EZ
363/* Temporarily overrides the current interpreter. */
364struct interp *
365interp_set_temp (const char *name)
366{
cb814510 367 struct ui_interp_info *ui_interp = get_current_interp_info ();
8322445e 368 struct interp *interp = interp_lookup (current_ui, name);
cb814510 369 struct interp *old_interp = ui_interp->current_interpreter;
c41535fd
EZ
370
371 if (interp)
cb814510 372 ui_interp->current_interpreter = interp;
c41535fd
EZ
373 return old_interp;
374}
375
4801a9a3 376/* Returns the interpreter's cookie. */
4a8f6654 377
4801a9a3
PA
378void *
379interp_data (struct interp *interp)
380{
381 return interp->data;
382}
383
384/* Returns the interpreter's name. */
385
386const char *
387interp_name (struct interp *interp)
388{
389 return interp->name;
4a8f6654
AC
390}
391
1777feb0 392/* Returns true if the current interp is the passed in name. */
4a8f6654
AC
393int
394current_interp_named_p (const char *interp_name)
395{
cb814510
PA
396 struct ui_interp_info *ui_interp = get_current_interp_info ();
397 struct interp *interp = ui_interp->current_interpreter;
398
399 if (interp != NULL)
400 return (strcmp (interp->name, interp_name) == 0);
4a8f6654
AC
401
402 return 0;
403}
404
17b2616c
PA
405/* The interpreter that was active when a command was executed.
406 Normally that'd always be CURRENT_INTERPRETER, except that MI's
407 -interpreter-exec command doesn't actually flip the current
408 interpreter when running its sub-command. The
409 `command_interpreter' global tracks when interp_exec is called
410 (IOW, when -interpreter-exec is called). If that is set, it is
411 INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
412 INTERP "CMD". Otherwise, interp_exec isn't active, and so the
413 interpreter running the command is the current interpreter. */
414
415struct interp *
416command_interp (void)
417{
cb814510
PA
418 struct ui_interp_info *ui_interp = get_current_interp_info ();
419
420 if (ui_interp->command_interpreter != NULL)
421 return ui_interp->command_interpreter;
17b2616c 422 else
cb814510 423 return ui_interp->current_interpreter;
17b2616c
PA
424}
425
b2d86570
PA
426/* See interps.h. */
427
4a8f6654 428void
b2d86570 429interp_pre_command_loop (struct interp *interp)
4a8f6654 430{
b2d86570 431 gdb_assert (interp != NULL);
4d09c5b4 432
b2d86570
PA
433 if (interp->procs->pre_command_loop_proc != NULL)
434 interp->procs->pre_command_loop_proc (interp);
4a8f6654
AC
435}
436
3c216924
PA
437/* See interp.h */
438
439int
440interp_supports_command_editing (struct interp *interp)
441{
442 if (interp->procs->supports_command_editing_proc != NULL)
443 return interp->procs->supports_command_editing_proc (interp);
444 return 0;
445}
446
4a8f6654
AC
447int
448interp_quiet_p (struct interp *interp)
449{
cb814510
PA
450 struct ui_interp_info *ui_interp = get_current_interp_info ();
451
4a8f6654
AC
452 if (interp != NULL)
453 return interp->quiet_p;
454 else
cb814510 455 return ui_interp->current_interpreter->quiet_p;
4a8f6654
AC
456}
457
b9362cc7 458static int
4a8f6654
AC
459interp_set_quiet (struct interp *interp, int quiet)
460{
461 int old_val = interp->quiet_p;
abbb1732 462
4a8f6654
AC
463 interp->quiet_p = quiet;
464 return old_val;
465}
466
467/* interp_exec - This executes COMMAND_STR in the current
1777feb0 468 interpreter. */
4a8f6654 469
71fff37b 470struct gdb_exception
4a8f6654
AC
471interp_exec (struct interp *interp, const char *command_str)
472{
cb814510
PA
473 struct ui_interp_info *ui_interp = get_current_interp_info ();
474
17b2616c
PA
475 struct gdb_exception ex;
476 struct interp *save_command_interp;
477
34dc884e
DE
478 gdb_assert (interp->procs->exec_proc != NULL);
479
17b2616c 480 /* See `command_interp' for why we do this. */
cb814510
PA
481 save_command_interp = ui_interp->command_interpreter;
482 ui_interp->command_interpreter = interp;
17b2616c
PA
483
484 ex = interp->procs->exec_proc (interp->data, command_str);
485
cb814510 486 ui_interp->command_interpreter = save_command_interp;
17b2616c
PA
487
488 return ex;
4a8f6654
AC
489}
490
907d819a
MS
491/* A convenience routine that nulls out all the common command hooks.
492 Use it when removing your interpreter in its suspend proc. */
4a8f6654 493void
11308a41 494clear_interpreter_hooks (void)
4a8f6654 495{
9a4105ab 496 deprecated_print_frame_info_listing_hook = 0;
4a8f6654 497 /*print_frame_more_info_hook = 0; */
9a4105ab
AC
498 deprecated_query_hook = 0;
499 deprecated_warning_hook = 0;
9a4105ab 500 deprecated_interactive_hook = 0;
9a4105ab
AC
501 deprecated_readline_begin_hook = 0;
502 deprecated_readline_hook = 0;
503 deprecated_readline_end_hook = 0;
9a4105ab
AC
504 deprecated_context_hook = 0;
505 deprecated_target_wait_hook = 0;
506 deprecated_call_command_hook = 0;
9a4105ab 507 deprecated_error_begin_hook = 0;
4a8f6654
AC
508}
509
b9362cc7 510static void
4a8f6654
AC
511interpreter_exec_cmd (char *args, int from_tty)
512{
cb814510 513 struct ui_interp_info *ui_interp = get_current_interp_info ();
4a8f6654
AC
514 struct interp *old_interp, *interp_to_use;
515 char **prules = NULL;
516 char **trule = NULL;
517 unsigned int nrules;
518 unsigned int i;
519 int old_quiet, use_quiet;
5b3fca71 520 struct cleanup *cleanup;
4a8f6654 521
d1a41061
PP
522 if (args == NULL)
523 error_no_arg (_("interpreter-exec command"));
524
525 prules = gdb_buildargv (args);
5b3fca71 526 cleanup = make_cleanup_freeargv (prules);
4a8f6654
AC
527
528 nrules = 0;
d1a41061
PP
529 for (trule = prules; *trule != NULL; trule++)
530 nrules++;
4a8f6654
AC
531
532 if (nrules < 2)
8a3fe4f8 533 error (_("usage: interpreter-exec <interpreter> [ <command> ... ]"));
4a8f6654 534
cb814510 535 old_interp = ui_interp->current_interpreter;
4a8f6654 536
8322445e 537 interp_to_use = interp_lookup (current_ui, prules[0]);
4a8f6654 538 if (interp_to_use == NULL)
8a3fe4f8 539 error (_("Could not find interpreter \"%s\"."), prules[0]);
4a8f6654 540
1777feb0 541 /* Temporarily set interpreters quiet. */
4a8f6654
AC
542 old_quiet = interp_set_quiet (old_interp, 1);
543 use_quiet = interp_set_quiet (interp_to_use, 1);
544
683f2885 545 if (!interp_set (interp_to_use, 0))
8a3fe4f8 546 error (_("Could not switch to interpreter \"%s\"."), prules[0]);
4a8f6654
AC
547
548 for (i = 1; i < nrules; i++)
549 {
71fff37b 550 struct gdb_exception e = interp_exec (interp_to_use, prules[i]);
abbb1732 551
a7479e7e 552 if (e.reason < 0)
4a8f6654 553 {
683f2885 554 interp_set (old_interp, 0);
04d1f770
DJ
555 interp_set_quiet (interp_to_use, use_quiet);
556 interp_set_quiet (old_interp, old_quiet);
8a3fe4f8 557 error (_("error in command: \"%s\"."), prules[i]);
4a8f6654
AC
558 }
559 }
560
683f2885 561 interp_set (old_interp, 0);
4a8f6654
AC
562 interp_set_quiet (interp_to_use, use_quiet);
563 interp_set_quiet (old_interp, old_quiet);
5b3fca71
TT
564
565 do_cleanups (cleanup);
4a8f6654
AC
566}
567
60eb5395
PA
568/* See interps.h. */
569
570VEC (char_ptr) *
6f937416
PA
571interpreter_completer (struct cmd_list_element *ignore,
572 const char *text, const char *word)
4a8f6654 573{
8322445e 574 struct interp_factory *interp;
4a8f6654 575 int textlen;
49c4e619 576 VEC (char_ptr) *matches = NULL;
8322445e 577 int ix;
4a8f6654 578
4a8f6654 579 textlen = strlen (text);
8322445e
PA
580 for (ix = 0;
581 VEC_iterate (interp_factory_p, interpreter_factories, ix, interp);
582 ++ix)
4a8f6654
AC
583 {
584 if (strncmp (interp->name, text, textlen) == 0)
585 {
49c4e619
TT
586 char *match;
587
588 match = (char *) xmalloc (strlen (word) + strlen (interp->name) + 1);
4a8f6654 589 if (word == text)
49c4e619 590 strcpy (match, interp->name);
4a8f6654
AC
591 else if (word > text)
592 {
1777feb0 593 /* Return some portion of interp->name. */
49c4e619 594 strcpy (match, interp->name + (word - text));
4a8f6654
AC
595 }
596 else
597 {
1777feb0 598 /* Return some of text plus interp->name. */
49c4e619
TT
599 strncpy (match, word, text - word);
600 match[text - word] = '\0';
601 strcat (match, interp->name);
4a8f6654 602 }
49c4e619 603 VEC_safe_push (char_ptr, matches, match);
4a8f6654
AC
604 }
605 }
606
4a8f6654
AC
607 return matches;
608}
609
eb18d289
NR
610struct interp *
611top_level_interpreter (void)
612{
cb814510
PA
613 struct ui_interp_info *ui_interp = get_current_interp_info ();
614
615 return ui_interp->top_level_interpreter;
eb18d289
NR
616}
617
618void *
683f2885
VP
619top_level_interpreter_data (void)
620{
cb814510
PA
621 struct interp *interp;
622
623 interp = top_level_interpreter ();
624 gdb_assert (interp != NULL);
625 return interp->data;
683f2885
VP
626}
627
9204d692
PA
628/* See interps.h. */
629
630struct interp *
631current_interpreter (void)
632{
633 struct ui_interp_info *ui_interp = get_interp_info (current_ui);
634
635 return ui_interp->current_interpreter;
636}
637
4a8f6654
AC
638/* This just adds the "interpreter-exec" command. */
639void
640_initialize_interpreter (void)
641{
642 struct cmd_list_element *c;
643
644 c = add_cmd ("interpreter-exec", class_support,
1a966eab
AC
645 interpreter_exec_cmd, _("\
646Execute a command in an interpreter. It takes two arguments:\n\
4a8f6654 647The first argument is the name of the interpreter to use.\n\
1a966eab 648The second argument is the command to execute.\n"), &cmdlist);
4a8f6654
AC
649 set_cmd_completer (c, interpreter_completer);
650}