1 /* Manages interpreters for GDB, the GNU debugger.
3 Copyright (C) 2000-2023 Free Software Foundation, Inc.
5 Written by Jim Ingham <jingham@apple.com> of Apple Computer, Inc.
7 This file is part of GDB.
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.
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.
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/>. */
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:
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. */
35 #include "gdbsupport/event-loop.h"
36 #include "event-top.h"
38 #include "completer.h"
41 #include "gdbsupport/buildargv.h"
42 #include "gdbsupport/scope-exit.h"
44 /* The magic initialization routine for this module. */
46 static struct interp
*interp_lookup_existing (struct ui
*ui
,
49 interp::interp (const char *name
)
54 interp::~interp () = default;
56 /* An interpreter factory. Maps an interpreter name to the factory
57 function that instantiates an interpreter by that name. */
61 interp_factory (const char *name_
, interp_factory_func func_
)
62 : name (name_
), func (func_
)
65 /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */
68 /* The function that creates the interpreter. */
69 interp_factory_func func
;
72 /* The registered interpreter factories. */
73 static std::vector
<interp_factory
> interpreter_factories
;
78 interp_factory_register (const char *name
, interp_factory_func func
)
80 /* Assert that no factory for NAME is already registered. */
81 for (const interp_factory
&f
: interpreter_factories
)
82 if (strcmp (f
.name
, name
) == 0)
84 internal_error (_("interpreter factory already registered: \"%s\"\n"),
88 interpreter_factories
.emplace_back (name
, func
);
91 /* Add interpreter INTERP to the gdb interpreter list. The
92 interpreter must not have previously been added. */
94 interp_add (struct ui
*ui
, struct interp
*interp
)
96 gdb_assert (interp_lookup_existing (ui
, interp
->name ()) == NULL
);
98 ui
->interp_list
.push_back (*interp
);
101 /* This sets the current interpreter to be INTERP. If INTERP has not
102 been initialized, then this will also run the init method.
104 The TOP_LEVEL parameter tells if this new interpreter is
105 the top-level one. The top-level is what is requested
106 on the command line, and is responsible for reporting general
107 notification about target state changes. For example, if
108 MI is the top-level interpreter, then it will always report
109 events such as target stops and new thread creation, even if they
110 are caused by CLI commands. */
113 interp_set (struct interp
*interp
, bool top_level
)
115 struct interp
*old_interp
= current_ui
->current_interpreter
;
117 /* If we already have an interpreter, then trying to
118 set top level interpreter is kinda pointless. */
119 gdb_assert (!top_level
|| !current_ui
->current_interpreter
);
120 gdb_assert (!top_level
|| !current_ui
->top_level_interpreter
);
122 if (old_interp
!= NULL
)
124 current_uiout
->flush ();
125 old_interp
->suspend ();
128 current_ui
->current_interpreter
= interp
;
130 current_ui
->top_level_interpreter
= interp
;
132 if (interpreter_p
!= interp
->name ())
133 interpreter_p
= interp
->name ();
135 /* Run the init proc. */
138 interp
->init (top_level
);
139 interp
->inited
= true;
142 /* Do this only after the interpreter is initialized. */
143 current_uiout
= interp
->interp_ui_out ();
145 /* Clear out any installed interpreter hooks/event handlers. */
146 clear_interpreter_hooks ();
151 /* Look up the interpreter for NAME. If no such interpreter exists,
152 return NULL, otherwise return a pointer to the interpreter. */
154 static struct interp
*
155 interp_lookup_existing (struct ui
*ui
, const char *name
)
157 for (interp
&interp
: ui
->interp_list
)
158 if (strcmp (interp
.name (), name
) == 0)
167 interp_lookup (struct ui
*ui
, const char *name
)
169 if (name
== NULL
|| strlen (name
) == 0)
172 /* Only create each interpreter once per top level. */
173 struct interp
*interp
= interp_lookup_existing (ui
, name
);
177 for (const interp_factory
&factory
: interpreter_factories
)
178 if (strcmp (factory
.name
, name
) == 0)
180 interp
= factory
.func (factory
.name
);
181 interp_add (ui
, interp
);
191 set_top_level_interpreter (const char *name
)
194 struct interp
*interp
= interp_lookup (current_ui
, name
);
197 error (_("Interpreter `%s' unrecognized"), name
);
199 interp_set (interp
, true);
203 current_interp_set_logging (ui_file_up logfile
, bool logging_redirect
,
206 struct interp
*interp
= current_ui
->current_interpreter
;
208 interp
->set_logging (std::move (logfile
), logging_redirect
, debug_redirect
);
211 /* Temporarily overrides the current interpreter. */
213 scoped_restore_interp::set_interp (const char *name
)
215 struct interp
*interp
= interp_lookup (current_ui
, name
);
216 struct interp
*old_interp
= current_ui
->current_interpreter
;
219 current_ui
->current_interpreter
= interp
;
224 /* Returns true if the current interp is the passed in name. */
226 current_interp_named_p (const char *interp_name
)
228 interp
*interp
= current_ui
->current_interpreter
;
231 return (strcmp (interp
->name (), interp_name
) == 0);
236 /* The interpreter that was active when a command was executed.
237 Normally that'd always be CURRENT_INTERPRETER, except that MI's
238 -interpreter-exec command doesn't actually flip the current
239 interpreter when running its sub-command. The
240 `command_interpreter' global tracks when interp_exec is called
241 (IOW, when -interpreter-exec is called). If that is set, it is
242 INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
243 INTERP "CMD". Otherwise, interp_exec isn't active, and so the
244 interpreter running the command is the current interpreter. */
247 command_interp (void)
249 if (current_ui
->command_interpreter
!= nullptr)
250 return current_ui
->command_interpreter
;
252 return current_ui
->current_interpreter
;
255 /* interp_exec - This executes COMMAND_STR in the current
259 interp_exec (struct interp
*interp
, const char *command_str
)
261 /* See `command_interp' for why we do this. */
262 scoped_restore save_command_interp
263 = make_scoped_restore (¤t_ui
->command_interpreter
, interp
);
265 interp
->exec (command_str
);
268 /* A convenience routine that nulls out all the common command hooks.
269 Use it when removing your interpreter in its suspend proc. */
271 clear_interpreter_hooks (void)
273 deprecated_print_frame_info_listing_hook
= 0;
274 /*print_frame_more_info_hook = 0; */
275 deprecated_query_hook
= 0;
276 deprecated_warning_hook
= 0;
277 deprecated_readline_begin_hook
= 0;
278 deprecated_readline_hook
= 0;
279 deprecated_readline_end_hook
= 0;
280 deprecated_context_hook
= 0;
281 deprecated_call_command_hook
= 0;
282 deprecated_error_begin_hook
= 0;
286 interpreter_exec_cmd (const char *args
, int from_tty
)
288 struct interp
*interp_to_use
;
292 /* Interpreters may clobber stdout/stderr (e.g. in mi_interp::resume at time
293 of writing), preserve their state here. */
294 scoped_restore save_stdout
= make_scoped_restore (&gdb_stdout
);
295 scoped_restore save_stderr
= make_scoped_restore (&gdb_stderr
);
296 scoped_restore save_stdlog
= make_scoped_restore (&gdb_stdlog
);
297 scoped_restore save_stdtarg
= make_scoped_restore (&gdb_stdtarg
);
298 scoped_restore save_stdtargerr
= make_scoped_restore (&gdb_stdtargerr
);
301 error_no_arg (_("interpreter-exec command"));
303 gdb_argv
prules (args
);
304 nrules
= prules
.count ();
307 error (_("Usage: interpreter-exec INTERPRETER COMMAND..."));
309 interp
*old_interp
= current_ui
->current_interpreter
;
311 interp_to_use
= interp_lookup (current_ui
, prules
[0]);
312 if (interp_to_use
== NULL
)
313 error (_("Could not find interpreter \"%s\"."), prules
[0]);
315 interp_set (interp_to_use
, false);
318 interp_set (old_interp
, false);
321 for (i
= 1; i
< nrules
; i
++)
322 interp_exec (interp_to_use
, prules
[i
]);
328 interpreter_completer (struct cmd_list_element
*ignore
,
329 completion_tracker
&tracker
,
330 const char *text
, const char *word
)
332 int textlen
= strlen (text
);
334 for (const interp_factory
&interp
: interpreter_factories
)
336 if (strncmp (interp
.name
, text
, textlen
) == 0)
338 tracker
.add_completion
339 (make_completion_match_str (interp
.name
, text
, word
));
345 top_level_interpreter (void)
347 return current_ui
->top_level_interpreter
;
353 current_interpreter (void)
355 return current_ui
->current_interpreter
;
358 /* Helper interps_notify_* functions. Call METHOD on the top-level interpreter
361 template <typename MethodType
, typename
...Args
>
363 interps_notify (MethodType method
, Args
&&... args
)
365 SWITCH_THRU_ALL_UIS ()
367 interp
*tli
= top_level_interpreter ();
369 (tli
->*method
) (std::forward
<Args
> (args
)...);
376 interps_notify_signal_received (gdb_signal sig
)
378 interps_notify (&interp::on_signal_received
, sig
);
384 interps_notify_signal_exited (gdb_signal sig
)
386 interps_notify (&interp::on_signal_exited
, sig
);
392 interps_notify_no_history ()
394 interps_notify (&interp::on_no_history
);
400 interps_notify_normal_stop (bpstat
*bs
, int print_frame
)
402 interps_notify (&interp::on_normal_stop
, bs
, print_frame
);
408 interps_notify_exited (int status
)
410 interps_notify (&interp::on_exited
, status
);
416 interps_notify_user_selected_context_changed (user_selected_what selection
)
418 interps_notify (&interp::on_user_selected_context_changed
, selection
);
424 interps_notify_new_thread (thread_info
*t
)
426 interps_notify (&interp::on_new_thread
, t
);
432 interps_notify_thread_exited (thread_info
*t
,
433 gdb::optional
<ULONGEST
> exit_code
,
436 interps_notify (&interp::on_thread_exited
, t
, exit_code
, silent
);
442 interps_notify_inferior_added (inferior
*inf
)
444 interps_notify (&interp::on_inferior_added
, inf
);
450 interps_notify_inferior_appeared (inferior
*inf
)
452 interps_notify (&interp::on_inferior_appeared
, inf
);
458 interps_notify_inferior_disappeared (inferior
*inf
)
460 interps_notify (&interp::on_inferior_disappeared
, inf
);
466 interps_notify_inferior_removed (inferior
*inf
)
468 interps_notify (&interp::on_inferior_removed
, inf
);
474 interps_notify_record_changed (inferior
*inf
, int started
, const char *method
,
477 interps_notify (&interp::on_record_changed
, inf
, started
, method
, format
);
483 interps_notify_target_resumed (ptid_t ptid
)
485 interps_notify (&interp::on_target_resumed
, ptid
);
491 interps_notify_solib_loaded (const shobj
&so
)
493 interps_notify (&interp::on_solib_loaded
, so
);
499 interps_notify_solib_unloaded (const shobj
&so
)
501 interps_notify (&interp::on_solib_unloaded
, so
);
507 interps_notify_traceframe_changed (int tfnum
, int tpnum
)
509 interps_notify (&interp::on_traceframe_changed
, tfnum
, tpnum
);
515 interps_notify_tsv_created (const trace_state_variable
*tsv
)
517 interps_notify (&interp::on_tsv_created
, tsv
);
523 interps_notify_tsv_deleted (const trace_state_variable
*tsv
)
525 interps_notify (&interp::on_tsv_deleted
, tsv
);
531 interps_notify_tsv_modified (const trace_state_variable
*tsv
)
533 interps_notify (&interp::on_tsv_modified
, tsv
);
539 interps_notify_breakpoint_created (breakpoint
*b
)
541 interps_notify (&interp::on_breakpoint_created
, b
);
547 interps_notify_breakpoint_deleted (breakpoint
*b
)
549 interps_notify (&interp::on_breakpoint_deleted
, b
);
555 interps_notify_breakpoint_modified (breakpoint
*b
)
557 interps_notify (&interp::on_breakpoint_modified
, b
);
563 interps_notify_param_changed (const char *param
, const char *value
)
565 interps_notify (&interp::on_param_changed
, param
, value
);
571 interps_notify_memory_changed (inferior
*inf
, CORE_ADDR addr
, ssize_t len
,
572 const bfd_byte
*data
)
574 interps_notify (&interp::on_memory_changed
, inf
, addr
, len
, data
);
577 /* This just adds the "interpreter-exec" command. */
578 void _initialize_interpreter ();
580 _initialize_interpreter ()
582 struct cmd_list_element
*c
;
584 c
= add_cmd ("interpreter-exec", class_support
,
585 interpreter_exec_cmd
, _("\
586 Execute a command in an interpreter.\n\
587 Usage: interpreter-exec INTERPRETER COMMAND...\n\
588 The first argument is the name of the interpreter to use.\n\
589 The following arguments are the commands to execute.\n\
590 A command can have arguments, separated by spaces.\n\
591 These spaces must be escaped using \\ or the command\n\
592 and its arguments must be enclosed in double quotes."), &cmdlist
);
593 set_cmd_completer (c
, interpreter_completer
);