event-plugin.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  4. *
  5. */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <dlfcn.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <unistd.h>
  14. #include <dirent.h>
  15. #include "event-parse.h"
  16. #include "event-utils.h"
  17. #include "trace-seq.h"
  18. #define LOCAL_PLUGIN_DIR ".traceevent/plugins"
  19. static struct registered_plugin_options {
  20. struct registered_plugin_options *next;
  21. struct tep_plugin_option *options;
  22. } *registered_options;
  23. static struct trace_plugin_options {
  24. struct trace_plugin_options *next;
  25. char *plugin;
  26. char *option;
  27. char *value;
  28. } *trace_plugin_options;
  29. struct plugin_list {
  30. struct plugin_list *next;
  31. char *name;
  32. void *handle;
  33. };
  34. static void lower_case(char *str)
  35. {
  36. if (!str)
  37. return;
  38. for (; *str; str++)
  39. *str = tolower(*str);
  40. }
  41. static int update_option_value(struct tep_plugin_option *op, const char *val)
  42. {
  43. char *op_val;
  44. if (!val) {
  45. /* toggle, only if option is boolean */
  46. if (op->value)
  47. /* Warn? */
  48. return 0;
  49. op->set ^= 1;
  50. return 0;
  51. }
  52. /*
  53. * If the option has a value then it takes a string
  54. * otherwise the option is a boolean.
  55. */
  56. if (op->value) {
  57. op->value = val;
  58. return 0;
  59. }
  60. /* Option is boolean, must be either "1", "0", "true" or "false" */
  61. op_val = strdup(val);
  62. if (!op_val)
  63. return -1;
  64. lower_case(op_val);
  65. if (strcmp(val, "1") == 0 || strcmp(val, "true") == 0)
  66. op->set = 1;
  67. else if (strcmp(val, "0") == 0 || strcmp(val, "false") == 0)
  68. op->set = 0;
  69. free(op_val);
  70. return 0;
  71. }
  72. /**
  73. * tep_plugin_list_options - get list of plugin options
  74. *
  75. * Returns an array of char strings that list the currently registered
  76. * plugin options in the format of <plugin>:<option>. This list can be
  77. * used by toggling the option.
  78. *
  79. * Returns NULL if there's no options registered. On error it returns
  80. * INVALID_PLUGIN_LIST_OPTION
  81. *
  82. * Must be freed with tep_plugin_free_options_list().
  83. */
  84. char **tep_plugin_list_options(void)
  85. {
  86. struct registered_plugin_options *reg;
  87. struct tep_plugin_option *op;
  88. char **list = NULL;
  89. char *name;
  90. int count = 0;
  91. for (reg = registered_options; reg; reg = reg->next) {
  92. for (op = reg->options; op->name; op++) {
  93. char *alias = op->plugin_alias ? op->plugin_alias : op->file;
  94. char **temp = list;
  95. int ret;
  96. ret = asprintf(&name, "%s:%s", alias, op->name);
  97. if (ret < 0)
  98. goto err;
  99. list = realloc(list, count + 2);
  100. if (!list) {
  101. list = temp;
  102. free(name);
  103. goto err;
  104. }
  105. list[count++] = name;
  106. list[count] = NULL;
  107. }
  108. }
  109. return list;
  110. err:
  111. while (--count >= 0)
  112. free(list[count]);
  113. free(list);
  114. return INVALID_PLUGIN_LIST_OPTION;
  115. }
  116. void tep_plugin_free_options_list(char **list)
  117. {
  118. int i;
  119. if (!list)
  120. return;
  121. if (list == INVALID_PLUGIN_LIST_OPTION)
  122. return;
  123. for (i = 0; list[i]; i++)
  124. free(list[i]);
  125. free(list);
  126. }
  127. static int
  128. update_option(const char *file, struct tep_plugin_option *option)
  129. {
  130. struct trace_plugin_options *op;
  131. char *plugin;
  132. int ret = 0;
  133. if (option->plugin_alias) {
  134. plugin = strdup(option->plugin_alias);
  135. if (!plugin)
  136. return -1;
  137. } else {
  138. char *p;
  139. plugin = strdup(file);
  140. if (!plugin)
  141. return -1;
  142. p = strstr(plugin, ".");
  143. if (p)
  144. *p = '\0';
  145. }
  146. /* first look for named options */
  147. for (op = trace_plugin_options; op; op = op->next) {
  148. if (!op->plugin)
  149. continue;
  150. if (strcmp(op->plugin, plugin) != 0)
  151. continue;
  152. if (strcmp(op->option, option->name) != 0)
  153. continue;
  154. ret = update_option_value(option, op->value);
  155. if (ret)
  156. goto out;
  157. break;
  158. }
  159. /* first look for unnamed options */
  160. for (op = trace_plugin_options; op; op = op->next) {
  161. if (op->plugin)
  162. continue;
  163. if (strcmp(op->option, option->name) != 0)
  164. continue;
  165. ret = update_option_value(option, op->value);
  166. break;
  167. }
  168. out:
  169. free(plugin);
  170. return ret;
  171. }
  172. /**
  173. * tep_plugin_add_options - Add a set of options by a plugin
  174. * @name: The name of the plugin adding the options
  175. * @options: The set of options being loaded
  176. *
  177. * Sets the options with the values that have been added by user.
  178. */
  179. int tep_plugin_add_options(const char *name,
  180. struct tep_plugin_option *options)
  181. {
  182. struct registered_plugin_options *reg;
  183. reg = malloc(sizeof(*reg));
  184. if (!reg)
  185. return -1;
  186. reg->next = registered_options;
  187. reg->options = options;
  188. registered_options = reg;
  189. while (options->name) {
  190. update_option(name, options);
  191. options++;
  192. }
  193. return 0;
  194. }
  195. /**
  196. * tep_plugin_remove_options - remove plugin options that were registered
  197. * @options: Options to removed that were registered with tep_plugin_add_options
  198. */
  199. void tep_plugin_remove_options(struct tep_plugin_option *options)
  200. {
  201. struct registered_plugin_options **last;
  202. struct registered_plugin_options *reg;
  203. for (last = &registered_options; *last; last = &(*last)->next) {
  204. if ((*last)->options == options) {
  205. reg = *last;
  206. *last = reg->next;
  207. free(reg);
  208. return;
  209. }
  210. }
  211. }
  212. /**
  213. * tep_print_plugins - print out the list of plugins loaded
  214. * @s: the trace_seq descripter to write to
  215. * @prefix: The prefix string to add before listing the option name
  216. * @suffix: The suffix string ot append after the option name
  217. * @list: The list of plugins (usually returned by tep_load_plugins()
  218. *
  219. * Writes to the trace_seq @s the list of plugins (files) that is
  220. * returned by tep_load_plugins(). Use @prefix and @suffix for formating:
  221. * @prefix = " ", @suffix = "\n".
  222. */
  223. void tep_print_plugins(struct trace_seq *s,
  224. const char *prefix, const char *suffix,
  225. const struct plugin_list *list)
  226. {
  227. while (list) {
  228. trace_seq_printf(s, "%s%s%s", prefix, list->name, suffix);
  229. list = list->next;
  230. }
  231. }
  232. static void
  233. load_plugin(struct tep_handle *pevent, const char *path,
  234. const char *file, void *data)
  235. {
  236. struct plugin_list **plugin_list = data;
  237. tep_plugin_load_func func;
  238. struct plugin_list *list;
  239. const char *alias;
  240. char *plugin;
  241. void *handle;
  242. int ret;
  243. ret = asprintf(&plugin, "%s/%s", path, file);
  244. if (ret < 0) {
  245. warning("could not allocate plugin memory\n");
  246. return;
  247. }
  248. handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL);
  249. if (!handle) {
  250. warning("could not load plugin '%s'\n%s\n",
  251. plugin, dlerror());
  252. goto out_free;
  253. }
  254. alias = dlsym(handle, TEP_PLUGIN_ALIAS_NAME);
  255. if (!alias)
  256. alias = file;
  257. func = dlsym(handle, TEP_PLUGIN_LOADER_NAME);
  258. if (!func) {
  259. warning("could not find func '%s' in plugin '%s'\n%s\n",
  260. TEP_PLUGIN_LOADER_NAME, plugin, dlerror());
  261. goto out_free;
  262. }
  263. list = malloc(sizeof(*list));
  264. if (!list) {
  265. warning("could not allocate plugin memory\n");
  266. goto out_free;
  267. }
  268. list->next = *plugin_list;
  269. list->handle = handle;
  270. list->name = plugin;
  271. *plugin_list = list;
  272. pr_stat("registering plugin: %s", plugin);
  273. func(pevent);
  274. return;
  275. out_free:
  276. free(plugin);
  277. }
  278. static void
  279. load_plugins_dir(struct tep_handle *pevent, const char *suffix,
  280. const char *path,
  281. void (*load_plugin)(struct tep_handle *pevent,
  282. const char *path,
  283. const char *name,
  284. void *data),
  285. void *data)
  286. {
  287. struct dirent *dent;
  288. struct stat st;
  289. DIR *dir;
  290. int ret;
  291. ret = stat(path, &st);
  292. if (ret < 0)
  293. return;
  294. if (!S_ISDIR(st.st_mode))
  295. return;
  296. dir = opendir(path);
  297. if (!dir)
  298. return;
  299. while ((dent = readdir(dir))) {
  300. const char *name = dent->d_name;
  301. if (strcmp(name, ".") == 0 ||
  302. strcmp(name, "..") == 0)
  303. continue;
  304. /* Only load plugins that end in suffix */
  305. if (strcmp(name + (strlen(name) - strlen(suffix)), suffix) != 0)
  306. continue;
  307. load_plugin(pevent, path, name, data);
  308. }
  309. closedir(dir);
  310. }
  311. static void
  312. load_plugins(struct tep_handle *pevent, const char *suffix,
  313. void (*load_plugin)(struct tep_handle *pevent,
  314. const char *path,
  315. const char *name,
  316. void *data),
  317. void *data)
  318. {
  319. char *home;
  320. char *path;
  321. char *envdir;
  322. int ret;
  323. if (pevent->flags & TEP_DISABLE_PLUGINS)
  324. return;
  325. /*
  326. * If a system plugin directory was defined,
  327. * check that first.
  328. */
  329. #ifdef PLUGIN_DIR
  330. if (!(pevent->flags & TEP_DISABLE_SYS_PLUGINS))
  331. load_plugins_dir(pevent, suffix, PLUGIN_DIR,
  332. load_plugin, data);
  333. #endif
  334. /*
  335. * Next let the environment-set plugin directory
  336. * override the system defaults.
  337. */
  338. envdir = getenv("TRACEEVENT_PLUGIN_DIR");
  339. if (envdir)
  340. load_plugins_dir(pevent, suffix, envdir, load_plugin, data);
  341. /*
  342. * Now let the home directory override the environment
  343. * or system defaults.
  344. */
  345. home = getenv("HOME");
  346. if (!home)
  347. return;
  348. ret = asprintf(&path, "%s/%s", home, LOCAL_PLUGIN_DIR);
  349. if (ret < 0) {
  350. warning("could not allocate plugin memory\n");
  351. return;
  352. }
  353. load_plugins_dir(pevent, suffix, path, load_plugin, data);
  354. free(path);
  355. }
  356. struct plugin_list*
  357. tep_load_plugins(struct tep_handle *pevent)
  358. {
  359. struct plugin_list *list = NULL;
  360. load_plugins(pevent, ".so", load_plugin, &list);
  361. return list;
  362. }
  363. void
  364. tep_unload_plugins(struct plugin_list *plugin_list, struct tep_handle *pevent)
  365. {
  366. tep_plugin_unload_func func;
  367. struct plugin_list *list;
  368. while (plugin_list) {
  369. list = plugin_list;
  370. plugin_list = list->next;
  371. func = dlsym(list->handle, TEP_PLUGIN_UNLOADER_NAME);
  372. if (func)
  373. func(pevent);
  374. dlclose(list->handle);
  375. free(list->name);
  376. free(list);
  377. }
  378. }