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