GDB (xrefs)

/tmp/gdb-8.1/gdb/interps.c
Go to the documentation of this file.
1 /* Manages interpreters for GDB, the GNU debugger.
2
3 Copyright (C) 2000-2018 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 interp_factory (const char *name_, interp_factory_func func_)
95 : name (name_), func (func_)
96 {}
97
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
105 /* The registered interpreter factories. */
106 static std::vector<interp_factory> interpreter_factories;
107
108 /* See interps.h. */
109
110 void
111 interp_factory_register (const char *name, interp_factory_func func)
112 {
113 /* Assert that no factory for NAME is already registered. */
114 for (const interp_factory &f : interpreter_factories)
115 if (strcmp (f.name, name) == 0)
116 {
117 internal_error (__FILE__, __LINE__,
118 _("interpreter factory already registered: \"%s\"\n"),
119 name);
120 }
121
122 interpreter_factories.emplace_back (name, func);
123 }
124
125 /* Add interpreter INTERP to the gdb interpreter list. The
126 interpreter must not have previously been added. */
127 void
128 interp_add (struct ui *ui, struct interp *interp)
129 {
130 struct ui_interp_info *ui_interp = get_interp_info (ui);
131
132 gdb_assert (interp_lookup_existing (ui, interp->name) == NULL);
133
134 interp->next = ui_interp->interp_list;
135 ui_interp->interp_list = interp;
136 }
137
138 /* This sets the current interpreter to be INTERP. If INTERP has not
139 been initialized, then this will also run the init method.
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. */
148
149 static void
150 interp_set (struct interp *interp, bool top_level)
151 {
152 struct ui_interp_info *ui_interp = get_current_interp_info ();
153 struct interp *old_interp = ui_interp->current_interpreter;
154
155 /* If we already have an interpreter, then trying to
156 set top level interpreter is kinda pointless. */
157 gdb_assert (!top_level || !ui_interp->current_interpreter);
158 gdb_assert (!top_level || !ui_interp->top_level_interpreter);
159
160 if (old_interp != NULL)
161 {
162 current_uiout->flush ();
163 old_interp->suspend ();
164 }
165
166 ui_interp->current_interpreter = interp;
167 if (top_level)
168 ui_interp->top_level_interpreter = interp;
169
170 /* We use interpreter_p for the "set interpreter" variable, so we need
171 to make sure we have a malloc'ed copy for the set command to free. */
172 if (interpreter_p != NULL
173 && strcmp (interp->name, interpreter_p) != 0)
174 {
175 xfree (interpreter_p);
176
177 interpreter_p = xstrdup (interp->name);
178 }
179
180 /* Run the init proc. */
181 if (!interp->inited)
182 {
183 interp->init (top_level);
184 interp->inited = true;
185 }
186
187 /* Do this only after the interpreter is initialized. */
188 current_uiout = interp->interp_ui_out ();
189
190 /* Clear out any installed interpreter hooks/event handlers. */
191 clear_interpreter_hooks ();
192
193 interp->resume ();
194 }
195
196 /* Look up the interpreter for NAME. If no such interpreter exists,
197 return NULL, otherwise return a pointer to the interpreter. */
198
199 static struct interp *
200 interp_lookup_existing (struct ui *ui, const char *name)
201 {
202 struct ui_interp_info *ui_interp = get_interp_info (ui);
203 struct interp *interp;
204
205 for (interp = ui_interp->interp_list;
206 interp != NULL;
207 interp = interp->next)
208 {
209 if (strcmp (interp->name, name) == 0)
210 return interp;
211 }
212
213 return NULL;
214 }
215
216 /* See interps.h. */
217
218 struct interp *
219 interp_lookup (struct ui *ui, const char *name)
220 {
221 if (name == NULL || strlen (name) == 0)
222 return NULL;
223
224 /* Only create each interpreter once per top level. */
225 struct interp *interp = interp_lookup_existing (ui, name);
226 if (interp != NULL)
227 return interp;
228
229 for (const interp_factory &factory : interpreter_factories)
230 if (strcmp (factory.name, name) == 0)
231 {
232 interp = factory.func (name);
233 interp_add (ui, interp);
234 return interp;
235 }
236
237 return NULL;
238 }
239
240 /* See interps.h. */
241
242 void
243 set_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. */
251 interp_set (interp, true);
252 }
253
254 /* Returns the current interpreter. */
255
256 struct ui_out *
257 interp_ui_out (struct interp *interp)
258 {
259 struct ui_interp_info *ui_interp = get_current_interp_info ();
260
261 if (interp == NULL)
262 interp = ui_interp->current_interpreter;
263 return interp->interp_ui_out ();
264 }
265
266 void
267 current_interp_set_logging (ui_file_up logfile,
268 bool logging_redirect)
269 {
270 struct ui_interp_info *ui_interp = get_current_interp_info ();
271 struct interp *interp = ui_interp->current_interpreter;
272
273 interp->set_logging (std::move (logfile), logging_redirect);
274 }
275
276 /* Temporarily overrides the current interpreter. */
277 struct interp *
278 scoped_restore_interp::set_interp (const char *name)
279 {
280 struct ui_interp_info *ui_interp = get_current_interp_info ();
281 struct interp *interp = interp_lookup (current_ui, name);
282 struct interp *old_interp = ui_interp->current_interpreter;
283
284 if (interp)
285 ui_interp->current_interpreter = interp;
286 return old_interp;
287 }
288
289 /* Returns the interpreter's name. */
290
291 const char *
292 interp_name (struct interp *interp)
293 {
294 return interp->name;
295 }
296
297 /* Returns true if the current interp is the passed in name. */
298 int
299 current_interp_named_p (const char *interp_name)
300 {
301 struct ui_interp_info *ui_interp = get_current_interp_info ();
302 struct interp *interp = ui_interp->current_interpreter;
303
304 if (interp != NULL)
305 return (strcmp (interp->name, interp_name) == 0);
306
307 return 0;
308 }
309
310 /* The interpreter that was active when a command was executed.
311 Normally that'd always be CURRENT_INTERPRETER, except that MI's
312 -interpreter-exec command doesn't actually flip the current
313 interpreter when running its sub-command. The
314 `command_interpreter' global tracks when interp_exec is called
315 (IOW, when -interpreter-exec is called). If that is set, it is
316 INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
317 INTERP "CMD". Otherwise, interp_exec isn't active, and so the
318 interpreter running the command is the current interpreter. */
319
320 struct interp *
321 command_interp (void)
322 {
323 struct ui_interp_info *ui_interp = get_current_interp_info ();
324
325 if (ui_interp->command_interpreter != NULL)
326 return ui_interp->command_interpreter;
327 else
328 return ui_interp->current_interpreter;
329 }
330
331 /* See interps.h. */
332
333 void
334 interp_pre_command_loop (struct interp *interp)
335 {
336 gdb_assert (interp != NULL);
337
338 interp->pre_command_loop ();
339 }
340
341 /* See interp.h */
342
343 int
344 interp_supports_command_editing (struct interp *interp)
345 {
346 return interp->supports_command_editing ();
347 }
348
349 /* interp_exec - This executes COMMAND_STR in the current
350 interpreter. */
351
352 struct gdb_exception
353 interp_exec (struct interp *interp, const char *command_str)
354 {
355 struct ui_interp_info *ui_interp = get_current_interp_info ();
356
357 struct gdb_exception ex;
358 struct interp *save_command_interp;
359
360 /* See `command_interp' for why we do this. */
361 save_command_interp = ui_interp->command_interpreter;
362 ui_interp->command_interpreter = interp;
363
364 ex = interp->exec (command_str);
365
366 ui_interp->command_interpreter = save_command_interp;
367
368 return ex;
369 }
370
371 /* A convenience routine that nulls out all the common command hooks.
372 Use it when removing your interpreter in its suspend proc. */
373 void
374 clear_interpreter_hooks (void)
375 {
376 deprecated_print_frame_info_listing_hook = 0;
377 /*print_frame_more_info_hook = 0; */
378 deprecated_query_hook = 0;
379 deprecated_warning_hook = 0;
380 deprecated_interactive_hook = 0;
381 deprecated_readline_begin_hook = 0;
382 deprecated_readline_hook = 0;
383 deprecated_readline_end_hook = 0;
384 deprecated_context_hook = 0;
385 deprecated_target_wait_hook = 0;
386 deprecated_call_command_hook = 0;
387 deprecated_error_begin_hook = 0;
388 }
389
390 static void
391 interpreter_exec_cmd (const char *args, int from_tty)
392 {
393 struct ui_interp_info *ui_interp = get_current_interp_info ();
394 struct interp *old_interp, *interp_to_use;
395 unsigned int nrules;
396 unsigned int i;
397
398 if (args == NULL)
399 error_no_arg (_("interpreter-exec command"));
400
401 gdb_argv prules (args);
402 nrules = prules.count ();
403
404 if (nrules < 2)
405 error (_("usage: interpreter-exec <interpreter> [ <command> ... ]"));
406
407 old_interp = ui_interp->current_interpreter;
408
409 interp_to_use = interp_lookup (current_ui, prules[0]);
410 if (interp_to_use == NULL)
411 error (_("Could not find interpreter \"%s\"."), prules[0]);
412
413 interp_set (interp_to_use, false);
414
415 for (i = 1; i < nrules; i++)
416 {
417 struct gdb_exception e = interp_exec (interp_to_use, prules[i]);
418
419 if (e.reason < 0)
420 {
421 interp_set (old_interp, 0);
422 error (_("error in command: \"%s\"."), prules[i]);
423 }
424 }
425
426 interp_set (old_interp, 0);
427 }
428
429 /* See interps.h. */
430
431 void
432 interpreter_completer (struct cmd_list_element *ignore,
433 completion_tracker &tracker,
434 const char *text, const char *word)
435 {
436 int textlen = strlen (text);
437
438 for (const interp_factory &interp : interpreter_factories)
439 {
440 if (strncmp (interp.name, text, textlen) == 0)
441 {
442 tracker.add_completion
443 (make_completion_match_str (interp.name, text, word));
444 }
445 }
446 }
447
448 struct interp *
449 top_level_interpreter (void)
450 {
451 struct ui_interp_info *ui_interp = get_current_interp_info ();
452
453 return ui_interp->top_level_interpreter;
454 }
455
456 /* See interps.h. */
457
458 struct interp *
459 current_interpreter (void)
460 {
461 struct ui_interp_info *ui_interp = get_interp_info (current_ui);
462
463 return ui_interp->current_interpreter;
464 }
465
466 /* This just adds the "interpreter-exec" command. */
467 void
468 _initialize_interpreter (void)
469 {
470 struct cmd_list_element *c;
471
472 c = add_cmd ("interpreter-exec", class_support,
473 interpreter_exec_cmd, _("\
474 Execute a command in an interpreter. It takes two arguments:\n\
475 The first argument is the name of the interpreter to use.\n\
476 The second argument is the command to execute.\n"), &cmdlist);
477 set_cmd_completer (c, interpreter_completer);
478 }
error_no_arg
void error_no_arg(const char *why)
Definition: cli-cmds.c:185
top.h
completer.h
gdb_argv::count
int count() const
Definition: utils.h:194
interp_factory::func
interp_factory_func func
Definition: interps.c:102
current_ui
struct ui * current_ui
Definition: event-top.c:453
interp::init
virtual void init(bool top_level)
Definition: interps.h:47
xfree
void xfree(void *)
ui_file_up
std::unique_ptr< ui_file > ui_file_up
Definition: ui-file.h:76
class_support
Definition: command.h:42
cmd_list_element
Definition: cli-decode.h:46
func
void(* func)(char *)
Definition: remote-fileio.c:1104
deprecated_query_hook
int(* deprecated_query_hook)(const char *, va_list) ATTRIBUTE_FPTR_PRINTF(1
Definition: top.c:196
interp
Definition: interps.h:41
internal_error
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
interp_ui_out
struct ui_out * interp_ui_out(struct interp *interp)
Definition: interps.c:257
interp::resume
virtual void resume()=0
interp::interp
interp(const char *name)
Definition: interps.c:80
add_cmd
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition: cli-decode.c:262
interpreter_completer
void interpreter_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition: interps.c:432
deprecated_context_hook
void(* deprecated_context_hook)(int)
Definition: top.c:244
deprecated_print_frame_info_listing_hook
void(* deprecated_print_frame_info_listing_hook)(struct symtab *s, int line, int stopline, int noerror)
Definition: top.c:190
_initialize_interpreter
void _initialize_interpreter(void)
Definition: interps.c:468
cmdlist
struct cmd_list_element * cmdlist
Definition: cli-cmds.c:79
_
#define _(String)
Definition: gdb_locale.h:35
interp_factory
Definition: interps.c:92
defs.h
deprecated_target_wait_hook
ptid_t(* deprecated_target_wait_hook)(ptid_t ptid, struct target_waitstatus *status, int options)
Definition: top.c:232
interp::~interp
virtual ~interp()=0
Definition: interps.c:86
interpreter_p
char * interpreter_p
Definition: main.c:53
event-loop.h
get_current_interp_info
static struct ui_interp_info * get_current_interp_info(void)
Definition: interps.c:70
gdb_argv
Definition: utils.h:128
name
const char *const name
Definition: aarch64-tdep.c:76
current_interpreter
struct interp * current_interpreter(void)
Definition: interps.c:459
interp_pre_command_loop
void interp_pre_command_loop(struct interp *interp)
Definition: interps.c:334
interpreter_factories
static std::vector< interp_factory > interpreter_factories
Definition: interps.c:106
logging_redirect
static int logging_redirect
Definition: cli-logging.c:64
scoped_restore_interp::set_interp
struct interp * set_interp(const char *name)
Definition: interps.c:278
interp_supports_command_editing
int interp_supports_command_editing(struct interp *interp)
Definition: interps.c:344
deprecated_warning_hook
int(*) void(* deprecated_warning_hook)(const char *, va_list) ATTRIBUTE_FPTR_PRINTF(1
Definition: top.c:200
set_cmd_completer
void set_cmd_completer(struct cmd_list_element *cmd, completer_ftype *completer)
Definition: cli-decode.c:160
interp::next
struct interp * next
Definition: interps.h:82
interp_factory_register
void interp_factory_register(const char *name, interp_factory_func func)
Definition: interps.c:111
clear_interpreter_hooks
void clear_interpreter_hooks(void)
Definition: interps.c:374
interp::interp_ui_out
virtual ui_out * interp_ui_out()=0
current_uiout
#define current_uiout
Definition: ui-out.h:39
interp_set
static void interp_set(struct interp *interp, bool top_level)
Definition: interps.c:150
interp::set_logging
virtual void set_logging(ui_file_up logfile, bool logging_redirect)=0
ui
Definition: top.h:56
interp::name
const char * name
Definition: interps.h:78
ui_interp_info::top_level_interpreter
struct interp * top_level_interpreter
Definition: interps.c:49
interp::pre_command_loop
virtual void pre_command_loop()
Definition: interps.h:68
ui_interp_info
Definition: interps.c:44
interps.h
interpreter_exec_cmd
static void interpreter_exec_cmd(const char *args, int from_tty)
Definition: interps.c:391
deprecated_error_begin_hook
void(* deprecated_error_begin_hook)(void)
Definition: utils.c:84
deprecated_readline_begin_hook
void(* deprecated_readline_begin_hook)(const char *,...) ATTRIBUTE_FPTR_PRINTF_1
Definition: top.c:214
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
interp::exec
virtual gdb_exception exec(const char *command)=0
deprecated_interactive_hook
int(*) void(*) void(* deprecated_interactive_hook)(void)
Definition: top.c:227
ui_out
Definition: ui-out.h:77
interp_name
const char * interp_name(struct interp *interp)
Definition: interps.c:292
interp_add
void interp_add(struct ui *ui, struct interp *interp)
Definition: interps.c:128
ui_interp_info::command_interpreter
struct interp * command_interpreter
Definition: interps.c:53
event-top.h
interp_lookup_existing
static struct interp * interp_lookup_existing(struct ui *ui, const char *name)
Definition: interps.c:200
interp_exec
struct gdb_exception interp_exec(struct interp *interp, const char *command_str)
Definition: interps.c:353
deprecated_readline_end_hook
void(* deprecated_readline_end_hook)(void)
Definition: top.c:216
ui::interp_info
struct ui_interp_info * interp_info
Definition: top.h:92
interp::suspend
virtual void suspend()=0
XCNEW
#define XCNEW(T)
Definition: poison.h:121
make_completion_match_str
gdb::unique_xmalloc_ptr< char > make_completion_match_str(const char *match_name, const char *text, const char *word)
Definition: completer.c:1603
ui_interp_info::current_interpreter
struct interp * current_interpreter
Definition: interps.c:48
get_interp_info
static struct ui_interp_info * get_interp_info(struct ui *ui)
Definition: interps.c:59
set_top_level_interpreter
void set_top_level_interpreter(const char *name)
Definition: interps.c:243
command_interp
struct interp * command_interp(void)
Definition: interps.c:321
completion_tracker::add_completion
void add_completion(gdb::unique_xmalloc_ptr< char > name, completion_match_for_lcd *match_for_lcd=NULL, const char *text=NULL, const char *word=NULL)
Definition: completer.c:1550
interp::supports_command_editing
virtual bool supports_command_editing()
Definition: interps.h:74
gdb_exception
Definition: common-exceptions.h:111
ignore
static int ignore(struct target_ops *ops, struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: corelow.c:879
top_level_interpreter
struct interp * top_level_interpreter(void)
Definition: interps.c:449
completion_tracker
Definition: completer.h:310
interp::inited
bool inited
Definition: interps.h:85
interp_factory::interp_factory
interp_factory(const char *name_, interp_factory_func func_)
Definition: interps.c:94
current_interp_named_p
int current_interp_named_p(const char *interp_name)
Definition: interps.c:299
gdbcmd.h
ui_interp_info::interp_list
struct interp * interp_list
Definition: interps.c:47
error
void error(const char *fmt,...)
Definition: errors.c:38
interp_factory_func
struct interp *(* interp_factory_func)(const char *name)
Definition: interps.h:29
current_interp_set_logging
void current_interp_set_logging(ui_file_up logfile, bool logging_redirect)
Definition: interps.c:267
interp_factory::name
const char * name
Definition: interps.c:99
gdb_exception::reason
enum return_reason reason
Definition: common-exceptions.h:113
ui-out.h
continuations.h
deprecated_call_command_hook
void(* deprecated_call_command_hook)(struct cmd_list_element *c, const char *cmd, int from_tty)
Definition: top.c:239
interp_lookup
struct interp * interp_lookup(struct ui *ui, const char *name)
Definition: interps.c:219
deprecated_readline_hook
char *(* deprecated_readline_hook)(const char *)
Definition: top.c:215