gdb/extension.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Interface between gdb and its extension languages.

  2.    Copyright (C) 2014-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. /* Note: With few exceptions, external functions and variables in this file
  15.    have "ext_lang" in the name, and no other symbol in gdb does.  */

  16. #include "defs.h"
  17. #include <signal.h>
  18. #include "auto-load.h"
  19. #include "breakpoint.h"
  20. #include "event-top.h"
  21. #include "extension.h"
  22. #include "extension-priv.h"
  23. #include "observer.h"
  24. #include "cli/cli-script.h"
  25. #include "python/python.h"
  26. #include "guile/guile.h"

  27. /* Iterate over all external extension languages, regardless of whether the
  28.    support has been compiled in or not.
  29.    This does not include GDB's own scripting language.  */

  30. #define ALL_EXTENSION_LANGUAGES(i, extlang) \
  31.   for (/*int*/ i = 0, extlang = extension_languages[0]; \
  32.        extlang != NULL; \
  33.        extlang = extension_languages[++i])

  34. /* Iterate over all external extension languages that are supported.
  35.    This does not include GDB's own scripting language.  */

  36. #define ALL_ENABLED_EXTENSION_LANGUAGES(i, extlang) \
  37.   for (/*int*/ i = 0, extlang = extension_languages[0]; \
  38.        extlang != NULL; \
  39.        extlang = extension_languages[++i]) \
  40.     if (extlang->ops != NULL)

  41. static script_sourcer_func source_gdb_script;
  42. static objfile_script_sourcer_func source_gdb_objfile_script;

  43. /* GDB's own scripting language.
  44.    This exists, in part, to support auto-loading ${prog}-gdb.gdb scripts.  */

  45. static const struct extension_language_script_ops
  46.   extension_language_gdb_script_ops =
  47. {
  48.   source_gdb_script,
  49.   source_gdb_objfile_script,
  50.   auto_load_gdb_scripts_enabled
  51. };

  52. const struct extension_language_defn extension_language_gdb =
  53. {
  54.   EXT_LANG_GDB,
  55.   "gdb",
  56.   "GDB",

  57.   /* We fall back to interpreting a script as a GDB script if it doesn't
  58.      match the other scripting languages, but for consistency's sake
  59.      give it a formal suffix.  */
  60.   ".gdb",
  61.   "-gdb.gdb",

  62.   /* cli_control_type: This is never used: GDB's own scripting language
  63.      has a variety of control types (if, while, etc.).  */
  64.   commands_control,

  65.   &extension_language_gdb_script_ops,

  66.   /* The rest of the extension language interface isn't supported by GDB's own
  67.      extension/scripting language.  */
  68.   NULL
  69. };

  70. /* NULL-terminated table of all external (non-native) extension languages.

  71.    The order of appearance in the table is important.
  72.    When multiple extension languages provide the same feature, for example
  73.    a pretty-printer for a particular type, which one gets used?
  74.    The algorithm employed here is "the first one wins".  For example, in
  75.    the case of pretty-printers this means the first one to provide a
  76.    pretty-printed value is the one that is used.  This algorithm is employed
  77.    throughout.  */

  78. static const struct extension_language_defn * const extension_languages[] =
  79. {
  80.   /* To preserve existing behaviour, python should always appear first.  */
  81.   &extension_language_python,
  82.   &extension_language_guile,
  83.   NULL
  84. };

  85. /* Return a pointer to the struct extension_language_defn object of
  86.    extension language LANG.
  87.    This always returns a non-NULL pointer, even if support for the language
  88.    is not compiled into this copy of GDB.  */

  89. const struct extension_language_defn *
  90. get_ext_lang_defn (enum extension_language lang)
  91. {
  92.   int i;
  93.   const struct extension_language_defn *extlang;

  94.   gdb_assert (lang != EXT_LANG_NONE);

  95.   if (lang == EXT_LANG_GDB)
  96.     return &extension_language_gdb;

  97.   ALL_EXTENSION_LANGUAGES (i, extlang)
  98.     {
  99.       if (extlang->language == lang)
  100.         return extlang;
  101.     }

  102.   gdb_assert_not_reached ("unable to find extension_language_defn");
  103. }

  104. /* Return TRUE if FILE has extension EXTENSION.  */

  105. static int
  106. has_extension (const char *file, const char *extension)
  107. {
  108.   int file_len = strlen (file);
  109.   int extension_len = strlen (extension);

  110.   return (file_len > extension_len
  111.           && strcmp (&file[file_len - extension_len], extension) == 0);
  112. }

  113. /* Return the extension language of FILE, or NULL if
  114.    the extension language of FILE is not recognized.
  115.    This is done by looking at the file's suffix.  */

  116. const struct extension_language_defn *
  117. get_ext_lang_of_file (const char *file)
  118. {
  119.   int i;
  120.   const struct extension_language_defn *extlang;

  121.   ALL_EXTENSION_LANGUAGES (i, extlang)
  122.     {
  123.       if (has_extension (file, extlang->suffix))
  124.         return extlang;
  125.     }

  126.   return NULL;
  127. }

  128. /* Return non-zero if support for the specified extension language
  129.    is compiled in.  */

  130. int
  131. ext_lang_present_p (const struct extension_language_defn *extlang)
  132. {
  133.   return extlang->script_ops != NULL;
  134. }

  135. /* Return non-zero if the specified extension language has successfully
  136.    initialized.  */

  137. int
  138. ext_lang_initialized_p (const struct extension_language_defn *extlang)
  139. {
  140.   if (extlang->ops != NULL)
  141.     {
  142.       /* This method is required.  */
  143.       gdb_assert (extlang->ops->initialized != NULL);
  144.       return extlang->ops->initialized (extlang);
  145.     }

  146.   return 0;
  147. }

  148. /* Throw an error indicating EXTLANG is not supported in this copy of GDB.  */

  149. void
  150. throw_ext_lang_unsupported (const struct extension_language_defn *extlang)
  151. {
  152.   error (_("Scripting in the \"%s\" language is not supported"
  153.            " in this copy of GDB."),
  154.          ext_lang_capitalized_name (extlang));
  155. }

  156. /* Methods for GDB's own extension/scripting language.  */

  157. /* The extension_language_script_ops.script_sourcer "method".  */

  158. static void
  159. source_gdb_script (const struct extension_language_defn *extlang,
  160.                    FILE *stream, const char *file)
  161. {
  162.   script_from_file (stream, file);
  163. }

  164. /* The extension_language_script_ops.objfile_script_sourcer "method".  */

  165. static void
  166. source_gdb_objfile_script (const struct extension_language_defn *extlang,
  167.                            struct objfile *objfile,
  168.                            FILE *stream, const char *file)
  169. {
  170.   script_from_file (stream, file);
  171. }

  172. /* Accessors for "public" attributes of struct extension_language.  */

  173. /* Return the "name" field of EXTLANG.  */

  174. const char *
  175. ext_lang_name (const struct extension_language_defn *extlang)
  176. {
  177.   return extlang->name;
  178. }

  179. /* Return the "capitalized_name" field of EXTLANG.  */

  180. const char *
  181. ext_lang_capitalized_name (const struct extension_language_defn *extlang)
  182. {
  183.   return extlang->capitalized_name;
  184. }

  185. /* Return the "suffix" field of EXTLANG.  */

  186. const char *
  187. ext_lang_suffix (const struct extension_language_defn *extlang)
  188. {
  189.   return extlang->suffix;
  190. }

  191. /* Return the "auto_load_suffix" field of EXTLANG.  */

  192. const char *
  193. ext_lang_auto_load_suffix (const struct extension_language_defn *extlang)
  194. {
  195.   return extlang->auto_load_suffix;
  196. }

  197. /* extension_language_script_ops wrappers.  */

  198. /* Return the script "sourcer" function for EXTLANG.
  199.    This is the function that loads and processes a script.
  200.    If support for this language isn't compiled in, NULL is returned.  */

  201. script_sourcer_func *
  202. ext_lang_script_sourcer (const struct extension_language_defn *extlang)
  203. {
  204.   if (extlang->script_ops == NULL)
  205.     return NULL;

  206.   /* The extension language is required to implement this function.  */
  207.   gdb_assert (extlang->script_ops->script_sourcer != NULL);

  208.   return extlang->script_ops->script_sourcer;
  209. }

  210. /* Return the objfile script "sourcer" function for EXTLANG.
  211.    This is the function that loads and processes a script for a particular
  212.    objfile.
  213.    If support for this language isn't compiled in, NULL is returned.  */

  214. objfile_script_sourcer_func *
  215. ext_lang_objfile_script_sourcer (const struct extension_language_defn *extlang)
  216. {
  217.   if (extlang->script_ops == NULL)
  218.     return NULL;

  219.   /* The extension language is required to implement this function.  */
  220.   gdb_assert (extlang->script_ops->objfile_script_sourcer != NULL);

  221.   return extlang->script_ops->objfile_script_sourcer;
  222. }

  223. /* Return non-zero if auto-loading of EXTLANG scripts is enabled.
  224.    Zero is returned if support for this language isn't compiled in.  */

  225. int
  226. ext_lang_auto_load_enabled (const struct extension_language_defn *extlang)
  227. {
  228.   if (extlang->script_ops == NULL)
  229.     return 0;

  230.   /* The extension language is required to implement this function.  */
  231.   gdb_assert (extlang->script_ops->auto_load_enabled != NULL);

  232.   return extlang->script_ops->auto_load_enabled (extlang);
  233. }

  234. /* Functions that iterate over all extension languages.
  235.    These only iterate over external extension languages, not including
  236.    GDB's own extension/scripting language, unless otherwise indicated.  */

  237. /* Wrapper to call the extension_language_ops.finish_initialization "method"
  238.    for each compiled-in extension language.  */

  239. void
  240. finish_ext_lang_initialization (void)
  241. {
  242.   int i;
  243.   const struct extension_language_defn *extlang;

  244.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  245.     {
  246.       if (extlang->ops->finish_initialization != NULL)
  247.         extlang->ops->finish_initialization (extlang);
  248.     }
  249. }

  250. /* Invoke the appropriate extension_language_ops.eval_from_control_command
  251.    method to perform CMD, which is a list of commands in an extension language.

  252.    This function is what implements, for example:

  253.    python
  254.    print 42
  255.    end

  256.    in a GDB script.  */

  257. void
  258. eval_ext_lang_from_control_command (struct command_line *cmd)
  259. {
  260.   int i;
  261.   const struct extension_language_defn *extlang;

  262.   ALL_EXTENSION_LANGUAGES (i, extlang)
  263.     {
  264.       if (extlang->cli_control_type == cmd->control_type)
  265.         {
  266.           if (extlang->ops != NULL
  267.               && extlang->ops->eval_from_control_command != NULL)
  268.             {
  269.               extlang->ops->eval_from_control_command (extlang, cmd);
  270.               return;
  271.             }
  272.           /* The requested extension language is not supported in this GDB.  */
  273.           throw_ext_lang_unsupported (extlang);
  274.         }
  275.     }

  276.   gdb_assert_not_reached ("unknown extension language in command_line");
  277. }

  278. /* Search for and load scripts for OBJFILE written in extension languages.
  279.    This includes GDB's own scripting language.

  280.    This function is what implements the loading of OBJFILE-gdb.py and
  281.    OBJFILE-gdb.gdb.  */

  282. void
  283. auto_load_ext_lang_scripts_for_objfile (struct objfile *objfile)
  284. {
  285.   int i;
  286.   const struct extension_language_defn *extlang;

  287.   extlang = &extension_language_gdb;
  288.   if (ext_lang_auto_load_enabled (extlang))
  289.     auto_load_objfile_script (objfile, extlang);

  290.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  291.     {
  292.       if (ext_lang_auto_load_enabled (extlang))
  293.         auto_load_objfile_script (objfile, extlang);
  294.     }
  295. }

  296. /* Interface to type pretty-printers implemented in an extension language.  */

  297. /* Call this at the start when preparing to pretty-print a type.
  298.    The result is a pointer to an opaque object (to the caller) to be passed
  299.    to apply_ext_lang_type_printers and free_ext_lang_type_printers.

  300.    We don't know in advance which extension language will provide a
  301.    pretty-printer for the type, so all are initialized.  */

  302. struct ext_lang_type_printers *
  303. start_ext_lang_type_printers (void)
  304. {
  305.   struct ext_lang_type_printers *printers
  306.     = XCNEW (struct ext_lang_type_printers);
  307.   int i;
  308.   const struct extension_language_defn *extlang;

  309.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  310.     {
  311.       if (extlang->ops->start_type_printers != NULL)
  312.         extlang->ops->start_type_printers (extlang, printers);
  313.     }

  314.   return printers;
  315. }

  316. /* Iteratively try the type pretty-printers specified by PRINTERS
  317.    according to the standard search order (specified by extension_languages),
  318.    returning the result of the first one that succeeds.
  319.    If there was an error, or if no printer succeeds, then NULL is returned.  */

  320. char *
  321. apply_ext_lang_type_printers (struct ext_lang_type_printers *printers,
  322.                               struct type *type)
  323. {
  324.   int i;
  325.   const struct extension_language_defn *extlang;

  326.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  327.     {
  328.       char *result = NULL;
  329.       enum ext_lang_rc rc;

  330.       if (extlang->ops->apply_type_printers == NULL)
  331.         continue;
  332.       rc = extlang->ops->apply_type_printers (extlang, printers, type,
  333.                                               &result);
  334.       switch (rc)
  335.         {
  336.         case EXT_LANG_RC_OK:
  337.           gdb_assert (result != NULL);
  338.           return result;
  339.         case EXT_LANG_RC_ERROR:
  340.           return NULL;
  341.         case EXT_LANG_RC_NOP:
  342.           break;
  343.         default:
  344.           gdb_assert_not_reached ("bad return from apply_type_printers");
  345.         }
  346.     }

  347.   return NULL;
  348. }

  349. /* Call this after pretty-printing a type to release all memory held
  350.    by PRINTERS.  */

  351. void
  352. free_ext_lang_type_printers (struct ext_lang_type_printers *printers)
  353. {
  354.   int i;
  355.   const struct extension_language_defn *extlang;

  356.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  357.     {
  358.       if (extlang->ops->free_type_printers != NULL)
  359.         extlang->ops->free_type_printers (extlang, printers);
  360.     }

  361.   xfree (printers);
  362. }

  363. /* Try to pretty-print a value of type TYPE located at VALADDR
  364.    + EMBEDDED_OFFSET, which came from the inferior at address ADDRESS
  365.    + EMBEDDED_OFFSET, onto stdio stream STREAM according to OPTIONS.
  366.    VAL is the whole object that came from ADDRESS.  VALADDR must point to
  367.    the head of VAL's contents buffer.
  368.    Returns non-zero if the value was successfully pretty-printed.

  369.    Extension languages are tried in the order specified by
  370.    extension_languages.  The first one to provide a pretty-printed
  371.    value "wins".

  372.    If an error is encountered in a pretty-printer, no further extension
  373.    languages are tried.
  374.    Note: This is different than encountering a memory error trying to read a
  375.    value for pretty-printing.  Here we're referring to, e.g., programming
  376.    errors that trigger an exception in the extension language.  */

  377. int
  378. apply_ext_lang_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
  379.                                    int embedded_offset, CORE_ADDR address,
  380.                                    struct ui_file *stream, int recurse,
  381.                                    const struct value *val,
  382.                                    const struct value_print_options *options,
  383.                                    const struct language_defn *language)
  384. {
  385.   int i;
  386.   const struct extension_language_defn *extlang;

  387.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  388.     {
  389.       enum ext_lang_rc rc;

  390.       if (extlang->ops->apply_val_pretty_printer == NULL)
  391.         continue;
  392.       rc = extlang->ops->apply_val_pretty_printer (extlang, type, valaddr,
  393.                                                    embedded_offset, address,
  394.                                                    stream, recurse, val,
  395.                                                    options, language);
  396.       switch (rc)
  397.         {
  398.         case EXT_LANG_RC_OK:
  399.           return 1;
  400.         case EXT_LANG_RC_ERROR:
  401.           return 0;
  402.         case EXT_LANG_RC_NOP:
  403.           break;
  404.         default:
  405.           gdb_assert_not_reached ("bad return from apply_val_pretty_printer");
  406.         }
  407.     }

  408.   return 0;
  409. }

  410. /* GDB access to the "frame filter" feature.
  411.    FRAME is the source frame to start frame-filter invocation.  FLAGS is an
  412.    integer holding the flags for printing.  The following elements of
  413.    the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
  414.    PRINT_LEVEL is a flag indicating whether to print the frame's
  415.    relative level in the output.  PRINT_FRAME_INFO is a flag that
  416.    indicates whether this function should print the frame
  417.    information, PRINT_ARGS is a flag that indicates whether to print
  418.    frame arguments, and PRINT_LOCALS, likewise, with frame local
  419.    variables.  ARGS_TYPE is an enumerator describing the argument
  420.    format, OUT is the output stream to print.  FRAME_LOW is the
  421.    beginning of the slice of frames to print, and FRAME_HIGH is the
  422.    upper limit of the frames to count.  Returns EXT_LANG_BT_ERROR on error,
  423.    or EXT_LANG_BT_COMPLETED on success.

  424.    Extension languages are tried in the order specified by
  425.    extension_languages.  The first one to provide a filter "wins".
  426.    If there is an error (EXT_LANG_BT_ERROR) it is reported immediately
  427.    rather than trying filters in other extension languages.  */

  428. enum ext_lang_bt_status
  429. apply_ext_lang_frame_filter (struct frame_info *frame, int flags,
  430.                              enum ext_lang_frame_args args_type,
  431.                              struct ui_out *out,
  432.                              int frame_low, int frame_high)
  433. {
  434.   int i;
  435.   const struct extension_language_defn *extlang;

  436.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  437.     {
  438.       enum ext_lang_bt_status status;

  439.       if (extlang->ops->apply_frame_filter == NULL)
  440.         continue;
  441.       status = extlang->ops->apply_frame_filter (extlang, frame, flags,
  442.                                                args_type, out,
  443.                                                frame_low, frame_high);
  444.       /* We use the filters from the first extension language that has
  445.          applicable filters.  Also, an error is reported immediately
  446.          rather than continue trying.  */
  447.       if (status != EXT_LANG_BT_NO_FILTERS)
  448.         return status;
  449.     }

  450.   return EXT_LANG_BT_NO_FILTERS;
  451. }

  452. /* Update values held by the extension language when OBJFILE is discarded.
  453.    New global types must be created for every such value, which must then be
  454.    updated to use the new types.
  455.    The function typically just iterates over all appropriate values and
  456.    calls preserve_one_value for each one.
  457.    COPIED_TYPES is used to prevent cycles / duplicates and is passed to
  458.    preserve_one_value.  */

  459. void
  460. preserve_ext_lang_values (struct objfile *objfile, htab_t copied_types)
  461. {
  462.   int i;
  463.   const struct extension_language_defn *extlang;

  464.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  465.     {
  466.       if (extlang->ops->preserve_values != NULL)
  467.         extlang->ops->preserve_values (extlang, objfile, copied_types);
  468.     }
  469. }

  470. /* If there is a stop condition implemented in an extension language for
  471.    breakpoint B, return a pointer to the extension language's definition.
  472.    Otherwise return NULL.
  473.    If SKIP_LANG is not EXT_LANG_NONE, skip checking this language.
  474.    This is for the case where we're setting a new condition: Only one
  475.    condition is allowed, so when setting a condition for any particular
  476.    extension language, we need to check if any other extension language
  477.    already has a condition set.  */

  478. const struct extension_language_defn *
  479. get_breakpoint_cond_ext_lang (struct breakpoint *b,
  480.                               enum extension_language skip_lang)
  481. {
  482.   int i;
  483.   const struct extension_language_defn *extlang;

  484.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  485.     {
  486.       if (extlang->language != skip_lang
  487.           && extlang->ops->breakpoint_has_cond != NULL
  488.           && extlang->ops->breakpoint_has_cond (extlang, b))
  489.         return extlang;
  490.     }

  491.   return NULL;
  492. }

  493. /* Return whether a stop condition for breakpoint B says to stop.
  494.    True is also returned if there is no stop condition for B.  */

  495. int
  496. breakpoint_ext_lang_cond_says_stop (struct breakpoint *b)
  497. {
  498.   int i;
  499.   const struct extension_language_defn *extlang;
  500.   enum ext_lang_bp_stop stop = EXT_LANG_BP_STOP_UNSET;

  501.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  502.     {
  503.       /* There is a rule that a breakpoint can have at most one of any of a
  504.          CLI or extension language condition.  However, Python hacks in "finish
  505.          breakpoints" on top of the "stop" check, so we have to call this for
  506.          every language, even if we could first determine whether a "stop"
  507.          method exists.  */
  508.       if (extlang->ops->breakpoint_cond_says_stop != NULL)
  509.         {
  510.           enum ext_lang_bp_stop this_stop
  511.             = extlang->ops->breakpoint_cond_says_stop (extlang, b);

  512.           if (this_stop != EXT_LANG_BP_STOP_UNSET)
  513.             {
  514.               /* Even though we have to check every extension language, only
  515.                  one of them can return yes/no (because only one of them
  516.                  can have a "stop" condition).  */
  517.               gdb_assert (stop == EXT_LANG_BP_STOP_UNSET);
  518.               stop = this_stop;
  519.             }
  520.         }
  521.     }

  522.   return stop == EXT_LANG_BP_STOP_NO ? 0 : 1;
  523. }

  524. /* ^C/SIGINT support.
  525.    This requires cooperation with the extension languages so the support
  526.    is defined here.  */

  527. /* This flag tracks quit requests when we haven't called out to an
  528.    extension language.  it also holds quit requests when we transition to
  529.    an extension language that doesn't have cooperative SIGINT handling.  */
  530. static int quit_flag;

  531. /* The current extension language we've called out to, or
  532.    extension_language_gdb if there isn't one.
  533.    This must be set everytime we call out to an extension language, and reset
  534.    to the previous value when it returns.  Note that the previous value may
  535.    be a different (or the same) extension language.  */
  536. static const struct extension_language_defn *active_ext_lang
  537.   = &extension_language_gdb;

  538. /* Return the currently active extension language.  */

  539. const struct extension_language_defn *
  540. get_active_ext_lang (void)
  541. {
  542.   return active_ext_lang;
  543. }

  544. /* Install a SIGINT handler.  */

  545. static void
  546. install_sigint_handler (const struct signal_handler *handler_state)
  547. {
  548.   gdb_assert (handler_state->handler_saved);

  549.   signal (SIGINT, handler_state->handler);
  550. }

  551. /* Install GDB's SIGINT handler, storing the previous version in *PREVIOUS.
  552.    As a simple optimization, if the previous version was GDB's SIGINT handler
  553.    then mark the previous handler as not having been saved, and thus it won't
  554.    be restored.  */

  555. static void
  556. install_gdb_sigint_handler (struct signal_handler *previous)
  557. {
  558.   /* Save here to simplify comparison.  */
  559.   RETSIGTYPE (*handle_sigint_for_compare) () = handle_sigint;

  560.   previous->handler = signal (SIGINT, handle_sigint);
  561.   if (previous->handler != handle_sigint_for_compare)
  562.     previous->handler_saved = 1;
  563.   else
  564.     previous->handler_saved = 0;
  565. }

  566. /* Set the currently active extension language to NOW_ACTIVE.
  567.    The result is a pointer to a malloc'd block of memory to pass to
  568.    restore_active_ext_lang.

  569.    N.B. This function must be called every time we call out to an extension
  570.    language, and the result must be passed to restore_active_ext_lang
  571.    afterwards.

  572.    If there is a pending SIGINT it is "moved" to the now active extension
  573.    language, if it supports cooperative SIGINT handling (i.e., it provides
  574.    {clear,set,check}_quit_flag methods).  If the extension language does not
  575.    support cooperative SIGINT handling, then the SIGINT is left queued and
  576.    we require the non-cooperative extension language to call check_quit_flag
  577.    at appropriate times.
  578.    It is important for the extension language to call check_quit_flag if it
  579.    installs its own SIGINT handler to prevent the situation where a SIGINT
  580.    is queued on entry, extension language code runs for a "long" time possibly
  581.    serving one or more SIGINTs, and then returns.  Upon return, if
  582.    check_quit_flag is not called, the original SIGINT will be thrown.
  583.    Non-cooperative extension languages are free to install their own SIGINT
  584.    handler but the original must be restored upon return, either itself
  585.    or via restore_active_ext_lang.  */

  586. struct active_ext_lang_state *
  587. set_active_ext_lang (const struct extension_language_defn *now_active)
  588. {
  589.   struct active_ext_lang_state *previous
  590.     = XCNEW (struct active_ext_lang_state);

  591.   previous->ext_lang = active_ext_lang;
  592.   active_ext_lang = now_active;

  593.   /* If the newly active extension language uses cooperative SIGINT handling
  594.      then ensure GDB's SIGINT handler is installed.  */
  595.   if (now_active->language == EXT_LANG_GDB
  596.       || now_active->ops->check_quit_flag != NULL)
  597.     install_gdb_sigint_handler (&previous->sigint_handler);

  598.   /* If there's a SIGINT recorded in the cooperative extension languages,
  599.      move it to the new language, or save it in GDB's global flag if the newly
  600.      active extension language doesn't use cooperative SIGINT handling.  */
  601.   if (check_quit_flag ())
  602.     set_quit_flag ();

  603.   return previous;
  604. }

  605. /* Restore active extension language from PREVIOUS.  */

  606. void
  607. restore_active_ext_lang (struct active_ext_lang_state *previous)
  608. {
  609.   const struct extension_language_defn *current = active_ext_lang;

  610.   active_ext_lang = previous->ext_lang;

  611.   /* Restore the previous SIGINT handler if one was saved.  */
  612.   if (previous->sigint_handler.handler_saved)
  613.     install_sigint_handler (&previous->sigint_handler);

  614.   /* If there's a SIGINT recorded in the cooperative extension languages,
  615.      move it to the new language, or save it in GDB's global flag if the newly
  616.      active extension language doesn't use cooperative SIGINT handling.  */
  617.   if (check_quit_flag ())
  618.     set_quit_flag ();

  619.   xfree (previous);
  620. }

  621. /* Clear the quit flag.
  622.    The flag is cleared in all extension languages,
  623.    not just the currently active one.  */

  624. void
  625. clear_quit_flag (void)
  626. {
  627.   int i;
  628.   const struct extension_language_defn *extlang;

  629.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  630.     {
  631.       if (extlang->ops->clear_quit_flag != NULL)
  632.         extlang->ops->clear_quit_flag (extlang);
  633.     }

  634.   quit_flag = 0;
  635. }

  636. /* Set the quit flag.
  637.    This only sets the flag in the currently active extension language.
  638.    If the currently active extension language does not have cooperative
  639.    SIGINT handling, then GDB's global flag is set, and it is up to the
  640.    extension language to call check_quit_flag.  The extension language
  641.    is free to install its own SIGINT handler, but we still need to handle
  642.    the transition.  */

  643. void
  644. set_quit_flag (void)
  645. {
  646.   if (active_ext_lang->ops != NULL
  647.       && active_ext_lang->ops->set_quit_flag != NULL)
  648.     active_ext_lang->ops->set_quit_flag (active_ext_lang);
  649.   else
  650.     quit_flag = 1;
  651. }

  652. /* Return true if the quit flag has been set, false otherwise.
  653.    Note: The flag is cleared as a side-effect.
  654.    The flag is checked in all extension languages that support cooperative
  655.    SIGINT handling, not just the current one.  This simplifies transitions.  */

  656. int
  657. check_quit_flag (void)
  658. {
  659.   int i, result = 0;
  660.   const struct extension_language_defn *extlang;

  661.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  662.     {
  663.       if (extlang->ops->check_quit_flag != NULL)
  664.         if (extlang->ops->check_quit_flag (extlang) != 0)
  665.           result = 1;
  666.     }

  667.   /* This is written in a particular way to avoid races.  */
  668.   if (quit_flag)
  669.     {
  670.       quit_flag = 0;
  671.       result = 1;
  672.     }

  673.   return result;
  674. }

  675. /* xmethod support.  */

  676. /* The xmethod API routines do not have "ext_lang" in the name because
  677.    the name "xmethod" implies that this routine deals with extension
  678.    languages.  Plus some of the methods take a xmethod_foo * "self/this"
  679.    arg, not an extension_language_defn * arg.  */

  680. /* Returns a new xmethod_worker with EXTLANG and DATA.  Space for the
  681.    result must be freed with free_xmethod_worker.  */

  682. struct xmethod_worker *
  683. new_xmethod_worker (const struct extension_language_defn *extlang, void *data)
  684. {
  685.   struct xmethod_worker *worker = XCNEW (struct xmethod_worker);

  686.   worker->extlang = extlang;
  687.   worker->data = data;
  688.   worker->value = NULL;

  689.   return worker;
  690. }

  691. /* Clones WORKER and returns a new but identical worker.
  692.    The function get_matching_xmethod_workers (see below), returns a
  693.    vector of matching workers.  If a particular worker is selected by GDB
  694.    to invoke a method, then this function can help in cloning the
  695.    selected worker and freeing up the vector via a cleanup.

  696.    Space for the result must be freed with free_xmethod_worker.  */

  697. struct xmethod_worker *
  698. clone_xmethod_worker (struct xmethod_worker *worker)
  699. {
  700.   struct xmethod_worker *new_worker;
  701.   const struct extension_language_defn *extlang = worker->extlang;

  702.   gdb_assert (extlang->ops->clone_xmethod_worker_data != NULL);

  703.   new_worker = new_xmethod_worker
  704.     (extlang,
  705.      extlang->ops->clone_xmethod_worker_data (extlang, worker->data));

  706.   return new_worker;
  707. }

  708. /* If a method with name METHOD_NAME is to be invoked on an object of type
  709.    TYPE, then all entension languages are searched for implementations of
  710.    methods with name METHOD.  All matches found are returned as a vector
  711.    of 'xmethod_worker_ptr' objects.  If no matching methods are
  712.    found, NULL is returned.  */

  713. VEC (xmethod_worker_ptr) *
  714. get_matching_xmethod_workers (struct type *type, const char *method_name)
  715. {
  716.   VEC (xmethod_worker_ptr) *workers = NULL;
  717.   int i;
  718.   const struct extension_language_defn *extlang;

  719.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  720.     {
  721.       VEC (xmethod_worker_ptr) *lang_workers, *new_vec;
  722.       enum ext_lang_rc rc;

  723.       /* If an extension language does not support xmethods, ignore
  724.          it.  */
  725.       if (extlang->ops->get_matching_xmethod_workers == NULL)
  726.         continue;

  727.       rc = extlang->ops->get_matching_xmethod_workers (extlang,
  728.                                                        type, method_name,
  729.                                                        &lang_workers);
  730.       if (rc == EXT_LANG_RC_ERROR)
  731.         {
  732.           free_xmethod_worker_vec (workers);
  733.           error (_("Error while looking for matching xmethod workers "
  734.                    "defined in %s."), extlang->capitalized_name);
  735.         }

  736.       new_vec = VEC_merge (xmethod_worker_ptr, workers, lang_workers);
  737.       /* Free only the vectors and not the elements as NEW_VEC still
  738.          contains them.  */
  739.       VEC_free (xmethod_worker_ptr, workers);
  740.       VEC_free (xmethod_worker_ptr, lang_workers);
  741.       workers = new_vec;
  742.     }

  743.   return workers;
  744. }

  745. /* Return the arg types of the xmethod encapsulated in WORKER.
  746.    An array of arg types is returned.  The length of the array is returned in
  747.    NARGS.  The type of the 'this' object is returned as the first element of
  748.    array.  */

  749. struct type **
  750. get_xmethod_arg_types (struct xmethod_worker *worker, int *nargs)
  751. {
  752.   enum ext_lang_rc rc;
  753.   struct type **type_array = NULL;
  754.   const struct extension_language_defn *extlang = worker->extlang;

  755.   gdb_assert (extlang->ops->get_xmethod_arg_types != NULL);

  756.   rc = extlang->ops->get_xmethod_arg_types (extlang, worker, nargs,
  757.                                             &type_array);
  758.   if (rc == EXT_LANG_RC_ERROR)
  759.     {
  760.       error (_("Error while looking for arg types of a xmethod worker "
  761.                "defined in %s."), extlang->capitalized_name);
  762.     }

  763.   return type_array;
  764. }

  765. /* Invokes the xmethod encapsulated in WORKER and returns the result.
  766.    The method is invoked on OBJ with arguments in the ARGS array.  NARGS is
  767.    the length of the this array.  */

  768. struct value *
  769. invoke_xmethod (struct xmethod_worker *worker, struct value *obj,
  770.                      struct value **args, int nargs)
  771. {
  772.   gdb_assert (worker->extlang->ops->invoke_xmethod != NULL);

  773.   return worker->extlang->ops->invoke_xmethod (worker->extlang, worker,
  774.                                                obj, args, nargs);
  775. }

  776. /* Frees the xmethod worker WORKER.  */

  777. void
  778. free_xmethod_worker (struct xmethod_worker *worker)
  779. {
  780.   gdb_assert (worker->extlang->ops->free_xmethod_worker_data != NULL);
  781.   worker->extlang->ops->free_xmethod_worker_data (worker->extlang,
  782.                                                   worker->data);
  783.   xfree (worker);
  784. }

  785. /* Frees a vector of xmethod_workers VEC.  */

  786. void
  787. free_xmethod_worker_vec (void *vec)
  788. {
  789.   int i;
  790.   struct xmethod_worker *worker;
  791.   VEC (xmethod_worker_ptr) *v = (VEC (xmethod_worker_ptr) *) vec;

  792.   for (i = 0; VEC_iterate (xmethod_worker_ptr, v, i, worker); i++)
  793.     free_xmethod_worker (worker);

  794.   VEC_free (xmethod_worker_ptr, v);
  795. }

  796. /* Called via an observer before gdb prints its prompt.
  797.    Iterate over the extension languages giving them a chance to
  798.    change the prompt.  The first one to change the prompt wins,
  799.    and no further languages are tried.  */

  800. static void
  801. ext_lang_before_prompt (const char *current_gdb_prompt)
  802. {
  803.   int i;
  804.   const struct extension_language_defn *extlang;

  805.   ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
  806.     {
  807.       enum ext_lang_rc rc;

  808.       if (extlang->ops->before_prompt == NULL)
  809.         continue;
  810.       rc = extlang->ops->before_prompt (extlang, current_gdb_prompt);
  811.       switch (rc)
  812.         {
  813.         case EXT_LANG_RC_OK:
  814.         case EXT_LANG_RC_ERROR:
  815.           return;
  816.         case EXT_LANG_RC_NOP:
  817.           break;
  818.         default:
  819.           gdb_assert_not_reached ("bad return from before_prompt");
  820.         }
  821.     }
  822. }

  823. extern initialize_file_ftype _initialize_extension;

  824. void
  825. _initialize_extension (void)
  826. {
  827.   observer_attach_before_prompt (ext_lang_before_prompt);
  828. }