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