1 // SPDX-License-Identifier: LGPL-2.1
12 #include <sys/types.h>
16 #include "event-parse.h"
17 #include "event-utils.h"
19 #define LOCAL_PLUGIN_DIR ".traceevent/plugins"
21 static struct registered_plugin_options {
22 struct registered_plugin_options *next;
23 struct tep_plugin_option *options;
24 } *registered_options;
26 static struct trace_plugin_options {
27 struct trace_plugin_options *next;
31 } *trace_plugin_options;
34 struct plugin_list *next;
39 static void lower_case(char *str)
47 static int update_option_value(struct tep_plugin_option *op, const char *val)
52 /* toggle, only if option is boolean */
61 * If the option has a value then it takes a string
62 * otherwise the option is a boolean.
69 /* Option is boolean, must be either "1", "0", "true" or "false" */
76 if (strcmp(val, "1") == 0 || strcmp(val, "true") == 0)
78 else if (strcmp(val, "0") == 0 || strcmp(val, "false") == 0)
86 * tep_plugin_list_options - get list of plugin options
88 * Returns an array of char strings that list the currently registered
89 * plugin options in the format of <plugin>:<option>. This list can be
90 * used by toggling the option.
92 * Returns NULL if there's no options registered. On error it returns
93 * INVALID_PLUGIN_LIST_OPTION
95 * Must be freed with tep_plugin_free_options_list().
97 char **tep_plugin_list_options(void)
99 struct registered_plugin_options *reg;
100 struct tep_plugin_option *op;
105 for (reg = registered_options; reg; reg = reg->next) {
106 for (op = reg->options; op->name; op++) {
107 char *alias = op->plugin_alias ? op->plugin_alias : op->file;
111 ret = asprintf(&name, "%s:%s", alias, op->name);
115 list = realloc(list, count + 2);
121 list[count++] = name;
132 return INVALID_PLUGIN_LIST_OPTION;
135 void tep_plugin_free_options_list(char **list)
142 if (list == INVALID_PLUGIN_LIST_OPTION)
145 for (i = 0; list[i]; i++)
152 update_option(const char *file, struct tep_plugin_option *option)
154 struct trace_plugin_options *op;
158 if (option->plugin_alias) {
159 plugin = strdup(option->plugin_alias);
164 plugin = strdup(file);
167 p = strstr(plugin, ".");
172 /* first look for named options */
173 for (op = trace_plugin_options; op; op = op->next) {
176 if (strcmp(op->plugin, plugin) != 0)
178 if (strcmp(op->option, option->name) != 0)
181 ret = update_option_value(option, op->value);
187 /* first look for unnamed options */
188 for (op = trace_plugin_options; op; op = op->next) {
191 if (strcmp(op->option, option->name) != 0)
194 ret = update_option_value(option, op->value);
204 * tep_plugin_add_options - Add a set of options by a plugin
205 * @name: The name of the plugin adding the options
206 * @options: The set of options being loaded
208 * Sets the options with the values that have been added by user.
210 int tep_plugin_add_options(const char *name,
211 struct tep_plugin_option *options)
213 struct registered_plugin_options *reg;
215 reg = malloc(sizeof(*reg));
218 reg->next = registered_options;
219 reg->options = options;
220 registered_options = reg;
222 while (options->name) {
223 update_option(name, options);
230 * tep_plugin_remove_options - remove plugin options that were registered
231 * @options: Options to removed that were registered with tep_plugin_add_options
233 void tep_plugin_remove_options(struct tep_plugin_option *options)
235 struct registered_plugin_options **last;
236 struct registered_plugin_options *reg;
238 for (last = ®istered_options; *last; last = &(*last)->next) {
239 if ((*last)->options == options) {
249 * tep_print_plugins - print out the list of plugins loaded
250 * @s: the trace_seq descripter to write to
251 * @prefix: The prefix string to add before listing the option name
252 * @suffix: The suffix string ot append after the option name
253 * @list: The list of plugins (usually returned by tep_load_plugins()
255 * Writes to the trace_seq @s the list of plugins (files) that is
256 * returned by tep_load_plugins(). Use @prefix and @suffix for formating:
257 * @prefix = " ", @suffix = "\n".
259 void tep_print_plugins(struct trace_seq *s,
260 const char *prefix, const char *suffix,
261 const struct plugin_list *list)
264 trace_seq_printf(s, "%s%s%s", prefix, list->name, suffix);
270 load_plugin(struct tep_handle *pevent, const char *path,
271 const char *file, void *data)
273 struct plugin_list **plugin_list = data;
274 tep_plugin_load_func func;
275 struct plugin_list *list;
281 ret = asprintf(&plugin, "%s/%s", path, file);
283 warning("could not allocate plugin memory\n");
287 handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL);
289 warning("could not load plugin '%s'\n%s\n",
294 alias = dlsym(handle, TEP_PLUGIN_ALIAS_NAME);
298 func = dlsym(handle, TEP_PLUGIN_LOADER_NAME);
300 warning("could not find func '%s' in plugin '%s'\n%s\n",
301 TEP_PLUGIN_LOADER_NAME, plugin, dlerror());
305 list = malloc(sizeof(*list));
307 warning("could not allocate plugin memory\n");
311 list->next = *plugin_list;
312 list->handle = handle;
316 pr_stat("registering plugin: %s", plugin);
325 load_plugins_dir(struct tep_handle *pevent, const char *suffix,
327 void (*load_plugin)(struct tep_handle *pevent,
338 ret = stat(path, &st);
342 if (!S_ISDIR(st.st_mode))
349 while ((dent = readdir(dir))) {
350 const char *name = dent->d_name;
352 if (strcmp(name, ".") == 0 ||
353 strcmp(name, "..") == 0)
356 /* Only load plugins that end in suffix */
357 if (strcmp(name + (strlen(name) - strlen(suffix)), suffix) != 0)
360 load_plugin(pevent, path, name, data);
367 load_plugins(struct tep_handle *pevent, const char *suffix,
368 void (*load_plugin)(struct tep_handle *pevent,
379 if (pevent->flags & TEP_DISABLE_PLUGINS)
383 * If a system plugin directory was defined,
387 if (!(pevent->flags & TEP_DISABLE_SYS_PLUGINS))
388 load_plugins_dir(pevent, suffix, PLUGIN_DIR,
393 * Next let the environment-set plugin directory
394 * override the system defaults.
396 envdir = getenv("TRACEEVENT_PLUGIN_DIR");
398 load_plugins_dir(pevent, suffix, envdir, load_plugin, data);
401 * Now let the home directory override the environment
402 * or system defaults.
404 home = getenv("HOME");
408 ret = asprintf(&path, "%s/%s", home, LOCAL_PLUGIN_DIR);
410 warning("could not allocate plugin memory\n");
414 load_plugins_dir(pevent, suffix, path, load_plugin, data);
420 tep_load_plugins(struct tep_handle *pevent)
422 struct plugin_list *list = NULL;
424 load_plugins(pevent, ".so", load_plugin, &list);
429 tep_unload_plugins(struct plugin_list *plugin_list, struct tep_handle *pevent)
431 tep_plugin_unload_func func;
432 struct plugin_list *list;
434 while (plugin_list) {
436 plugin_list = list->next;
437 func = dlsym(list->handle, TEP_PLUGIN_UNLOADER_NAME);
440 dlclose(list->handle);