1 /* Support for GCC plugin mechanism. 2 Copyright (C) 2009-2016 Free Software Foundation, Inc. 3 4 This file is part of GCC. 5 6 GCC is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3, or (at your option) 9 any later version. 10 11 GCC is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GCC; see the file COPYING3. If not see 18 <http://www.gnu.org/licenses/>. */ 19 20 /* This file contains the support for GCC plugin mechanism based on the 21 APIs described in doc/plugin.texi. */ 22 23 #include "config.h" 24 #include "system.h" 25 #include "coretypes.h" 26 #include "options.h" 27 #include "tree-pass.h" 28 #include "diagnostic-core.h" 29 #include "flags.h" 30 #include "intl.h" 31 #include "plugin.h" 32 33 #ifdef ENABLE_PLUGIN 34 #include "plugin-version.h" 35 #endif 36 37 #define GCC_PLUGIN_STRINGIFY0(X) #X 38 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X) 39 40 /* Event names as strings. Keep in sync with enum plugin_event. */ 41 static const char *plugin_event_name_init[] = 42 { 43 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME), 44 # include "plugin.def" 45 # undef DEFEVENT 46 }; 47 48 /* A printf format large enough for the largest event above. */ 49 #define FMT_FOR_PLUGIN_EVENT "%-32s" 50 51 const char **plugin_event_name = plugin_event_name_init; 52 53 /* Event hashtable helpers. */ 54 55 struct event_hasher : nofree_ptr_hash <const char *> 56 { 57 static inline hashval_t hash (const char **); 58 static inline bool equal (const char **, const char **); 59 }; 60 61 /* Helper function for the event hash table that hashes the entry V. */ 62 63 inline hashval_t 64 event_hasher::hash (const char **v) 65 { 66 return htab_hash_string (*v); 67 } 68 69 /* Helper function for the event hash table that compares the name of an 70 existing entry (S1) with the given string (S2). */ 71 72 inline bool 73 event_hasher::equal (const char **s1, const char **s2) 74 { 75 return !strcmp (*s1, *s2); 76 } 77 78 /* A hash table to map event names to the position of the names in the 79 plugin_event_name table. */ 80 static hash_table<event_hasher> *event_tab; 81 82 /* Keep track of the limit of allocated events and space ready for 83 allocating events. */ 84 static int event_last = PLUGIN_EVENT_FIRST_DYNAMIC; 85 static int event_horizon = PLUGIN_EVENT_FIRST_DYNAMIC; 86 87 /* Hash table for the plugin_name_args objects created during command-line 88 parsing. */ 89 static htab_t plugin_name_args_tab = NULL; 90 91 /* List node for keeping track of plugin-registered callback. */ 92 struct callback_info 93 { 94 const char *plugin_name; /* Name of plugin that registers the callback. */ 95 plugin_callback_func func; /* Callback to be called. */ 96 void *user_data; /* plugin-specified data. */ 97 struct callback_info *next; 98 }; 99 100 /* An array of lists of 'callback_info' objects indexed by the event id. */ 101 static struct callback_info *plugin_callbacks_init[PLUGIN_EVENT_FIRST_DYNAMIC]; 102 static struct callback_info **plugin_callbacks = plugin_callbacks_init; 103 104 /* For invoke_plugin_callbacks(), see plugin.h. */ 105 bool flag_plugin_added = false; 106 107 #ifdef ENABLE_PLUGIN 108 /* Each plugin should define an initialization function with exactly 109 this name. */ 110 static const char *str_plugin_init_func_name = "plugin_init"; 111 112 /* Each plugin should define this symbol to assert that it is 113 distributed under a GPL-compatible license. */ 114 static const char *str_license = "plugin_is_GPL_compatible"; 115 #endif 116 117 /* Helper function for hashing the base_name of the plugin_name_args 118 structure to be inserted into the hash table. */ 119 120 static hashval_t 121 htab_hash_plugin (const PTR p) 122 { 123 const struct plugin_name_args *plugin = (const struct plugin_name_args *) p; 124 return htab_hash_string (plugin->base_name); 125 } 126 127 /* Helper function for the hash table that compares the base_name of the 128 existing entry (S1) with the given string (S2). */ 129 130 static int 131 htab_str_eq (const void *s1, const void *s2) 132 { 133 const struct plugin_name_args *plugin = (const struct plugin_name_args *) s1; 134 return !strcmp (plugin->base_name, (const char *) s2); 135 } 136 137 138 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so, 139 return NAME. */ 140 141 static char * 142 get_plugin_base_name (const char *full_name) 143 { 144 /* First get the base name part of the full-path name, i.e. NAME.so. */ 145 char *base_name = xstrdup (lbasename (full_name)); 146 147 /* Then get rid of '.so' part of the name. */ 148 strip_off_ending (base_name, strlen (base_name)); 149 150 return base_name; 151 } 152 153 154 /* Create a plugin_name_args object for the given plugin and insert it 155 to the hash table. This function is called when 156 -fplugin=/path/to/NAME.so or -fplugin=NAME option is processed. */ 157 158 void 159 add_new_plugin (const char* plugin_name) 160 { 161 struct plugin_name_args *plugin; 162 void **slot; 163 char *base_name; 164 bool name_is_short; 165 const char *pc; 166 167 flag_plugin_added = true; 168 169 /* Replace short names by their full path when relevant. */ 170 name_is_short = !IS_ABSOLUTE_PATH (plugin_name); 171 for (pc = plugin_name; name_is_short && *pc; pc++) 172 if (*pc == '.' || IS_DIR_SEPARATOR (*pc)) 173 name_is_short = false; 174 175 if (name_is_short) 176 { 177 base_name = CONST_CAST (char*, plugin_name); 178 /* FIXME: the ".so" suffix is currently builtin, since plugins 179 only work on ELF host systems like e.g. Linux or Solaris. 180 When plugins shall be available on non ELF systems such as 181 Windows or MacOS, this code has to be greatly improved. */ 182 plugin_name = concat (default_plugin_dir_name (), "/", 183 plugin_name, ".so", NULL); 184 if (access (plugin_name, R_OK)) 185 fatal_error 186 (input_location, 187 "inaccessible plugin file %s expanded from short plugin name %s: %m", 188 plugin_name, base_name); 189 } 190 else 191 base_name = get_plugin_base_name (plugin_name); 192 193 /* If this is the first -fplugin= option we encounter, create 194 'plugin_name_args_tab' hash table. */ 195 if (!plugin_name_args_tab) 196 plugin_name_args_tab = htab_create (10, htab_hash_plugin, htab_str_eq, 197 NULL); 198 199 slot = htab_find_slot_with_hash (plugin_name_args_tab, base_name, 200 htab_hash_string (base_name), INSERT); 201 202 /* If the same plugin (name) has been specified earlier, either emit an 203 error or a warning message depending on if they have identical full 204 (path) names. */ 205 if (*slot) 206 { 207 plugin = (struct plugin_name_args *) *slot; 208 if (strcmp (plugin->full_name, plugin_name)) 209 error ("plugin %s was specified with different paths:\n%s\n%s", 210 plugin->base_name, plugin->full_name, plugin_name); 211 return; 212 } 213 214 plugin = XCNEW (struct plugin_name_args); 215 plugin->base_name = base_name; 216 plugin->full_name = plugin_name; 217 218 *slot = plugin; 219 } 220 221 222 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a 223 'plugin_argument' object for the parsed key-value pair. ARG is 224 the <name>-<key>[=<value>] part of the option. */ 225 226 void 227 parse_plugin_arg_opt (const char *arg) 228 { 229 size_t len = 0, name_len = 0, key_len = 0, value_len = 0; 230 const char *ptr, *name_start = arg, *key_start = NULL, *value_start = NULL; 231 char *name, *key, *value; 232 void **slot; 233 bool name_parsed = false, key_parsed = false; 234 235 /* Iterate over the ARG string and identify the starting character position 236 of 'name', 'key', and 'value' and their lengths. */ 237 for (ptr = arg; *ptr; ++ptr) 238 { 239 /* Only the first '-' encountered is considered a separator between 240 'name' and 'key'. All the subsequent '-'s are considered part of 241 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value, 242 the plugin name is 'foo' and the key is 'bar-primary-key'. */ 243 if (*ptr == '-' && !name_parsed) 244 { 245 name_len = len; 246 len = 0; 247 key_start = ptr + 1; 248 name_parsed = true; 249 continue; 250 } 251 else if (*ptr == '=') 252 { 253 if (!key_parsed) 254 { 255 key_len = len; 256 len = 0; 257 value_start = ptr + 1; 258 key_parsed = true; 259 } 260 continue; 261 } 262 else 263 ++len; 264 } 265 266 if (!key_start) 267 { 268 error ("malformed option -fplugin-arg-%s (missing -<key>[=<value>])", 269 arg); 270 return; 271 } 272 273 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN. 274 Otherwise, it is the VALUE_LEN. */ 275 if (!value_start) 276 key_len = len; 277 else 278 value_len = len; 279 280 name = XNEWVEC (char, name_len + 1); 281 strncpy (name, name_start, name_len); 282 name[name_len] = '\0'; 283 284 /* Check if the named plugin has already been specified earlier in the 285 command-line. */ 286 if (plugin_name_args_tab 287 && ((slot = htab_find_slot_with_hash (plugin_name_args_tab, name, 288 htab_hash_string (name), NO_INSERT)) 289 != NULL)) 290 { 291 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot; 292 293 key = XNEWVEC (char, key_len + 1); 294 strncpy (key, key_start, key_len); 295 key[key_len] = '\0'; 296 if (value_start) 297 { 298 value = XNEWVEC (char, value_len + 1); 299 strncpy (value, value_start, value_len); 300 value[value_len] = '\0'; 301 } 302 else 303 value = NULL; 304 305 /* Create a plugin_argument object for the parsed key-value pair. 306 If there are already arguments for this plugin, we will need to 307 adjust the argument array size by creating a new array and deleting 308 the old one. If the performance ever becomes an issue, we can 309 change the code by pre-allocating a larger array first. */ 310 if (plugin->argc > 0) 311 { 312 struct plugin_argument *args = XNEWVEC (struct plugin_argument, 313 plugin->argc + 1); 314 memcpy (args, plugin->argv, 315 sizeof (struct plugin_argument) * plugin->argc); 316 XDELETEVEC (plugin->argv); 317 plugin->argv = args; 318 ++plugin->argc; 319 } 320 else 321 { 322 gcc_assert (plugin->argv == NULL); 323 plugin->argv = XNEWVEC (struct plugin_argument, 1); 324 plugin->argc = 1; 325 } 326 327 plugin->argv[plugin->argc - 1].key = key; 328 plugin->argv[plugin->argc - 1].value = value; 329 } 330 else 331 error ("plugin %s should be specified before -fplugin-arg-%s " 332 "in the command line", name, arg); 333 334 /* We don't need the plugin's name anymore. Just release it. */ 335 XDELETEVEC (name); 336 } 337 338 /* Register additional plugin information. NAME is the name passed to 339 plugin_init. INFO is the information that should be registered. */ 340 341 static void 342 register_plugin_info (const char* name, struct plugin_info *info) 343 { 344 void **slot = htab_find_slot_with_hash (plugin_name_args_tab, name, 345 htab_hash_string (name), NO_INSERT); 346 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot; 347 plugin->version = info->version; 348 plugin->help = info->help; 349 } 350 351 /* Look up the event id for NAME. If the name is not found, return -1 352 if INSERT is NO_INSERT. */ 353 354 int 355 get_named_event_id (const char *name, enum insert_option insert) 356 { 357 const char ***slot; 358 359 if (!event_tab) 360 { 361 int i; 362 363 event_tab = new hash_table<event_hasher> (150); 364 for (i = 0; i < event_last; i++) 365 { 366 slot = event_tab->find_slot (&plugin_event_name[i], INSERT); 367 gcc_assert (*slot == HTAB_EMPTY_ENTRY); 368 *slot = &plugin_event_name[i]; 369 } 370 } 371 slot = event_tab->find_slot (&name, insert); 372 if (slot == NULL) 373 return -1; 374 if (*slot != HTAB_EMPTY_ENTRY) 375 return *slot - &plugin_event_name[0]; 376 377 if (event_last >= event_horizon) 378 { 379 event_horizon = event_last * 2; 380 if (plugin_event_name == plugin_event_name_init) 381 { 382 plugin_event_name = XNEWVEC (const char *, event_horizon); 383 memcpy (plugin_event_name, plugin_event_name_init, 384 sizeof plugin_event_name_init); 385 plugin_callbacks = XNEWVEC (struct callback_info *, event_horizon); 386 memcpy (plugin_callbacks, plugin_callbacks_init, 387 sizeof plugin_callbacks_init); 388 } 389 else 390 { 391 plugin_event_name 392 = XRESIZEVEC (const char *, plugin_event_name, event_horizon); 393 plugin_callbacks = XRESIZEVEC (struct callback_info *, 394 plugin_callbacks, event_horizon); 395 } 396 /* All the pointers in the hash table will need to be updated. */ 397 delete event_tab; 398 event_tab = NULL; 399 } 400 else 401 *slot = &plugin_event_name[event_last]; 402 plugin_event_name[event_last] = name; 403 return event_last++; 404 } 405 406 /* Called from the plugin's initialization code. Register a single callback. 407 This function can be called multiple times. 408 409 PLUGIN_NAME - display name for this plugin 410 EVENT - which event the callback is for 411 CALLBACK - the callback to be called at the event 412 USER_DATA - plugin-provided data */ 413 414 void 415 register_callback (const char *plugin_name, 416 int event, 417 plugin_callback_func callback, 418 void *user_data) 419 { 420 switch (event) 421 { 422 case PLUGIN_PASS_MANAGER_SETUP: 423 gcc_assert (!callback); 424 register_pass ((struct register_pass_info *) user_data); 425 break; 426 case PLUGIN_INFO: 427 gcc_assert (!callback); 428 register_plugin_info (plugin_name, (struct plugin_info *) user_data); 429 break; 430 case PLUGIN_REGISTER_GGC_ROOTS: 431 gcc_assert (!callback); 432 ggc_register_root_tab ((const struct ggc_root_tab*) user_data); 433 break; 434 case PLUGIN_EVENT_FIRST_DYNAMIC: 435 default: 436 if (event < PLUGIN_EVENT_FIRST_DYNAMIC || event >= event_last) 437 { 438 error ("unknown callback event registered by plugin %s", 439 plugin_name); 440 return; 441 } 442 /* Fall through. */ 443 case PLUGIN_START_PARSE_FUNCTION: 444 case PLUGIN_FINISH_PARSE_FUNCTION: 445 case PLUGIN_FINISH_TYPE: 446 case PLUGIN_FINISH_DECL: 447 case PLUGIN_START_UNIT: 448 case PLUGIN_FINISH_UNIT: 449 case PLUGIN_PRE_GENERICIZE: 450 case PLUGIN_GGC_START: 451 case PLUGIN_GGC_MARKING: 452 case PLUGIN_GGC_END: 453 case PLUGIN_ATTRIBUTES: 454 case PLUGIN_PRAGMAS: 455 case PLUGIN_FINISH: 456 case PLUGIN_ALL_PASSES_START: 457 case PLUGIN_ALL_PASSES_END: 458 case PLUGIN_ALL_IPA_PASSES_START: 459 case PLUGIN_ALL_IPA_PASSES_END: 460 case PLUGIN_OVERRIDE_GATE: 461 case PLUGIN_PASS_EXECUTION: 462 case PLUGIN_EARLY_GIMPLE_PASSES_START: 463 case PLUGIN_EARLY_GIMPLE_PASSES_END: 464 case PLUGIN_NEW_PASS: 465 case PLUGIN_INCLUDE_FILE: 466 { 467 struct callback_info *new_callback; 468 if (!callback) 469 { 470 error ("plugin %s registered a null callback function " 471 "for event %s", plugin_name, plugin_event_name[event]); 472 return; 473 } 474 new_callback = XNEW (struct callback_info); 475 new_callback->plugin_name = plugin_name; 476 new_callback->func = callback; 477 new_callback->user_data = user_data; 478 new_callback->next = plugin_callbacks[event]; 479 plugin_callbacks[event] = new_callback; 480 } 481 break; 482 } 483 } 484 485 /* Remove a callback for EVENT which has been registered with for a plugin 486 PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was 487 found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching 488 callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */ 489 int 490 unregister_callback (const char *plugin_name, int event) 491 { 492 struct callback_info *callback, **cbp; 493 494 if (event >= event_last) 495 return PLUGEVT_NO_SUCH_EVENT; 496 497 for (cbp = &plugin_callbacks[event]; (callback = *cbp); cbp = &callback->next) 498 if (strcmp (callback->plugin_name, plugin_name) == 0) 499 { 500 *cbp = callback->next; 501 return PLUGEVT_SUCCESS; 502 } 503 return PLUGEVT_NO_CALLBACK; 504 } 505 506 /* Invoke all plugin callbacks registered with the specified event, 507 called from invoke_plugin_callbacks(). */ 508 509 int 510 invoke_plugin_callbacks_full (int event, void *gcc_data) 511 { 512 int retval = PLUGEVT_SUCCESS; 513 514 timevar_push (TV_PLUGIN_RUN); 515 516 switch (event) 517 { 518 case PLUGIN_EVENT_FIRST_DYNAMIC: 519 default: 520 gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC); 521 gcc_assert (event < event_last); 522 /* Fall through. */ 523 case PLUGIN_START_PARSE_FUNCTION: 524 case PLUGIN_FINISH_PARSE_FUNCTION: 525 case PLUGIN_FINISH_TYPE: 526 case PLUGIN_FINISH_DECL: 527 case PLUGIN_START_UNIT: 528 case PLUGIN_FINISH_UNIT: 529 case PLUGIN_PRE_GENERICIZE: 530 case PLUGIN_ATTRIBUTES: 531 case PLUGIN_PRAGMAS: 532 case PLUGIN_FINISH: 533 case PLUGIN_GGC_START: 534 case PLUGIN_GGC_MARKING: 535 case PLUGIN_GGC_END: 536 case PLUGIN_ALL_PASSES_START: 537 case PLUGIN_ALL_PASSES_END: 538 case PLUGIN_ALL_IPA_PASSES_START: 539 case PLUGIN_ALL_IPA_PASSES_END: 540 case PLUGIN_OVERRIDE_GATE: 541 case PLUGIN_PASS_EXECUTION: 542 case PLUGIN_EARLY_GIMPLE_PASSES_START: 543 case PLUGIN_EARLY_GIMPLE_PASSES_END: 544 case PLUGIN_NEW_PASS: 545 case PLUGIN_INCLUDE_FILE: 546 { 547 /* Iterate over every callback registered with this event and 548 call it. */ 549 struct callback_info *callback = plugin_callbacks[event]; 550 551 if (!callback) 552 retval = PLUGEVT_NO_CALLBACK; 553 for ( ; callback; callback = callback->next) 554 (*callback->func) (gcc_data, callback->user_data); 555 } 556 break; 557 558 case PLUGIN_PASS_MANAGER_SETUP: 559 case PLUGIN_REGISTER_GGC_ROOTS: 560 gcc_assert (false); 561 } 562 563 timevar_pop (TV_PLUGIN_RUN); 564 return retval; 565 } 566 567 #ifdef ENABLE_PLUGIN 568 /* We need a union to cast dlsym return value to a function pointer 569 as ISO C forbids assignment between function pointer and 'void *'. 570 Use explicit union instead of __extension__(<union_cast>) for 571 portability. */ 572 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; } 573 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q) 574 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq) 575 576 /* Try to initialize PLUGIN. Return true if successful. */ 577 578 static bool 579 try_init_one_plugin (struct plugin_name_args *plugin) 580 { 581 void *dl_handle; 582 plugin_init_func plugin_init; 583 const char *err; 584 PTR_UNION_TYPE (plugin_init_func) plugin_init_union; 585 586 /* We use RTLD_NOW to accelerate binding and detect any mismatch 587 between the API expected by the plugin and the GCC API; we use 588 RTLD_GLOBAL which is useful to plugins which themselves call 589 dlopen. */ 590 dl_handle = dlopen (plugin->full_name, RTLD_NOW | RTLD_GLOBAL); 591 if (!dl_handle) 592 { 593 error ("cannot load plugin %s\n%s", plugin->full_name, dlerror ()); 594 return false; 595 } 596 597 /* Clear any existing error. */ 598 dlerror (); 599 600 /* Check the plugin license. */ 601 if (dlsym (dl_handle, str_license) == NULL) 602 fatal_error (input_location, 603 "plugin %s is not licensed under a GPL-compatible license\n" 604 "%s", plugin->full_name, dlerror ()); 605 606 PTR_UNION_AS_VOID_PTR (plugin_init_union) = 607 dlsym (dl_handle, str_plugin_init_func_name); 608 plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union); 609 610 if ((err = dlerror ()) != NULL) 611 { 612 error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name, 613 plugin->full_name, err); 614 return false; 615 } 616 617 /* Call the plugin-provided initialization routine with the arguments. */ 618 if ((*plugin_init) (plugin, &gcc_version)) 619 { 620 error ("fail to initialize plugin %s", plugin->full_name); 621 return false; 622 } 623 624 return true; 625 } 626 627 628 /* Routine to dlopen and initialize one plugin. This function is passed to 629 (and called by) the hash table traverse routine. Return 1 for the 630 htab_traverse to continue scan, 0 to stop. 631 632 SLOT - slot of the hash table element 633 INFO - auxiliary pointer handed to hash table traverse routine 634 (unused in this function) */ 635 636 static int 637 init_one_plugin (void **slot, void * ARG_UNUSED (info)) 638 { 639 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot; 640 bool ok = try_init_one_plugin (plugin); 641 if (!ok) 642 { 643 htab_remove_elt_with_hash (plugin_name_args_tab, plugin->base_name, 644 htab_hash_string (plugin->base_name)); 645 XDELETE (plugin); 646 } 647 return 1; 648 } 649 650 #endif /* ENABLE_PLUGIN */ 651 652 /* Main plugin initialization function. Called from compile_file() in 653 toplev.c. */ 654 655 void 656 initialize_plugins (void) 657 { 658 /* If no plugin was specified in the command-line, simply return. */ 659 if (!plugin_name_args_tab) 660 return; 661 662 timevar_push (TV_PLUGIN_INIT); 663 664 #ifdef ENABLE_PLUGIN 665 /* Traverse and initialize each plugin specified in the command-line. */ 666 htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL); 667 #endif 668 669 timevar_pop (TV_PLUGIN_INIT); 670 } 671 672 /* Release memory used by one plugin. */ 673 674 static int 675 finalize_one_plugin (void **slot, void * ARG_UNUSED (info)) 676 { 677 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot; 678 XDELETE (plugin); 679 return 1; 680 } 681 682 /* Free memory allocated by the plugin system. */ 683 684 void 685 finalize_plugins (void) 686 { 687 if (!plugin_name_args_tab) 688 return; 689 690 /* We can now delete the plugin_name_args object as it will no longer 691 be used. Note that base_name and argv fields (both of which were also 692 dynamically allocated) are not freed as they could still be used by 693 the plugin code. */ 694 695 htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL); 696 697 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */ 698 htab_delete (plugin_name_args_tab); 699 plugin_name_args_tab = NULL; 700 } 701 702 /* Used to pass options to htab_traverse callbacks. */ 703 704 struct print_options 705 { 706 FILE *file; 707 const char *indent; 708 }; 709 710 /* Print the version of one plugin. */ 711 712 static int 713 print_version_one_plugin (void **slot, void *data) 714 { 715 struct print_options *opt = (struct print_options *) data; 716 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot; 717 const char *version = plugin->version ? plugin->version : "Unknown version."; 718 719 fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version); 720 return 1; 721 } 722 723 /* Print the version of each plugin. */ 724 725 void 726 print_plugins_versions (FILE *file, const char *indent) 727 { 728 struct print_options opt; 729 opt.file = file; 730 opt.indent = indent; 731 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0) 732 return; 733 734 fprintf (file, "%sVersions of loaded plugins:\n", indent); 735 htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt); 736 } 737 738 /* Print help for one plugin. SLOT is the hash table slot. DATA is the 739 argument to htab_traverse_noresize. */ 740 741 static int 742 print_help_one_plugin (void **slot, void *data) 743 { 744 struct print_options *opt = (struct print_options *) data; 745 struct plugin_name_args *plugin = (struct plugin_name_args *) *slot; 746 const char *help = plugin->help ? plugin->help : "No help available ."; 747 748 char *dup = xstrdup (help); 749 char *p, *nl; 750 fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name); 751 752 for (p = nl = dup; nl; p = nl) 753 { 754 nl = strchr (nl, '\n'); 755 if (nl) 756 { 757 *nl = '\0'; 758 nl++; 759 } 760 fprintf (opt->file, " %s %s\n", opt->indent, p); 761 } 762 763 free (dup); 764 return 1; 765 } 766 767 /* Print help for each plugin. The output goes to FILE and every line starts 768 with INDENT. */ 769 770 void 771 print_plugins_help (FILE *file, const char *indent) 772 { 773 struct print_options opt; 774 opt.file = file; 775 opt.indent = indent; 776 if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0) 777 return; 778 779 fprintf (file, "%sHelp for the loaded plugins:\n", indent); 780 htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt); 781 } 782 783 784 /* Return true if plugins have been loaded. */ 785 786 bool 787 plugins_active_p (void) 788 { 789 int event; 790 791 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++) 792 if (plugin_callbacks[event]) 793 return true; 794 795 return false; 796 } 797 798 799 /* Dump to FILE the names and associated events for all the active 800 plugins. */ 801 802 DEBUG_FUNCTION void 803 dump_active_plugins (FILE *file) 804 { 805 int event; 806 807 if (!plugins_active_p ()) 808 return; 809 810 fprintf (file, FMT_FOR_PLUGIN_EVENT " | %s\n", _("Event"), _("Plugins")); 811 for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++) 812 if (plugin_callbacks[event]) 813 { 814 struct callback_info *ci; 815 816 fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]); 817 818 for (ci = plugin_callbacks[event]; ci; ci = ci->next) 819 fprintf (file, " %s", ci->plugin_name); 820 821 putc ('\n', file); 822 } 823 } 824 825 826 /* Dump active plugins to stderr. */ 827 828 DEBUG_FUNCTION void 829 debug_active_plugins (void) 830 { 831 dump_active_plugins (stderr); 832 } 833 834 /* Give a warning if plugins are present, before an ICE message asking 835 to submit a bug report. */ 836 837 void 838 warn_if_plugins (void) 839 { 840 if (plugins_active_p ()) 841 { 842 fnotice (stderr, "*** WARNING *** there are active plugins, do not report" 843 " this as a bug unless you can reproduce it without enabling" 844 " any plugins.\n"); 845 dump_active_plugins (stderr); 846 } 847 848 } 849 850 /* Likewise, as a callback from the diagnostics code. */ 851 852 void 853 plugins_internal_error_function (diagnostic_context *context ATTRIBUTE_UNUSED, 854 const char *msgid ATTRIBUTE_UNUSED, 855 va_list *ap ATTRIBUTE_UNUSED) 856 { 857 warn_if_plugins (); 858 } 859 860 /* The default version check. Compares every field in VERSION. */ 861 862 bool 863 plugin_default_version_check (struct plugin_gcc_version *gcc_version, 864 struct plugin_gcc_version *plugin_version) 865 { 866 if (!gcc_version || !plugin_version) 867 return false; 868 869 if (strcmp (gcc_version->basever, plugin_version->basever)) 870 return false; 871 if (strcmp (gcc_version->datestamp, plugin_version->datestamp)) 872 return false; 873 if (strcmp (gcc_version->devphase, plugin_version->devphase)) 874 return false; 875 if (strcmp (gcc_version->revision, plugin_version->revision)) 876 return false; 877 if (strcmp (gcc_version->configuration_arguments, 878 plugin_version->configuration_arguments)) 879 return false; 880 return true; 881 } 882 883 884 /* Return the current value of event_last, so that plugins which provide 885 additional functionality for events for the benefit of high-level plugins 886 know how many valid entries plugin_event_name holds. */ 887 888 int 889 get_event_last (void) 890 { 891 return event_last; 892 } 893 894 895 /* Retrieve the default plugin directory. The gcc driver should have passed 896 it as -iplugindir <dir> to the cc1 program, and it is queriable through the 897 -print-file-name=plugin option to gcc. */ 898 const char* 899 default_plugin_dir_name (void) 900 { 901 if (!plugindir_string) 902 fatal_error (input_location, 903 "-iplugindir <dir> option not passed from the gcc driver"); 904 return plugindir_string; 905 } 906