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