]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/extension.c
2d692d054315fa9be0e4b5528a3935203d6cd597
[thirdparty/binutils-gdb.git] / gdb / extension.c
1 /* Interface between gdb and its extension languages.
2
3 Copyright (C) 2014-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* Note: With few exceptions, external functions and variables in this file
21 have "ext_lang" in the name, and no other symbol in gdb does. */
22
23 #include <signal.h>
24 #include "target.h"
25 #include "auto-load.h"
26 #include "breakpoint.h"
27 #include "event-top.h"
28 #include "extension.h"
29 #include "extension-priv.h"
30 #include "observable.h"
31 #include "cli/cli-script.h"
32 #include "python/python.h"
33 #include "guile/guile.h"
34 #include <array>
35 #include "inferior.h"
36
37 static script_sourcer_func source_gdb_script;
38 static objfile_script_sourcer_func source_gdb_objfile_script;
39
40 /* GDB's own scripting language.
41 This exists, in part, to support auto-loading ${prog}-gdb.gdb scripts. */
42
43 static const struct extension_language_script_ops
44 extension_language_gdb_script_ops =
45 {
46 source_gdb_script,
47 source_gdb_objfile_script,
48 NULL, /* objfile_script_executor */
49 auto_load_gdb_scripts_enabled
50 };
51
52 const struct extension_language_defn extension_language_gdb =
53 {
54 EXT_LANG_GDB,
55 "gdb",
56 "GDB",
57
58 /* We fall back to interpreting a script as a GDB script if it doesn't
59 match the other scripting languages, but for consistency's sake
60 give it a formal suffix. */
61 ".gdb",
62 "-gdb.gdb",
63
64 /* cli_control_type: This is never used: GDB's own scripting language
65 has a variety of control types (if, while, etc.). */
66 commands_control,
67
68 &extension_language_gdb_script_ops,
69
70 /* The rest of the extension language interface isn't supported by GDB's own
71 extension/scripting language. */
72 NULL
73 };
74
75 /* NULL-terminated table of all external (non-native) extension languages.
76
77 The order of appearance in the table is important.
78 When multiple extension languages provide the same feature, for example
79 a pretty-printer for a particular type, which one gets used?
80 The algorithm employed here is "the first one wins". For example, in
81 the case of pretty-printers this means the first one to provide a
82 pretty-printed value is the one that is used. This algorithm is employed
83 throughout. */
84
85 static const std::array<const extension_language_defn *, 2> extension_languages
86 {
87 /* To preserve existing behaviour, python should always appear first. */
88 &extension_language_python,
89 &extension_language_guile,
90 };
91
92 /* Return a pointer to the struct extension_language_defn object of
93 extension language LANG.
94 This always returns a non-NULL pointer, even if support for the language
95 is not compiled into this copy of GDB. */
96
97 const struct extension_language_defn *
98 get_ext_lang_defn (enum extension_language lang)
99 {
100 gdb_assert (lang != EXT_LANG_NONE);
101
102 if (lang == EXT_LANG_GDB)
103 return &extension_language_gdb;
104
105 for (const struct extension_language_defn *extlang : extension_languages)
106 {
107 if (extlang->language == lang)
108 return extlang;
109 }
110
111 gdb_assert_not_reached ("unable to find extension_language_defn");
112 }
113
114 /* Return TRUE if FILE has extension EXTENSION. */
115
116 static int
117 has_extension (const char *file, const char *extension)
118 {
119 int file_len = strlen (file);
120 int extension_len = strlen (extension);
121
122 return (file_len > extension_len
123 && strcmp (&file[file_len - extension_len], extension) == 0);
124 }
125
126 /* Return the extension language of FILE, or NULL if
127 the extension language of FILE is not recognized.
128 This is done by looking at the file's suffix. */
129
130 const struct extension_language_defn *
131 get_ext_lang_of_file (const char *file)
132 {
133 if (has_extension (file, extension_language_gdb.suffix))
134 return &extension_language_gdb;
135
136 for (const struct extension_language_defn *extlang : extension_languages)
137 {
138 if (has_extension (file, extlang->suffix))
139 return extlang;
140 }
141
142 return NULL;
143 }
144
145 /* Return non-zero if support for the specified extension language
146 is compiled in. */
147
148 int
149 ext_lang_present_p (const struct extension_language_defn *extlang)
150 {
151 return extlang->script_ops != NULL;
152 }
153
154 /* Return non-zero if the specified extension language has successfully
155 initialized. */
156
157 int
158 ext_lang_initialized_p (const struct extension_language_defn *extlang)
159 {
160 if (extlang->ops != NULL)
161 {
162 /* This method is required. */
163 gdb_assert (extlang->ops->initialized != NULL);
164 return extlang->ops->initialized (extlang);
165 }
166
167 return 0;
168 }
169
170 /* Throw an error indicating EXTLANG is not supported in this copy of GDB. */
171
172 void
173 throw_ext_lang_unsupported (const struct extension_language_defn *extlang)
174 {
175 error (_("Scripting in the \"%s\" language is not supported"
176 " in this copy of GDB."),
177 ext_lang_capitalized_name (extlang));
178 }
179 \f
180 /* Methods for GDB's own extension/scripting language. */
181
182 /* The extension_language_script_ops.script_sourcer "method". */
183
184 static void
185 source_gdb_script (const struct extension_language_defn *extlang,
186 FILE *stream, const char *file)
187 {
188 script_from_file (stream, file);
189 }
190
191 /* The extension_language_script_ops.objfile_script_sourcer "method". */
192
193 static void
194 source_gdb_objfile_script (const struct extension_language_defn *extlang,
195 struct objfile *objfile,
196 FILE *stream, const char *file)
197 {
198 script_from_file (stream, file);
199 }
200 \f
201 /* Accessors for "public" attributes of struct extension_language. */
202
203 /* Return the "name" field of EXTLANG. */
204
205 const char *
206 ext_lang_name (const struct extension_language_defn *extlang)
207 {
208 return extlang->name;
209 }
210
211 /* Return the "capitalized_name" field of EXTLANG. */
212
213 const char *
214 ext_lang_capitalized_name (const struct extension_language_defn *extlang)
215 {
216 return extlang->capitalized_name;
217 }
218
219 /* Return the "suffix" field of EXTLANG. */
220
221 const char *
222 ext_lang_suffix (const struct extension_language_defn *extlang)
223 {
224 return extlang->suffix;
225 }
226
227 /* Return the "auto_load_suffix" field of EXTLANG. */
228
229 const char *
230 ext_lang_auto_load_suffix (const struct extension_language_defn *extlang)
231 {
232 return extlang->auto_load_suffix;
233 }
234 \f
235 /* extension_language_script_ops wrappers. */
236
237 /* Return the script "sourcer" function for EXTLANG.
238 This is the function that loads and processes a script.
239 If support for this language isn't compiled in, NULL is returned. */
240
241 script_sourcer_func *
242 ext_lang_script_sourcer (const struct extension_language_defn *extlang)
243 {
244 if (extlang->script_ops == NULL)
245 return NULL;
246
247 /* The extension language is required to implement this function. */
248 gdb_assert (extlang->script_ops->script_sourcer != NULL);
249
250 return extlang->script_ops->script_sourcer;
251 }
252
253 /* Return the objfile script "sourcer" function for EXTLANG.
254 This is the function that loads and processes a script for a particular
255 objfile.
256 If support for this language isn't compiled in, NULL is returned. */
257
258 objfile_script_sourcer_func *
259 ext_lang_objfile_script_sourcer (const struct extension_language_defn *extlang)
260 {
261 if (extlang->script_ops == NULL)
262 return NULL;
263
264 /* The extension language is required to implement this function. */
265 gdb_assert (extlang->script_ops->objfile_script_sourcer != NULL);
266
267 return extlang->script_ops->objfile_script_sourcer;
268 }
269
270 /* Return the objfile script "executor" function for EXTLANG.
271 This is the function that executes a script for a particular objfile.
272 If support for this language isn't compiled in, NULL is returned.
273 The extension language is not required to implement this function. */
274
275 objfile_script_executor_func *
276 ext_lang_objfile_script_executor
277 (const struct extension_language_defn *extlang)
278 {
279 if (extlang->script_ops == NULL)
280 return NULL;
281
282 return extlang->script_ops->objfile_script_executor;
283 }
284
285 /* See extension.h. */
286
287 bool
288 ext_lang_auto_load_enabled (const struct extension_language_defn *extlang)
289 {
290 if (extlang->script_ops == NULL)
291 return false;
292
293 /* The extension language is required to implement this function. */
294 gdb_assert (extlang->script_ops->auto_load_enabled != NULL);
295
296 return extlang->script_ops->auto_load_enabled (extlang);
297 }
298 \f
299
300 /* RAII class used to temporarily return SIG to its default handler. */
301
302 template<int SIG>
303 struct scoped_default_signal
304 {
305 scoped_default_signal ()
306 { m_old_sig_handler = signal (SIG, SIG_DFL); }
307
308 ~scoped_default_signal ()
309 { signal (SIG, m_old_sig_handler); }
310
311 DISABLE_COPY_AND_ASSIGN (scoped_default_signal);
312
313 private:
314 /* The previous signal handler that needs to be restored. */
315 sighandler_t m_old_sig_handler;
316 };
317
318 /* Class to temporarily return SIGINT to its default handler. */
319
320 using scoped_default_sigint = scoped_default_signal<SIGINT>;
321
322 /* Functions that iterate over all extension languages.
323 These only iterate over external extension languages, not including
324 GDB's own extension/scripting language, unless otherwise indicated. */
325
326 /* Wrapper to call the extension_language_ops.initialize "method" for each
327 compiled-in extension language. */
328
329 void
330 ext_lang_initialization (void)
331 {
332 for (const struct extension_language_defn *extlang : extension_languages)
333 {
334 if (extlang->ops != nullptr
335 && extlang->ops->initialize != NULL)
336 {
337 scoped_default_sigint set_sigint_to_default_handler;
338 extlang->ops->initialize (extlang);
339 }
340 }
341 }
342
343 /* See extension.h. */
344
345 void
346 ext_lang_shutdown ()
347 {
348 for (const struct extension_language_defn *extlang : extension_languages)
349 {
350 if (extlang->ops != nullptr && extlang->ops->shutdown != nullptr)
351 extlang->ops->shutdown (extlang);
352 }
353 }
354
355 /* Invoke the appropriate extension_language_ops.eval_from_control_command
356 method to perform CMD, which is a list of commands in an extension language.
357
358 This function is what implements, for example:
359
360 python
361 print 42
362 end
363
364 in a GDB script. */
365
366 void
367 eval_ext_lang_from_control_command (struct command_line *cmd)
368 {
369 for (const struct extension_language_defn *extlang : extension_languages)
370 {
371 if (extlang->cli_control_type == cmd->control_type)
372 {
373 if (extlang->ops != NULL
374 && extlang->ops->eval_from_control_command != NULL)
375 {
376 extlang->ops->eval_from_control_command (extlang, cmd);
377 return;
378 }
379 /* The requested extension language is not supported in this GDB. */
380 throw_ext_lang_unsupported (extlang);
381 }
382 }
383
384 gdb_assert_not_reached ("unknown extension language in command_line");
385 }
386
387 /* Search for and load scripts for OBJFILE written in extension languages.
388 This includes GDB's own scripting language.
389
390 This function is what implements the loading of OBJFILE-gdb.py and
391 OBJFILE-gdb.gdb. */
392
393 void
394 auto_load_ext_lang_scripts_for_objfile (struct objfile *objfile)
395 {
396 const struct extension_language_defn *gdb = &extension_language_gdb;
397 if (ext_lang_auto_load_enabled (gdb))
398 auto_load_objfile_script (objfile, gdb);
399
400 for (const struct extension_language_defn *extlang : extension_languages)
401 {
402 if (extlang->ops != nullptr
403 && ext_lang_auto_load_enabled (extlang))
404 auto_load_objfile_script (objfile, extlang);
405 }
406 }
407 \f
408 /* Interface to type pretty-printers implemented in an extension language. */
409
410 /* Call this at the start when preparing to pretty-print a type.
411 The result is a pointer to an opaque object (to the caller) to be passed
412 to apply_ext_lang_type_printers and free_ext_lang_type_printers.
413
414 We don't know in advance which extension language will provide a
415 pretty-printer for the type, so all are initialized. */
416
417 ext_lang_type_printers::ext_lang_type_printers ()
418 {
419 for (const struct extension_language_defn *extlang : extension_languages)
420 {
421 if (extlang->ops != nullptr
422 && extlang->ops->start_type_printers != NULL)
423 extlang->ops->start_type_printers (extlang, this);
424 }
425 }
426
427 /* Iteratively try the type pretty-printers specified by PRINTERS
428 according to the standard search order (specified by extension_languages),
429 returning the result of the first one that succeeds.
430 If there was an error, or if no printer succeeds, then NULL is returned. */
431
432 gdb::unique_xmalloc_ptr<char>
433 apply_ext_lang_type_printers (struct ext_lang_type_printers *printers,
434 struct type *type)
435 {
436 for (const struct extension_language_defn *extlang : extension_languages)
437 {
438 gdb::unique_xmalloc_ptr<char> result;
439 enum ext_lang_rc rc;
440
441 if (extlang->ops == nullptr
442 || extlang->ops->apply_type_printers == NULL)
443 continue;
444 rc = extlang->ops->apply_type_printers (extlang, printers, type,
445 &result);
446 switch (rc)
447 {
448 case EXT_LANG_RC_OK:
449 gdb_assert (result != nullptr);
450 return result;
451 case EXT_LANG_RC_ERROR:
452 return NULL;
453 case EXT_LANG_RC_NOP:
454 break;
455 default:
456 gdb_assert_not_reached ("bad return from apply_type_printers");
457 }
458 }
459
460 return NULL;
461 }
462
463 ext_lang_type_printers::~ext_lang_type_printers ()
464 {
465 for (const struct extension_language_defn *extlang : extension_languages)
466 {
467 if (extlang->ops != nullptr
468 && extlang->ops->free_type_printers != NULL)
469 extlang->ops->free_type_printers (extlang, this);
470 }
471 }
472 \f
473 /* Try to pretty-print a value onto stdio stream STREAM according to
474 OPTIONS. VAL is the object to print. Returns non-zero if the
475 value was successfully pretty-printed.
476
477 Extension languages are tried in the order specified by
478 extension_languages. The first one to provide a pretty-printed
479 value "wins".
480
481 If an error is encountered in a pretty-printer, no further extension
482 languages are tried.
483 Note: This is different than encountering a memory error trying to read a
484 value for pretty-printing. Here we're referring to, e.g., programming
485 errors that trigger an exception in the extension language. */
486
487 int
488 apply_ext_lang_val_pretty_printer (struct value *val,
489 struct ui_file *stream, int recurse,
490 const struct value_print_options *options,
491 const struct language_defn *language)
492 {
493 for (const struct extension_language_defn *extlang : extension_languages)
494 {
495 enum ext_lang_rc rc;
496
497 if (extlang->ops == nullptr
498 || extlang->ops->apply_val_pretty_printer == NULL)
499 continue;
500 rc = extlang->ops->apply_val_pretty_printer (extlang, val, stream,
501 recurse, options, language);
502 switch (rc)
503 {
504 case EXT_LANG_RC_OK:
505 return 1;
506 case EXT_LANG_RC_ERROR:
507 return 0;
508 case EXT_LANG_RC_NOP:
509 break;
510 default:
511 gdb_assert_not_reached ("bad return from apply_val_pretty_printer");
512 }
513 }
514
515 return 0;
516 }
517
518 /* GDB access to the "frame filter" feature.
519 FRAME is the source frame to start frame-filter invocation. FLAGS is an
520 integer holding the flags for printing. The following elements of
521 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
522 PRINT_LEVEL is a flag indicating whether to print the frame's
523 relative level in the output. PRINT_FRAME_INFO is a flag that
524 indicates whether this function should print the frame
525 information, PRINT_ARGS is a flag that indicates whether to print
526 frame arguments, and PRINT_LOCALS, likewise, with frame local
527 variables. ARGS_TYPE is an enumerator describing the argument
528 format, OUT is the output stream to print. FRAME_LOW is the
529 beginning of the slice of frames to print, and FRAME_HIGH is the
530 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
531 or EXT_LANG_BT_COMPLETED on success.
532
533 Extension languages are tried in the order specified by
534 extension_languages. The first one to provide a filter "wins".
535 If there is an error (EXT_LANG_BT_ERROR) it is reported immediately
536 rather than trying filters in other extension languages. */
537
538 enum ext_lang_bt_status
539 apply_ext_lang_frame_filter (const frame_info_ptr &frame,
540 frame_filter_flags flags,
541 enum ext_lang_frame_args args_type,
542 struct ui_out *out,
543 int frame_low, int frame_high)
544 {
545 for (const struct extension_language_defn *extlang : extension_languages)
546 {
547 enum ext_lang_bt_status status;
548
549 if (extlang->ops == nullptr
550 || extlang->ops->apply_frame_filter == NULL)
551 continue;
552 status = extlang->ops->apply_frame_filter (extlang, frame, flags,
553 args_type, out,
554 frame_low, frame_high);
555 /* We use the filters from the first extension language that has
556 applicable filters. Also, an error is reported immediately
557 rather than continue trying. */
558 if (status != EXT_LANG_BT_NO_FILTERS)
559 return status;
560 }
561
562 return EXT_LANG_BT_NO_FILTERS;
563 }
564
565 /* Update values held by the extension language when OBJFILE is discarded.
566 New global types must be created for every such value, which must then be
567 updated to use the new types.
568 The function typically just iterates over all appropriate values and
569 calls preserve_one_value for each one.
570 COPIED_TYPES is used to prevent cycles / duplicates and is passed to
571 preserve_one_value. */
572
573 void
574 preserve_ext_lang_values (struct objfile *objfile, htab_t copied_types)
575 {
576 for (const struct extension_language_defn *extlang : extension_languages)
577 {
578 if (extlang->ops != nullptr
579 && extlang->ops->preserve_values != NULL)
580 extlang->ops->preserve_values (extlang, objfile, copied_types);
581 }
582 }
583
584 /* If there is a stop condition implemented in an extension language for
585 breakpoint B, return a pointer to the extension language's definition.
586 Otherwise return NULL.
587 If SKIP_LANG is not EXT_LANG_NONE, skip checking this language.
588 This is for the case where we're setting a new condition: Only one
589 condition is allowed, so when setting a condition for any particular
590 extension language, we need to check if any other extension language
591 already has a condition set. */
592
593 const struct extension_language_defn *
594 get_breakpoint_cond_ext_lang (struct breakpoint *b,
595 enum extension_language skip_lang)
596 {
597 for (const struct extension_language_defn *extlang : extension_languages)
598 {
599 if (extlang->ops != nullptr
600 && extlang->language != skip_lang
601 && extlang->ops->breakpoint_has_cond != NULL
602 && extlang->ops->breakpoint_has_cond (extlang, b))
603 return extlang;
604 }
605
606 return NULL;
607 }
608
609 /* Return whether a stop condition for breakpoint B says to stop.
610 True is also returned if there is no stop condition for B. */
611
612 bool
613 breakpoint_ext_lang_cond_says_stop (struct breakpoint *b)
614 {
615 enum ext_lang_bp_stop stop = EXT_LANG_BP_STOP_UNSET;
616
617 for (const struct extension_language_defn *extlang : extension_languages)
618 {
619 /* There is a rule that a breakpoint can have at most one of any of a
620 CLI or extension language condition. However, Python hacks in "finish
621 breakpoints" on top of the "stop" check, so we have to call this for
622 every language, even if we could first determine whether a "stop"
623 method exists. */
624 if (extlang->ops != nullptr
625 && extlang->ops->breakpoint_cond_says_stop != NULL)
626 {
627 enum ext_lang_bp_stop this_stop
628 = extlang->ops->breakpoint_cond_says_stop (extlang, b);
629
630 if (this_stop != EXT_LANG_BP_STOP_UNSET)
631 {
632 /* Even though we have to check every extension language, only
633 one of them can return yes/no (because only one of them
634 can have a "stop" condition). */
635 gdb_assert (stop == EXT_LANG_BP_STOP_UNSET);
636 stop = this_stop;
637 }
638 }
639 }
640
641 return stop != EXT_LANG_BP_STOP_NO;
642 }
643 \f
644 /* ^C/SIGINT support.
645 This requires cooperation with the extension languages so the support
646 is defined here. */
647
648 #if CXX_STD_THREAD
649
650 #include <mutex>
651
652 /* DAP needs a way to interrupt the main thread, so we added
653 gdb.interrupt. However, as this can run from any thread, we need
654 locking for the current extension language. If threading is not
655 available, DAP will not start.
656
657 This lock is held for accesses to quit_flag, active_ext_lang, and
658 cooperative_sigint_handling_disabled. */
659 static std::recursive_mutex ext_lang_mutex;
660
661 #endif /* CXX_STD_THREAD */
662
663 /* This flag tracks quit requests when we haven't called out to an
664 extension language. it also holds quit requests when we transition to
665 an extension language that doesn't have cooperative SIGINT handling. */
666 static int quit_flag;
667
668 /* The current extension language we've called out to, or
669 extension_language_gdb if there isn't one.
670 This must be set everytime we call out to an extension language, and reset
671 to the previous value when it returns. Note that the previous value may
672 be a different (or the same) extension language. */
673 static const struct extension_language_defn *active_ext_lang
674 = &extension_language_gdb;
675
676 /* Install a SIGINT handler. */
677
678 static void
679 install_ext_sigint_handler (const struct signal_handler *handler_state)
680 {
681 gdb_assert (handler_state->handler_saved);
682
683 install_sigint_handler (handler_state->handler);
684 }
685
686 /* Install GDB's SIGINT handler, storing the previous version in *PREVIOUS.
687 As a simple optimization, if the previous version was GDB's SIGINT handler
688 then mark the previous handler as not having been saved, and thus it won't
689 be restored. */
690
691 static void
692 install_gdb_sigint_handler (struct signal_handler *previous)
693 {
694 /* Save here to simplify comparison. */
695 sighandler_t handle_sigint_for_compare = handle_sigint;
696
697 previous->handler = install_sigint_handler (handle_sigint);
698 if (previous->handler != handle_sigint_for_compare)
699 previous->handler_saved = 1;
700 else
701 previous->handler_saved = 0;
702 }
703
704 #if GDB_SELF_TEST
705 namespace selftests {
706 void (*hook_set_active_ext_lang) () = nullptr;
707 }
708 #endif
709
710 /* True if cooperative SIGINT handling is disabled. This is needed so
711 that calls to set_active_ext_lang do not re-enable cooperative
712 handling, which if enabled would make set_quit_flag store the
713 SIGINT in an extension language. */
714 static bool cooperative_sigint_handling_disabled = false;
715
716 scoped_disable_cooperative_sigint_handling::scoped_disable_cooperative_sigint_handling ()
717 {
718 #if CXX_STD_THREAD
719 std::lock_guard guard (ext_lang_mutex);
720 #endif /* CXX_STD_THREAD */
721
722 /* Force the active extension language to the GDB scripting
723 language. This ensures that a previously saved SIGINT is moved
724 to the quit_flag global, as well as ensures that future SIGINTs
725 are also saved in the global. */
726 m_prev_active_ext_lang_state
727 = set_active_ext_lang (&extension_language_gdb);
728
729 /* Set the "cooperative SIGINT handling disabled" global flag, so
730 that a future call to set_active_ext_lang does not re-enable
731 cooperative mode. */
732 m_prev_cooperative_sigint_handling_disabled
733 = cooperative_sigint_handling_disabled;
734 cooperative_sigint_handling_disabled = true;
735 }
736
737 scoped_disable_cooperative_sigint_handling::~scoped_disable_cooperative_sigint_handling ()
738 {
739 #if CXX_STD_THREAD
740 std::lock_guard guard (ext_lang_mutex);
741 #endif /* CXX_STD_THREAD */
742
743 cooperative_sigint_handling_disabled = m_prev_cooperative_sigint_handling_disabled;
744 restore_active_ext_lang (m_prev_active_ext_lang_state);
745 }
746
747 /* Set the currently active extension language to NOW_ACTIVE.
748 The result is a pointer to a malloc'd block of memory to pass to
749 restore_active_ext_lang.
750
751 N.B. This function must be called every time we call out to an extension
752 language, and the result must be passed to restore_active_ext_lang
753 afterwards.
754
755 If there is a pending SIGINT it is "moved" to the now active extension
756 language, if it supports cooperative SIGINT handling (i.e., it provides
757 {clear,set,check}_quit_flag methods). If the extension language does not
758 support cooperative SIGINT handling, then the SIGINT is left queued and
759 we require the non-cooperative extension language to call check_quit_flag
760 at appropriate times.
761 It is important for the extension language to call check_quit_flag if it
762 installs its own SIGINT handler to prevent the situation where a SIGINT
763 is queued on entry, extension language code runs for a "long" time possibly
764 serving one or more SIGINTs, and then returns. Upon return, if
765 check_quit_flag is not called, the original SIGINT will be thrown.
766 Non-cooperative extension languages are free to install their own SIGINT
767 handler but the original must be restored upon return, either itself
768 or via restore_active_ext_lang.
769
770 If cooperative SIGINT handling is force-disabled (e.g., we're in
771 the middle of handling an inferior event), then we don't actually
772 record NOW_ACTIVE as the current active extension language, so that
773 set_quit_flag saves the SIGINT in the global quit flag instead of
774 in the extension language. The caller does not need to concern
775 itself about this, though. The currently active extension language
776 concept only exists for cooperative SIGINT handling. */
777
778 struct active_ext_lang_state *
779 set_active_ext_lang (const struct extension_language_defn *now_active)
780 {
781 #if CXX_STD_THREAD
782 std::lock_guard guard (ext_lang_mutex);
783 #endif /* CXX_STD_THREAD */
784
785 #if GDB_SELF_TEST
786 if (selftests::hook_set_active_ext_lang)
787 selftests::hook_set_active_ext_lang ();
788 #endif
789
790 /* If cooperative SIGINT handling was previously force-disabled,
791 make sure to not re-enable it (as NOW_ACTIVE could be a language
792 that supports cooperative SIGINT handling). */
793 if (cooperative_sigint_handling_disabled)
794 {
795 /* Ensure set_quit_flag saves SIGINT in the quit_flag
796 global. */
797 gdb_assert (active_ext_lang->ops == nullptr
798 || active_ext_lang->ops->check_quit_flag == nullptr);
799
800 /* The only thing the caller can do with the result is pass it
801 to restore_active_ext_lang, which expects NULL when
802 cooperative SIGINT handling is disabled. */
803 return nullptr;
804 }
805
806 struct active_ext_lang_state *previous
807 = XCNEW (struct active_ext_lang_state);
808
809 previous->ext_lang = active_ext_lang;
810 previous->sigint_handler.handler_saved = 0;
811 active_ext_lang = now_active;
812
813 if (target_terminal::is_ours ())
814 {
815 /* If the newly active extension language uses cooperative SIGINT
816 handling then ensure GDB's SIGINT handler is installed. */
817 if (now_active->language == EXT_LANG_GDB
818 || now_active->ops->check_quit_flag != NULL)
819 install_gdb_sigint_handler (&previous->sigint_handler);
820
821 /* If there's a SIGINT recorded in the cooperative extension languages,
822 move it to the new language, or save it in GDB's global flag if the
823 newly active extension language doesn't use cooperative SIGINT
824 handling. */
825 if (check_quit_flag ())
826 set_quit_flag ();
827 }
828
829 return previous;
830 }
831
832 /* Restore active extension language from PREVIOUS. */
833
834 void
835 restore_active_ext_lang (struct active_ext_lang_state *previous)
836 {
837 #if CXX_STD_THREAD
838 std::lock_guard guard (ext_lang_mutex);
839 #endif /* CXX_STD_THREAD */
840
841 if (cooperative_sigint_handling_disabled)
842 {
843 /* See set_active_ext_lang. */
844 gdb_assert (previous == nullptr);
845 return;
846 }
847
848 active_ext_lang = previous->ext_lang;
849
850 if (target_terminal::is_ours ())
851 {
852 /* Restore the previous SIGINT handler if one was saved. */
853 if (previous->sigint_handler.handler_saved)
854 install_ext_sigint_handler (&previous->sigint_handler);
855
856 /* If there's a SIGINT recorded in the cooperative extension languages,
857 move it to the new language, or save it in GDB's global flag if the
858 newly active extension language doesn't use cooperative SIGINT
859 handling. */
860 if (check_quit_flag ())
861 set_quit_flag ();
862 }
863 xfree (previous);
864 }
865
866 /* See extension.h. */
867
868 void
869 set_quit_flag ()
870 {
871 #if CXX_STD_THREAD
872 std::lock_guard guard (ext_lang_mutex);
873 #endif /* CXX_STD_THREAD */
874
875 if (active_ext_lang->ops != NULL
876 && active_ext_lang->ops->set_quit_flag != NULL)
877 active_ext_lang->ops->set_quit_flag (active_ext_lang);
878 else
879 {
880 quit_flag = 1;
881
882 /* Now wake up the event loop, or any interruptible_select. Do
883 this after setting the flag, because signals on Windows
884 actually run on a separate thread, and thus otherwise the
885 main code could be woken up and find quit_flag still
886 clear. */
887 quit_serial_event_set ();
888 }
889 }
890
891 /* See extension.h. */
892
893 int
894 check_quit_flag ()
895 {
896 #if CXX_STD_THREAD
897 std::lock_guard guard (ext_lang_mutex);
898 #endif /* CXX_STD_THREAD */
899
900 int result = 0;
901
902 for (const struct extension_language_defn *extlang : extension_languages)
903 {
904 if (extlang->ops != nullptr
905 && extlang->ops->check_quit_flag != NULL)
906 if (extlang->ops->check_quit_flag (extlang) != 0)
907 result = 1;
908 }
909
910 /* This is written in a particular way to avoid races. */
911 if (quit_flag)
912 {
913 /* No longer need to wake up the event loop or any
914 interruptible_select. The caller handles the quit
915 request. */
916 quit_serial_event_clear ();
917 quit_flag = 0;
918 result = 1;
919 }
920
921 return result;
922 }
923
924 /* See extension.h. */
925
926 void
927 get_matching_xmethod_workers (struct type *type, const char *method_name,
928 std::vector<xmethod_worker_up> *workers)
929 {
930 for (const struct extension_language_defn *extlang : extension_languages)
931 {
932 enum ext_lang_rc rc;
933
934 /* If an extension language does not support xmethods, ignore
935 it. */
936 if (extlang->ops == nullptr
937 || extlang->ops->get_matching_xmethod_workers == NULL)
938 continue;
939
940 rc = extlang->ops->get_matching_xmethod_workers (extlang,
941 type, method_name,
942 workers);
943 if (rc == EXT_LANG_RC_ERROR)
944 error (_("Error while looking for matching xmethod workers "
945 "defined in %s."), extlang->capitalized_name);
946 }
947 }
948
949 /* See extension.h. */
950
951 std::vector<type *>
952 xmethod_worker::get_arg_types ()
953 {
954 std::vector<type *> type_array;
955
956 ext_lang_rc rc = do_get_arg_types (&type_array);
957 if (rc == EXT_LANG_RC_ERROR)
958 error (_("Error while looking for arg types of a xmethod worker "
959 "defined in %s."), m_extlang->capitalized_name);
960
961 return type_array;
962 }
963
964 /* See extension.h. */
965
966 struct type *
967 xmethod_worker::get_result_type (value *object, gdb::array_view<value *> args)
968 {
969 type *result_type;
970
971 ext_lang_rc rc = do_get_result_type (object, args, &result_type);
972 if (rc == EXT_LANG_RC_ERROR)
973 {
974 error (_("Error while fetching result type of an xmethod worker "
975 "defined in %s."), m_extlang->capitalized_name);
976 }
977
978 return result_type;
979 }
980
981 /* See extension.h. */
982
983 std::optional<std::string>
984 ext_lang_colorize (const std::string &filename, const std::string &contents)
985 {
986 std::optional<std::string> result;
987
988 for (const struct extension_language_defn *extlang : extension_languages)
989 {
990 if (extlang->ops == nullptr
991 || extlang->ops->colorize == nullptr)
992 continue;
993 result = extlang->ops->colorize (filename, contents);
994 if (result.has_value ())
995 return result;
996 }
997
998 return result;
999 }
1000
1001 /* See extension.h. */
1002
1003 std::optional<std::string>
1004 ext_lang_colorize_disasm (const std::string &content, gdbarch *gdbarch)
1005 {
1006 std::optional<std::string> result;
1007
1008 for (const struct extension_language_defn *extlang : extension_languages)
1009 {
1010 if (extlang->ops == nullptr
1011 || extlang->ops->colorize_disasm == nullptr)
1012 continue;
1013 result = extlang->ops->colorize_disasm (content, gdbarch);
1014 if (result.has_value ())
1015 return result;
1016 }
1017
1018 return result;
1019 }
1020
1021 /* See extension.h. */
1022
1023 std::optional<int>
1024 ext_lang_print_insn (struct gdbarch *gdbarch, CORE_ADDR address,
1025 struct disassemble_info *info)
1026 {
1027 for (const struct extension_language_defn *extlang : extension_languages)
1028 {
1029 if (extlang->ops == nullptr
1030 || extlang->ops->print_insn == nullptr)
1031 continue;
1032 std::optional<int> length
1033 = extlang->ops->print_insn (gdbarch, address, info);
1034 if (length.has_value ())
1035 return length;
1036 }
1037
1038 return {};
1039 }
1040
1041 /* See extension.h. */
1042
1043 ext_lang_missing_debuginfo_result
1044 ext_lang_handle_missing_debuginfo (struct objfile *objfile)
1045 {
1046 for (const struct extension_language_defn *extlang : extension_languages)
1047 {
1048 if (extlang->ops == nullptr
1049 || extlang->ops->handle_missing_debuginfo == nullptr)
1050 continue;
1051 ext_lang_missing_debuginfo_result result
1052 = extlang->ops->handle_missing_debuginfo (extlang, objfile);
1053 if (!result.filename ().empty () || result.try_again ())
1054 return result;
1055 }
1056
1057 return {};
1058 }
1059
1060 /* Called via an observer before gdb prints its prompt.
1061 Iterate over the extension languages giving them a chance to
1062 change the prompt. The first one to change the prompt wins,
1063 and no further languages are tried. */
1064
1065 static void
1066 ext_lang_before_prompt (const char *current_gdb_prompt)
1067 {
1068 for (const struct extension_language_defn *extlang : extension_languages)
1069 {
1070 enum ext_lang_rc rc;
1071
1072 if (extlang->ops == nullptr
1073 || extlang->ops->before_prompt == NULL)
1074 continue;
1075 rc = extlang->ops->before_prompt (extlang, current_gdb_prompt);
1076 switch (rc)
1077 {
1078 case EXT_LANG_RC_OK:
1079 case EXT_LANG_RC_ERROR:
1080 return;
1081 case EXT_LANG_RC_NOP:
1082 break;
1083 default:
1084 gdb_assert_not_reached ("bad return from before_prompt");
1085 }
1086 }
1087 }
1088
1089 void _initialize_extension ();
1090 void
1091 _initialize_extension ()
1092 {
1093 gdb::observers::before_prompt.attach (ext_lang_before_prompt, "extension");
1094 }