1 // SPDX-License-Identifier: LGPL-2.1
3 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
12 #include <sys/types.h>
17 #include "event-parse.h"
18 #include "event-parse-local.h"
19 #include "event-utils.h"
20 #include "trace-seq.h"
22 #define LOCAL_PLUGIN_DIR ".local/lib/traceevent/plugins/"
24 static struct registered_plugin_options {
25 struct registered_plugin_options *next;
26 struct tep_plugin_option *options;
27 } *registered_options;
29 static struct trace_plugin_options {
30 struct trace_plugin_options *next;
34 } *trace_plugin_options;
36 struct tep_plugin_list {
37 struct tep_plugin_list *next;
42 struct tep_plugins_dir {
43 struct tep_plugins_dir *next;
45 enum tep_plugin_load_priority prio;
48 static void lower_case(char *str)
56 static int update_option_value(struct tep_plugin_option *op, const char *val)
61 /* toggle, only if option is boolean */
70 * If the option has a value then it takes a string
71 * otherwise the option is a boolean.
78 /* Option is boolean, must be either "1", "0", "true" or "false" */
85 if (strcmp(val, "1") == 0 || strcmp(val, "true") == 0)
87 else if (strcmp(val, "0") == 0 || strcmp(val, "false") == 0)
95 * tep_plugin_list_options - get list of plugin options
97 * Returns an array of char strings that list the currently registered
98 * plugin options in the format of <plugin>:<option>. This list can be
99 * used by toggling the option.
101 * Returns NULL if there's no options registered. On error it returns
102 * INVALID_PLUGIN_LIST_OPTION
104 * Must be freed with tep_plugin_free_options_list().
106 char **tep_plugin_list_options(void)
108 struct registered_plugin_options *reg;
109 struct tep_plugin_option *op;
114 for (reg = registered_options; reg; reg = reg->next) {
115 for (op = reg->options; op->name; op++) {
116 char *alias = op->plugin_alias ? op->plugin_alias : op->file;
120 ret = asprintf(&name, "%s:%s", alias, op->name);
124 list = realloc(list, count + 2);
130 list[count++] = name;
141 return INVALID_PLUGIN_LIST_OPTION;
144 void tep_plugin_free_options_list(char **list)
151 if (list == INVALID_PLUGIN_LIST_OPTION)
154 for (i = 0; list[i]; i++)
161 update_option(const char *file, struct tep_plugin_option *option)
163 struct trace_plugin_options *op;
167 if (option->plugin_alias) {
168 plugin = strdup(option->plugin_alias);
173 plugin = strdup(file);
176 p = strstr(plugin, ".");
181 /* first look for named options */
182 for (op = trace_plugin_options; op; op = op->next) {
185 if (strcmp(op->plugin, plugin) != 0)
187 if (strcmp(op->option, option->name) != 0)
190 ret = update_option_value(option, op->value);
196 /* first look for unnamed options */
197 for (op = trace_plugin_options; op; op = op->next) {
200 if (strcmp(op->option, option->name) != 0)
203 ret = update_option_value(option, op->value);
213 * tep_plugin_add_options - Add a set of options by a plugin
214 * @name: The name of the plugin adding the options
215 * @options: The set of options being loaded
217 * Sets the options with the values that have been added by user.
219 int tep_plugin_add_options(const char *name,
220 struct tep_plugin_option *options)
222 struct registered_plugin_options *reg;
224 reg = malloc(sizeof(*reg));
227 reg->next = registered_options;
228 reg->options = options;
229 registered_options = reg;
231 while (options->name) {
232 update_option(name, options);
239 * tep_plugin_remove_options - remove plugin options that were registered
240 * @options: Options to removed that were registered with tep_plugin_add_options
242 void tep_plugin_remove_options(struct tep_plugin_option *options)
244 struct registered_plugin_options **last;
245 struct registered_plugin_options *reg;
247 for (last = ®istered_options; *last; last = &(*last)->next) {
248 if ((*last)->options == options) {
257 static int parse_option_name(char **option, char **plugin)
263 if ((p = strstr(*option, ":"))) {
266 *option = strdup(p + 1);
273 static struct tep_plugin_option *
274 find_registered_option(const char *plugin, const char *option)
276 struct registered_plugin_options *reg;
277 struct tep_plugin_option *op;
278 const char *op_plugin;
280 for (reg = registered_options; reg; reg = reg->next) {
281 for (op = reg->options; op->name; op++) {
282 if (op->plugin_alias)
283 op_plugin = op->plugin_alias;
285 op_plugin = op->file;
287 if (plugin && strcmp(plugin, op_plugin) != 0)
289 if (strcmp(option, op->name) != 0)
299 static int process_option(const char *plugin, const char *option, const char *val)
301 struct tep_plugin_option *op;
303 op = find_registered_option(plugin, option);
307 return update_option_value(op, val);
311 * tep_plugin_add_option - add an option/val pair to set plugin options
312 * @name: The name of the option (format: <plugin>:<option> or just <option>)
313 * @val: (optional) the value for the option
315 * Modify a plugin option. If @val is given than the value of the option
316 * is set (note, some options just take a boolean, so @val must be either
317 * "1" or "0" or "true" or "false").
319 int tep_plugin_add_option(const char *name, const char *val)
321 struct trace_plugin_options *op;
325 option_str = strdup(name);
329 if (parse_option_name(&option_str, &plugin) < 0)
332 /* If the option exists, update the val */
333 for (op = trace_plugin_options; op; op = op->next) {
334 /* Both must be NULL or not NULL */
335 if ((!plugin || !op->plugin) && plugin != op->plugin)
337 if (plugin && strcmp(plugin, op->plugin) != 0)
339 if (strcmp(op->option, option_str) != 0)
345 op->value = strdup(val);
351 /* plugin and option_str don't get freed at the end */
356 option_str = op->option;
360 /* If not found, create */
362 op = malloc(sizeof(*op));
365 memset(op, 0, sizeof(*op));
367 op->option = option_str;
369 op->value = strdup(val);
375 op->next = trace_plugin_options;
376 trace_plugin_options = op;
379 return process_option(plugin, option_str, val);
387 static void print_op_data(struct trace_seq *s, const char *name,
391 trace_seq_printf(s, "%8s:\t%s\n", name, op);
395 * tep_plugin_print_options - print out the registered plugin options
396 * @s: The trace_seq descriptor to write the plugin options into
398 * Writes a list of options into trace_seq @s.
400 void tep_plugin_print_options(struct trace_seq *s)
402 struct registered_plugin_options *reg;
403 struct tep_plugin_option *op;
405 for (reg = registered_options; reg; reg = reg->next) {
406 if (reg != registered_options)
407 trace_seq_printf(s, "============\n");
408 for (op = reg->options; op->name; op++) {
409 if (op != reg->options)
410 trace_seq_printf(s, "------------\n");
411 print_op_data(s, "file", op->file);
412 print_op_data(s, "plugin", op->plugin_alias);
413 print_op_data(s, "option", op->name);
414 print_op_data(s, "desc", op->description);
415 print_op_data(s, "value", op->value);
416 trace_seq_printf(s, "%8s:\t%d\n", "set", op->set);
422 * tep_print_plugins - print out the list of plugins loaded
423 * @s: the trace_seq descripter to write to
424 * @prefix: The prefix string to add before listing the option name
425 * @suffix: The suffix string ot append after the option name
426 * @list: The list of plugins (usually returned by tep_load_plugins()
428 * Writes to the trace_seq @s the list of plugins (files) that is
429 * returned by tep_load_plugins(). Use @prefix and @suffix for formating:
430 * @prefix = " ", @suffix = "\n".
432 void tep_print_plugins(struct trace_seq *s,
433 const char *prefix, const char *suffix,
434 const struct tep_plugin_list *list)
437 trace_seq_printf(s, "%s%s%s", prefix, list->name, suffix);
443 load_plugin(struct tep_handle *tep, const char *path,
444 const char *file, void *data)
446 struct tep_plugin_list **plugin_list = data;
447 struct tep_plugin_option *options;
448 tep_plugin_load_func func;
449 struct tep_plugin_list *list;
455 ret = asprintf(&plugin, "%s/%s", path, file);
457 warning("could not allocate plugin memory\n");
461 handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL);
463 warning("could not load plugin '%s'\n%s\n",
468 alias = dlsym(handle, TEP_PLUGIN_ALIAS_NAME);
472 options = dlsym(handle, TEP_PLUGIN_OPTIONS_NAME);
474 while (options->name) {
475 ret = update_option(alias, options);
482 func = dlsym(handle, TEP_PLUGIN_LOADER_NAME);
484 warning("could not find func '%s' in plugin '%s'\n%s\n",
485 TEP_PLUGIN_LOADER_NAME, plugin, dlerror());
489 list = malloc(sizeof(*list));
491 warning("could not allocate plugin memory\n");
495 list->next = *plugin_list;
496 list->handle = handle;
500 pr_stat("registering plugin: %s", plugin);
509 load_plugins_dir(struct tep_handle *tep, const char *suffix,
511 void (*load_plugin)(struct tep_handle *tep,
522 ret = stat(path, &st);
526 if (!S_ISDIR(st.st_mode))
533 while ((dent = readdir(dir))) {
534 const char *name = dent->d_name;
536 if (strcmp(name, ".") == 0 ||
537 strcmp(name, "..") == 0)
540 /* Only load plugins that end in suffix */
541 if (strcmp(name + (strlen(name) - strlen(suffix)), suffix) != 0)
544 load_plugin(tep, path, name, data);
551 * tep_load_plugins_hook - call a user specified callback to load a plugin
552 * @tep: handler to traceevent context
553 * @suffix: filter only plugin files with given suffix
554 * @load_plugin: user specified callback, called for each plugin file
555 * @data: custom context, passed to @load_plugin
557 * Searches for traceevent plugin files and calls @load_plugin for each
558 * The order of plugins search is:
559 * - Directories, specified in @tep->plugins_dir and priority TEP_PLUGIN_FIRST
560 * - Directory, specified at compile time with PLUGIN_TRACEEVENT_DIR
561 * - Directory, specified by environment variable TRACEEVENT_PLUGIN_DIR
562 * - In user's home: ~/.local/lib/traceevent/plugins/
563 * - Directories, specified in @tep->plugins_dir and priority TEP_PLUGIN_LAST
566 void tep_load_plugins_hook(struct tep_handle *tep, const char *suffix,
567 void (*load_plugin)(struct tep_handle *tep,
573 struct tep_plugins_dir *dir = NULL;
579 if (tep && tep->flags & TEP_DISABLE_PLUGINS)
583 dir = tep->plugins_dir;
585 if (dir->prio == TEP_PLUGIN_FIRST)
586 load_plugins_dir(tep, suffix, dir->path,
592 * If a system plugin directory was defined,
596 if (!tep || !(tep->flags & TEP_DISABLE_SYS_PLUGINS))
597 load_plugins_dir(tep, suffix, PLUGIN_DIR,
602 * Next let the environment-set plugin directory
603 * override the system defaults.
605 envdir = getenv("TRACEEVENT_PLUGIN_DIR");
607 load_plugins_dir(tep, suffix, envdir, load_plugin, data);
610 * Now let the home directory override the environment
611 * or system defaults.
613 home = getenv("HOME");
617 ret = asprintf(&path, "%s/%s", home, LOCAL_PLUGIN_DIR);
619 warning("could not allocate plugin memory\n");
623 load_plugins_dir(tep, suffix, path, load_plugin, data);
626 dir = tep->plugins_dir;
628 if (dir->prio == TEP_PLUGIN_LAST)
629 load_plugins_dir(tep, suffix, dir->path,
637 struct tep_plugin_list*
638 tep_load_plugins(struct tep_handle *tep)
640 struct tep_plugin_list *list = NULL;
642 tep_load_plugins_hook(tep, ".so", load_plugin, &list);
647 * tep_add_plugin_path - Add a new plugin directory.
648 * @tep: Trace event handler.
649 * @path: Path to a directory. All plugin files in that
650 * directory will be loaded.
651 *@prio: Load priority of the plugins in that directory.
653 * Returns -1 in case of an error, 0 otherwise.
655 int tep_add_plugin_path(struct tep_handle *tep, char *path,
656 enum tep_plugin_load_priority prio)
658 struct tep_plugins_dir *dir;
663 dir = calloc(1, sizeof(*dir));
667 dir->path = strdup(path);
673 dir->next = tep->plugins_dir;
674 tep->plugins_dir = dir;
679 __hidden void free_tep_plugin_paths(struct tep_handle *tep)
681 struct tep_plugins_dir *dir;
686 dir = tep->plugins_dir;
688 tep->plugins_dir = tep->plugins_dir->next;
691 dir = tep->plugins_dir;
696 tep_unload_plugins(struct tep_plugin_list *plugin_list, struct tep_handle *tep)
698 tep_plugin_unload_func func;
699 struct tep_plugin_list *list;
701 while (plugin_list) {
703 plugin_list = list->next;
704 func = dlsym(list->handle, TEP_PLUGIN_UNLOADER_NAME);
707 dlclose(list->handle);