]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/interps.c
f665e86b65c751c4cd6b003677c37a5760cd464c
[thirdparty/binutils-gdb.git] / gdb / interps.c
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 "ui.h"
40 #include "main.h"
41 #include "gdbsupport/buildargv.h"
42 #include "gdbsupport/scope-exit.h"
43
44 /* The magic initialization routine for this module. */
45
46 static struct interp *interp_lookup_existing (struct ui *ui,
47 const char *name);
48
49 interp::interp (const char *name)
50 : m_name (name)
51 {
52 }
53
54 interp::~interp () = default;
55
56 /* An interpreter factory. Maps an interpreter name to the factory
57 function that instantiates an interpreter by that name. */
58
59 struct interp_factory
60 {
61 interp_factory (const char *name_, interp_factory_func func_)
62 : name (name_), func (func_)
63 {}
64
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
72 /* The registered interpreter factories. */
73 static std::vector<interp_factory> interpreter_factories;
74
75 /* See interps.h. */
76
77 void
78 interp_factory_register (const char *name, interp_factory_func func)
79 {
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)
83 {
84 internal_error (_("interpreter factory already registered: \"%s\"\n"),
85 name);
86 }
87
88 interpreter_factories.emplace_back (name, func);
89 }
90
91 /* Add interpreter INTERP to the gdb interpreter list. The
92 interpreter must not have previously been added. */
93 static void
94 interp_add (struct ui *ui, struct interp *interp)
95 {
96 gdb_assert (interp_lookup_existing (ui, interp->name ()) == NULL);
97
98 ui->interp_list.push_back (*interp);
99 }
100
101 /* This sets the current interpreter to be INTERP. If INTERP has not
102 been initialized, then this will also run the init method.
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. */
111
112 static void
113 interp_set (struct interp *interp, bool top_level)
114 {
115 struct interp *old_interp = current_ui->current_interpreter;
116
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);
121
122 if (old_interp != NULL)
123 {
124 current_uiout->flush ();
125 old_interp->suspend ();
126 }
127
128 current_ui->current_interpreter = interp;
129 if (top_level)
130 current_ui->top_level_interpreter = interp;
131
132 if (interpreter_p != interp->name ())
133 interpreter_p = interp->name ();
134
135 bool warn_about_mi1 = false;
136
137 /* Run the init proc. */
138 if (!interp->inited)
139 {
140 interp->init (top_level);
141 interp->inited = true;
142
143 if (streq (interp->name (), "mi1"))
144 warn_about_mi1 = true;
145 }
146
147 /* Do this only after the interpreter is initialized. */
148 current_uiout = interp->interp_ui_out ();
149
150 /* Clear out any installed interpreter hooks/event handlers. */
151 clear_interpreter_hooks ();
152
153 interp->resume ();
154
155 if (warn_about_mi1)
156 warning (_("MI version 1 is deprecated in GDB 13 and "
157 "will be removed in GDB 14. Please upgrade "
158 "to a newer version of MI."));
159 }
160
161 /* Look up the interpreter for NAME. If no such interpreter exists,
162 return NULL, otherwise return a pointer to the interpreter. */
163
164 static struct interp *
165 interp_lookup_existing (struct ui *ui, const char *name)
166 {
167 for (interp &interp : ui->interp_list)
168 if (strcmp (interp.name (), name) == 0)
169 return &interp;
170
171 return nullptr;
172 }
173
174 /* See interps.h. */
175
176 struct interp *
177 interp_lookup (struct ui *ui, const char *name)
178 {
179 if (name == NULL || strlen (name) == 0)
180 return NULL;
181
182 /* Only create each interpreter once per top level. */
183 struct interp *interp = interp_lookup_existing (ui, name);
184 if (interp != NULL)
185 return interp;
186
187 for (const interp_factory &factory : interpreter_factories)
188 if (strcmp (factory.name, name) == 0)
189 {
190 interp = factory.func (factory.name);
191 interp_add (ui, interp);
192 return interp;
193 }
194
195 return NULL;
196 }
197
198 /* See interps.h. */
199
200 void
201 set_top_level_interpreter (const char *name)
202 {
203 /* Find it. */
204 struct interp *interp = interp_lookup (current_ui, name);
205
206 if (interp == NULL)
207 error (_("Interpreter `%s' unrecognized"), name);
208 /* Install it. */
209 interp_set (interp, true);
210 }
211
212 void
213 current_interp_set_logging (ui_file_up logfile, bool logging_redirect,
214 bool debug_redirect)
215 {
216 struct interp *interp = current_ui->current_interpreter;
217
218 interp->set_logging (std::move (logfile), logging_redirect, debug_redirect);
219 }
220
221 /* Temporarily overrides the current interpreter. */
222 struct interp *
223 scoped_restore_interp::set_interp (const char *name)
224 {
225 struct interp *interp = interp_lookup (current_ui, name);
226 struct interp *old_interp = current_ui->current_interpreter;
227
228 if (interp)
229 current_ui->current_interpreter = interp;
230
231 return old_interp;
232 }
233
234 /* Returns true if the current interp is the passed in name. */
235 int
236 current_interp_named_p (const char *interp_name)
237 {
238 interp *interp = current_ui->current_interpreter;
239
240 if (interp != NULL)
241 return (strcmp (interp->name (), interp_name) == 0);
242
243 return 0;
244 }
245
246 /* The interpreter that was active when a command was executed.
247 Normally that'd always be CURRENT_INTERPRETER, except that MI's
248 -interpreter-exec command doesn't actually flip the current
249 interpreter when running its sub-command. The
250 `command_interpreter' global tracks when interp_exec is called
251 (IOW, when -interpreter-exec is called). If that is set, it is
252 INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec
253 INTERP "CMD". Otherwise, interp_exec isn't active, and so the
254 interpreter running the command is the current interpreter. */
255
256 struct interp *
257 command_interp (void)
258 {
259 if (current_ui->command_interpreter != nullptr)
260 return current_ui->command_interpreter;
261 else
262 return current_ui->current_interpreter;
263 }
264
265 /* See interps.h. */
266
267 void
268 interp_pre_command_loop (struct interp *interp)
269 {
270 gdb_assert (interp != NULL);
271
272 interp->pre_command_loop ();
273 }
274
275 /* See interp.h */
276
277 int
278 interp_supports_command_editing (struct interp *interp)
279 {
280 return interp->supports_command_editing ();
281 }
282
283 /* interp_exec - This executes COMMAND_STR in the current
284 interpreter. */
285
286 void
287 interp_exec (struct interp *interp, const char *command_str)
288 {
289 /* See `command_interp' for why we do this. */
290 scoped_restore save_command_interp
291 = make_scoped_restore (&current_ui->command_interpreter, interp);
292
293 interp->exec (command_str);
294 }
295
296 /* A convenience routine that nulls out all the common command hooks.
297 Use it when removing your interpreter in its suspend proc. */
298 void
299 clear_interpreter_hooks (void)
300 {
301 deprecated_print_frame_info_listing_hook = 0;
302 /*print_frame_more_info_hook = 0; */
303 deprecated_query_hook = 0;
304 deprecated_warning_hook = 0;
305 deprecated_readline_begin_hook = 0;
306 deprecated_readline_hook = 0;
307 deprecated_readline_end_hook = 0;
308 deprecated_context_hook = 0;
309 deprecated_call_command_hook = 0;
310 deprecated_error_begin_hook = 0;
311 }
312
313 static void
314 interpreter_exec_cmd (const char *args, int from_tty)
315 {
316 struct interp *interp_to_use;
317 unsigned int nrules;
318 unsigned int i;
319
320 /* Interpreters may clobber stdout/stderr (e.g. in mi_interp::resume at time
321 of writing), preserve their state here. */
322 scoped_restore save_stdout = make_scoped_restore (&gdb_stdout);
323 scoped_restore save_stderr = make_scoped_restore (&gdb_stderr);
324 scoped_restore save_stdlog = make_scoped_restore (&gdb_stdlog);
325 scoped_restore save_stdtarg = make_scoped_restore (&gdb_stdtarg);
326 scoped_restore save_stdtargerr = make_scoped_restore (&gdb_stdtargerr);
327
328 if (args == NULL)
329 error_no_arg (_("interpreter-exec command"));
330
331 gdb_argv prules (args);
332 nrules = prules.count ();
333
334 if (nrules < 2)
335 error (_("Usage: interpreter-exec INTERPRETER COMMAND..."));
336
337 interp *old_interp = current_ui->current_interpreter;
338
339 interp_to_use = interp_lookup (current_ui, prules[0]);
340 if (interp_to_use == NULL)
341 error (_("Could not find interpreter \"%s\"."), prules[0]);
342
343 interp_set (interp_to_use, false);
344 SCOPE_EXIT
345 {
346 interp_set (old_interp, false);
347 };
348
349 for (i = 1; i < nrules; i++)
350 interp_exec (interp_to_use, prules[i]);
351 }
352
353 /* See interps.h. */
354
355 void
356 interpreter_completer (struct cmd_list_element *ignore,
357 completion_tracker &tracker,
358 const char *text, const char *word)
359 {
360 int textlen = strlen (text);
361
362 for (const interp_factory &interp : interpreter_factories)
363 {
364 if (strncmp (interp.name, text, textlen) == 0)
365 {
366 tracker.add_completion
367 (make_completion_match_str (interp.name, text, word));
368 }
369 }
370 }
371
372 struct interp *
373 top_level_interpreter (void)
374 {
375 return current_ui->top_level_interpreter;
376 }
377
378 /* See interps.h. */
379
380 struct interp *
381 current_interpreter (void)
382 {
383 return current_ui->current_interpreter;
384 }
385
386 /* Helper interps_notify_* functions. Call METHOD on the top-level interpreter
387 of all UIs. */
388
389 template <typename ...Args>
390 void
391 interps_notify (void (interp::*method) (Args...), Args... args)
392 {
393 SWITCH_THRU_ALL_UIS ()
394 {
395 interp *tli = top_level_interpreter ();
396 if (tli != nullptr)
397 (tli->*method) (args...);
398 }
399 }
400
401 /* See interps.h. */
402
403 void
404 interps_notify_signal_received (gdb_signal sig)
405 {
406 interps_notify (&interp::on_signal_received, sig);
407 }
408
409 /* See interps.h. */
410
411 void
412 interps_notify_signal_exited (gdb_signal sig)
413 {
414 interps_notify (&interp::on_signal_exited, sig);
415 }
416
417 /* See interps.h. */
418
419 void
420 interps_notify_no_history ()
421 {
422 interps_notify (&interp::on_no_history);
423 }
424
425 /* See interps.h. */
426
427 void
428 interps_notify_normal_stop (bpstat *bs, int print_frame)
429 {
430 interps_notify (&interp::on_normal_stop, bs, print_frame);
431 }
432
433 /* See interps.h. */
434
435 void
436 interps_notify_exited (int status)
437 {
438 interps_notify (&interp::on_exited, status);
439 }
440
441 /* See interps.h. */
442
443 void
444 interps_notify_user_selected_context_changed (user_selected_what selection)
445 {
446 interps_notify (&interp::on_user_selected_context_changed, selection);
447 }
448
449 /* See interps.h. */
450
451 void
452 interps_notify_new_thread (thread_info *t)
453 {
454 interps_notify (&interp::on_new_thread, t);
455 }
456
457 /* See interps.h. */
458
459 void
460 interps_notify_thread_exited (thread_info *t, int silent)
461 {
462 interps_notify (&interp::on_thread_exited, t, silent);
463 }
464
465 /* See interps.h. */
466
467 void
468 interps_notify_inferior_added (inferior *inf)
469 {
470 interps_notify (&interp::on_inferior_added, inf);
471 }
472
473 /* See interps.h. */
474
475 void
476 interps_notify_inferior_appeared (inferior *inf)
477 {
478 interps_notify (&interp::on_inferior_appeared, inf);
479 }
480
481 /* See interps.h. */
482
483 void
484 interps_notify_inferior_disappeared (inferior *inf)
485 {
486 interps_notify (&interp::on_inferior_disappeared, inf);
487 }
488
489 /* See interps.h. */
490
491 void
492 interps_notify_inferior_removed (inferior *inf)
493 {
494 interps_notify (&interp::on_inferior_removed, inf);
495 }
496
497 /* See interps.h. */
498
499 void
500 interps_notify_record_changed (inferior *inf, int started, const char *method,
501 const char *format)
502 {
503 interps_notify (&interp::on_record_changed, inf, started, method, format);
504 }
505
506 /* See interps.h. */
507
508 void
509 interps_notify_target_resumed (ptid_t ptid)
510 {
511 interps_notify (&interp::on_target_resumed, ptid);
512 }
513
514 /* See interps.h. */
515
516 void
517 interps_notify_solib_loaded (so_list *so)
518 {
519 interps_notify (&interp::on_solib_loaded, so);
520 }
521
522 /* See interps.h. */
523
524 void
525 interps_notify_solib_unloaded (so_list *so)
526 {
527 interps_notify (&interp::on_solib_unloaded, so);
528 }
529
530 /* See interps.h. */
531
532 void
533 interps_notify_traceframe_changed (int tfnum, int tpnum)
534 {
535 interps_notify (&interp::on_traceframe_changed, tfnum, tpnum);
536 }
537
538 /* See interps.h. */
539
540 void
541 interps_notify_tsv_created (const trace_state_variable *tsv)
542 {
543 interps_notify (&interp::on_tsv_created, tsv);
544 }
545
546 /* See interps.h. */
547
548 void
549 interps_notify_tsv_deleted (const trace_state_variable *tsv)
550 {
551 interps_notify (&interp::on_tsv_deleted, tsv);
552 }
553
554 /* See interps.h. */
555
556 void
557 interps_notify_tsv_modified (const trace_state_variable *tsv)
558 {
559 interps_notify (&interp::on_tsv_modified, tsv);
560 }
561
562 /* See interps.h. */
563
564 void
565 interps_notify_breakpoint_created (breakpoint *b)
566 {
567 interps_notify (&interp::on_breakpoint_created, b);
568 }
569
570 /* See interps.h. */
571
572 void
573 interps_notify_breakpoint_deleted (breakpoint *b)
574 {
575 interps_notify (&interp::on_breakpoint_deleted, b);
576 }
577
578 /* See interps.h. */
579
580 void
581 interps_notify_breakpoint_modified (breakpoint *b)
582 {
583 interps_notify (&interp::on_breakpoint_modified, b);
584 }
585
586 /* See interps.h. */
587
588 void
589 interps_notify_param_changed (const char *param, const char *value)
590 {
591 interps_notify (&interp::on_param_changed, param, value);
592 }
593
594 /* See interps.h. */
595
596 void
597 interps_notify_memory_changed (inferior *inf, CORE_ADDR addr, ssize_t len,
598 const bfd_byte *data)
599 {
600 interps_notify (&interp::on_memory_changed, inf, addr, len, data);
601 }
602
603 /* This just adds the "interpreter-exec" command. */
604 void _initialize_interpreter ();
605 void
606 _initialize_interpreter ()
607 {
608 struct cmd_list_element *c;
609
610 c = add_cmd ("interpreter-exec", class_support,
611 interpreter_exec_cmd, _("\
612 Execute a command in an interpreter.\n\
613 Usage: interpreter-exec INTERPRETER COMMAND...\n\
614 The first argument is the name of the interpreter to use.\n\
615 The following arguments are the commands to execute.\n\
616 A command can have arguments, separated by spaces.\n\
617 These spaces must be escaped using \\ or the command\n\
618 and its arguments must be enclosed in double quotes."), &cmdlist);
619 set_cmd_completer (c, interpreter_completer);
620 }