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