Move "tee" building down to interpreter::set_logging_proc
[deliverable/binutils-gdb.git] / gdb / interps.c
1 /* Manages interpreters for GDB, the GNU debugger.
2
3 Copyright (C) 2000-2017 Free Software Foundation, Inc.
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
11 the Free Software Foundation; either version 3 of the License, or
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
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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"
39 #include "top.h" /* For command_loop. */
40 #include "continuations.h"
41
42 /* Each UI has its own independent set of interpreters. */
43
44 struct 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
56 /* Get UI's ui_interp_info object. Never returns NULL. */
57
58 static struct ui_interp_info *
59 get_interp_info (struct ui *ui)
60 {
61 if (ui->interp_info == NULL)
62 ui->interp_info = XCNEW (struct ui_interp_info);
63 return ui->interp_info;
64 }
65
66 /* Get the current UI's ui_interp_info object. Never returns
67 NULL. */
68
69 static struct ui_interp_info *
70 get_current_interp_info (void)
71 {
72 return get_interp_info (current_ui);
73 }
74
75 struct interp
76 {
77 /* This is the name in "-i=" and set interpreter. */
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
90 /* Has the init_proc been run? */
91 int inited;
92
93 const struct interp_procs *procs;
94 int quiet_p;
95 };
96
97 /* The magic initialization routine for this module. */
98
99 void _initialize_interpreter (void);
100
101 static struct interp *interp_lookup_existing (struct ui *ui,
102 const char *name);
103
104 /* interp_new - This allocates space for a new interpreter,
105 fills the fields from the inputs, and returns a pointer to the
106 interpreter. */
107 struct interp *
108 interp_new (const char *name, const struct interp_procs *procs, void *data)
109 {
110 struct interp *new_interp;
111
112 new_interp = XNEW (struct interp);
113
114 new_interp->name = xstrdup (name);
115 new_interp->data = data;
116 new_interp->quiet_p = 0;
117 new_interp->procs = procs;
118 new_interp->inited = 0;
119
120 return new_interp;
121 }
122
123 /* An interpreter factory. Maps an interpreter name to the factory
124 function that instantiates an interpreter by that name. */
125
126 struct 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
135 typedef struct interp_factory *interp_factory_p;
136 DEF_VEC_P(interp_factory_p);
137
138 /* The registered interpreter factories. */
139 static VEC(interp_factory_p) *interpreter_factories = NULL;
140
141 /* See interps.h. */
142
143 void
144 interp_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
166 /* Add interpreter INTERP to the gdb interpreter list. The
167 interpreter must not have previously been added. */
168 void
169 interp_add (struct ui *ui, struct interp *interp)
170 {
171 struct ui_interp_info *ui_interp = get_interp_info (ui);
172
173 gdb_assert (interp_lookup_existing (ui, interp->name) == NULL);
174
175 interp->next = ui_interp->interp_list;
176 ui_interp->interp_list = interp;
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
184 pretty bad shape at this point.
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. */
193 int
194 interp_set (struct interp *interp, int top_level)
195 {
196 struct ui_interp_info *ui_interp = get_current_interp_info ();
197 struct interp *old_interp = ui_interp->current_interpreter;
198 int first_time = 0;
199 char buffer[64];
200
201 /* If we already have an interpreter, then trying to
202 set top level interpreter is kinda pointless. */
203 gdb_assert (!top_level || !ui_interp->current_interpreter);
204 gdb_assert (!top_level || !ui_interp->top_level_interpreter);
205
206 if (old_interp != NULL)
207 {
208 current_uiout->flush ();
209 if (old_interp->procs->suspend_proc
210 && !old_interp->procs->suspend_proc (old_interp->data))
211 {
212 error (_("Could not suspend interpreter \"%s\"."),
213 old_interp->name);
214 }
215 }
216 else
217 {
218 first_time = 1;
219 }
220
221 ui_interp->current_interpreter = interp;
222 if (top_level)
223 ui_interp->top_level_interpreter = interp;
224
225 /* We use interpreter_p for the "set interpreter" variable, so we need
226 to make sure we have a malloc'ed copy for the set command to free. */
227 if (interpreter_p != NULL
228 && strcmp (interp->name, interpreter_p) != 0)
229 {
230 xfree (interpreter_p);
231
232 interpreter_p = xstrdup (interp->name);
233 }
234
235 /* Run the init proc. If it fails, try to restore the old interp. */
236
237 if (!interp->inited)
238 {
239 if (interp->procs->init_proc != NULL)
240 {
241 interp->data = interp->procs->init_proc (interp, top_level);
242 }
243 interp->inited = 1;
244 }
245
246 /* Do this only after the interpreter is initialized. */
247 current_uiout = interp->procs->ui_out_proc (interp);
248
249 /* Clear out any installed interpreter hooks/event handlers. */
250 clear_interpreter_hooks ();
251
252 if (interp->procs->resume_proc != NULL
253 && (!interp->procs->resume_proc (interp->data)))
254 {
255 if (old_interp == NULL || !interp_set (old_interp, 0))
256 internal_error (__FILE__, __LINE__,
257 _("Failed to initialize new interp \"%s\" %s"),
258 interp->name, "and could not restore old interp!\n");
259 return 0;
260 }
261
262 if (!first_time && !interp_quiet_p (interp))
263 {
264 xsnprintf (buffer, sizeof (buffer),
265 "Switching to interpreter \"%.24s\".\n", interp->name);
266 current_uiout->text (buffer);
267 }
268
269 return 1;
270 }
271
272 /* Look up the interpreter for NAME. If no such interpreter exists,
273 return NULL, otherwise return a pointer to the interpreter. */
274
275 static struct interp *
276 interp_lookup_existing (struct ui *ui, const char *name)
277 {
278 struct ui_interp_info *ui_interp = get_interp_info (ui);
279 struct interp *interp;
280
281 for (interp = ui_interp->interp_list;
282 interp != NULL;
283 interp = interp->next)
284 {
285 if (strcmp (interp->name, name) == 0)
286 return interp;
287 }
288
289 return NULL;
290 }
291
292 /* See interps.h. */
293
294 struct interp *
295 interp_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
322 /* See interps.h. */
323
324 void
325 set_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
337 /* Returns the current interpreter. */
338
339 struct ui_out *
340 interp_ui_out (struct interp *interp)
341 {
342 struct ui_interp_info *ui_interp = get_current_interp_info ();
343
344 if (interp == NULL)
345 interp = ui_interp->current_interpreter;
346 return interp->procs->ui_out_proc (interp);
347 }
348
349 void
350 current_interp_set_logging (ui_file_up logfile,
351 bool logging_redirect)
352 {
353 struct ui_interp_info *ui_interp = get_current_interp_info ();
354 struct interp *interp = ui_interp->current_interpreter;
355
356 return interp->procs->set_logging_proc (interp, std::move (logfile),
357 logging_redirect);
358 }
359
360 /* Temporarily overrides the current interpreter. */
361 struct interp *
362 interp_set_temp (const char *name)
363 {
364 struct ui_interp_info *ui_interp = get_current_interp_info ();
365 struct interp *interp = interp_lookup (current_ui, name);
366 struct interp *old_interp = ui_interp->current_interpreter;
367
368 if (interp)
369 ui_interp->current_interpreter = interp;
370 return old_interp;
371 }
372
373 /* Returns the interpreter's cookie. */
374
375 void *
376 interp_data (struct interp *interp)
377 {
378 return interp->data;
379 }
380
381 /* Returns the interpreter's name. */
382
383 const char *
384 interp_name (struct interp *interp)
385 {
386 return interp->name;
387 }
388
389 /* Returns true if the current interp is the passed in name. */
390 int
391 current_interp_named_p (const char *interp_name)
392 {
393 struct ui_interp_info *ui_interp = get_current_interp_info ();
394 struct interp *interp = ui_interp->current_interpreter;
395
396 if (interp != NULL)
397 return (strcmp (interp->name, interp_name) == 0);
398
399 return 0;
400 }
401
402 /* The interpreter that was active when a command was executed.
403 Normally that'd always be CURRENT_INTERPRETER, except that MI's
404 -interpreter-exec command doesn't actually flip the current
405 interpreter when running its sub-command. The
406 `command_interpreter' global tracks when interp_exec is called
407 (IOW, when -interpreter-exec is called). If that is set, it is
408 INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
409 INTERP "CMD". Otherwise, interp_exec isn't active, and so the
410 interpreter running the command is the current interpreter. */
411
412 struct interp *
413 command_interp (void)
414 {
415 struct ui_interp_info *ui_interp = get_current_interp_info ();
416
417 if (ui_interp->command_interpreter != NULL)
418 return ui_interp->command_interpreter;
419 else
420 return ui_interp->current_interpreter;
421 }
422
423 /* See interps.h. */
424
425 void
426 interp_pre_command_loop (struct interp *interp)
427 {
428 gdb_assert (interp != NULL);
429
430 if (interp->procs->pre_command_loop_proc != NULL)
431 interp->procs->pre_command_loop_proc (interp);
432 }
433
434 /* See interp.h */
435
436 int
437 interp_supports_command_editing (struct interp *interp)
438 {
439 if (interp->procs->supports_command_editing_proc != NULL)
440 return interp->procs->supports_command_editing_proc (interp);
441 return 0;
442 }
443
444 int
445 interp_quiet_p (struct interp *interp)
446 {
447 struct ui_interp_info *ui_interp = get_current_interp_info ();
448
449 if (interp != NULL)
450 return interp->quiet_p;
451 else
452 return ui_interp->current_interpreter->quiet_p;
453 }
454
455 static int
456 interp_set_quiet (struct interp *interp, int quiet)
457 {
458 int old_val = interp->quiet_p;
459
460 interp->quiet_p = quiet;
461 return old_val;
462 }
463
464 /* interp_exec - This executes COMMAND_STR in the current
465 interpreter. */
466
467 struct gdb_exception
468 interp_exec (struct interp *interp, const char *command_str)
469 {
470 struct ui_interp_info *ui_interp = get_current_interp_info ();
471
472 struct gdb_exception ex;
473 struct interp *save_command_interp;
474
475 gdb_assert (interp->procs->exec_proc != NULL);
476
477 /* See `command_interp' for why we do this. */
478 save_command_interp = ui_interp->command_interpreter;
479 ui_interp->command_interpreter = interp;
480
481 ex = interp->procs->exec_proc (interp->data, command_str);
482
483 ui_interp->command_interpreter = save_command_interp;
484
485 return ex;
486 }
487
488 /* A convenience routine that nulls out all the common command hooks.
489 Use it when removing your interpreter in its suspend proc. */
490 void
491 clear_interpreter_hooks (void)
492 {
493 deprecated_print_frame_info_listing_hook = 0;
494 /*print_frame_more_info_hook = 0; */
495 deprecated_query_hook = 0;
496 deprecated_warning_hook = 0;
497 deprecated_interactive_hook = 0;
498 deprecated_readline_begin_hook = 0;
499 deprecated_readline_hook = 0;
500 deprecated_readline_end_hook = 0;
501 deprecated_context_hook = 0;
502 deprecated_target_wait_hook = 0;
503 deprecated_call_command_hook = 0;
504 deprecated_error_begin_hook = 0;
505 }
506
507 static void
508 interpreter_exec_cmd (char *args, int from_tty)
509 {
510 struct ui_interp_info *ui_interp = get_current_interp_info ();
511 struct interp *old_interp, *interp_to_use;
512 char **prules = NULL;
513 char **trule = NULL;
514 unsigned int nrules;
515 unsigned int i;
516 int old_quiet, use_quiet;
517 struct cleanup *cleanup;
518
519 if (args == NULL)
520 error_no_arg (_("interpreter-exec command"));
521
522 prules = gdb_buildargv (args);
523 cleanup = make_cleanup_freeargv (prules);
524
525 nrules = 0;
526 for (trule = prules; *trule != NULL; trule++)
527 nrules++;
528
529 if (nrules < 2)
530 error (_("usage: interpreter-exec <interpreter> [ <command> ... ]"));
531
532 old_interp = ui_interp->current_interpreter;
533
534 interp_to_use = interp_lookup (current_ui, prules[0]);
535 if (interp_to_use == NULL)
536 error (_("Could not find interpreter \"%s\"."), prules[0]);
537
538 /* Temporarily set interpreters quiet. */
539 old_quiet = interp_set_quiet (old_interp, 1);
540 use_quiet = interp_set_quiet (interp_to_use, 1);
541
542 if (!interp_set (interp_to_use, 0))
543 error (_("Could not switch to interpreter \"%s\"."), prules[0]);
544
545 for (i = 1; i < nrules; i++)
546 {
547 struct gdb_exception e = interp_exec (interp_to_use, prules[i]);
548
549 if (e.reason < 0)
550 {
551 interp_set (old_interp, 0);
552 interp_set_quiet (interp_to_use, use_quiet);
553 interp_set_quiet (old_interp, old_quiet);
554 error (_("error in command: \"%s\"."), prules[i]);
555 }
556 }
557
558 interp_set (old_interp, 0);
559 interp_set_quiet (interp_to_use, use_quiet);
560 interp_set_quiet (old_interp, old_quiet);
561
562 do_cleanups (cleanup);
563 }
564
565 /* See interps.h. */
566
567 VEC (char_ptr) *
568 interpreter_completer (struct cmd_list_element *ignore,
569 const char *text, const char *word)
570 {
571 struct interp_factory *interp;
572 int textlen;
573 VEC (char_ptr) *matches = NULL;
574 int ix;
575
576 textlen = strlen (text);
577 for (ix = 0;
578 VEC_iterate (interp_factory_p, interpreter_factories, ix, interp);
579 ++ix)
580 {
581 if (strncmp (interp->name, text, textlen) == 0)
582 {
583 char *match;
584
585 match = (char *) xmalloc (strlen (word) + strlen (interp->name) + 1);
586 if (word == text)
587 strcpy (match, interp->name);
588 else if (word > text)
589 {
590 /* Return some portion of interp->name. */
591 strcpy (match, interp->name + (word - text));
592 }
593 else
594 {
595 /* Return some of text plus interp->name. */
596 strncpy (match, word, text - word);
597 match[text - word] = '\0';
598 strcat (match, interp->name);
599 }
600 VEC_safe_push (char_ptr, matches, match);
601 }
602 }
603
604 return matches;
605 }
606
607 struct interp *
608 top_level_interpreter (void)
609 {
610 struct ui_interp_info *ui_interp = get_current_interp_info ();
611
612 return ui_interp->top_level_interpreter;
613 }
614
615 void *
616 top_level_interpreter_data (void)
617 {
618 struct interp *interp;
619
620 interp = top_level_interpreter ();
621 gdb_assert (interp != NULL);
622 return interp->data;
623 }
624
625 /* See interps.h. */
626
627 struct interp *
628 current_interpreter (void)
629 {
630 struct ui_interp_info *ui_interp = get_interp_info (current_ui);
631
632 return ui_interp->current_interpreter;
633 }
634
635 /* This just adds the "interpreter-exec" command. */
636 void
637 _initialize_interpreter (void)
638 {
639 struct cmd_list_element *c;
640
641 c = add_cmd ("interpreter-exec", class_support,
642 interpreter_exec_cmd, _("\
643 Execute a command in an interpreter. It takes two arguments:\n\
644 The first argument is the name of the interpreter to use.\n\
645 The second argument is the command to execute.\n"), &cmdlist);
646 set_cmd_completer (c, interpreter_completer);
647 }
This page took 0.043573 seconds and 5 git commands to generate.