]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/interps.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / interps.c
CommitLineData
4a8f6654
AC
1/* Manages interpreters for GDB, the GNU debugger.
2
1d506c26 3 Copyright (C) 2000-2024 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"
400b5eca 35#include "gdbsupport/event-loop.h"
4a8f6654
AC
36#include "event-top.h"
37#include "interps.h"
38#include "completer.h"
13d03262 39#include "ui.h"
b0be6c91 40#include "main.h"
7904e961 41#include "gdbsupport/buildargv.h"
b885aea1 42#include "gdbsupport/scope-exit.h"
4a8f6654 43
1777feb0 44/* The magic initialization routine for this module. */
4a8f6654 45
8322445e
PA
46static struct interp *interp_lookup_existing (struct ui *ui,
47 const char *name);
48
d6f9b0fb 49interp::interp (const char *name)
5a8ac2cb 50 : m_name (name)
4a8f6654 51{
4a8f6654
AC
52}
53
5a8ac2cb 54interp::~interp () = default;
d6f9b0fb 55
8322445e
PA
56/* An interpreter factory. Maps an interpreter name to the factory
57 function that instantiates an interpreter by that name. */
58
59struct interp_factory
60{
4c2287b0
SM
61 interp_factory (const char *name_, interp_factory_func func_)
62 : name (name_), func (func_)
63 {}
64
8322445e
PA
65 /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */
66 const char *name;
67
68 /* The function that creates the interpreter. */
69 interp_factory_func func;
70};
71
8322445e 72/* The registered interpreter factories. */
4c2287b0 73static std::vector<interp_factory> interpreter_factories;
8322445e
PA
74
75/* See interps.h. */
76
77void
78interp_factory_register (const char *name, interp_factory_func func)
79{
8322445e 80 /* Assert that no factory for NAME is already registered. */
4c2287b0
SM
81 for (const interp_factory &f : interpreter_factories)
82 if (strcmp (f.name, name) == 0)
8322445e 83 {
f34652de 84 internal_error (_("interpreter factory already registered: \"%s\"\n"),
8322445e
PA
85 name);
86 }
87
4c2287b0 88 interpreter_factories.emplace_back (name, func);
8322445e
PA
89}
90
4a8f6654
AC
91/* Add interpreter INTERP to the gdb interpreter list. The
92 interpreter must not have previously been added. */
9716aa0a 93static void
8322445e 94interp_add (struct ui *ui, struct interp *interp)
4a8f6654 95{
d525a99b 96 gdb_assert (interp_lookup_existing (ui, interp->name ()) == NULL);
4a8f6654 97
970c6b7e 98 ui->interp_list.push_back (*interp);
4a8f6654
AC
99}
100
101/* This sets the current interpreter to be INTERP. If INTERP has not
d6f9b0fb 102 been initialized, then this will also run the init method.
683f2885
VP
103
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. */
d6f9b0fb 111
a474bd8e 112static void
d6f9b0fb 113interp_set (struct interp *interp, bool top_level)
4a8f6654 114{
970c6b7e 115 struct interp *old_interp = current_ui->current_interpreter;
4a8f6654 116
683f2885
VP
117 /* If we already have an interpreter, then trying to
118 set top level interpreter is kinda pointless. */
970c6b7e
SM
119 gdb_assert (!top_level || !current_ui->current_interpreter);
120 gdb_assert (!top_level || !current_ui->top_level_interpreter);
683f2885 121
cb814510 122 if (old_interp != NULL)
4a8f6654 123 {
112e8700 124 current_uiout->flush ();
d6f9b0fb 125 old_interp->suspend ();
4a8f6654 126 }
4a8f6654 127
970c6b7e 128 current_ui->current_interpreter = interp;
683f2885 129 if (top_level)
970c6b7e 130 current_ui->top_level_interpreter = interp;
4a8f6654 131
b2a696a8
TT
132 if (interpreter_p != interp->name ())
133 interpreter_p = interp->name ();
4a8f6654 134
d6f9b0fb 135 /* Run the init proc. */
4a8f6654
AC
136 if (!interp->inited)
137 {
d6f9b0fb
PA
138 interp->init (top_level);
139 interp->inited = true;
4a8f6654
AC
140 }
141
4801a9a3 142 /* Do this only after the interpreter is initialized. */
d6f9b0fb 143 current_uiout = interp->interp_ui_out ();
4801a9a3 144
907d819a 145 /* Clear out any installed interpreter hooks/event handlers. */
4a8f6654
AC
146 clear_interpreter_hooks ();
147
d6f9b0fb 148 interp->resume ();
4a8f6654
AC
149}
150
8322445e
PA
151/* Look up the interpreter for NAME. If no such interpreter exists,
152 return NULL, otherwise return a pointer to the interpreter. */
153
154static struct interp *
155interp_lookup_existing (struct ui *ui, const char *name)
4a8f6654 156{
970c6b7e 157 for (interp &interp : ui->interp_list)
4a91f820
SM
158 if (strcmp (interp.name (), name) == 0)
159 return &interp;
4a8f6654 160
4a91f820 161 return nullptr;
4a8f6654
AC
162}
163
8322445e
PA
164/* See interps.h. */
165
166struct interp *
167interp_lookup (struct ui *ui, const char *name)
168{
8322445e
PA
169 if (name == NULL || strlen (name) == 0)
170 return NULL;
171
172 /* Only create each interpreter once per top level. */
4c2287b0 173 struct interp *interp = interp_lookup_existing (ui, name);
8322445e
PA
174 if (interp != NULL)
175 return interp;
176
4c2287b0
SM
177 for (const interp_factory &factory : interpreter_factories)
178 if (strcmp (factory.name, name) == 0)
8322445e 179 {
5a8ac2cb 180 interp = factory.func (factory.name);
8322445e
PA
181 interp_add (ui, interp);
182 return interp;
183 }
184
185 return NULL;
186}
187
60eb5395
PA
188/* See interps.h. */
189
190void
191set_top_level_interpreter (const char *name)
192{
193 /* Find it. */
194 struct interp *interp = interp_lookup (current_ui, name);
195
196 if (interp == NULL)
197 error (_("Interpreter `%s' unrecognized"), name);
198 /* Install it. */
d6f9b0fb 199 interp_set (interp, true);
60eb5395
PA
200}
201
616268b6 202void
ca1285d1
AH
203current_interp_set_logging (ui_file_up logfile, bool logging_redirect,
204 bool debug_redirect)
37ce89eb 205{
970c6b7e 206 struct interp *interp = current_ui->current_interpreter;
cb814510 207
ca1285d1 208 interp->set_logging (std::move (logfile), logging_redirect, debug_redirect);
37ce89eb
SS
209}
210
c41535fd
EZ
211/* Temporarily overrides the current interpreter. */
212struct interp *
be0d7abb 213scoped_restore_interp::set_interp (const char *name)
c41535fd 214{
8322445e 215 struct interp *interp = interp_lookup (current_ui, name);
970c6b7e 216 struct interp *old_interp = current_ui->current_interpreter;
c41535fd
EZ
217
218 if (interp)
970c6b7e
SM
219 current_ui->current_interpreter = interp;
220
c41535fd
EZ
221 return old_interp;
222}
223
1777feb0 224/* Returns true if the current interp is the passed in name. */
4a8f6654
AC
225int
226current_interp_named_p (const char *interp_name)
227{
970c6b7e 228 interp *interp = current_ui->current_interpreter;
cb814510
PA
229
230 if (interp != NULL)
d525a99b 231 return (strcmp (interp->name (), interp_name) == 0);
4a8f6654
AC
232
233 return 0;
234}
235
17b2616c
PA
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. */
245
246struct interp *
247command_interp (void)
248{
970c6b7e
SM
249 if (current_ui->command_interpreter != nullptr)
250 return current_ui->command_interpreter;
17b2616c 251 else
970c6b7e 252 return current_ui->current_interpreter;
17b2616c
PA
253}
254
4a8f6654 255/* interp_exec - This executes COMMAND_STR in the current
1777feb0 256 interpreter. */
4a8f6654 257
b885aea1 258void
4a8f6654
AC
259interp_exec (struct interp *interp, const char *command_str)
260{
17b2616c 261 /* See `command_interp' for why we do this. */
753ff9bd 262 scoped_restore save_command_interp
970c6b7e 263 = make_scoped_restore (&current_ui->command_interpreter, interp);
17b2616c 264
b885aea1 265 interp->exec (command_str);
4a8f6654
AC
266}
267
907d819a
MS
268/* A convenience routine that nulls out all the common command hooks.
269 Use it when removing your interpreter in its suspend proc. */
4a8f6654 270void
11308a41 271clear_interpreter_hooks (void)
4a8f6654 272{
9a4105ab 273 deprecated_print_frame_info_listing_hook = 0;
4a8f6654 274 /*print_frame_more_info_hook = 0; */
9a4105ab
AC
275 deprecated_query_hook = 0;
276 deprecated_warning_hook = 0;
9a4105ab
AC
277 deprecated_readline_begin_hook = 0;
278 deprecated_readline_hook = 0;
279 deprecated_readline_end_hook = 0;
9a4105ab 280 deprecated_context_hook = 0;
9a4105ab 281 deprecated_call_command_hook = 0;
9a4105ab 282 deprecated_error_begin_hook = 0;
4a8f6654
AC
283}
284
b9362cc7 285static void
1970a12f 286interpreter_exec_cmd (const char *args, int from_tty)
4a8f6654 287{
970c6b7e 288 struct interp *interp_to_use;
4a8f6654
AC
289 unsigned int nrules;
290 unsigned int i;
4a8f6654 291
e1ff6226
PW
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);
299
d1a41061
PP
300 if (args == NULL)
301 error_no_arg (_("interpreter-exec command"));
302
773a1edc
TT
303 gdb_argv prules (args);
304 nrules = prules.count ();
4a8f6654
AC
305
306 if (nrules < 2)
590042fc 307 error (_("Usage: interpreter-exec INTERPRETER COMMAND..."));
4a8f6654 308
970c6b7e 309 interp *old_interp = current_ui->current_interpreter;
4a8f6654 310
8322445e 311 interp_to_use = interp_lookup (current_ui, prules[0]);
4a8f6654 312 if (interp_to_use == NULL)
8a3fe4f8 313 error (_("Could not find interpreter \"%s\"."), prules[0]);
4a8f6654 314
d6f9b0fb 315 interp_set (interp_to_use, false);
b885aea1 316 SCOPE_EXIT
4a8f6654 317 {
b885aea1
PA
318 interp_set (old_interp, false);
319 };
abbb1732 320
b885aea1
PA
321 for (i = 1; i < nrules; i++)
322 interp_exec (interp_to_use, prules[i]);
4a8f6654
AC
323}
324
60eb5395
PA
325/* See interps.h. */
326
eb3ff9a5 327void
6f937416 328interpreter_completer (struct cmd_list_element *ignore,
eb3ff9a5 329 completion_tracker &tracker,
6f937416 330 const char *text, const char *word)
4a8f6654 331{
4c2287b0
SM
332 int textlen = strlen (text);
333
334 for (const interp_factory &interp : interpreter_factories)
4a8f6654 335 {
4c2287b0 336 if (strncmp (interp.name, text, textlen) == 0)
4a8f6654 337 {
60a20c19
PA
338 tracker.add_completion
339 (make_completion_match_str (interp.name, text, word));
4a8f6654
AC
340 }
341 }
4a8f6654
AC
342}
343
eb18d289
NR
344struct interp *
345top_level_interpreter (void)
346{
970c6b7e 347 return current_ui->top_level_interpreter;
eb18d289
NR
348}
349
9204d692
PA
350/* See interps.h. */
351
352struct interp *
353current_interpreter (void)
354{
970c6b7e 355 return current_ui->current_interpreter;
9204d692
PA
356}
357
3f75a984
SM
358/* Helper interps_notify_* functions. Call METHOD on the top-level interpreter
359 of all UIs. */
360
c1d21880 361template <typename MethodType, typename ...Args>
3f75a984 362void
c1d21880 363interps_notify (MethodType method, Args&&... args)
3f75a984
SM
364{
365 SWITCH_THRU_ALL_UIS ()
366 {
367 interp *tli = top_level_interpreter ();
368 if (tli != nullptr)
c1d21880 369 (tli->*method) (std::forward<Args> (args)...);
3f75a984
SM
370 }
371}
372
373/* See interps.h. */
374
375void
376interps_notify_signal_received (gdb_signal sig)
377{
378 interps_notify (&interp::on_signal_received, sig);
379}
380
87829267
SM
381/* See interps.h. */
382
d6bd2ef5
SM
383void
384interps_notify_signal_exited (gdb_signal sig)
385{
386 interps_notify (&interp::on_signal_exited, sig);
387}
388
389/* See interps.h. */
390
2e5dbfab
SM
391void
392interps_notify_no_history ()
393{
394 interps_notify (&interp::on_no_history);
395}
396
397/* See interps.h. */
398
87829267
SM
399void
400interps_notify_normal_stop (bpstat *bs, int print_frame)
401{
402 interps_notify (&interp::on_normal_stop, bs, print_frame);
403}
404
bf64d1d5
SM
405/* See interps.h. */
406
407void
408interps_notify_exited (int status)
409{
410 interps_notify (&interp::on_exited, status);
411}
412
77cd03e2
SM
413/* See interps.h. */
414
415void
416interps_notify_user_selected_context_changed (user_selected_what selection)
417{
418 interps_notify (&interp::on_user_selected_context_changed, selection);
419}
420
30e7e0a9
SM
421/* See interps.h. */
422
423void
424interps_notify_new_thread (thread_info *t)
425{
426 interps_notify (&interp::on_new_thread, t);
427}
428
8e7af843
SM
429/* See interps.h. */
430
431void
9d7d58e7 432interps_notify_thread_exited (thread_info *t,
6b09f134 433 std::optional<ULONGEST> exit_code,
9d7d58e7 434 int silent)
8e7af843 435{
9d7d58e7 436 interps_notify (&interp::on_thread_exited, t, exit_code, silent);
8e7af843
SM
437}
438
023c6d45
SM
439/* See interps.h. */
440
441void
442interps_notify_inferior_added (inferior *inf)
443{
444 interps_notify (&interp::on_inferior_added, inf);
445}
446
0c613e17
SM
447/* See interps.h. */
448
449void
450interps_notify_inferior_appeared (inferior *inf)
451{
452 interps_notify (&interp::on_inferior_appeared, inf);
453}
454
d38086cc
SM
455/* See interps.h. */
456
457void
458interps_notify_inferior_disappeared (inferior *inf)
459{
460 interps_notify (&interp::on_inferior_disappeared, inf);
461}
462
2646bfa7
SM
463/* See interps.h. */
464
465void
466interps_notify_inferior_removed (inferior *inf)
467{
468 interps_notify (&interp::on_inferior_removed, inf);
469}
470
44fbffc6
SM
471/* See interps.h. */
472
473void
474interps_notify_record_changed (inferior *inf, int started, const char *method,
475 const char *format)
476{
477 interps_notify (&interp::on_record_changed, inf, started, method, format);
478}
479
52d98df7
SM
480/* See interps.h. */
481
482void
483interps_notify_target_resumed (ptid_t ptid)
484{
485 interps_notify (&interp::on_target_resumed, ptid);
486}
487
f6485481
SM
488/* See interps.h. */
489
490void
3fe0dfd1 491interps_notify_solib_loaded (const shobj &so)
f6485481
SM
492{
493 interps_notify (&interp::on_solib_loaded, so);
494}
495
d711fe3b
SM
496/* See interps.h. */
497
498void
3fe0dfd1 499interps_notify_solib_unloaded (const shobj &so)
d711fe3b
SM
500{
501 interps_notify (&interp::on_solib_unloaded, so);
502}
503
0bc845fc
SM
504/* See interps.h. */
505
506void
507interps_notify_traceframe_changed (int tfnum, int tpnum)
508{
509 interps_notify (&interp::on_traceframe_changed, tfnum, tpnum);
510}
511
bf506f27
SM
512/* See interps.h. */
513
514void
515interps_notify_tsv_created (const trace_state_variable *tsv)
516{
517 interps_notify (&interp::on_tsv_created, tsv);
518}
519
f0dffaff
SM
520/* See interps.h. */
521
522void
523interps_notify_tsv_deleted (const trace_state_variable *tsv)
524{
525 interps_notify (&interp::on_tsv_deleted, tsv);
526}
527
c27ec5c0
SM
528/* See interps.h. */
529
530void
531interps_notify_tsv_modified (const trace_state_variable *tsv)
532{
533 interps_notify (&interp::on_tsv_modified, tsv);
534}
535
e7692320
SM
536/* See interps.h. */
537
538void
539interps_notify_breakpoint_created (breakpoint *b)
540{
541 interps_notify (&interp::on_breakpoint_created, b);
542}
543
e4239559
SM
544/* See interps.h. */
545
546void
547interps_notify_breakpoint_deleted (breakpoint *b)
548{
549 interps_notify (&interp::on_breakpoint_deleted, b);
550}
551
19081eb5
SM
552/* See interps.h. */
553
554void
555interps_notify_breakpoint_modified (breakpoint *b)
556{
557 interps_notify (&interp::on_breakpoint_modified, b);
558}
559
3d654fa7
SM
560/* See interps.h. */
561
562void
563interps_notify_param_changed (const char *param, const char *value)
564{
565 interps_notify (&interp::on_param_changed, param, value);
566}
567
ec517d10
SM
568/* See interps.h. */
569
570void
571interps_notify_memory_changed (inferior *inf, CORE_ADDR addr, ssize_t len,
572 const bfd_byte *data)
573{
574 interps_notify (&interp::on_memory_changed, inf, addr, len, data);
575}
576
4a8f6654 577/* This just adds the "interpreter-exec" command. */
6c265988 578void _initialize_interpreter ();
4a8f6654 579void
6c265988 580_initialize_interpreter ()
4a8f6654
AC
581{
582 struct cmd_list_element *c;
583
584 c = add_cmd ("interpreter-exec", class_support,
1a966eab 585 interpreter_exec_cmd, _("\
89549d7f 586Execute a command in an interpreter.\n\
590042fc 587Usage: interpreter-exec INTERPRETER COMMAND...\n\
4a8f6654 588The first argument is the name of the interpreter to use.\n\
590042fc
PW
589The following arguments are the commands to execute.\n\
590A command can have arguments, separated by spaces.\n\
591These spaces must be escaped using \\ or the command\n\
592and its arguments must be enclosed in double quotes."), &cmdlist);
4a8f6654
AC
593 set_cmd_completer (c, interpreter_completer);
594}