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