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