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