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