Skip to content

Commit 7400628

Browse files
tzstoyanovacmel
authored andcommitted
tools lib traceevent: Add interface for options to plugins
Add tep_plugin_add_option() and tep_plugin_print_options() to lib traceevent library that allows plugins to have their own options. For example, the function plugin by default does not print the parent, as it uses the parent to do the indenting. The "parent" option is created by the function plugin that will print the parent of the function like it does in the trace file. The tep_plugin_print_options() will print out the list of options that a Cc: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com> plugin has defined. Link: http://lore.kernel.org/linux-trace-devel/20190802110101.14759-3-tz.stoyanov@gmail.com Link: http://lore.kernel.org/linux-trace-devel/20200625100516.365338-5-tz.stoyanov@gmail.com Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: linux-trace-devel@vger.kernel.org Link: http://lore.kernel.org/lkml/20200702185704.092654084@goodmis.org Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 662081a commit 7400628

2 files changed

Lines changed: 174 additions & 0 deletions

File tree

‎tools/lib/traceevent/event-parse.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,9 @@ char **tep_plugin_list_options(void);
406406
void tep_plugin_free_options_list(char **list);
407407
int tep_plugin_add_options(const char *name,
408408
struct tep_plugin_option *options);
409+
int tep_plugin_add_option(const char *name, const char *val);
409410
void tep_plugin_remove_options(struct tep_plugin_option *options);
411+
void tep_plugin_print_options(struct trace_seq *s);
410412
void tep_print_plugins(struct trace_seq *s,
411413
const char *prefix, const char *suffix,
412414
const struct tep_plugin_list *list);

‎tools/lib/traceevent/event-plugin.c‎

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <sys/stat.h>
1414
#include <unistd.h>
1515
#include <dirent.h>
16+
#include <errno.h>
1617
#include "event-parse.h"
1718
#include "event-parse-local.h"
1819
#include "event-utils.h"
@@ -247,6 +248,166 @@ void tep_plugin_remove_options(struct tep_plugin_option *options)
247248
}
248249
}
249250

251+
static void parse_option_name(char **option, char **plugin)
252+
{
253+
char *p;
254+
255+
*plugin = NULL;
256+
257+
if ((p = strstr(*option, ":"))) {
258+
*plugin = *option;
259+
*p = '\0';
260+
*option = strdup(p + 1);
261+
if (!*option)
262+
return;
263+
}
264+
}
265+
266+
static struct tep_plugin_option *
267+
find_registered_option(const char *plugin, const char *option)
268+
{
269+
struct registered_plugin_options *reg;
270+
struct tep_plugin_option *op;
271+
const char *op_plugin;
272+
273+
for (reg = registered_options; reg; reg = reg->next) {
274+
for (op = reg->options; op->name; op++) {
275+
if (op->plugin_alias)
276+
op_plugin = op->plugin_alias;
277+
else
278+
op_plugin = op->file;
279+
280+
if (plugin && strcmp(plugin, op_plugin) != 0)
281+
continue;
282+
if (strcmp(option, op->name) != 0)
283+
continue;
284+
285+
return op;
286+
}
287+
}
288+
289+
return NULL;
290+
}
291+
292+
static int process_option(const char *plugin, const char *option, const char *val)
293+
{
294+
struct tep_plugin_option *op;
295+
296+
op = find_registered_option(plugin, option);
297+
if (!op)
298+
return 0;
299+
300+
return update_option_value(op, val);
301+
}
302+
303+
/**
304+
* tep_plugin_add_option - add an option/val pair to set plugin options
305+
* @name: The name of the option (format: <plugin>:<option> or just <option>)
306+
* @val: (optiona) the value for the option
307+
*
308+
* Modify a plugin option. If @val is given than the value of the option
309+
* is set (note, some options just take a boolean, so @val must be either
310+
* "1" or "0" or "true" or "false").
311+
*/
312+
int tep_plugin_add_option(const char *name, const char *val)
313+
{
314+
struct trace_plugin_options *op;
315+
char *option_str;
316+
char *plugin;
317+
318+
option_str = strdup(name);
319+
if (!option_str)
320+
return -ENOMEM;
321+
322+
parse_option_name(&option_str, &plugin);
323+
324+
/* If the option exists, update the val */
325+
for (op = trace_plugin_options; op; op = op->next) {
326+
/* Both must be NULL or not NULL */
327+
if ((!plugin || !op->plugin) && plugin != op->plugin)
328+
continue;
329+
if (plugin && strcmp(plugin, op->plugin) != 0)
330+
continue;
331+
if (strcmp(op->option, option_str) != 0)
332+
continue;
333+
334+
/* update option */
335+
free(op->value);
336+
if (val) {
337+
op->value = strdup(val);
338+
if (!op->value)
339+
goto out_free;
340+
} else
341+
op->value = NULL;
342+
343+
/* plugin and option_str don't get freed at the end */
344+
free(plugin);
345+
free(option_str);
346+
347+
plugin = op->plugin;
348+
option_str = op->option;
349+
break;
350+
}
351+
352+
/* If not found, create */
353+
if (!op) {
354+
op = malloc(sizeof(*op));
355+
if (!op)
356+
return -ENOMEM;
357+
memset(op, 0, sizeof(*op));
358+
op->next = trace_plugin_options;
359+
trace_plugin_options = op;
360+
361+
op->plugin = plugin;
362+
op->option = option_str;
363+
364+
if (val) {
365+
op->value = strdup(val);
366+
if (!op->value)
367+
goto out_free;
368+
}
369+
}
370+
371+
return process_option(plugin, option_str, val);
372+
out_free:
373+
free(option_str);
374+
return -ENOMEM;
375+
}
376+
377+
static void print_op_data(struct trace_seq *s, const char *name,
378+
const char *op)
379+
{
380+
if (op)
381+
trace_seq_printf(s, "%8s:\t%s\n", name, op);
382+
}
383+
384+
/**
385+
* tep_plugin_print_options - print out the registered plugin options
386+
* @s: The trace_seq descriptor to write the plugin options into
387+
*
388+
* Writes a list of options into trace_seq @s.
389+
*/
390+
void tep_plugin_print_options(struct trace_seq *s)
391+
{
392+
struct registered_plugin_options *reg;
393+
struct tep_plugin_option *op;
394+
395+
for (reg = registered_options; reg; reg = reg->next) {
396+
if (reg != registered_options)
397+
trace_seq_printf(s, "============\n");
398+
for (op = reg->options; op->name; op++) {
399+
if (op != reg->options)
400+
trace_seq_printf(s, "------------\n");
401+
print_op_data(s, "file", op->file);
402+
print_op_data(s, "plugin", op->plugin_alias);
403+
print_op_data(s, "option", op->name);
404+
print_op_data(s, "desc", op->description);
405+
print_op_data(s, "value", op->value);
406+
trace_seq_printf(s, "%8s:\t%d\n", "set", op->set);
407+
}
408+
}
409+
}
410+
250411
/**
251412
* tep_print_plugins - print out the list of plugins loaded
252413
* @s: the trace_seq descripter to write to
@@ -273,6 +434,7 @@ load_plugin(struct tep_handle *tep, const char *path,
273434
const char *file, void *data)
274435
{
275436
struct tep_plugin_list **plugin_list = data;
437+
struct tep_plugin_option *options;
276438
tep_plugin_load_func func;
277439
struct tep_plugin_list *list;
278440
const char *alias;
@@ -297,6 +459,16 @@ load_plugin(struct tep_handle *tep, const char *path,
297459
if (!alias)
298460
alias = file;
299461

462+
options = dlsym(handle, TEP_PLUGIN_OPTIONS_NAME);
463+
if (options) {
464+
while (options->name) {
465+
ret = update_option(alias, options);
466+
if (ret < 0)
467+
goto out_free;
468+
options++;
469+
}
470+
}
471+
300472
func = dlsym(handle, TEP_PLUGIN_LOADER_NAME);
301473
if (!func) {
302474
warning("could not find func '%s' in plugin '%s'\n%s\n",

0 commit comments

Comments
 (0)