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