event-plugin.c 10 KB

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