1 /* Interface between gdb and its extension languages.
3 Copyright (C) 2014 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. */
25 #include "auto-load.h"
26 #include "breakpoint.h"
27 #include "event-top.h"
28 #include "extension.h"
29 #include "extension-priv.h"
31 #include "cli/cli-script.h"
32 #include "python/python.h"
33 #include "guile/guile.h"
35 /* Iterate over all external extension languages, regardless of whether the
36 support has been compiled in or not.
37 This does not include GDB's own scripting language. */
39 #define ALL_EXTENSION_LANGUAGES(i, extlang) \
40 for (/*int*/ i = 0, extlang = extension_languages[0]; \
42 extlang = extension_languages[++i])
44 /* Iterate over all external extension languages that are supported.
45 This does not include GDB's own scripting language. */
47 #define ALL_ENABLED_EXTENSION_LANGUAGES(i, extlang) \
48 for (/*int*/ i = 0, extlang = extension_languages[0]; \
50 extlang = extension_languages[++i]) \
51 if (extlang->ops != NULL)
53 static script_sourcer_func source_gdb_script;
54 static objfile_script_sourcer_func source_gdb_objfile_script;
56 /* GDB's own scripting language.
57 This exists, in part, to support auto-loading ${prog}-gdb.gdb scripts. */
59 static const struct extension_language_script_ops
60 extension_language_gdb_script_ops =
63 source_gdb_objfile_script,
64 auto_load_gdb_scripts_enabled
67 const struct extension_language_defn extension_language_gdb =
73 /* We fall back to interpreting a script as a GDB script if it doesn't
74 match the other scripting languages, but for consistency's sake
75 give it a formal suffix. */
79 /* cli_control_type: This is never used: GDB's own scripting language
80 has a variety of control types (if, while, etc.). */
83 &extension_language_gdb_script_ops,
85 /* The rest of the extension language interface isn't supported by GDB's own
86 extension/scripting language. */
90 /* NULL-terminated table of all external (non-native) extension languages.
92 The order of appearance in the table is important.
93 When multiple extension languages provide the same feature, for example
94 a pretty-printer for a particular type, which one gets used?
95 The algorithm employed here is "the first one wins". For example, in
96 the case of pretty-printers this means the first one to provide a
97 pretty-printed value is the one that is used. This algorithm is employed
100 static const struct extension_language_defn * const extension_languages[] =
102 /* To preserve existing behaviour, python should always appear first. */
103 &extension_language_python,
104 &extension_language_guile,
108 /* Return a pointer to the struct extension_language_defn object of
109 extension language LANG.
110 This always returns a non-NULL pointer, even if support for the language
111 is not compiled into this copy of GDB. */
113 const struct extension_language_defn *
114 get_ext_lang_defn (enum extension_language lang)
117 const struct extension_language_defn *extlang;
119 gdb_assert (lang != EXT_LANG_NONE);
121 if (lang == EXT_LANG_GDB)
122 return &extension_language_gdb;
124 ALL_EXTENSION_LANGUAGES (i, extlang)
126 if (extlang->language == lang)
130 gdb_assert_not_reached ("unable to find extension_language_defn");
133 /* Return TRUE if FILE has extension EXTENSION. */
136 has_extension (const char *file, const char *extension)
138 int file_len = strlen (file);
139 int extension_len = strlen (extension);
141 return (file_len > extension_len
142 && strcmp (&file[file_len - extension_len], extension) == 0);
145 /* Return the extension language of FILE, or NULL if
146 the extension language of FILE is not recognized.
147 This is done by looking at the file's suffix. */
149 const struct extension_language_defn *
150 get_ext_lang_of_file (const char *file)
153 const struct extension_language_defn *extlang;
155 ALL_EXTENSION_LANGUAGES (i, extlang)
157 if (has_extension (file, extlang->suffix))
164 /* Return non-zero if support for the specified extension language
168 ext_lang_present_p (const struct extension_language_defn *extlang)
170 return extlang->script_ops != NULL;
173 /* Return non-zero if the specified extension language has successfully
177 ext_lang_initialized_p (const struct extension_language_defn *extlang)
179 if (extlang->ops != NULL)
181 /* This method is required. */
182 gdb_assert (extlang->ops->initialized != NULL);
183 return extlang->ops->initialized (extlang);
189 /* Throw an error indicating EXTLANG is not supported in this copy of GDB. */
192 throw_ext_lang_unsupported (const struct extension_language_defn *extlang)
194 error (_("Scripting in the \"%s\" language is not supported"
195 " in this copy of GDB."),
196 ext_lang_capitalized_name (extlang));
199 /* Methods for GDB's own extension/scripting language. */
201 /* The extension_language_script_ops.script_sourcer "method". */
204 source_gdb_script (const struct extension_language_defn *extlang,
205 FILE *stream, const char *file)
207 script_from_file (stream, file);
210 /* The extension_language_script_ops.objfile_script_sourcer "method". */
213 source_gdb_objfile_script (const struct extension_language_defn *extlang,
214 struct objfile *objfile,
215 FILE *stream, const char *file)
217 script_from_file (stream, file);
220 /* Accessors for "public" attributes of struct extension_language. */
222 /* Return the "name" field of EXTLANG. */
225 ext_lang_name (const struct extension_language_defn *extlang)
227 return extlang->name;
230 /* Return the "capitalized_name" field of EXTLANG. */
233 ext_lang_capitalized_name (const struct extension_language_defn *extlang)
235 return extlang->capitalized_name;
238 /* Return the "suffix" field of EXTLANG. */
241 ext_lang_suffix (const struct extension_language_defn *extlang)
243 return extlang->suffix;
246 /* Return the "auto_load_suffix" field of EXTLANG. */
249 ext_lang_auto_load_suffix (const struct extension_language_defn *extlang)
251 return extlang->auto_load_suffix;
254 /* extension_language_script_ops wrappers. */
256 /* Return the script "sourcer" function for EXTLANG.
257 This is the function that loads and processes a script.
258 If support for this language isn't compiled in, NULL is returned. */
260 script_sourcer_func *
261 ext_lang_script_sourcer (const struct extension_language_defn *extlang)
263 if (extlang->script_ops == NULL)
266 /* The extension language is required to implement this function. */
267 gdb_assert (extlang->script_ops->script_sourcer != NULL);
269 return extlang->script_ops->script_sourcer;
272 /* Return the objfile script "sourcer" function for EXTLANG.
273 This is the function that loads and processes a script for a particular
275 If support for this language isn't compiled in, NULL is returned. */
277 objfile_script_sourcer_func *
278 ext_lang_objfile_script_sourcer (const struct extension_language_defn *extlang)
280 if (extlang->script_ops == NULL)
283 /* The extension language is required to implement this function. */
284 gdb_assert (extlang->script_ops->objfile_script_sourcer != NULL);
286 return extlang->script_ops->objfile_script_sourcer;
289 /* Return non-zero if auto-loading of EXTLANG scripts is enabled.
290 Zero is returned if support for this language isn't compiled in. */
293 ext_lang_auto_load_enabled (const struct extension_language_defn *extlang)
295 if (extlang->script_ops == NULL)
298 /* The extension language is required to implement this function. */
299 gdb_assert (extlang->script_ops->auto_load_enabled != NULL);
301 return extlang->script_ops->auto_load_enabled (extlang);
304 /* Functions that iterate over all extension languages.
305 These only iterate over external extension languages, not including
306 GDB's own extension/scripting language, unless otherwise indicated. */
308 /* Wrapper to call the extension_language_ops.finish_initialization "method"
309 for each compiled-in extension language. */
312 finish_ext_lang_initialization (void)
315 const struct extension_language_defn *extlang;
317 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
319 if (extlang->ops->finish_initialization != NULL)
320 extlang->ops->finish_initialization (extlang);
324 /* Invoke the appropriate extension_language_ops.eval_from_control_command
325 method to perform CMD, which is a list of commands in an extension language.
327 This function is what implements, for example:
336 eval_ext_lang_from_control_command (struct command_line *cmd)
339 const struct extension_language_defn *extlang;
341 ALL_EXTENSION_LANGUAGES (i, extlang)
343 if (extlang->cli_control_type == cmd->control_type)
345 if (extlang->ops->eval_from_control_command != NULL)
347 extlang->ops->eval_from_control_command (extlang, cmd);
350 /* The requested extension language is not supported in this GDB. */
351 throw_ext_lang_unsupported (extlang);
355 gdb_assert_not_reached ("unknown extension language in command_line");
358 /* Search for and load scripts for OBJFILE written in extension languages.
359 This includes GDB's own scripting language.
361 This function is what implements the loading of OBJFILE-gdb.py and
365 auto_load_ext_lang_scripts_for_objfile (struct objfile *objfile)
368 const struct extension_language_defn *extlang;
370 extlang = &extension_language_gdb;
371 if (ext_lang_auto_load_enabled (extlang))
372 auto_load_objfile_script (objfile, extlang);
374 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
376 if (ext_lang_auto_load_enabled (extlang))
377 auto_load_objfile_script (objfile, extlang);
381 /* Interface to type pretty-printers implemented in an extension language. */
383 /* Call this at the start when preparing to pretty-print a type.
384 The result is a pointer to an opaque object (to the caller) to be passed
385 to apply_ext_lang_type_printers and free_ext_lang_type_printers.
387 We don't know in advance which extension language will provide a
388 pretty-printer for the type, so all are initialized. */
390 struct ext_lang_type_printers *
391 start_ext_lang_type_printers (void)
393 struct ext_lang_type_printers *printers
394 = XCNEW (struct ext_lang_type_printers);
396 const struct extension_language_defn *extlang;
398 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
400 if (extlang->ops->start_type_printers != NULL)
401 extlang->ops->start_type_printers (extlang, printers);
407 /* Iteratively try the type pretty-printers specified by PRINTERS
408 according to the standard search order (specified by extension_languages),
409 returning the result of the first one that succeeds.
410 If there was an error, or if no printer succeeds, then NULL is returned. */
413 apply_ext_lang_type_printers (struct ext_lang_type_printers *printers,
417 const struct extension_language_defn *extlang;
419 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
424 if (extlang->ops->apply_type_printers == NULL)
426 rc = extlang->ops->apply_type_printers (extlang, printers, type,
431 gdb_assert (result != NULL);
433 case EXT_LANG_RC_ERROR:
435 case EXT_LANG_RC_NOP:
438 gdb_assert_not_reached ("bad return from apply_type_printers");
445 /* Call this after pretty-printing a type to release all memory held
449 free_ext_lang_type_printers (struct ext_lang_type_printers *printers)
452 const struct extension_language_defn *extlang;
454 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
456 if (extlang->ops->free_type_printers != NULL)
457 extlang->ops->free_type_printers (extlang, printers);
463 /* Try to pretty-print a value of type TYPE located at VALADDR
464 + EMBEDDED_OFFSET, which came from the inferior at address ADDRESS
465 + EMBEDDED_OFFSET, onto stdio stream STREAM according to OPTIONS.
466 VAL is the whole object that came from ADDRESS. VALADDR must point to
467 the head of VAL's contents buffer.
468 Returns non-zero if the value was successfully pretty-printed.
470 Extension languages are tried in the order specified by
471 extension_languages. The first one to provide a pretty-printed
474 If an error is encountered in a pretty-printer, no further extension
476 Note: This is different than encountering a memory error trying to read a
477 value for pretty-printing. Here we're referring to, e.g., programming
478 errors that trigger an exception in the extension language. */
481 apply_ext_lang_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
482 int embedded_offset, CORE_ADDR address,
483 struct ui_file *stream, int recurse,
484 const struct value *val,
485 const struct value_print_options *options,
486 const struct language_defn *language)
489 const struct extension_language_defn *extlang;
491 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
495 if (extlang->ops->apply_val_pretty_printer == NULL)
497 rc = extlang->ops->apply_val_pretty_printer (extlang, type, valaddr,
498 embedded_offset, address,
499 stream, recurse, val,
505 case EXT_LANG_RC_ERROR:
507 case EXT_LANG_RC_NOP:
510 gdb_assert_not_reached ("bad return from apply_val_pretty_printer");
517 /* GDB access to the "frame filter" feature.
518 FRAME is the source frame to start frame-filter invocation. FLAGS is an
519 integer holding the flags for printing. The following elements of
520 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
521 PRINT_LEVEL is a flag indicating whether to print the frame's
522 relative level in the output. PRINT_FRAME_INFO is a flag that
523 indicates whether this function should print the frame
524 information, PRINT_ARGS is a flag that indicates whether to print
525 frame arguments, and PRINT_LOCALS, likewise, with frame local
526 variables. ARGS_TYPE is an enumerator describing the argument
527 format, OUT is the output stream to print. FRAME_LOW is the
528 beginning of the slice of frames to print, and FRAME_HIGH is the
529 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
530 or EXT_LANG_BT_COMPLETED on success.
532 Extension languages are tried in the order specified by
533 extension_languages. The first one to provide a filter "wins".
534 If there is an error (EXT_LANG_BT_ERROR) it is reported immediately
535 rather than trying filters in other extension languages. */
537 enum ext_lang_bt_status
538 apply_ext_lang_frame_filter (struct frame_info *frame, int flags,
539 enum ext_lang_frame_args args_type,
541 int frame_low, int frame_high)
544 const struct extension_language_defn *extlang;
546 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
548 enum ext_lang_bt_status status;
550 if (extlang->ops->apply_frame_filter == NULL)
552 status = extlang->ops->apply_frame_filter (extlang, frame, flags,
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)
562 return EXT_LANG_BT_NO_FILTERS;
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. */
574 preserve_ext_lang_values (struct objfile *objfile, htab_t copied_types)
577 const struct extension_language_defn *extlang;
579 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
581 if (extlang->ops->preserve_values != NULL)
582 extlang->ops->preserve_values (extlang, objfile, copied_types);
586 /* If there is a stop condition implemented in an extension language for
587 breakpoint B, return a pointer to the extension language's definition.
588 Otherwise return NULL.
589 If SKIP_LANG is not EXT_LANG_NONE, skip checking this language.
590 This is for the case where we're setting a new condition: Only one
591 condition is allowed, so when setting a condition for any particular
592 extension language, we need to check if any other extension language
593 already has a condition set. */
595 const struct extension_language_defn *
596 get_breakpoint_cond_ext_lang (struct breakpoint *b,
597 enum extension_language skip_lang)
600 const struct extension_language_defn *extlang;
602 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
604 if (extlang->language != skip_lang
605 && extlang->ops->breakpoint_has_cond != NULL
606 && extlang->ops->breakpoint_has_cond (extlang, b))
613 /* Return whether a stop condition for breakpoint B says to stop.
614 True is also returned if there is no stop condition for B. */
617 breakpoint_ext_lang_cond_says_stop (struct breakpoint *b)
620 const struct extension_language_defn *extlang;
621 enum ext_lang_bp_stop stop = EXT_LANG_BP_STOP_UNSET;
623 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
625 /* There is a rule that a breakpoint can have at most one of any of a
626 CLI or extension language condition. However, Python hacks in "finish
627 breakpoints" on top of the "stop" check, so we have to call this for
628 every language, even if we could first determine whether a "stop"
630 if (extlang->ops->breakpoint_cond_says_stop != NULL)
632 enum ext_lang_bp_stop this_stop
633 = extlang->ops->breakpoint_cond_says_stop (extlang, b);
635 if (this_stop != EXT_LANG_BP_STOP_UNSET)
637 /* Even though we have to check every extension language, only
638 one of them can return yes/no (because only one of them
639 can have a "stop" condition). */
640 gdb_assert (stop == EXT_LANG_BP_STOP_UNSET);
646 return stop == EXT_LANG_BP_STOP_NO ? 0 : 1;
649 /* ^C/SIGINT support.
650 This requires cooperation with the extension languages so the support
653 /* This flag tracks quit requests when we haven't called out to an
654 extension language. it also holds quit requests when we transition to
655 an extension language that doesn't have cooperative SIGINT handling. */
656 static int quit_flag;
658 /* The current extension language we've called out to, or
659 extension_language_gdb if there isn't one.
660 This must be set everytime we call out to an extension language, and reset
661 to the previous value when it returns. Note that the previous value may
662 be a different (or the same) extension language. */
663 static const struct extension_language_defn *active_ext_lang
664 = &extension_language_gdb;
666 /* Return the currently active extension language. */
668 const struct extension_language_defn *
669 get_active_ext_lang (void)
671 return active_ext_lang;
674 /* Install a SIGINT handler. */
677 install_sigint_handler (const struct signal_handler *handler_state)
679 gdb_assert (handler_state->handler_saved);
681 signal (SIGINT, handler_state->handler);
684 /* Install GDB's SIGINT handler, storing the previous version in *PREVIOUS.
685 As a simple optimization, if the previous version was GDB's SIGINT handler
686 then mark the previous handler as not having been saved, and thus it won't
690 install_gdb_sigint_handler (struct signal_handler *previous)
692 /* Save here to simplify comparison. */
693 RETSIGTYPE (*handle_sigint_for_compare) () = handle_sigint;
695 previous->handler = signal (SIGINT, handle_sigint);
696 if (previous->handler != handle_sigint_for_compare)
697 previous->handler_saved = 1;
699 previous->handler_saved = 0;
702 /* Set the currently active extension language to NOW_ACTIVE.
703 The result is a pointer to a malloc'd block of memory to pass to
704 restore_active_ext_lang.
706 N.B. This function must be called every time we call out to an extension
707 language, and the result must be passed to restore_active_ext_lang
710 If there is a pending SIGINT it is "moved" to the now active extension
711 language, if it supports cooperative SIGINT handling (i.e., it provides
712 {clear,set,check}_quit_flag methods). If the extension language does not
713 support cooperative SIGINT handling, then the SIGINT is left queued and
714 we require the non-cooperative extension language to call check_quit_flag
715 at appropriate times.
716 It is important for the extension language to call check_quit_flag if it
717 installs its own SIGINT handler to prevent the situation where a SIGINT
718 is queued on entry, extension language code runs for a "long" time possibly
719 serving one or more SIGINTs, and then returns. Upon return, if
720 check_quit_flag is not called, the original SIGINT will be thrown.
721 Non-cooperative extension languages are free to install their own SIGINT
722 handler but the original must be restored upon return, either itself
723 or via restore_active_ext_lang. */
725 struct active_ext_lang_state *
726 set_active_ext_lang (const struct extension_language_defn *now_active)
728 struct active_ext_lang_state *previous
729 = XCNEW (struct active_ext_lang_state);
731 previous->ext_lang = active_ext_lang;
732 active_ext_lang = now_active;
734 /* If the newly active extension language uses cooperative SIGINT handling
735 then ensure GDB's SIGINT handler is installed. */
736 if (now_active->language == EXT_LANG_GDB
737 || now_active->ops->check_quit_flag != NULL)
738 install_gdb_sigint_handler (&previous->sigint_handler);
740 /* If there's a SIGINT recorded in the cooperative extension languages,
741 move it to the new language, or save it in GDB's global flag if the newly
742 active extension language doesn't use cooperative SIGINT handling. */
743 if (check_quit_flag ())
749 /* Restore active extension language from PREVIOUS. */
752 restore_active_ext_lang (struct active_ext_lang_state *previous)
754 const struct extension_language_defn *current = active_ext_lang;
756 active_ext_lang = previous->ext_lang;
758 /* Restore the previous SIGINT handler if one was saved. */
759 if (previous->sigint_handler.handler_saved)
760 install_sigint_handler (&previous->sigint_handler);
762 /* If there's a SIGINT recorded in the cooperative extension languages,
763 move it to the new language, or save it in GDB's global flag if the newly
764 active extension language doesn't use cooperative SIGINT handling. */
765 if (check_quit_flag ())
771 /* Clear the quit flag.
772 The flag is cleared in all extension languages,
773 not just the currently active one. */
776 clear_quit_flag (void)
779 const struct extension_language_defn *extlang;
781 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
783 if (extlang->ops->clear_quit_flag != NULL)
784 extlang->ops->clear_quit_flag (extlang);
790 /* Set the quit flag.
791 This only sets the flag in the currently active extension language.
792 If the currently active extension language does not have cooperative
793 SIGINT handling, then GDB's global flag is set, and it is up to the
794 extension language to call check_quit_flag. The extension language
795 is free to install its own SIGINT handler, but we still need to handle
801 if (active_ext_lang->ops != NULL
802 && active_ext_lang->ops->set_quit_flag != NULL)
803 active_ext_lang->ops->set_quit_flag (active_ext_lang);
808 /* Return true if the quit flag has been set, false otherwise.
809 Note: The flag is cleared as a side-effect.
810 The flag is checked in all extension languages that support cooperative
811 SIGINT handling, not just the current one. This simplifies transitions. */
814 check_quit_flag (void)
817 const struct extension_language_defn *extlang;
819 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
821 if (extlang->ops->check_quit_flag != NULL)
822 if (extlang->ops->check_quit_flag (extlang) != 0)
826 /* This is written in a particular way to avoid races. */
836 /* Called via an observer before gdb prints its prompt.
837 Iterate over the extension languages giving them a chance to
838 change the prompt. The first one to change the prompt wins,
839 and no further languages are tried. */
842 ext_lang_before_prompt (const char *current_gdb_prompt)
845 const struct extension_language_defn *extlang;
847 ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
851 if (extlang->ops->before_prompt == NULL)
853 rc = extlang->ops->before_prompt (extlang, current_gdb_prompt);
857 case EXT_LANG_RC_ERROR:
859 case EXT_LANG_RC_NOP:
862 gdb_assert_not_reached ("bad return from before_prompt");
867 extern initialize_file_ftype _initialize_extension;
870 _initialize_extension (void)
872 observer_attach_before_prompt (ext_lang_before_prompt);