^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: LGPL-2.1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <dlfcn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "event-parse.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "event-parse-local.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "event-utils.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "trace-seq.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define LOCAL_PLUGIN_DIR ".local/lib/traceevent/plugins/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static struct registered_plugin_options {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct registered_plugin_options *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct tep_plugin_option *options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) } *registered_options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static struct trace_plugin_options {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct trace_plugin_options *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) char *plugin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) char *option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) char *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) } *trace_plugin_options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct tep_plugin_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct tep_plugin_list *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct tep_plugins_dir {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct tep_plugins_dir *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) enum tep_plugin_load_priority prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static void lower_case(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (!str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) for (; *str; str++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *str = tolower(*str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int update_option_value(struct tep_plugin_option *op, const char *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) char *op_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* toggle, only if option is boolean */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (op->value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* Warn? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) op->set ^= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * If the option has a value then it takes a string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * otherwise the option is a boolean.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (op->value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) op->value = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* Option is boolean, must be either "1", "0", "true" or "false" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) op_val = strdup(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (!op_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) lower_case(op_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (strcmp(val, "1") == 0 || strcmp(val, "true") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) op->set = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) else if (strcmp(val, "0") == 0 || strcmp(val, "false") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) op->set = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) free(op_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * tep_plugin_list_options - get list of plugin options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * Returns an array of char strings that list the currently registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * plugin options in the format of <plugin>:<option>. This list can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * used by toggling the option.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * Returns NULL if there's no options registered. On error it returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * INVALID_PLUGIN_LIST_OPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * Must be freed with tep_plugin_free_options_list().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) char **tep_plugin_list_options(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct registered_plugin_options *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct tep_plugin_option *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) char **list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) for (reg = registered_options; reg; reg = reg->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) for (op = reg->options; op->name; op++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) char *alias = op->plugin_alias ? op->plugin_alias : op->file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) char **temp = list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ret = asprintf(&name, "%s:%s", alias, op->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) list = realloc(list, count + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) list = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) free(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) list[count++] = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) list[count] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) while (--count >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) free(list[count]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) free(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return INVALID_PLUGIN_LIST_OPTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) void tep_plugin_free_options_list(char **list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (!list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (list == INVALID_PLUGIN_LIST_OPTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) for (i = 0; list[i]; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) free(list[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) free(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) update_option(const char *file, struct tep_plugin_option *option)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct trace_plugin_options *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) char *plugin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (option->plugin_alias) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) plugin = strdup(option->plugin_alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!plugin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) plugin = strdup(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!plugin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) p = strstr(plugin, ".");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* first look for named options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) for (op = trace_plugin_options; op; op = op->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!op->plugin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (strcmp(op->plugin, plugin) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (strcmp(op->option, option->name) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ret = update_option_value(option, op->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /* first look for unnamed options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) for (op = trace_plugin_options; op; op = op->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (op->plugin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (strcmp(op->option, option->name) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) ret = update_option_value(option, op->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) free(plugin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * tep_plugin_add_options - Add a set of options by a plugin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * @name: The name of the plugin adding the options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * @options: The set of options being loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * Sets the options with the values that have been added by user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) int tep_plugin_add_options(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct tep_plugin_option *options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct registered_plugin_options *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) reg = malloc(sizeof(*reg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (!reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) reg->next = registered_options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) reg->options = options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) registered_options = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) while (options->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) update_option(name, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) options++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * tep_plugin_remove_options - remove plugin options that were registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * @options: Options to removed that were registered with tep_plugin_add_options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) void tep_plugin_remove_options(struct tep_plugin_option *options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct registered_plugin_options **last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct registered_plugin_options *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) for (last = ®istered_options; *last; last = &(*last)->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if ((*last)->options == options) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) reg = *last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *last = reg->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) free(reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static int parse_option_name(char **option, char **plugin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) *plugin = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if ((p = strstr(*option, ":"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) *plugin = *option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) *option = strdup(p + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (!*option)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static struct tep_plugin_option *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) find_registered_option(const char *plugin, const char *option)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct registered_plugin_options *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct tep_plugin_option *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) const char *op_plugin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) for (reg = registered_options; reg; reg = reg->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) for (op = reg->options; op->name; op++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (op->plugin_alias)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) op_plugin = op->plugin_alias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) op_plugin = op->file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (plugin && strcmp(plugin, op_plugin) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (strcmp(option, op->name) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int process_option(const char *plugin, const char *option, const char *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct tep_plugin_option *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) op = find_registered_option(plugin, option);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (!op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return update_option_value(op, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * tep_plugin_add_option - add an option/val pair to set plugin options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * @name: The name of the option (format: <plugin>:<option> or just <option>)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * @val: (optional) the value for the option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * Modify a plugin option. If @val is given than the value of the option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * is set (note, some options just take a boolean, so @val must be either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * "1" or "0" or "true" or "false").
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) int tep_plugin_add_option(const char *name, const char *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct trace_plugin_options *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) char *option_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) char *plugin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) option_str = strdup(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (!option_str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (parse_option_name(&option_str, &plugin) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* If the option exists, update the val */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) for (op = trace_plugin_options; op; op = op->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* Both must be NULL or not NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if ((!plugin || !op->plugin) && plugin != op->plugin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (plugin && strcmp(plugin, op->plugin) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (strcmp(op->option, option_str) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /* update option */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) free(op->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) op->value = strdup(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (!op->value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) op->value = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /* plugin and option_str don't get freed at the end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) free(plugin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) free(option_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) plugin = op->plugin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) option_str = op->option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /* If not found, create */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (!op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) op = malloc(sizeof(*op));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (!op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) memset(op, 0, sizeof(*op));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) op->plugin = plugin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) op->option = option_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) op->value = strdup(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (!op->value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) free(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) op->next = trace_plugin_options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) trace_plugin_options = op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return process_option(plugin, option_str, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) free(plugin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) free(option_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static void print_op_data(struct trace_seq *s, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) const char *op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) trace_seq_printf(s, "%8s:\t%s\n", name, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * tep_plugin_print_options - print out the registered plugin options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * @s: The trace_seq descriptor to write the plugin options into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * Writes a list of options into trace_seq @s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) void tep_plugin_print_options(struct trace_seq *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) struct registered_plugin_options *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct tep_plugin_option *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) for (reg = registered_options; reg; reg = reg->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (reg != registered_options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) trace_seq_printf(s, "============\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) for (op = reg->options; op->name; op++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (op != reg->options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) trace_seq_printf(s, "------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) print_op_data(s, "file", op->file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) print_op_data(s, "plugin", op->plugin_alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) print_op_data(s, "option", op->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) print_op_data(s, "desc", op->description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) print_op_data(s, "value", op->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) trace_seq_printf(s, "%8s:\t%d\n", "set", op->set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * tep_print_plugins - print out the list of plugins loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * @s: the trace_seq descripter to write to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * @prefix: The prefix string to add before listing the option name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * @suffix: The suffix string ot append after the option name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * @list: The list of plugins (usually returned by tep_load_plugins()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * Writes to the trace_seq @s the list of plugins (files) that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * returned by tep_load_plugins(). Use @prefix and @suffix for formating:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * @prefix = " ", @suffix = "\n".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) void tep_print_plugins(struct trace_seq *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) const char *prefix, const char *suffix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) const struct tep_plugin_list *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) while (list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) trace_seq_printf(s, "%s%s%s", prefix, list->name, suffix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) list = list->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) load_plugin(struct tep_handle *tep, const char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) const char *file, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) struct tep_plugin_list **plugin_list = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct tep_plugin_option *options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) tep_plugin_load_func func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct tep_plugin_list *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) const char *alias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) char *plugin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) void *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) ret = asprintf(&plugin, "%s/%s", path, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) warning("could not allocate plugin memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (!handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) warning("could not load plugin '%s'\n%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) plugin, dlerror());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) alias = dlsym(handle, TEP_PLUGIN_ALIAS_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (!alias)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) alias = file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) options = dlsym(handle, TEP_PLUGIN_OPTIONS_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (options) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) while (options->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ret = update_option(alias, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) options++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) func = dlsym(handle, TEP_PLUGIN_LOADER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (!func) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) warning("could not find func '%s' in plugin '%s'\n%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) TEP_PLUGIN_LOADER_NAME, plugin, dlerror());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) list = malloc(sizeof(*list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (!list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) warning("could not allocate plugin memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) list->next = *plugin_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) list->handle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) list->name = plugin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) *plugin_list = list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) pr_stat("registering plugin: %s", plugin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) func(tep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) free(plugin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) load_plugins_dir(struct tep_handle *tep, const char *suffix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) const char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) void (*load_plugin)(struct tep_handle *tep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) const char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) DIR *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) ret = stat(path, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (!S_ISDIR(st.st_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) dir = opendir(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (!dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) while ((dent = readdir(dir))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) const char *name = dent->d_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (strcmp(name, ".") == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) strcmp(name, "..") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /* Only load plugins that end in suffix */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (strcmp(name + (strlen(name) - strlen(suffix)), suffix) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) load_plugin(tep, path, name, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) closedir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * tep_load_plugins_hook - call a user specified callback to load a plugin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * @tep: handler to traceevent context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * @suffix: filter only plugin files with given suffix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * @load_plugin: user specified callback, called for each plugin file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * @data: custom context, passed to @load_plugin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * Searches for traceevent plugin files and calls @load_plugin for each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * The order of plugins search is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * - Directories, specified in @tep->plugins_dir and priority TEP_PLUGIN_FIRST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * - Directory, specified at compile time with PLUGIN_TRACEEVENT_DIR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * - Directory, specified by environment variable TRACEEVENT_PLUGIN_DIR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * - In user's home: ~/.local/lib/traceevent/plugins/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) * - Directories, specified in @tep->plugins_dir and priority TEP_PLUGIN_LAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) void tep_load_plugins_hook(struct tep_handle *tep, const char *suffix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) void (*load_plugin)(struct tep_handle *tep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) const char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) struct tep_plugins_dir *dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) char *home;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) char *envdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (tep && tep->flags & TEP_DISABLE_PLUGINS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (tep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) dir = tep->plugins_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) while (dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (dir->prio == TEP_PLUGIN_FIRST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) load_plugins_dir(tep, suffix, dir->path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) load_plugin, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) dir = dir->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * If a system plugin directory was defined,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) * check that first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) #ifdef PLUGIN_DIR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (!tep || !(tep->flags & TEP_DISABLE_SYS_PLUGINS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) load_plugins_dir(tep, suffix, PLUGIN_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) load_plugin, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * Next let the environment-set plugin directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) * override the system defaults.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) envdir = getenv("TRACEEVENT_PLUGIN_DIR");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (envdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) load_plugins_dir(tep, suffix, envdir, load_plugin, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * Now let the home directory override the environment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * or system defaults.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) home = getenv("HOME");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (!home)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) ret = asprintf(&path, "%s/%s", home, LOCAL_PLUGIN_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) warning("could not allocate plugin memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) load_plugins_dir(tep, suffix, path, load_plugin, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (tep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) dir = tep->plugins_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) while (dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (dir->prio == TEP_PLUGIN_LAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) load_plugins_dir(tep, suffix, dir->path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) load_plugin, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) dir = dir->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) free(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) struct tep_plugin_list*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) tep_load_plugins(struct tep_handle *tep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) struct tep_plugin_list *list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) tep_load_plugins_hook(tep, ".so", load_plugin, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * tep_add_plugin_path - Add a new plugin directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) * @tep: Trace event handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * @path: Path to a directory. All plugin files in that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * directory will be loaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) *@prio: Load priority of the plugins in that directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) * Returns -1 in case of an error, 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) int tep_add_plugin_path(struct tep_handle *tep, char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) enum tep_plugin_load_priority prio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) struct tep_plugins_dir *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (!tep || !path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) dir = calloc(1, sizeof(*dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (!dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) dir->path = strdup(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (!dir->path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) free(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) dir->prio = prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) dir->next = tep->plugins_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) tep->plugins_dir = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) __hidden void free_tep_plugin_paths(struct tep_handle *tep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) struct tep_plugins_dir *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (!tep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) dir = tep->plugins_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) while (dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) tep->plugins_dir = tep->plugins_dir->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) free(dir->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) free(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) dir = tep->plugins_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) tep_unload_plugins(struct tep_plugin_list *plugin_list, struct tep_handle *tep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) tep_plugin_unload_func func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) struct tep_plugin_list *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) while (plugin_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) list = plugin_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) plugin_list = list->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) func = dlsym(list->handle, TEP_PLUGIN_UNLOADER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) func(tep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) dlclose(list->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) free(list->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) free(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }