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