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>
16 #include "event-parse.h"
17 #include "event-parse-local.h"
18 #include "event-utils.h"
19 #include "trace-seq.h"
21 #define LOCAL_PLUGIN_DIR ".traceevent/plugins"
23 static struct registered_plugin_options
{
24 struct registered_plugin_options
*next
;
25 struct tep_plugin_option
*options
;
26 } *registered_options
;
28 static struct trace_plugin_options
{
29 struct trace_plugin_options
*next
;
33 } *trace_plugin_options
;
35 struct tep_plugin_list
{
36 struct tep_plugin_list
*next
;
41 static void lower_case(char *str
)
49 static int update_option_value(struct tep_plugin_option
*op
, const char *val
)
54 /* toggle, only if option is boolean */
63 * If the option has a value then it takes a string
64 * otherwise the option is a boolean.
71 /* Option is boolean, must be either "1", "0", "true" or "false" */
78 if (strcmp(val
, "1") == 0 || strcmp(val
, "true") == 0)
80 else if (strcmp(val
, "0") == 0 || strcmp(val
, "false") == 0)
88 * tep_plugin_list_options - get list of plugin options
90 * Returns an array of char strings that list the currently registered
91 * plugin options in the format of <plugin>:<option>. This list can be
92 * used by toggling the option.
94 * Returns NULL if there's no options registered. On error it returns
95 * INVALID_PLUGIN_LIST_OPTION
97 * Must be freed with tep_plugin_free_options_list().
99 char **tep_plugin_list_options(void)
101 struct registered_plugin_options
*reg
;
102 struct tep_plugin_option
*op
;
107 for (reg
= registered_options
; reg
; reg
= reg
->next
) {
108 for (op
= reg
->options
; op
->name
; op
++) {
109 char *alias
= op
->plugin_alias
? op
->plugin_alias
: op
->file
;
113 ret
= asprintf(&name
, "%s:%s", alias
, op
->name
);
117 list
= realloc(list
, count
+ 2);
123 list
[count
++] = name
;
134 return INVALID_PLUGIN_LIST_OPTION
;
137 void tep_plugin_free_options_list(char **list
)
144 if (list
== INVALID_PLUGIN_LIST_OPTION
)
147 for (i
= 0; list
[i
]; i
++)
154 update_option(const char *file
, struct tep_plugin_option
*option
)
156 struct trace_plugin_options
*op
;
160 if (option
->plugin_alias
) {
161 plugin
= strdup(option
->plugin_alias
);
166 plugin
= strdup(file
);
169 p
= strstr(plugin
, ".");
174 /* first look for named options */
175 for (op
= trace_plugin_options
; op
; op
= op
->next
) {
178 if (strcmp(op
->plugin
, plugin
) != 0)
180 if (strcmp(op
->option
, option
->name
) != 0)
183 ret
= update_option_value(option
, op
->value
);
189 /* first look for unnamed options */
190 for (op
= trace_plugin_options
; op
; op
= op
->next
) {
193 if (strcmp(op
->option
, option
->name
) != 0)
196 ret
= update_option_value(option
, op
->value
);
206 * tep_plugin_add_options - Add a set of options by a plugin
207 * @name: The name of the plugin adding the options
208 * @options: The set of options being loaded
210 * Sets the options with the values that have been added by user.
212 int tep_plugin_add_options(const char *name
,
213 struct tep_plugin_option
*options
)
215 struct registered_plugin_options
*reg
;
217 reg
= malloc(sizeof(*reg
));
220 reg
->next
= registered_options
;
221 reg
->options
= options
;
222 registered_options
= reg
;
224 while (options
->name
) {
225 update_option(name
, options
);
232 * tep_plugin_remove_options - remove plugin options that were registered
233 * @options: Options to removed that were registered with tep_plugin_add_options
235 void tep_plugin_remove_options(struct tep_plugin_option
*options
)
237 struct registered_plugin_options
**last
;
238 struct registered_plugin_options
*reg
;
240 for (last
= ®istered_options
; *last
; last
= &(*last
)->next
) {
241 if ((*last
)->options
== options
) {
251 * tep_print_plugins - print out the list of plugins loaded
252 * @s: the trace_seq descripter to write to
253 * @prefix: The prefix string to add before listing the option name
254 * @suffix: The suffix string ot append after the option name
255 * @list: The list of plugins (usually returned by tep_load_plugins()
257 * Writes to the trace_seq @s the list of plugins (files) that is
258 * returned by tep_load_plugins(). Use @prefix and @suffix for formating:
259 * @prefix = " ", @suffix = "\n".
261 void tep_print_plugins(struct trace_seq
*s
,
262 const char *prefix
, const char *suffix
,
263 const struct tep_plugin_list
*list
)
266 trace_seq_printf(s
, "%s%s%s", prefix
, list
->name
, suffix
);
272 load_plugin(struct tep_handle
*pevent
, const char *path
,
273 const char *file
, void *data
)
275 struct tep_plugin_list
**plugin_list
= data
;
276 tep_plugin_load_func func
;
277 struct tep_plugin_list
*list
;
283 ret
= asprintf(&plugin
, "%s/%s", path
, file
);
285 warning("could not allocate plugin memory\n");
289 handle
= dlopen(plugin
, RTLD_NOW
| RTLD_GLOBAL
);
291 warning("could not load plugin '%s'\n%s\n",
296 alias
= dlsym(handle
, TEP_PLUGIN_ALIAS_NAME
);
300 func
= dlsym(handle
, TEP_PLUGIN_LOADER_NAME
);
302 warning("could not find func '%s' in plugin '%s'\n%s\n",
303 TEP_PLUGIN_LOADER_NAME
, plugin
, dlerror());
307 list
= malloc(sizeof(*list
));
309 warning("could not allocate plugin memory\n");
313 list
->next
= *plugin_list
;
314 list
->handle
= handle
;
318 pr_stat("registering plugin: %s", plugin
);
327 load_plugins_dir(struct tep_handle
*pevent
, const char *suffix
,
329 void (*load_plugin
)(struct tep_handle
*pevent
,
340 ret
= stat(path
, &st
);
344 if (!S_ISDIR(st
.st_mode
))
351 while ((dent
= readdir(dir
))) {
352 const char *name
= dent
->d_name
;
354 if (strcmp(name
, ".") == 0 ||
355 strcmp(name
, "..") == 0)
358 /* Only load plugins that end in suffix */
359 if (strcmp(name
+ (strlen(name
) - strlen(suffix
)), suffix
) != 0)
362 load_plugin(pevent
, path
, name
, data
);
369 load_plugins(struct tep_handle
*pevent
, const char *suffix
,
370 void (*load_plugin
)(struct tep_handle
*pevent
,
381 if (pevent
->flags
& TEP_DISABLE_PLUGINS
)
385 * If a system plugin directory was defined,
389 if (!(pevent
->flags
& TEP_DISABLE_SYS_PLUGINS
))
390 load_plugins_dir(pevent
, suffix
, PLUGIN_DIR
,
395 * Next let the environment-set plugin directory
396 * override the system defaults.
398 envdir
= getenv("TRACEEVENT_PLUGIN_DIR");
400 load_plugins_dir(pevent
, suffix
, envdir
, load_plugin
, data
);
403 * Now let the home directory override the environment
404 * or system defaults.
406 home
= getenv("HOME");
410 ret
= asprintf(&path
, "%s/%s", home
, LOCAL_PLUGIN_DIR
);
412 warning("could not allocate plugin memory\n");
416 load_plugins_dir(pevent
, suffix
, path
, load_plugin
, data
);
421 struct tep_plugin_list
*
422 tep_load_plugins(struct tep_handle
*pevent
)
424 struct tep_plugin_list
*list
= NULL
;
426 load_plugins(pevent
, ".so", load_plugin
, &list
);
431 tep_unload_plugins(struct tep_plugin_list
*plugin_list
, struct tep_handle
*pevent
)
433 tep_plugin_unload_func func
;
434 struct tep_plugin_list
*list
;
436 while (plugin_list
) {
438 plugin_list
= list
->next
;
439 func
= dlsym(list
->handle
, TEP_PLUGIN_UNLOADER_NAME
);
442 dlclose(list
->handle
);