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