Automatic date update in version.in
[binutils-gdb.git] / gdb / extension.c
blob5a805bea00e1e58a4d0440f803ffe8f4a521279b
1 /* Interface between gdb and its extension languages.
3 Copyright (C) 2014-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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. */
23 #include "defs.h"
24 #include <signal.h>
25 #include "target.h"
26 #include "auto-load.h"
27 #include "breakpoint.h"
28 #include "event-top.h"
29 #include "extension.h"
30 #include "extension-priv.h"
31 #include "observable.h"
32 #include "cli/cli-script.h"
33 #include "python/python.h"
34 #include "guile/guile.h"
35 #include <array>
37 static script_sourcer_func source_gdb_script;
38 static objfile_script_sourcer_func source_gdb_objfile_script;
40 /* GDB's own scripting language.
41 This exists, in part, to support auto-loading ${prog}-gdb.gdb scripts. */
43 static const struct extension_language_script_ops
44 extension_language_gdb_script_ops =
46 source_gdb_script,
47 source_gdb_objfile_script,
48 NULL, /* objfile_script_executor */
49 auto_load_gdb_scripts_enabled
52 const struct extension_language_defn extension_language_gdb =
54 EXT_LANG_GDB,
55 "gdb",
56 "GDB",
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",
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,
68 &extension_language_gdb_script_ops,
70 /* The rest of the extension language interface isn't supported by GDB's own
71 extension/scripting language. */
72 NULL
75 /* NULL-terminated table of all external (non-native) extension languages.
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. */
85 static const std::array<const extension_language_defn *, 2> extension_languages
87 /* To preserve existing behaviour, python should always appear first. */
88 &extension_language_python,
89 &extension_language_guile,
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. */
97 const struct extension_language_defn *
98 get_ext_lang_defn (enum extension_language lang)
100 gdb_assert (lang != EXT_LANG_NONE);
102 if (lang == EXT_LANG_GDB)
103 return &extension_language_gdb;
105 for (const struct extension_language_defn *extlang : extension_languages)
107 if (extlang->language == lang)
108 return extlang;
111 gdb_assert_not_reached ("unable to find extension_language_defn");
114 /* Return TRUE if FILE has extension EXTENSION. */
116 static int
117 has_extension (const char *file, const char *extension)
119 int file_len = strlen (file);
120 int extension_len = strlen (extension);
122 return (file_len > extension_len
123 && strcmp (&file[file_len - extension_len], extension) == 0);
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. */
130 const struct extension_language_defn *
131 get_ext_lang_of_file (const char *file)
133 if (has_extension (file, extension_language_gdb.suffix))
134 return &extension_language_gdb;
136 for (const struct extension_language_defn *extlang : extension_languages)
138 if (has_extension (file, extlang->suffix))
139 return extlang;
142 return NULL;
145 /* Return non-zero if support for the specified extension language
146 is compiled in. */
149 ext_lang_present_p (const struct extension_language_defn *extlang)
151 return extlang->script_ops != NULL;
154 /* Return non-zero if the specified extension language has successfully
155 initialized. */
158 ext_lang_initialized_p (const struct extension_language_defn *extlang)
160 if (extlang->ops != NULL)
162 /* This method is required. */
163 gdb_assert (extlang->ops->initialized != NULL);
164 return extlang->ops->initialized (extlang);
167 return 0;
170 /* Throw an error indicating EXTLANG is not supported in this copy of GDB. */
172 void
173 throw_ext_lang_unsupported (const struct extension_language_defn *extlang)
175 error (_("Scripting in the \"%s\" language is not supported"
176 " in this copy of GDB."),
177 ext_lang_capitalized_name (extlang));
180 /* Methods for GDB's own extension/scripting language. */
182 /* The extension_language_script_ops.script_sourcer "method". */
184 static void
185 source_gdb_script (const struct extension_language_defn *extlang,
186 FILE *stream, const char *file)
188 script_from_file (stream, file);
191 /* The extension_language_script_ops.objfile_script_sourcer "method". */
193 static void
194 source_gdb_objfile_script (const struct extension_language_defn *extlang,
195 struct objfile *objfile,
196 FILE *stream, const char *file)
198 script_from_file (stream, file);
201 /* Accessors for "public" attributes of struct extension_language. */
203 /* Return the "name" field of EXTLANG. */
205 const char *
206 ext_lang_name (const struct extension_language_defn *extlang)
208 return extlang->name;
211 /* Return the "capitalized_name" field of EXTLANG. */
213 const char *
214 ext_lang_capitalized_name (const struct extension_language_defn *extlang)
216 return extlang->capitalized_name;
219 /* Return the "suffix" field of EXTLANG. */
221 const char *
222 ext_lang_suffix (const struct extension_language_defn *extlang)
224 return extlang->suffix;
227 /* Return the "auto_load_suffix" field of EXTLANG. */
229 const char *
230 ext_lang_auto_load_suffix (const struct extension_language_defn *extlang)
232 return extlang->auto_load_suffix;
235 /* extension_language_script_ops wrappers. */
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. */
241 script_sourcer_func *
242 ext_lang_script_sourcer (const struct extension_language_defn *extlang)
244 if (extlang->script_ops == NULL)
245 return NULL;
247 /* The extension language is required to implement this function. */
248 gdb_assert (extlang->script_ops->script_sourcer != NULL);
250 return extlang->script_ops->script_sourcer;
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. */
258 objfile_script_sourcer_func *
259 ext_lang_objfile_script_sourcer (const struct extension_language_defn *extlang)
261 if (extlang->script_ops == NULL)
262 return NULL;
264 /* The extension language is required to implement this function. */
265 gdb_assert (extlang->script_ops->objfile_script_sourcer != NULL);
267 return extlang->script_ops->objfile_script_sourcer;
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. */
275 objfile_script_executor_func *
276 ext_lang_objfile_script_executor
277 (const struct extension_language_defn *extlang)
279 if (extlang->script_ops == NULL)
280 return NULL;
282 return extlang->script_ops->objfile_script_executor;
285 /* See extension.h. */
287 bool
288 ext_lang_auto_load_enabled (const struct extension_language_defn *extlang)
290 if (extlang->script_ops == NULL)
291 return false;
293 /* The extension language is required to implement this function. */
294 gdb_assert (extlang->script_ops->auto_load_enabled != NULL);
296 return extlang->script_ops->auto_load_enabled (extlang);
300 /* RAII class used to temporarily return SIG to its default handler. */
302 template<int SIG>
303 struct scoped_default_signal
305 scoped_default_signal ()
306 { m_old_sig_handler = signal (SIG, SIG_DFL); }
308 ~scoped_default_signal ()
309 { signal (SIG, m_old_sig_handler); }
311 DISABLE_COPY_AND_ASSIGN (scoped_default_signal);
313 private:
314 /* The previous signal handler that needs to be restored. */
315 sighandler_t m_old_sig_handler;
318 /* Class to temporarily return SIGINT to its default handler. */
320 using scoped_default_sigint = scoped_default_signal<SIGINT>;
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. */
326 /* Wrapper to call the extension_language_ops.initialize "method" for each
327 compiled-in extension language. */
329 void
330 ext_lang_initialization (void)
332 for (const struct extension_language_defn *extlang : extension_languages)
334 if (extlang->ops != nullptr
335 && extlang->ops->initialize != NULL)
337 scoped_default_sigint set_sigint_to_default_handler;
338 extlang->ops->initialize (extlang);
343 /* Invoke the appropriate extension_language_ops.eval_from_control_command
344 method to perform CMD, which is a list of commands in an extension language.
346 This function is what implements, for example:
348 python
349 print 42
352 in a GDB script. */
354 void
355 eval_ext_lang_from_control_command (struct command_line *cmd)
357 for (const struct extension_language_defn *extlang : extension_languages)
359 if (extlang->cli_control_type == cmd->control_type)
361 if (extlang->ops != NULL
362 && extlang->ops->eval_from_control_command != NULL)
364 extlang->ops->eval_from_control_command (extlang, cmd);
365 return;
367 /* The requested extension language is not supported in this GDB. */
368 throw_ext_lang_unsupported (extlang);
372 gdb_assert_not_reached ("unknown extension language in command_line");
375 /* Search for and load scripts for OBJFILE written in extension languages.
376 This includes GDB's own scripting language.
378 This function is what implements the loading of OBJFILE-gdb.py and
379 OBJFILE-gdb.gdb. */
381 void
382 auto_load_ext_lang_scripts_for_objfile (struct objfile *objfile)
384 const struct extension_language_defn *gdb = &extension_language_gdb;
385 if (ext_lang_auto_load_enabled (gdb))
386 auto_load_objfile_script (objfile, gdb);
388 for (const struct extension_language_defn *extlang : extension_languages)
390 if (extlang->ops != nullptr
391 && ext_lang_auto_load_enabled (extlang))
392 auto_load_objfile_script (objfile, extlang);
396 /* Interface to type pretty-printers implemented in an extension language. */
398 /* Call this at the start when preparing to pretty-print a type.
399 The result is a pointer to an opaque object (to the caller) to be passed
400 to apply_ext_lang_type_printers and free_ext_lang_type_printers.
402 We don't know in advance which extension language will provide a
403 pretty-printer for the type, so all are initialized. */
405 ext_lang_type_printers::ext_lang_type_printers ()
407 for (const struct extension_language_defn *extlang : extension_languages)
409 if (extlang->ops != nullptr
410 && extlang->ops->start_type_printers != NULL)
411 extlang->ops->start_type_printers (extlang, this);
415 /* Iteratively try the type pretty-printers specified by PRINTERS
416 according to the standard search order (specified by extension_languages),
417 returning the result of the first one that succeeds.
418 If there was an error, or if no printer succeeds, then NULL is returned. */
420 char *
421 apply_ext_lang_type_printers (struct ext_lang_type_printers *printers,
422 struct type *type)
424 for (const struct extension_language_defn *extlang : extension_languages)
426 char *result = NULL;
427 enum ext_lang_rc rc;
429 if (extlang->ops == nullptr
430 || extlang->ops->apply_type_printers == NULL)
431 continue;
432 rc = extlang->ops->apply_type_printers (extlang, printers, type,
433 &result);
434 switch (rc)
436 case EXT_LANG_RC_OK:
437 gdb_assert (result != NULL);
438 return result;
439 case EXT_LANG_RC_ERROR:
440 return NULL;
441 case EXT_LANG_RC_NOP:
442 break;
443 default:
444 gdb_assert_not_reached ("bad return from apply_type_printers");
448 return NULL;
451 ext_lang_type_printers::~ext_lang_type_printers ()
453 for (const struct extension_language_defn *extlang : extension_languages)
455 if (extlang->ops != nullptr
456 && extlang->ops->free_type_printers != NULL)
457 extlang->ops->free_type_printers (extlang, this);
461 /* Try to pretty-print a value onto stdio stream STREAM according to
462 OPTIONS. VAL is the object to print. Returns non-zero if the
463 value was successfully pretty-printed.
465 Extension languages are tried in the order specified by
466 extension_languages. The first one to provide a pretty-printed
467 value "wins".
469 If an error is encountered in a pretty-printer, no further extension
470 languages are tried.
471 Note: This is different than encountering a memory error trying to read a
472 value for pretty-printing. Here we're referring to, e.g., programming
473 errors that trigger an exception in the extension language. */
476 apply_ext_lang_val_pretty_printer (struct value *val,
477 struct ui_file *stream, int recurse,
478 const struct value_print_options *options,
479 const struct language_defn *language)
481 for (const struct extension_language_defn *extlang : extension_languages)
483 enum ext_lang_rc rc;
485 if (extlang->ops == nullptr
486 || extlang->ops->apply_val_pretty_printer == NULL)
487 continue;
488 rc = extlang->ops->apply_val_pretty_printer (extlang, val, stream,
489 recurse, options, language);
490 switch (rc)
492 case EXT_LANG_RC_OK:
493 return 1;
494 case EXT_LANG_RC_ERROR:
495 return 0;
496 case EXT_LANG_RC_NOP:
497 break;
498 default:
499 gdb_assert_not_reached ("bad return from apply_val_pretty_printer");
503 return 0;
506 /* GDB access to the "frame filter" feature.
507 FRAME is the source frame to start frame-filter invocation. FLAGS is an
508 integer holding the flags for printing. The following elements of
509 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
510 PRINT_LEVEL is a flag indicating whether to print the frame's
511 relative level in the output. PRINT_FRAME_INFO is a flag that
512 indicates whether this function should print the frame
513 information, PRINT_ARGS is a flag that indicates whether to print
514 frame arguments, and PRINT_LOCALS, likewise, with frame local
515 variables. ARGS_TYPE is an enumerator describing the argument
516 format, OUT is the output stream to print. FRAME_LOW is the
517 beginning of the slice of frames to print, and FRAME_HIGH is the
518 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
519 or EXT_LANG_BT_COMPLETED on success.
521 Extension languages are tried in the order specified by
522 extension_languages. The first one to provide a filter "wins".
523 If there is an error (EXT_LANG_BT_ERROR) it is reported immediately
524 rather than trying filters in other extension languages. */
526 enum ext_lang_bt_status
527 apply_ext_lang_frame_filter (struct frame_info *frame,
528 frame_filter_flags flags,
529 enum ext_lang_frame_args args_type,
530 struct ui_out *out,
531 int frame_low, int frame_high)
533 for (const struct extension_language_defn *extlang : extension_languages)
535 enum ext_lang_bt_status status;
537 if (extlang->ops == nullptr
538 || extlang->ops->apply_frame_filter == NULL)
539 continue;
540 status = extlang->ops->apply_frame_filter (extlang, frame, flags,
541 args_type, out,
542 frame_low, frame_high);
543 /* We use the filters from the first extension language that has
544 applicable filters. Also, an error is reported immediately
545 rather than continue trying. */
546 if (status != EXT_LANG_BT_NO_FILTERS)
547 return status;
550 return EXT_LANG_BT_NO_FILTERS;
553 /* Update values held by the extension language when OBJFILE is discarded.
554 New global types must be created for every such value, which must then be
555 updated to use the new types.
556 The function typically just iterates over all appropriate values and
557 calls preserve_one_value for each one.
558 COPIED_TYPES is used to prevent cycles / duplicates and is passed to
559 preserve_one_value. */
561 void
562 preserve_ext_lang_values (struct objfile *objfile, htab_t copied_types)
564 for (const struct extension_language_defn *extlang : extension_languages)
566 if (extlang->ops != nullptr
567 && extlang->ops->preserve_values != NULL)
568 extlang->ops->preserve_values (extlang, objfile, copied_types);
572 /* If there is a stop condition implemented in an extension language for
573 breakpoint B, return a pointer to the extension language's definition.
574 Otherwise return NULL.
575 If SKIP_LANG is not EXT_LANG_NONE, skip checking this language.
576 This is for the case where we're setting a new condition: Only one
577 condition is allowed, so when setting a condition for any particular
578 extension language, we need to check if any other extension language
579 already has a condition set. */
581 const struct extension_language_defn *
582 get_breakpoint_cond_ext_lang (struct breakpoint *b,
583 enum extension_language skip_lang)
585 for (const struct extension_language_defn *extlang : extension_languages)
587 if (extlang->ops != nullptr
588 && extlang->language != skip_lang
589 && extlang->ops->breakpoint_has_cond != NULL
590 && extlang->ops->breakpoint_has_cond (extlang, b))
591 return extlang;
594 return NULL;
597 /* Return whether a stop condition for breakpoint B says to stop.
598 True is also returned if there is no stop condition for B. */
601 breakpoint_ext_lang_cond_says_stop (struct breakpoint *b)
603 enum ext_lang_bp_stop stop = EXT_LANG_BP_STOP_UNSET;
605 for (const struct extension_language_defn *extlang : extension_languages)
607 /* There is a rule that a breakpoint can have at most one of any of a
608 CLI or extension language condition. However, Python hacks in "finish
609 breakpoints" on top of the "stop" check, so we have to call this for
610 every language, even if we could first determine whether a "stop"
611 method exists. */
612 if (extlang->ops != nullptr
613 && extlang->ops->breakpoint_cond_says_stop != NULL)
615 enum ext_lang_bp_stop this_stop
616 = extlang->ops->breakpoint_cond_says_stop (extlang, b);
618 if (this_stop != EXT_LANG_BP_STOP_UNSET)
620 /* Even though we have to check every extension language, only
621 one of them can return yes/no (because only one of them
622 can have a "stop" condition). */
623 gdb_assert (stop == EXT_LANG_BP_STOP_UNSET);
624 stop = this_stop;
629 return stop == EXT_LANG_BP_STOP_NO ? 0 : 1;
632 /* ^C/SIGINT support.
633 This requires cooperation with the extension languages so the support
634 is defined here. */
636 /* This flag tracks quit requests when we haven't called out to an
637 extension language. it also holds quit requests when we transition to
638 an extension language that doesn't have cooperative SIGINT handling. */
639 static int quit_flag;
641 /* The current extension language we've called out to, or
642 extension_language_gdb if there isn't one.
643 This must be set everytime we call out to an extension language, and reset
644 to the previous value when it returns. Note that the previous value may
645 be a different (or the same) extension language. */
646 static const struct extension_language_defn *active_ext_lang
647 = &extension_language_gdb;
649 /* Return the currently active extension language. */
651 const struct extension_language_defn *
652 get_active_ext_lang (void)
654 return active_ext_lang;
657 /* Install a SIGINT handler. */
659 static void
660 install_sigint_handler (const struct signal_handler *handler_state)
662 gdb_assert (handler_state->handler_saved);
664 signal (SIGINT, handler_state->handler);
667 /* Install GDB's SIGINT handler, storing the previous version in *PREVIOUS.
668 As a simple optimization, if the previous version was GDB's SIGINT handler
669 then mark the previous handler as not having been saved, and thus it won't
670 be restored. */
672 static void
673 install_gdb_sigint_handler (struct signal_handler *previous)
675 /* Save here to simplify comparison. */
676 sighandler_t handle_sigint_for_compare = handle_sigint;
678 previous->handler = signal (SIGINT, handle_sigint);
679 if (previous->handler != handle_sigint_for_compare)
680 previous->handler_saved = 1;
681 else
682 previous->handler_saved = 0;
685 #if GDB_SELF_TEST
686 namespace selftests {
687 void (*hook_set_active_ext_lang) () = nullptr;
689 #endif
691 /* Set the currently active extension language to NOW_ACTIVE.
692 The result is a pointer to a malloc'd block of memory to pass to
693 restore_active_ext_lang.
695 N.B. This function must be called every time we call out to an extension
696 language, and the result must be passed to restore_active_ext_lang
697 afterwards.
699 If there is a pending SIGINT it is "moved" to the now active extension
700 language, if it supports cooperative SIGINT handling (i.e., it provides
701 {clear,set,check}_quit_flag methods). If the extension language does not
702 support cooperative SIGINT handling, then the SIGINT is left queued and
703 we require the non-cooperative extension language to call check_quit_flag
704 at appropriate times.
705 It is important for the extension language to call check_quit_flag if it
706 installs its own SIGINT handler to prevent the situation where a SIGINT
707 is queued on entry, extension language code runs for a "long" time possibly
708 serving one or more SIGINTs, and then returns. Upon return, if
709 check_quit_flag is not called, the original SIGINT will be thrown.
710 Non-cooperative extension languages are free to install their own SIGINT
711 handler but the original must be restored upon return, either itself
712 or via restore_active_ext_lang. */
714 struct active_ext_lang_state *
715 set_active_ext_lang (const struct extension_language_defn *now_active)
717 #if GDB_SELF_TEST
718 if (selftests::hook_set_active_ext_lang)
719 selftests::hook_set_active_ext_lang ();
720 #endif
722 struct active_ext_lang_state *previous
723 = XCNEW (struct active_ext_lang_state);
725 previous->ext_lang = active_ext_lang;
726 previous->sigint_handler.handler_saved = 0;
727 active_ext_lang = now_active;
729 if (target_terminal::is_ours ())
731 /* If the newly active extension language uses cooperative SIGINT
732 handling then ensure GDB's SIGINT handler is installed. */
733 if (now_active->language == EXT_LANG_GDB
734 || now_active->ops->check_quit_flag != NULL)
735 install_gdb_sigint_handler (&previous->sigint_handler);
737 /* If there's a SIGINT recorded in the cooperative extension languages,
738 move it to the new language, or save it in GDB's global flag if the
739 newly active extension language doesn't use cooperative SIGINT
740 handling. */
741 if (check_quit_flag ())
742 set_quit_flag ();
745 return previous;
748 /* Restore active extension language from PREVIOUS. */
750 void
751 restore_active_ext_lang (struct active_ext_lang_state *previous)
753 active_ext_lang = previous->ext_lang;
755 if (target_terminal::is_ours ())
757 /* Restore the previous SIGINT handler if one was saved. */
758 if (previous->sigint_handler.handler_saved)
759 install_sigint_handler (&previous->sigint_handler);
761 /* If there's a SIGINT recorded in the cooperative extension languages,
762 move it to the new language, or save it in GDB's global flag if the
763 newly active extension language doesn't use cooperative SIGINT
764 handling. */
765 if (check_quit_flag ())
766 set_quit_flag ();
768 xfree (previous);
771 /* Set the quit flag.
772 This only sets the flag in the currently active extension language.
773 If the currently active extension language does not have cooperative
774 SIGINT handling, then GDB's global flag is set, and it is up to the
775 extension language to call check_quit_flag. The extension language
776 is free to install its own SIGINT handler, but we still need to handle
777 the transition. */
779 void
780 set_quit_flag (void)
782 if (active_ext_lang->ops != NULL
783 && active_ext_lang->ops->set_quit_flag != NULL)
784 active_ext_lang->ops->set_quit_flag (active_ext_lang);
785 else
787 quit_flag = 1;
789 /* Now wake up the event loop, or any interruptible_select. Do
790 this after setting the flag, because signals on Windows
791 actually run on a separate thread, and thus otherwise the
792 main code could be woken up and find quit_flag still
793 clear. */
794 quit_serial_event_set ();
798 /* Return true if the quit flag has been set, false otherwise.
799 Note: The flag is cleared as a side-effect.
800 The flag is checked in all extension languages that support cooperative
801 SIGINT handling, not just the current one. This simplifies transitions. */
804 check_quit_flag (void)
806 int result = 0;
808 for (const struct extension_language_defn *extlang : extension_languages)
810 if (extlang->ops != nullptr
811 && extlang->ops->check_quit_flag != NULL)
812 if (extlang->ops->check_quit_flag (extlang) != 0)
813 result = 1;
816 /* This is written in a particular way to avoid races. */
817 if (quit_flag)
819 /* No longer need to wake up the event loop or any
820 interruptible_select. The caller handles the quit
821 request. */
822 quit_serial_event_clear ();
823 quit_flag = 0;
824 result = 1;
827 return result;
830 /* See extension.h. */
832 void
833 get_matching_xmethod_workers (struct type *type, const char *method_name,
834 std::vector<xmethod_worker_up> *workers)
836 for (const struct extension_language_defn *extlang : extension_languages)
838 enum ext_lang_rc rc;
840 /* If an extension language does not support xmethods, ignore
841 it. */
842 if (extlang->ops == nullptr
843 || extlang->ops->get_matching_xmethod_workers == NULL)
844 continue;
846 rc = extlang->ops->get_matching_xmethod_workers (extlang,
847 type, method_name,
848 workers);
849 if (rc == EXT_LANG_RC_ERROR)
850 error (_("Error while looking for matching xmethod workers "
851 "defined in %s."), extlang->capitalized_name);
855 /* See extension.h. */
857 std::vector<type *>
858 xmethod_worker::get_arg_types ()
860 std::vector<type *> type_array;
862 ext_lang_rc rc = do_get_arg_types (&type_array);
863 if (rc == EXT_LANG_RC_ERROR)
864 error (_("Error while looking for arg types of a xmethod worker "
865 "defined in %s."), m_extlang->capitalized_name);
867 return type_array;
870 /* See extension.h. */
872 struct type *
873 xmethod_worker::get_result_type (value *object, gdb::array_view<value *> args)
875 type *result_type;
877 ext_lang_rc rc = do_get_result_type (object, args, &result_type);
878 if (rc == EXT_LANG_RC_ERROR)
880 error (_("Error while fetching result type of an xmethod worker "
881 "defined in %s."), m_extlang->capitalized_name);
884 return result_type;
887 /* See extension.h. */
889 gdb::optional<std::string>
890 ext_lang_colorize (const std::string &filename, const std::string &contents)
892 gdb::optional<std::string> result;
894 for (const struct extension_language_defn *extlang : extension_languages)
896 if (extlang->ops == nullptr
897 || extlang->ops->colorize == nullptr)
898 continue;
899 result = extlang->ops->colorize (filename, contents);
900 if (result.has_value ())
901 return result;
904 return result;
907 /* See extension.h. */
909 gdb::optional<std::string>
910 ext_lang_colorize_disasm (const std::string &content, gdbarch *gdbarch)
912 gdb::optional<std::string> result;
914 for (const struct extension_language_defn *extlang : extension_languages)
916 if (extlang->ops == nullptr
917 || extlang->ops->colorize_disasm == nullptr)
918 continue;
919 result = extlang->ops->colorize_disasm (content, gdbarch);
920 if (result.has_value ())
921 return result;
924 return result;
927 /* See extension.h. */
929 gdb::optional<int>
930 ext_lang_print_insn (struct gdbarch *gdbarch, CORE_ADDR address,
931 struct disassemble_info *info)
933 for (const struct extension_language_defn *extlang : extension_languages)
935 if (extlang->ops == nullptr
936 || extlang->ops->print_insn == nullptr)
937 continue;
938 gdb::optional<int> length
939 = extlang->ops->print_insn (gdbarch, address, info);
940 if (length.has_value ())
941 return length;
944 return {};
947 /* Called via an observer before gdb prints its prompt.
948 Iterate over the extension languages giving them a chance to
949 change the prompt. The first one to change the prompt wins,
950 and no further languages are tried. */
952 static void
953 ext_lang_before_prompt (const char *current_gdb_prompt)
955 for (const struct extension_language_defn *extlang : extension_languages)
957 enum ext_lang_rc rc;
959 if (extlang->ops == nullptr
960 || extlang->ops->before_prompt == NULL)
961 continue;
962 rc = extlang->ops->before_prompt (extlang, current_gdb_prompt);
963 switch (rc)
965 case EXT_LANG_RC_OK:
966 case EXT_LANG_RC_ERROR:
967 return;
968 case EXT_LANG_RC_NOP:
969 break;
970 default:
971 gdb_assert_not_reached ("bad return from before_prompt");
976 void _initialize_extension ();
977 void
978 _initialize_extension ()
980 gdb::observers::before_prompt.attach (ext_lang_before_prompt, "extension");