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