GDB (xrefs)
Loading...
Searching...
No Matches
interps.c
Go to the documentation of this file.
1/* Manages interpreters for GDB, the GNU debugger.
2
3 Copyright (C) 2000-2023 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 "gdbsupport/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 "main.h"
41#include "gdbsupport/buildargv.h"
42
43/* Each UI has its own independent set of interpreters. */
44
46{
47 /* Each top level has its own independent set of interpreters. */
51
52 /* The interpreter that is active while `interp_exec' is active, NULL
53 at all other times. */
55};
56
57/* Get UI's ui_interp_info object. Never returns NULL. */
58
59static struct ui_interp_info *
61{
62 if (ui->interp_info == NULL)
63 ui->interp_info = XCNEW (struct ui_interp_info);
64 return ui->interp_info;
65}
66
67/* Get the current UI's ui_interp_info object. Never returns
68 NULL. */
69
70static struct ui_interp_info *
72{
74}
75
76/* The magic initialization routine for this module. */
77
78static struct interp *interp_lookup_existing (struct ui *ui,
79 const char *name);
80
81interp::interp (const char *name)
82 : m_name (make_unique_xstrdup (name))
83{
84}
85
87{
88}
89
90/* An interpreter factory. Maps an interpreter name to the factory
91 function that instantiates an interpreter by that name. */
92
94{
95 interp_factory (const char *name_, interp_factory_func func_)
96 : name (name_), func (func_)
97 {}
98
99 /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */
100 const char *name;
101
102 /* The function that creates the interpreter. */
104};
105
106/* The registered interpreter factories. */
107static std::vector<interp_factory> interpreter_factories;
108
109/* See interps.h. */
110
111void
113{
114 /* Assert that no factory for NAME is already registered. */
116 if (strcmp (f.name, name) == 0)
117 {
118 internal_error (_("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. */
127static void
128interp_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
149static void
150interp_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 if (interpreter_p != interp->name ())
172
173 bool warn_about_mi1 = false;
174
175 /* Run the init proc. */
176 if (!interp->inited)
177 {
178 interp->init (top_level);
179 interp->inited = true;
180
181 if (streq (interp->name (), "mi1"))
182 warn_about_mi1 = true;
183 }
184
185 /* Do this only after the interpreter is initialized. */
187
188 /* Clear out any installed interpreter hooks/event handlers. */
190
191 interp->resume ();
192
193 if (warn_about_mi1)
194 warning (_("MI version 1 is deprecated in GDB 13 and "
195 "will be removed in GDB 14. Please upgrade "
196 "to a newer version of MI."));
197}
198
199/* Look up the interpreter for NAME. If no such interpreter exists,
200 return NULL, otherwise return a pointer to the interpreter. */
201
202static struct interp *
203interp_lookup_existing (struct ui *ui, const char *name)
204{
205 struct ui_interp_info *ui_interp = get_interp_info (ui);
206 struct interp *interp;
207
208 for (interp = ui_interp->interp_list;
209 interp != NULL;
210 interp = interp->next)
211 {
212 if (strcmp (interp->name (), name) == 0)
213 return interp;
214 }
215
216 return NULL;
217}
218
219/* See interps.h. */
220
221struct interp *
222interp_lookup (struct ui *ui, const char *name)
223{
224 if (name == NULL || strlen (name) == 0)
225 return NULL;
226
227 /* Only create each interpreter once per top level. */
229 if (interp != NULL)
230 return interp;
231
232 for (const interp_factory &factory : interpreter_factories)
233 if (strcmp (factory.name, name) == 0)
234 {
235 interp = factory.func (name);
237 return interp;
238 }
239
240 return NULL;
241}
242
243/* See interps.h. */
244
245void
247{
248 /* Find it. */
250
251 if (interp == NULL)
252 error (_("Interpreter `%s' unrecognized"), name);
253 /* Install it. */
254 interp_set (interp, true);
255}
256
257void
259 bool debug_redirect)
260{
261 struct ui_interp_info *ui_interp = get_current_interp_info ();
262 struct interp *interp = ui_interp->current_interpreter;
263
264 interp->set_logging (std::move (logfile), logging_redirect, debug_redirect);
265}
266
267/* Temporarily overrides the current interpreter. */
268struct interp *
270{
271 struct ui_interp_info *ui_interp = get_current_interp_info ();
273 struct interp *old_interp = ui_interp->current_interpreter;
274
275 if (interp)
276 ui_interp->current_interpreter = interp;
277 return old_interp;
278}
279
280/* Returns true if the current interp is the passed in name. */
281int
282current_interp_named_p (const char *interp_name)
283{
284 struct ui_interp_info *ui_interp = get_current_interp_info ();
285 struct interp *interp = ui_interp->current_interpreter;
286
287 if (interp != NULL)
288 return (strcmp (interp->name (), interp_name) == 0);
289
290 return 0;
291}
292
293/* The interpreter that was active when a command was executed.
294 Normally that'd always be CURRENT_INTERPRETER, except that MI's
295 -interpreter-exec command doesn't actually flip the current
296 interpreter when running its sub-command. The
297 `command_interpreter' global tracks when interp_exec is called
298 (IOW, when -interpreter-exec is called). If that is set, it is
299 INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
300 INTERP "CMD". Otherwise, interp_exec isn't active, and so the
301 interpreter running the command is the current interpreter. */
302
303struct interp *
305{
306 struct ui_interp_info *ui_interp = get_current_interp_info ();
307
308 if (ui_interp->command_interpreter != NULL)
309 return ui_interp->command_interpreter;
310 else
311 return ui_interp->current_interpreter;
312}
313
314/* See interps.h. */
315
316void
318{
319 gdb_assert (interp != NULL);
320
322}
323
324/* See interp.h */
325
326int
328{
330}
331
332/* interp_exec - This executes COMMAND_STR in the current
333 interpreter. */
334
335struct gdb_exception
336interp_exec (struct interp *interp, const char *command_str)
337{
338 struct ui_interp_info *ui_interp = get_current_interp_info ();
339
340 /* See `command_interp' for why we do this. */
341 scoped_restore save_command_interp
342 = make_scoped_restore (&ui_interp->command_interpreter, interp);
343
344 return interp->exec (command_str);
345}
346
347/* A convenience routine that nulls out all the common command hooks.
348 Use it when removing your interpreter in its suspend proc. */
349void
351{
353 /*print_frame_more_info_hook = 0; */
362}
363
364static void
365interpreter_exec_cmd (const char *args, int from_tty)
366{
367 struct ui_interp_info *ui_interp = get_current_interp_info ();
368 struct interp *old_interp, *interp_to_use;
369 unsigned int nrules;
370 unsigned int i;
371
372 /* Interpreters may clobber stdout/stderr (e.g. in mi_interp::resume at time
373 of writing), preserve their state here. */
374 scoped_restore save_stdout = make_scoped_restore (&gdb_stdout);
375 scoped_restore save_stderr = make_scoped_restore (&gdb_stderr);
376 scoped_restore save_stdlog = make_scoped_restore (&gdb_stdlog);
377 scoped_restore save_stdtarg = make_scoped_restore (&gdb_stdtarg);
378 scoped_restore save_stdtargerr = make_scoped_restore (&gdb_stdtargerr);
379
380 if (args == NULL)
381 error_no_arg (_("interpreter-exec command"));
382
383 gdb_argv prules (args);
384 nrules = prules.count ();
385
386 if (nrules < 2)
387 error (_("Usage: interpreter-exec INTERPRETER COMMAND..."));
388
389 old_interp = ui_interp->current_interpreter;
390
391 interp_to_use = interp_lookup (current_ui, prules[0]);
392 if (interp_to_use == NULL)
393 error (_("Could not find interpreter \"%s\"."), prules[0]);
394
395 interp_set (interp_to_use, false);
396
397 for (i = 1; i < nrules; i++)
398 {
399 struct gdb_exception e = interp_exec (interp_to_use, prules[i]);
400
401 if (e.reason < 0)
402 {
403 interp_set (old_interp, 0);
404 error (_("error in command: \"%s\"."), prules[i]);
405 }
406 }
407
408 interp_set (old_interp, 0);
409}
410
411/* See interps.h. */
412
413void
415 completion_tracker &tracker,
416 const char *text, const char *word)
417{
418 int textlen = strlen (text);
419
421 {
422 if (strncmp (interp.name, text, textlen) == 0)
423 {
424 tracker.add_completion
425 (make_completion_match_str (interp.name, text, word));
426 }
427 }
428}
429
430struct interp *
432{
433 struct ui_interp_info *ui_interp = get_current_interp_info ();
434
435 return ui_interp->top_level_interpreter;
436}
437
438/* See interps.h. */
439
440struct interp *
442{
443 struct ui_interp_info *ui_interp = get_interp_info (current_ui);
444
445 return ui_interp->current_interpreter;
446}
447
448/* This just adds the "interpreter-exec" command. */
450void
452{
453 struct cmd_list_element *c;
454
455 c = add_cmd ("interpreter-exec", class_support,
457Execute a command in an interpreter.\n\
458Usage: interpreter-exec INTERPRETER COMMAND...\n\
459The first argument is the name of the interpreter to use.\n\
460The following arguments are the commands to execute.\n\
461A command can have arguments, separated by spaces.\n\
462These spaces must be escaped using \\ or the command\n\
463and its arguments must be enclosed in double quotes."), &cmdlist);
465}
const char *const name
void f()
Definition 1.cc:36
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:1579
gdb::unique_xmalloc_ptr< char > m_name
Definition interps.h:86
virtual void suspend()=0
virtual ~interp()=0
Definition interps.c:86
virtual bool supports_command_editing()
Definition interps.h:76
virtual void resume()=0
virtual void set_logging(ui_file_up logfile, bool logging_redirect, bool debug_redirect)=0
virtual ui_out * interp_ui_out()=0
virtual void pre_command_loop()
Definition interps.h:70
virtual gdb_exception exec(const char *command)=0
bool inited
Definition interps.h:94
const char * name() const
Definition interps.h:79
struct interp * next
Definition interps.h:91
virtual void init(bool top_level)
Definition interps.h:48
interp(const char *name)
Definition interps.c:81
struct interp * set_interp(const char *name)
Definition interps.c:269
void error_no_arg(const char *why)
Definition cli-cmds.c:204
struct cmd_list_element * cmdlist
Definition cli-cmds.c:85
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:233
void set_cmd_completer(struct cmd_list_element *cmd, completer_ftype *completer)
Definition cli-decode.c:117
static bool debug_redirect
Definition cli-logging.c:67
static bool logging_redirect
Definition cli-logging.c:66
@ class_support
Definition command.h:58
gdb::unique_xmalloc_ptr< char > make_completion_match_str(const char *match_name, const char *text, const char *word)
Definition completer.c:1646
void(* deprecated_readline_end_hook)(void)
Definition top.c:239
int(*) void(*) void(* deprecated_readline_begin_hook)(const char *,...) ATTRIBUTE_FPTR_PRINTF_1
Definition top.c:237
void(* deprecated_print_frame_info_listing_hook)(struct symtab *s, int line, int stopline, int noerror)
Definition top.c:213
int(* deprecated_query_hook)(const char *, va_list) ATTRIBUTE_FPTR_PRINTF(1
Definition top.c:219
char *(* deprecated_readline_hook)(const char *)
Definition top.c:238
void(* deprecated_call_command_hook)(struct cmd_list_element *c, const char *cmd, int from_tty)
Definition top.c:250
int(*) void(* deprecated_warning_hook)(const char *, va_list) ATTRIBUTE_FPTR_PRINTF(1
Definition top.c:223
void(* deprecated_context_hook)(int)
Definition top.c:255
struct ui * current_ui
Definition event-top.c:483
struct interp * interp_lookup(struct ui *ui, const char *name)
Definition interps.c:222
static struct interp * interp_lookup_existing(struct ui *ui, const char *name)
Definition interps.c:203
void interp_pre_command_loop(struct interp *interp)
Definition interps.c:317
void clear_interpreter_hooks(void)
Definition interps.c:350
static struct ui_interp_info * get_interp_info(struct ui *ui)
Definition interps.c:60
int current_interp_named_p(const char *interp_name)
Definition interps.c:282
struct interp * current_interpreter(void)
Definition interps.c:441
struct interp * top_level_interpreter(void)
Definition interps.c:431
static void interpreter_exec_cmd(const char *args, int from_tty)
Definition interps.c:365
void set_top_level_interpreter(const char *name)
Definition interps.c:246
void _initialize_interpreter()
Definition interps.c:451
struct interp * command_interp(void)
Definition interps.c:304
void interp_factory_register(const char *name, interp_factory_func func)
Definition interps.c:112
static void interp_add(struct ui *ui, struct interp *interp)
Definition interps.c:128
int interp_supports_command_editing(struct interp *interp)
Definition interps.c:327
static struct ui_interp_info * get_current_interp_info(void)
Definition interps.c:71
void interpreter_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition interps.c:414
void current_interp_set_logging(ui_file_up logfile, bool logging_redirect, bool debug_redirect)
Definition interps.c:258
static std::vector< interp_factory > interpreter_factories
Definition interps.c:107
static void interp_set(struct interp *interp, bool top_level)
Definition interps.c:150
struct gdb_exception interp_exec(struct interp *interp, const char *command_str)
Definition interps.c:336
struct interp * interp_lookup(struct ui *ui, const char *name)
Definition interps.c:222
struct interp *(* interp_factory_func)(const char *name)
Definition interps.h:30
std::string interpreter_p
Definition main.c:60
struct ui_file * gdb_stdtarg
Definition main.c:79
struct ui_file * gdb_stdtargerr
Definition main.c:80
void(* func)(remote_target *remote, char *)
interp_factory_func func
Definition interps.c:103
interp_factory(const char *name_, interp_factory_func func_)
Definition interps.c:95
const char * name
Definition interps.c:100
struct interp * command_interpreter
Definition interps.c:54
struct interp * interp_list
Definition interps.c:48
struct interp * top_level_interpreter
Definition interps.c:50
struct interp * current_interpreter
Definition interps.c:49
Definition top.h:56
struct ui_interp_info * interp_info
Definition top.h:91
std::unique_ptr< ui_file > ui_file_up
Definition ui-file.h:148
#define current_uiout
Definition ui-out.h:40
void(* deprecated_error_begin_hook)(void)
Definition utils.c:83
#define gdb_stderr
Definition utils.h:193
#define gdb_stdlog
Definition utils.h:196
#define gdb_stdout
Definition utils.h:188