1 /* Support for GCC plugin mechanism.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This file is part of GCC.
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)
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.
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/>. */
20 /* This file contains the support for GCC plugin mechanism based on the
21 APIs described in doc/plugin.texi. */
26 /* If plugin support is not enabled, do not try to execute any code
27 that may reference libdl. The generic code is still compiled in to
28 avoid including to many conditional compilation paths in the rest
34 #include "coretypes.h"
37 #include "tree-pass.h"
42 /* Event names as strings. Keep in sync with enum plugin_event. */
43 const char *plugin_event_name
[] =
45 "PLUGIN_PASS_MANAGER_SETUP",
48 "PLUGIN_CXX_CP_PRE_GENERICIZE",
54 /* Object that keeps track of the plugin name and its arguments
55 when parsing the command-line options -fplugin=/path/to/NAME.so and
56 -fplugin-arg-NAME-<key>[=<value>]. */
57 struct plugin_name_args
60 const char *full_name
;
62 struct plugin_argument
*argv
;
67 /* Hash table for the plugin_name_args objects created during command-line
69 static htab_t plugin_name_args_tab
= NULL
;
71 /* List node for keeping track of plugin-registered callback. */
74 const char *plugin_name
; /* Name of plugin that registers the callback. */
75 plugin_callback_func func
; /* Callback to be called. */
76 void *user_data
; /* plugin-specified data. */
77 struct callback_info
*next
;
80 /* An array of lists of 'callback_info' objects indexed by the event id. */
81 static struct callback_info
*plugin_callbacks
[PLUGIN_EVENT_LAST
] = { NULL
};
83 /* List node for an inserted pass instance. We need to keep track of all
84 the newly-added pass instances (with 'added_pass_nodes' defined below)
85 so that we can register their dump files after pass-positioning is finished.
86 Registering dumping files needs to be post-processed or the
87 static_pass_number of the opt_pass object would be modified and mess up
88 the dump file names of future pass instances to be added. */
91 struct opt_pass
*pass
;
92 struct pass_list_node
*next
;
95 static struct pass_list_node
*added_pass_nodes
= NULL
;
96 static struct pass_list_node
*prev_added_pass_node
;
99 /* Each plugin should define an initialization function with exactly
101 static const char *str_plugin_init_func_name
= "plugin_init";
102 static const char *str_plugin_gcc_version_name
= "plugin_gcc_version";
105 /* Helper function for the hash table that compares the base_name of the
106 existing entry (S1) with the given string (S2). */
109 htab_str_eq (const void *s1
, const void *s2
)
111 const struct plugin_name_args
*plugin
= (const struct plugin_name_args
*) s1
;
112 return !strcmp (plugin
->base_name
, (const char *) s2
);
116 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
120 get_plugin_base_name (const char *full_name
)
122 /* First get the base name part of the full-path name, i.e. NAME.so. */
123 char *base_name
= xstrdup (lbasename (full_name
));
125 /* Then get rid of '.so' part of the name. */
126 strip_off_ending (base_name
, strlen (base_name
));
132 /* Create a plugin_name_args object for the give plugin and insert it to
133 the hash table. This function is called when -fplugin=/path/to/NAME.so
134 option is processed. */
137 add_new_plugin (const char* plugin_name
)
139 struct plugin_name_args
*plugin
;
141 char *base_name
= get_plugin_base_name (plugin_name
);
143 /* If this is the first -fplugin= option we encounter, create
144 'plugin_name_args_tab' hash table. */
145 if (!plugin_name_args_tab
)
146 plugin_name_args_tab
= htab_create (10, htab_hash_string
, htab_str_eq
,
149 slot
= htab_find_slot (plugin_name_args_tab
, base_name
, INSERT
);
151 /* If the same plugin (name) has been specified earlier, either emit an
152 error or a warning message depending on if they have identical full
156 plugin
= (struct plugin_name_args
*) *slot
;
157 if (strcmp (plugin
->full_name
, plugin_name
))
158 error ("Plugin %s was specified with different paths:\n%s\n%s",
159 plugin
->base_name
, plugin
->full_name
, plugin_name
);
163 plugin
= XCNEW (struct plugin_name_args
);
164 plugin
->base_name
= base_name
;
165 plugin
->full_name
= plugin_name
;
171 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
172 'plugin_argument' object for the parsed key-value pair. ARG is
173 the <name>-<key>[=<value>] part of the option. */
176 parse_plugin_arg_opt (const char *arg
)
178 size_t len
= 0, name_len
= 0, key_len
= 0, value_len
= 0;
179 const char *ptr
, *name_start
= arg
, *key_start
= NULL
, *value_start
= NULL
;
180 char *name
, *key
, *value
;
182 bool name_parsed
= false, key_parsed
= false;
184 /* Iterate over the ARG string and identify the starting character position
185 of 'name', 'key', and 'value' and their lengths. */
186 for (ptr
= arg
; *ptr
; ++ptr
)
188 /* Only the first '-' encountered is considered a separator between
189 'name' and 'key'. All the subsequent '-'s are considered part of
190 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
191 the plugin name is 'foo' and the key is 'bar-primary-key'. */
192 if (*ptr
== '-' && !name_parsed
)
200 else if (*ptr
== '=')
204 error ("Malformed option -fplugin-arg-%s (multiple '=' signs)",
210 value_start
= ptr
+ 1;
220 error ("Malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
225 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
226 Otherwise, it is the VALUE_LEN. */
232 name
= XNEWVEC (char, name_len
+ 1);
233 strncpy (name
, name_start
, name_len
);
234 name
[name_len
] = '\0';
236 /* Check if the named plugin has already been specified earlier in the
238 if (plugin_name_args_tab
239 && ((slot
= htab_find_slot (plugin_name_args_tab
, name
, NO_INSERT
))
242 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
244 key
= XNEWVEC (char, key_len
+ 1);
245 strncpy (key
, key_start
, key_len
);
249 value
= XNEWVEC (char, value_len
+ 1);
250 strncpy (value
, value_start
, value_len
);
251 value
[value_len
] = '\0';
256 /* Create a plugin_argument object for the parsed key-value pair.
257 If there are already arguments for this plugin, we will need to
258 adjust the argument array size by creating a new array and deleting
259 the old one. If the performance ever becomes an issue, we can
260 change the code by pre-allocating a larger array first. */
261 if (plugin
->argc
> 0)
263 struct plugin_argument
*args
= XNEWVEC (struct plugin_argument
,
265 memcpy (args
, plugin
->argv
,
266 sizeof (struct plugin_argument
) * plugin
->argc
);
267 XDELETEVEC (plugin
->argv
);
273 gcc_assert (plugin
->argv
== NULL
);
274 plugin
->argv
= XNEWVEC (struct plugin_argument
, 1);
278 plugin
->argv
[plugin
->argc
- 1].key
= key
;
279 plugin
->argv
[plugin
->argc
- 1].value
= value
;
282 error ("Plugin %s should be specified before -fplugin-arg-%s "
283 "in the command line", name
, arg
);
285 /* We don't need the plugin's name anymore. Just release it. */
290 /* Insert the plugin pass at the proper position. Return true if the pass
291 is successfully added.
293 PLUGIN_PASS_INFO - new pass to be inserted
294 PASS_LIST - root of the pass list to insert the new pass to */
297 position_pass (struct plugin_pass
*plugin_pass_info
,
298 struct opt_pass
**pass_list
)
300 struct opt_pass
*pass
= *pass_list
, *prev_pass
= NULL
;
301 bool success
= false;
303 for ( ; pass
; prev_pass
= pass
, pass
= pass
->next
)
305 /* Check if the current pass is of the same type as the new pass and
306 matches the name and the instance number of the reference pass. */
307 if (pass
->type
== plugin_pass_info
->pass
->type
309 && !strcmp (pass
->name
, plugin_pass_info
->reference_pass_name
)
310 && ((plugin_pass_info
->ref_pass_instance_number
== 0)
311 || (plugin_pass_info
->ref_pass_instance_number
==
312 pass
->static_pass_number
)
313 || (plugin_pass_info
->ref_pass_instance_number
== 1
314 && pass
->todo_flags_start
& TODO_mark_first_instance
)))
316 struct opt_pass
*new_pass
= plugin_pass_info
->pass
;
317 struct pass_list_node
*new_pass_node
;
319 /* The following code (if-statement) is adopted from next_pass_1. */
320 if (new_pass
->static_pass_number
)
322 new_pass
= XNEW (struct opt_pass
);
323 memcpy (new_pass
, plugin_pass_info
->pass
, sizeof (*new_pass
));
324 new_pass
->next
= NULL
;
326 new_pass
->todo_flags_start
&= ~TODO_mark_first_instance
;
328 plugin_pass_info
->pass
->static_pass_number
-= 1;
329 new_pass
->static_pass_number
=
330 -plugin_pass_info
->pass
->static_pass_number
;
334 new_pass
->todo_flags_start
|= TODO_mark_first_instance
;
335 new_pass
->static_pass_number
= -1;
338 /* Insert the new pass instance based on the positioning op. */
339 switch (plugin_pass_info
->pos_op
)
341 case PASS_POS_INSERT_AFTER
:
342 new_pass
->next
= pass
->next
;
343 pass
->next
= new_pass
;
345 case PASS_POS_INSERT_BEFORE
:
346 new_pass
->next
= pass
;
348 prev_pass
->next
= new_pass
;
350 *pass_list
= new_pass
;
352 case PASS_POS_REPLACE
:
353 new_pass
->next
= pass
->next
;
355 prev_pass
->next
= new_pass
;
357 *pass_list
= new_pass
;
358 new_pass
->sub
= pass
->sub
;
359 new_pass
->tv_id
= pass
->tv_id
;
363 error ("Invalid pass positioning operation");
367 /* Save the newly added pass (instance) in the added_pass_nodes
368 list so that we can register its dump file later. Note that
369 we cannot register the dump file now because doing so will modify
370 the static_pass_number of the opt_pass object and therefore
371 mess up the dump file name of future instances. */
372 new_pass_node
= XCNEW (struct pass_list_node
);
373 new_pass_node
->pass
= new_pass
;
374 if (!added_pass_nodes
)
375 added_pass_nodes
= new_pass_node
;
377 prev_added_pass_node
->next
= new_pass_node
;
378 prev_added_pass_node
= new_pass_node
;
383 if (pass
->sub
&& position_pass (plugin_pass_info
, &pass
->sub
))
391 /* Hook into the pass lists (trees) a new pass registered by a plugin.
393 PLUGIN_NAME - display name for the plugin
394 PASS_INFO - plugin pass information that specifies the opt_pass object,
395 reference pass, instance number, and how to position
399 register_pass (const char *plugin_name
, struct plugin_pass
*pass_info
)
401 if (!pass_info
->pass
)
403 error ("No pass specified when registering a new pass in plugin %s",
408 if (!pass_info
->reference_pass_name
)
410 error ("No reference pass specified for positioning the pass "
411 " from plugin %s", plugin_name
);
415 /* Try to insert the new pass to the pass lists. We need to check all
416 three lists as the reference pass could be in one (or all) of them. */
417 if (!position_pass (pass_info
, &all_lowering_passes
)
418 && !position_pass (pass_info
, &all_ipa_passes
)
419 && !position_pass (pass_info
, &all_passes
))
420 error ("Failed to position pass %s registered by plugin %s. "
421 "Cannot find the (specified instance of) reference pass %s",
422 pass_info
->pass
->name
, plugin_name
, pass_info
->reference_pass_name
);
425 /* OK, we have successfully inserted the new pass. We need to register
426 the dump files for the newly added pass and its duplicates (if any).
427 Because the registration of plugin passes happens after the
428 command-line options are parsed, the options that specify single
429 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
430 plugin passes. Therefore we currently can only enable dumping of
431 new plugin passes when the 'dump-all' flags (e.g. -fdump-tree-all)
432 are specified. While doing so, we also delete the pass_list_node
433 objects created during pass positioning. */
434 while (added_pass_nodes
)
436 struct pass_list_node
*next_node
= added_pass_nodes
->next
;
437 enum tree_dump_index tdi
;
438 register_one_dump_file (added_pass_nodes
->pass
);
439 if (added_pass_nodes
->pass
->type
== SIMPLE_IPA_PASS
440 || added_pass_nodes
->pass
->type
== IPA_PASS
)
442 else if (added_pass_nodes
->pass
->type
== GIMPLE_PASS
)
446 /* Check if dump-all flag is specified. */
447 if (get_dump_file_info (tdi
)->state
)
448 get_dump_file_info (added_pass_nodes
->pass
->static_pass_number
)
449 ->state
= get_dump_file_info (tdi
)->state
;
450 XDELETE (added_pass_nodes
);
451 added_pass_nodes
= next_node
;
457 /* Register additional plugin information. NAME is the name passed to
458 plugin_init. INFO is the information that should be registered. */
461 register_plugin_info (const char* name
, struct plugin_info
*info
)
463 void **slot
= htab_find_slot (plugin_name_args_tab
, name
, NO_INSERT
);
464 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
465 plugin
->version
= info
->version
;
466 plugin
->help
= info
->help
;
469 /* Called from the plugin's initialization code. Register a single callback.
470 This function can be called multiple times.
472 PLUGIN_NAME - display name for this plugin
473 EVENT - which event the callback is for
474 CALLBACK - the callback to be called at the event
475 USER_DATA - plugin-provided data */
478 register_callback (const char *plugin_name
,
479 enum plugin_event event
,
480 plugin_callback_func callback
,
485 case PLUGIN_PASS_MANAGER_SETUP
:
486 register_pass (plugin_name
, (struct plugin_pass
*) user_data
);
489 register_plugin_info (plugin_name
, (struct plugin_info
*) user_data
);
491 case PLUGIN_FINISH_TYPE
:
492 case PLUGIN_FINISH_UNIT
:
493 case PLUGIN_CXX_CP_PRE_GENERICIZE
:
496 struct callback_info
*new_callback
;
499 error ("Plugin %s registered a null callback function "
500 "for event %s", plugin_name
, plugin_event_name
[event
]);
503 new_callback
= XNEW (struct callback_info
);
504 new_callback
->plugin_name
= plugin_name
;
505 new_callback
->func
= callback
;
506 new_callback
->user_data
= user_data
;
507 new_callback
->next
= plugin_callbacks
[event
];
508 plugin_callbacks
[event
] = new_callback
;
511 case PLUGIN_EVENT_LAST
:
513 error ("Unkown callback event registered by plugin %s",
519 /* Called from inside GCC. Invoke all plug-in callbacks registered with
522 EVENT - the event identifier
523 GCC_DATA - event-specific data provided by the compiler */
526 invoke_plugin_callbacks (enum plugin_event event
, void *gcc_data
)
528 timevar_push (TV_PLUGIN_RUN
);
532 case PLUGIN_FINISH_TYPE
:
533 case PLUGIN_FINISH_UNIT
:
534 case PLUGIN_CXX_CP_PRE_GENERICIZE
:
537 /* Iterate over every callback registered with this event and
539 struct callback_info
*callback
= plugin_callbacks
[event
];
540 for ( ; callback
; callback
= callback
->next
)
541 (*callback
->func
) (gcc_data
, callback
->user_data
);
545 case PLUGIN_PASS_MANAGER_SETUP
:
546 case PLUGIN_EVENT_LAST
:
551 timevar_pop (TV_PLUGIN_RUN
);
555 /* We need a union to cast dlsym return value to a function pointer
556 as ISO C forbids assignment between function pointer and 'void *'.
557 Use explicit union instead of __extension__(<union_cast>) for
559 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
560 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
561 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
563 /* Try to initialize PLUGIN. Return true if successful. */
566 try_init_one_plugin (struct plugin_name_args
*plugin
)
569 plugin_init_func plugin_init
;
570 struct plugin_gcc_version
*version
;
572 PTR_UNION_TYPE (plugin_init_func
) plugin_init_union
;
573 PTR_UNION_TYPE (struct plugin_gcc_version
*) version_union
;
575 dl_handle
= dlopen (plugin
->full_name
, RTLD_NOW
);
578 error ("Cannot load plugin %s\n%s", plugin
->full_name
, dlerror ());
582 /* Clear any existing error. */
585 PTR_UNION_AS_VOID_PTR (plugin_init_union
) =
586 dlsym (dl_handle
, str_plugin_init_func_name
);
587 plugin_init
= PTR_UNION_AS_CAST_PTR (plugin_init_union
);
589 if ((err
= dlerror ()) != NULL
)
591 error ("Cannot find %s in plugin %s\n%s", str_plugin_init_func_name
,
592 plugin
->full_name
, err
);
596 PTR_UNION_AS_VOID_PTR (version_union
) =
597 dlsym (dl_handle
, str_plugin_gcc_version_name
);
598 version
= PTR_UNION_AS_CAST_PTR (version_union
);
600 /* Call the plugin-provided initialization routine with the arguments. */
601 if ((*plugin_init
) (plugin
->base_name
, version
, plugin
->argc
, plugin
->argv
))
603 error ("Fail to initialize plugin %s", plugin
->full_name
);
611 /* Routine to dlopen and initialize one plugin. This function is passed to
612 (and called by) the hash table traverse routine. Return 1 for the
613 htab_traverse to continue scan, 0 to stop.
615 SLOT - slot of the hash table element
616 INFO - auxiliary pointer handed to hash table traverse routine
617 (unused in this function) */
620 init_one_plugin (void **slot
, void * ARG_UNUSED (info
))
622 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
623 bool ok
= try_init_one_plugin (plugin
);
626 htab_remove_elt (plugin_name_args_tab
, plugin
->base_name
);
632 #endif /* ENABLE_PLUGIN */
634 /* Main plugin initialization function. Called from compile_file() in
638 initialize_plugins (void)
640 /* If no plugin was specified in the command-line, simply return. */
641 if (!plugin_name_args_tab
)
644 timevar_push (TV_PLUGIN_INIT
);
647 /* Traverse and initialize each plugin specified in the command-line. */
648 htab_traverse_noresize (plugin_name_args_tab
, init_one_plugin
, NULL
);
651 timevar_pop (TV_PLUGIN_INIT
);
654 /* Release memory used by one plugin. */
657 finalize_one_plugin (void **slot
, void * ARG_UNUSED (info
))
659 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
664 /* Free memory allocated by the plugin system. */
667 finalize_plugins (void)
669 if (!plugin_name_args_tab
)
672 /* We can now delete the plugin_name_args object as it will no longer
673 be used. Note that base_name and argv fields (both of which were also
674 dynamically allocated) are not freed as they could still be used by
677 htab_traverse_noresize (plugin_name_args_tab
, finalize_one_plugin
, NULL
);
679 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
680 htab_delete (plugin_name_args_tab
);
681 plugin_name_args_tab
= NULL
;
684 /* Used to pass options to htab_traverse callbacks. */
692 /* Print the version of one plugin. */
695 print_version_one_plugin (void **slot
, void *data
)
697 struct print_options
*opt
= (struct print_options
*) data
;
698 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
699 const char *version
= plugin
->version
? plugin
->version
: "Unknown version.";
701 fprintf (opt
->file
, " %s%s: %s\n", opt
->indent
, plugin
->base_name
, version
);
705 /* Print the version of each plugin. */
708 print_plugins_versions (FILE *file
, const char *indent
)
710 struct print_options opt
;
713 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
716 fprintf (file
, "%sVersions of loaded plugins:\n", indent
);
717 htab_traverse_noresize (plugin_name_args_tab
, print_version_one_plugin
, &opt
);
720 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
721 argument to htab_traverse_noresize. */
724 print_help_one_plugin (void **slot
, void *data
)
726 struct print_options
*opt
= (struct print_options
*) data
;
727 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
728 const char *help
= plugin
->help
? plugin
->help
: "No help available .";
730 char *dup
= xstrdup (help
);
732 fprintf (opt
->file
, " %s%s:\n", opt
->indent
, plugin
->base_name
);
734 for (p
= nl
= dup
; nl
; p
= nl
)
736 nl
= strchr (nl
, '\n');
742 fprintf (opt
->file
, " %s %s\n", opt
->indent
, p
);
749 /* Print help for each plugin. The output goes to FILE and every line starts
753 print_plugins_help (FILE *file
, const char *indent
)
755 struct print_options opt
;
758 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
761 fprintf (file
, "%sHelp for the loaded plugins:\n", indent
);
762 htab_traverse_noresize (plugin_name_args_tab
, print_help_one_plugin
, &opt
);
766 /* Return true if plugins have been loaded. */
769 plugins_active_p (void)
771 enum plugin_event event
;
773 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< PLUGIN_EVENT_LAST
; event
++)
774 if (plugin_callbacks
[event
])
781 /* Dump to FILE the names and associated events for all the active
785 dump_active_plugins (FILE *file
)
787 enum plugin_event event
;
789 if (!plugins_active_p ())
792 fprintf (stderr
, "Event\t\t\tPlugins\n");
793 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< PLUGIN_EVENT_LAST
; event
++)
794 if (plugin_callbacks
[event
])
796 struct callback_info
*ci
;
798 fprintf (file
, "%s\t", plugin_event_name
[event
]);
800 for (ci
= plugin_callbacks
[event
]; ci
; ci
= ci
->next
)
801 fprintf (file
, "%s ", ci
->plugin_name
);
803 fprintf (file
, "\n");
808 /* Dump active plugins to stderr. */
811 debug_active_plugins (void)
813 dump_active_plugins (stderr
);
816 /* The default version check. Compares every field in VERSION. */
819 plugin_default_version_check(struct plugin_gcc_version
*version
)
821 /* version is NULL if the plugin was not linked with plugin-version.o */
825 if (strcmp (version
->basever
, plugin_gcc_version
.basever
))
827 if (strcmp (version
->datestamp
, plugin_gcc_version
.datestamp
))
829 if (strcmp (version
->devphase
, plugin_gcc_version
.devphase
))
831 if (strcmp (version
->revision
, plugin_gcc_version
.revision
))
833 if (strcmp (version
->configuration_arguments
,
834 plugin_gcc_version
.configuration_arguments
))