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 #include "plugin-version.h"
45 /* Event names as strings. Keep in sync with enum plugin_event. */
46 const char *plugin_event_name
[] =
48 "PLUGIN_PASS_MANAGER_SETUP",
51 "PLUGIN_CXX_CP_PRE_GENERICIZE",
57 /* Object that keeps track of the plugin name and its arguments
58 when parsing the command-line options -fplugin=/path/to/NAME.so and
59 -fplugin-arg-NAME-<key>[=<value>]. */
60 struct plugin_name_args
63 const char *full_name
;
65 struct plugin_argument
*argv
;
70 /* Hash table for the plugin_name_args objects created during command-line
72 static htab_t plugin_name_args_tab
= NULL
;
74 /* List node for keeping track of plugin-registered callback. */
77 const char *plugin_name
; /* Name of plugin that registers the callback. */
78 plugin_callback_func func
; /* Callback to be called. */
79 void *user_data
; /* plugin-specified data. */
80 struct callback_info
*next
;
83 /* An array of lists of 'callback_info' objects indexed by the event id. */
84 static struct callback_info
*plugin_callbacks
[PLUGIN_EVENT_LAST
] = { NULL
};
86 /* List node for an inserted pass instance. We need to keep track of all
87 the newly-added pass instances (with 'added_pass_nodes' defined below)
88 so that we can register their dump files after pass-positioning is finished.
89 Registering dumping files needs to be post-processed or the
90 static_pass_number of the opt_pass object would be modified and mess up
91 the dump file names of future pass instances to be added. */
94 struct opt_pass
*pass
;
95 struct pass_list_node
*next
;
98 static struct pass_list_node
*added_pass_nodes
= NULL
;
99 static struct pass_list_node
*prev_added_pass_node
;
102 /* Each plugin should define an initialization function with exactly
104 static const char *str_plugin_init_func_name
= "plugin_init";
107 /* Helper function for the hash table that compares the base_name of the
108 existing entry (S1) with the given string (S2). */
111 htab_str_eq (const void *s1
, const void *s2
)
113 const struct plugin_name_args
*plugin
= (const struct plugin_name_args
*) s1
;
114 return !strcmp (plugin
->base_name
, (const char *) s2
);
118 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
122 get_plugin_base_name (const char *full_name
)
124 /* First get the base name part of the full-path name, i.e. NAME.so. */
125 char *base_name
= xstrdup (lbasename (full_name
));
127 /* Then get rid of '.so' part of the name. */
128 strip_off_ending (base_name
, strlen (base_name
));
134 /* Create a plugin_name_args object for the give plugin and insert it to
135 the hash table. This function is called when -fplugin=/path/to/NAME.so
136 option is processed. */
139 add_new_plugin (const char* plugin_name
)
141 struct plugin_name_args
*plugin
;
143 char *base_name
= get_plugin_base_name (plugin_name
);
145 /* If this is the first -fplugin= option we encounter, create
146 'plugin_name_args_tab' hash table. */
147 if (!plugin_name_args_tab
)
148 plugin_name_args_tab
= htab_create (10, htab_hash_string
, htab_str_eq
,
151 slot
= htab_find_slot (plugin_name_args_tab
, base_name
, INSERT
);
153 /* If the same plugin (name) has been specified earlier, either emit an
154 error or a warning message depending on if they have identical full
158 plugin
= (struct plugin_name_args
*) *slot
;
159 if (strcmp (plugin
->full_name
, plugin_name
))
160 error ("Plugin %s was specified with different paths:\n%s\n%s",
161 plugin
->base_name
, plugin
->full_name
, plugin_name
);
165 plugin
= XCNEW (struct plugin_name_args
);
166 plugin
->base_name
= base_name
;
167 plugin
->full_name
= plugin_name
;
173 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
174 'plugin_argument' object for the parsed key-value pair. ARG is
175 the <name>-<key>[=<value>] part of the option. */
178 parse_plugin_arg_opt (const char *arg
)
180 size_t len
= 0, name_len
= 0, key_len
= 0, value_len
= 0;
181 const char *ptr
, *name_start
= arg
, *key_start
= NULL
, *value_start
= NULL
;
182 char *name
, *key
, *value
;
184 bool name_parsed
= false, key_parsed
= false;
186 /* Iterate over the ARG string and identify the starting character position
187 of 'name', 'key', and 'value' and their lengths. */
188 for (ptr
= arg
; *ptr
; ++ptr
)
190 /* Only the first '-' encountered is considered a separator between
191 'name' and 'key'. All the subsequent '-'s are considered part of
192 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
193 the plugin name is 'foo' and the key is 'bar-primary-key'. */
194 if (*ptr
== '-' && !name_parsed
)
202 else if (*ptr
== '=')
206 error ("Malformed option -fplugin-arg-%s (multiple '=' signs)",
212 value_start
= ptr
+ 1;
222 error ("Malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
227 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
228 Otherwise, it is the VALUE_LEN. */
234 name
= XNEWVEC (char, name_len
+ 1);
235 strncpy (name
, name_start
, name_len
);
236 name
[name_len
] = '\0';
238 /* Check if the named plugin has already been specified earlier in the
240 if (plugin_name_args_tab
241 && ((slot
= htab_find_slot (plugin_name_args_tab
, name
, NO_INSERT
))
244 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
246 key
= XNEWVEC (char, key_len
+ 1);
247 strncpy (key
, key_start
, key_len
);
251 value
= XNEWVEC (char, value_len
+ 1);
252 strncpy (value
, value_start
, value_len
);
253 value
[value_len
] = '\0';
258 /* Create a plugin_argument object for the parsed key-value pair.
259 If there are already arguments for this plugin, we will need to
260 adjust the argument array size by creating a new array and deleting
261 the old one. If the performance ever becomes an issue, we can
262 change the code by pre-allocating a larger array first. */
263 if (plugin
->argc
> 0)
265 struct plugin_argument
*args
= XNEWVEC (struct plugin_argument
,
267 memcpy (args
, plugin
->argv
,
268 sizeof (struct plugin_argument
) * plugin
->argc
);
269 XDELETEVEC (plugin
->argv
);
275 gcc_assert (plugin
->argv
== NULL
);
276 plugin
->argv
= XNEWVEC (struct plugin_argument
, 1);
280 plugin
->argv
[plugin
->argc
- 1].key
= key
;
281 plugin
->argv
[plugin
->argc
- 1].value
= value
;
284 error ("Plugin %s should be specified before -fplugin-arg-%s "
285 "in the command line", name
, arg
);
287 /* We don't need the plugin's name anymore. Just release it. */
292 /* Insert the plugin pass at the proper position. Return true if the pass
293 is successfully added.
295 PLUGIN_PASS_INFO - new pass to be inserted
296 PASS_LIST - root of the pass list to insert the new pass to */
299 position_pass (struct plugin_pass
*plugin_pass_info
,
300 struct opt_pass
**pass_list
)
302 struct opt_pass
*pass
= *pass_list
, *prev_pass
= NULL
;
303 bool success
= false;
305 for ( ; pass
; prev_pass
= pass
, pass
= pass
->next
)
307 /* Check if the current pass is of the same type as the new pass and
308 matches the name and the instance number of the reference pass. */
309 if (pass
->type
== plugin_pass_info
->pass
->type
311 && !strcmp (pass
->name
, plugin_pass_info
->reference_pass_name
)
312 && ((plugin_pass_info
->ref_pass_instance_number
== 0)
313 || (plugin_pass_info
->ref_pass_instance_number
==
314 pass
->static_pass_number
)
315 || (plugin_pass_info
->ref_pass_instance_number
== 1
316 && pass
->todo_flags_start
& TODO_mark_first_instance
)))
318 struct opt_pass
*new_pass
= plugin_pass_info
->pass
;
319 struct pass_list_node
*new_pass_node
;
321 /* The following code (if-statement) is adopted from next_pass_1. */
322 if (new_pass
->static_pass_number
)
324 new_pass
= XNEW (struct opt_pass
);
325 memcpy (new_pass
, plugin_pass_info
->pass
, sizeof (*new_pass
));
326 new_pass
->next
= NULL
;
328 new_pass
->todo_flags_start
&= ~TODO_mark_first_instance
;
330 plugin_pass_info
->pass
->static_pass_number
-= 1;
331 new_pass
->static_pass_number
=
332 -plugin_pass_info
->pass
->static_pass_number
;
336 new_pass
->todo_flags_start
|= TODO_mark_first_instance
;
337 new_pass
->static_pass_number
= -1;
340 /* Insert the new pass instance based on the positioning op. */
341 switch (plugin_pass_info
->pos_op
)
343 case PASS_POS_INSERT_AFTER
:
344 new_pass
->next
= pass
->next
;
345 pass
->next
= new_pass
;
347 case PASS_POS_INSERT_BEFORE
:
348 new_pass
->next
= pass
;
350 prev_pass
->next
= new_pass
;
352 *pass_list
= new_pass
;
354 case PASS_POS_REPLACE
:
355 new_pass
->next
= pass
->next
;
357 prev_pass
->next
= new_pass
;
359 *pass_list
= new_pass
;
360 new_pass
->sub
= pass
->sub
;
361 new_pass
->tv_id
= pass
->tv_id
;
365 error ("Invalid pass positioning operation");
369 /* Save the newly added pass (instance) in the added_pass_nodes
370 list so that we can register its dump file later. Note that
371 we cannot register the dump file now because doing so will modify
372 the static_pass_number of the opt_pass object and therefore
373 mess up the dump file name of future instances. */
374 new_pass_node
= XCNEW (struct pass_list_node
);
375 new_pass_node
->pass
= new_pass
;
376 if (!added_pass_nodes
)
377 added_pass_nodes
= new_pass_node
;
379 prev_added_pass_node
->next
= new_pass_node
;
380 prev_added_pass_node
= new_pass_node
;
385 if (pass
->sub
&& position_pass (plugin_pass_info
, &pass
->sub
))
393 /* Hook into the pass lists (trees) a new pass registered by a plugin.
395 PLUGIN_NAME - display name for the plugin
396 PASS_INFO - plugin pass information that specifies the opt_pass object,
397 reference pass, instance number, and how to position
401 register_pass (const char *plugin_name
, struct plugin_pass
*pass_info
)
403 if (!pass_info
->pass
)
405 error ("No pass specified when registering a new pass in plugin %s",
410 if (!pass_info
->reference_pass_name
)
412 error ("No reference pass specified for positioning the pass "
413 " from plugin %s", plugin_name
);
417 /* Try to insert the new pass to the pass lists. We need to check all
418 three lists as the reference pass could be in one (or all) of them. */
419 if (!position_pass (pass_info
, &all_lowering_passes
)
420 && !position_pass (pass_info
, &all_ipa_passes
)
421 && !position_pass (pass_info
, &all_passes
))
422 error ("Failed to position pass %s registered by plugin %s. "
423 "Cannot find the (specified instance of) reference pass %s",
424 pass_info
->pass
->name
, plugin_name
, pass_info
->reference_pass_name
);
427 /* OK, we have successfully inserted the new pass. We need to register
428 the dump files for the newly added pass and its duplicates (if any).
429 Because the registration of plugin passes happens after the
430 command-line options are parsed, the options that specify single
431 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
432 plugin passes. Therefore we currently can only enable dumping of
433 new plugin passes when the 'dump-all' flags (e.g. -fdump-tree-all)
434 are specified. While doing so, we also delete the pass_list_node
435 objects created during pass positioning. */
436 while (added_pass_nodes
)
438 struct pass_list_node
*next_node
= added_pass_nodes
->next
;
439 enum tree_dump_index tdi
;
440 register_one_dump_file (added_pass_nodes
->pass
);
441 if (added_pass_nodes
->pass
->type
== SIMPLE_IPA_PASS
442 || added_pass_nodes
->pass
->type
== IPA_PASS
)
444 else if (added_pass_nodes
->pass
->type
== GIMPLE_PASS
)
448 /* Check if dump-all flag is specified. */
449 if (get_dump_file_info (tdi
)->state
)
450 get_dump_file_info (added_pass_nodes
->pass
->static_pass_number
)
451 ->state
= get_dump_file_info (tdi
)->state
;
452 XDELETE (added_pass_nodes
);
453 added_pass_nodes
= next_node
;
459 /* Register additional plugin information. NAME is the name passed to
460 plugin_init. INFO is the information that should be registered. */
463 register_plugin_info (const char* name
, struct plugin_info
*info
)
465 void **slot
= htab_find_slot (plugin_name_args_tab
, name
, NO_INSERT
);
466 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
467 plugin
->version
= info
->version
;
468 plugin
->help
= info
->help
;
471 /* Called from the plugin's initialization code. Register a single callback.
472 This function can be called multiple times.
474 PLUGIN_NAME - display name for this plugin
475 EVENT - which event the callback is for
476 CALLBACK - the callback to be called at the event
477 USER_DATA - plugin-provided data */
480 register_callback (const char *plugin_name
,
481 enum plugin_event event
,
482 plugin_callback_func callback
,
487 case PLUGIN_PASS_MANAGER_SETUP
:
488 register_pass (plugin_name
, (struct plugin_pass
*) user_data
);
491 register_plugin_info (plugin_name
, (struct plugin_info
*) user_data
);
493 case PLUGIN_FINISH_TYPE
:
494 case PLUGIN_FINISH_UNIT
:
495 case PLUGIN_CXX_CP_PRE_GENERICIZE
:
498 struct callback_info
*new_callback
;
501 error ("Plugin %s registered a null callback function "
502 "for event %s", plugin_name
, plugin_event_name
[event
]);
505 new_callback
= XNEW (struct callback_info
);
506 new_callback
->plugin_name
= plugin_name
;
507 new_callback
->func
= callback
;
508 new_callback
->user_data
= user_data
;
509 new_callback
->next
= plugin_callbacks
[event
];
510 plugin_callbacks
[event
] = new_callback
;
513 case PLUGIN_EVENT_LAST
:
515 error ("Unkown callback event registered by plugin %s",
521 /* Called from inside GCC. Invoke all plug-in callbacks registered with
524 EVENT - the event identifier
525 GCC_DATA - event-specific data provided by the compiler */
528 invoke_plugin_callbacks (enum plugin_event event
, void *gcc_data
)
530 timevar_push (TV_PLUGIN_RUN
);
534 case PLUGIN_FINISH_TYPE
:
535 case PLUGIN_FINISH_UNIT
:
536 case PLUGIN_CXX_CP_PRE_GENERICIZE
:
539 /* Iterate over every callback registered with this event and
541 struct callback_info
*callback
= plugin_callbacks
[event
];
542 for ( ; callback
; callback
= callback
->next
)
543 (*callback
->func
) (gcc_data
, callback
->user_data
);
547 case PLUGIN_PASS_MANAGER_SETUP
:
548 case PLUGIN_EVENT_LAST
:
553 timevar_pop (TV_PLUGIN_RUN
);
557 /* We need a union to cast dlsym return value to a function pointer
558 as ISO C forbids assignment between function pointer and 'void *'.
559 Use explicit union instead of __extension__(<union_cast>) for
561 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
562 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
563 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
565 /* Try to initialize PLUGIN. Return true if successful. */
568 try_init_one_plugin (struct plugin_name_args
*plugin
)
571 plugin_init_func plugin_init
;
573 PTR_UNION_TYPE (plugin_init_func
) plugin_init_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 /* Call the plugin-provided initialization routine with the arguments. */
597 if ((*plugin_init
) (plugin
->base_name
, &gcc_version
, plugin
->argc
,
600 error ("Fail to initialize plugin %s", plugin
->full_name
);
608 /* Routine to dlopen and initialize one plugin. This function is passed to
609 (and called by) the hash table traverse routine. Return 1 for the
610 htab_traverse to continue scan, 0 to stop.
612 SLOT - slot of the hash table element
613 INFO - auxiliary pointer handed to hash table traverse routine
614 (unused in this function) */
617 init_one_plugin (void **slot
, void * ARG_UNUSED (info
))
619 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
620 bool ok
= try_init_one_plugin (plugin
);
623 htab_remove_elt (plugin_name_args_tab
, plugin
->base_name
);
629 #endif /* ENABLE_PLUGIN */
631 /* Main plugin initialization function. Called from compile_file() in
635 initialize_plugins (void)
637 /* If no plugin was specified in the command-line, simply return. */
638 if (!plugin_name_args_tab
)
641 timevar_push (TV_PLUGIN_INIT
);
644 /* Traverse and initialize each plugin specified in the command-line. */
645 htab_traverse_noresize (plugin_name_args_tab
, init_one_plugin
, NULL
);
648 timevar_pop (TV_PLUGIN_INIT
);
651 /* Release memory used by one plugin. */
654 finalize_one_plugin (void **slot
, void * ARG_UNUSED (info
))
656 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
661 /* Free memory allocated by the plugin system. */
664 finalize_plugins (void)
666 if (!plugin_name_args_tab
)
669 /* We can now delete the plugin_name_args object as it will no longer
670 be used. Note that base_name and argv fields (both of which were also
671 dynamically allocated) are not freed as they could still be used by
674 htab_traverse_noresize (plugin_name_args_tab
, finalize_one_plugin
, NULL
);
676 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
677 htab_delete (plugin_name_args_tab
);
678 plugin_name_args_tab
= NULL
;
681 /* Used to pass options to htab_traverse callbacks. */
689 /* Print the version of one plugin. */
692 print_version_one_plugin (void **slot
, void *data
)
694 struct print_options
*opt
= (struct print_options
*) data
;
695 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
696 const char *version
= plugin
->version
? plugin
->version
: "Unknown version.";
698 fprintf (opt
->file
, " %s%s: %s\n", opt
->indent
, plugin
->base_name
, version
);
702 /* Print the version of each plugin. */
705 print_plugins_versions (FILE *file
, const char *indent
)
707 struct print_options opt
;
710 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
713 fprintf (file
, "%sVersions of loaded plugins:\n", indent
);
714 htab_traverse_noresize (plugin_name_args_tab
, print_version_one_plugin
, &opt
);
717 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
718 argument to htab_traverse_noresize. */
721 print_help_one_plugin (void **slot
, void *data
)
723 struct print_options
*opt
= (struct print_options
*) data
;
724 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
725 const char *help
= plugin
->help
? plugin
->help
: "No help available .";
727 char *dup
= xstrdup (help
);
729 fprintf (opt
->file
, " %s%s:\n", opt
->indent
, plugin
->base_name
);
731 for (p
= nl
= dup
; nl
; p
= nl
)
733 nl
= strchr (nl
, '\n');
739 fprintf (opt
->file
, " %s %s\n", opt
->indent
, p
);
746 /* Print help for each plugin. The output goes to FILE and every line starts
750 print_plugins_help (FILE *file
, const char *indent
)
752 struct print_options opt
;
755 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
758 fprintf (file
, "%sHelp for the loaded plugins:\n", indent
);
759 htab_traverse_noresize (plugin_name_args_tab
, print_help_one_plugin
, &opt
);
763 /* Return true if plugins have been loaded. */
766 plugins_active_p (void)
768 enum plugin_event event
;
770 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< PLUGIN_EVENT_LAST
; event
++)
771 if (plugin_callbacks
[event
])
778 /* Dump to FILE the names and associated events for all the active
782 dump_active_plugins (FILE *file
)
784 enum plugin_event event
;
786 if (!plugins_active_p ())
789 fprintf (stderr
, "Event\t\t\tPlugins\n");
790 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< PLUGIN_EVENT_LAST
; event
++)
791 if (plugin_callbacks
[event
])
793 struct callback_info
*ci
;
795 fprintf (file
, "%s\t", plugin_event_name
[event
]);
797 for (ci
= plugin_callbacks
[event
]; ci
; ci
= ci
->next
)
798 fprintf (file
, "%s ", ci
->plugin_name
);
800 fprintf (file
, "\n");
805 /* Dump active plugins to stderr. */
808 debug_active_plugins (void)
810 dump_active_plugins (stderr
);
813 /* The default version check. Compares every field in VERSION. */
816 plugin_default_version_check (struct plugin_gcc_version
*gcc_version
,
817 struct plugin_gcc_version
*plugin_version
)
819 /* version is NULL if the plugin was not linked with plugin-version.o */
820 if (!gcc_version
|| !plugin_version
)
823 if (strcmp (gcc_version
->basever
, plugin_version
->basever
))
825 if (strcmp (gcc_version
->datestamp
, plugin_version
->datestamp
))
827 if (strcmp (gcc_version
->devphase
, plugin_version
->devphase
))
829 if (strcmp (gcc_version
->revision
, plugin_version
->revision
))
831 if (strcmp (gcc_version
->configuration_arguments
,
832 plugin_version
->configuration_arguments
))