4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <sys/types.h>
30 #include "event-parse.h"
31 #include "event-utils.h"
33 #define LOCAL_PLUGIN_DIR ".traceevent/plugins"
35 static struct registered_plugin_options {
36 struct registered_plugin_options *next;
37 struct pevent_plugin_option *options;
38 } *registered_options;
40 static struct trace_plugin_options {
41 struct trace_plugin_options *next;
45 } *trace_plugin_options;
48 struct plugin_list *next;
53 static void lower_case(char *str)
61 static int update_option_value(struct pevent_plugin_option *op, const char *val)
66 /* toggle, only if option is boolean */
75 * If the option has a value then it takes a string
76 * otherwise the option is a boolean.
83 /* Option is boolean, must be either "1", "0", "true" or "false" */
90 if (strcmp(val, "1") == 0 || strcmp(val, "true") == 0)
92 else if (strcmp(val, "0") == 0 || strcmp(val, "false") == 0)
100 * traceevent_plugin_list_options - get list of plugin options
102 * Returns an array of char strings that list the currently registered
103 * plugin options in the format of <plugin>:<option>. This list can be
104 * used by toggling the option.
106 * Returns NULL if there's no options registered. On error it returns
107 * INVALID_PLUGIN_LIST_OPTION
109 * Must be freed with traceevent_plugin_free_options_list().
111 char **traceevent_plugin_list_options(void)
113 struct registered_plugin_options *reg;
114 struct pevent_plugin_option *op;
119 for (reg = registered_options; reg; reg = reg->next) {
120 for (op = reg->options; op->name; op++) {
121 char *alias = op->plugin_alias ? op->plugin_alias : op->file;
124 name = malloc(strlen(op->name) + strlen(alias) + 2);
128 sprintf(name, "%s:%s", alias, op->name);
129 list = realloc(list, count + 2);
135 list[count++] = name;
146 return INVALID_PLUGIN_LIST_OPTION;
149 void traceevent_plugin_free_options_list(char **list)
156 if (list == INVALID_PLUGIN_LIST_OPTION)
159 for (i = 0; list[i]; i++)
166 update_option(const char *file, struct pevent_plugin_option *option)
168 struct trace_plugin_options *op;
172 if (option->plugin_alias) {
173 plugin = strdup(option->plugin_alias);
178 plugin = strdup(file);
181 p = strstr(plugin, ".");
186 /* first look for named options */
187 for (op = trace_plugin_options; op; op = op->next) {
190 if (strcmp(op->plugin, plugin) != 0)
192 if (strcmp(op->option, option->name) != 0)
195 ret = update_option_value(option, op->value);
201 /* first look for unnamed options */
202 for (op = trace_plugin_options; op; op = op->next) {
205 if (strcmp(op->option, option->name) != 0)
208 ret = update_option_value(option, op->value);
218 * traceevent_plugin_add_options - Add a set of options by a plugin
219 * @name: The name of the plugin adding the options
220 * @options: The set of options being loaded
222 * Sets the options with the values that have been added by user.
224 int traceevent_plugin_add_options(const char *name,
225 struct pevent_plugin_option *options)
227 struct registered_plugin_options *reg;
229 reg = malloc(sizeof(*reg));
232 reg->next = registered_options;
233 reg->options = options;
234 registered_options = reg;
236 while (options->name) {
237 update_option(name, options);
244 * traceevent_plugin_remove_options - remove plugin options that were registered
245 * @options: Options to removed that were registered with traceevent_plugin_add_options
247 void traceevent_plugin_remove_options(struct pevent_plugin_option *options)
249 struct registered_plugin_options **last;
250 struct registered_plugin_options *reg;
252 for (last = ®istered_options; *last; last = &(*last)->next) {
253 if ((*last)->options == options) {
263 * traceevent_print_plugins - print out the list of plugins loaded
264 * @s: the trace_seq descripter to write to
265 * @prefix: The prefix string to add before listing the option name
266 * @suffix: The suffix string ot append after the option name
267 * @list: The list of plugins (usually returned by traceevent_load_plugins()
269 * Writes to the trace_seq @s the list of plugins (files) that is
270 * returned by traceevent_load_plugins(). Use @prefix and @suffix for formating:
271 * @prefix = " ", @suffix = "\n".
273 void traceevent_print_plugins(struct trace_seq *s,
274 const char *prefix, const char *suffix,
275 const struct plugin_list *list)
278 trace_seq_printf(s, "%s%s%s", prefix, list->name, suffix);
284 load_plugin(struct pevent *pevent, const char *path,
285 const char *file, void *data)
287 struct plugin_list **plugin_list = data;
288 pevent_plugin_load_func func;
289 struct plugin_list *list;
294 plugin = malloc(strlen(path) + strlen(file) + 2);
296 warning("could not allocate plugin memory\n");
300 strcpy(plugin, path);
302 strcat(plugin, file);
304 handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL);
306 warning("could not load plugin '%s'\n%s\n",
311 alias = dlsym(handle, PEVENT_PLUGIN_ALIAS_NAME);
315 func = dlsym(handle, PEVENT_PLUGIN_LOADER_NAME);
317 warning("could not find func '%s' in plugin '%s'\n%s\n",
318 PEVENT_PLUGIN_LOADER_NAME, plugin, dlerror());
322 list = malloc(sizeof(*list));
324 warning("could not allocate plugin memory\n");
328 list->next = *plugin_list;
329 list->handle = handle;
333 pr_stat("registering plugin: %s", plugin);
342 load_plugins_dir(struct pevent *pevent, const char *suffix,
344 void (*load_plugin)(struct pevent *pevent,
355 ret = stat(path, &st);
359 if (!S_ISDIR(st.st_mode))
366 while ((dent = readdir(dir))) {
367 const char *name = dent->d_name;
369 if (strcmp(name, ".") == 0 ||
370 strcmp(name, "..") == 0)
373 /* Only load plugins that end in suffix */
374 if (strcmp(name + (strlen(name) - strlen(suffix)), suffix) != 0)
377 load_plugin(pevent, path, name, data);
384 load_plugins(struct pevent *pevent, const char *suffix,
385 void (*load_plugin)(struct pevent *pevent,
395 if (pevent->flags & PEVENT_DISABLE_PLUGINS)
399 * If a system plugin directory was defined,
403 if (!(pevent->flags & PEVENT_DISABLE_SYS_PLUGINS))
404 load_plugins_dir(pevent, suffix, PLUGIN_DIR,
409 * Next let the environment-set plugin directory
410 * override the system defaults.
412 envdir = getenv("TRACEEVENT_PLUGIN_DIR");
414 load_plugins_dir(pevent, suffix, envdir, load_plugin, data);
417 * Now let the home directory override the environment
418 * or system defaults.
420 home = getenv("HOME");
424 path = malloc(strlen(home) + strlen(LOCAL_PLUGIN_DIR) + 2);
426 warning("could not allocate plugin memory\n");
432 strcat(path, LOCAL_PLUGIN_DIR);
434 load_plugins_dir(pevent, suffix, path, load_plugin, data);
440 traceevent_load_plugins(struct pevent *pevent)
442 struct plugin_list *list = NULL;
444 load_plugins(pevent, ".so", load_plugin, &list);
449 traceevent_unload_plugins(struct plugin_list *plugin_list, struct pevent *pevent)
451 pevent_plugin_unload_func func;
452 struct plugin_list *list;
454 while (plugin_list) {
456 plugin_list = list->next;
457 func = dlsym(list->handle, PEVENT_PLUGIN_UNLOADER_NAME);
460 dlclose(list->handle);