[PATCH 5/7] trace-cmd,libtraceevent: Remove API for plugin options print

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The functionality for printing registered plugin
options is moved from libtraceevent to the application.
A more generic walk API is introduced.

Removed APIs:
	tep_plugin_list_options()
	tep_plugin_free_options_list()
	tep_plugin_print_options()

Added API
	tep_plugin_walk_options()

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@xxxxxxxxx>
---
 include/traceevent/event-parse.h |  9 ++-
 lib/traceevent/event-plugin.c    | 98 ++++----------------------------
 tracecmd/trace-list.c            | 43 +++++++++++++-
 3 files changed, 58 insertions(+), 92 deletions(-)

diff --git a/include/traceevent/event-parse.h b/include/traceevent/event-parse.h
index cb1b46e..3b4f3a5 100644
--- a/include/traceevent/event-parse.h
+++ b/include/traceevent/event-parse.h
@@ -346,8 +346,6 @@ enum tep_errno {
 
 struct tep_plugin_list;
 
-#define INVALID_PLUGIN_LIST_OPTION	((char **)((unsigned long)-1))
-
 enum tep_plugin_load_priority {
 	TEP_PLUGIN_FIRST,
 	TEP_PLUGIN_LAST,
@@ -364,13 +362,14 @@ void tep_load_plugins_hook(struct tep_handle *tep, const char *suffix,
 					       const char *name,
 					       void *data),
 			   void *data);
-char **tep_plugin_list_options(void);
-void tep_plugin_free_options_list(char **list);
 int tep_plugin_add_options(const char *name,
 			   struct tep_plugin_option *options);
 int tep_plugin_add_option(const char *name, const char *val);
 void tep_plugin_remove_options(struct tep_plugin_option *options);
-void tep_plugin_print_options(struct trace_seq *s);
+void tep_plugin_walk_options(int (*callback)(struct tep_plugin_option *op,
+					     void *context),
+			     void *context);
+
 void tep_print_plugins(struct trace_seq *s,
 			const char *prefix, const char *suffix,
 			const struct tep_plugin_list *list);
diff --git a/lib/traceevent/event-plugin.c b/lib/traceevent/event-plugin.c
index a33cf09..191b27b 100644
--- a/lib/traceevent/event-plugin.c
+++ b/lib/traceevent/event-plugin.c
@@ -91,71 +91,6 @@ static int update_option_value(struct tep_plugin_option *op, const char *val)
 	return 0;
 }
 
-/**
- * tep_plugin_list_options - get list of plugin options
- *
- * Returns an array of char strings that list the currently registered
- * plugin options in the format of <plugin>:<option>. This list can be
- * used by toggling the option.
- *
- * Returns NULL if there's no options registered. On error it returns
- * INVALID_PLUGIN_LIST_OPTION
- *
- * Must be freed with tep_plugin_free_options_list().
- */
-char **tep_plugin_list_options(void)
-{
-	struct registered_plugin_options *reg;
-	struct tep_plugin_option *op;
-	char **list = NULL;
-	char *name;
-	int count = 0;
-
-	for (reg = registered_options; reg; reg = reg->next) {
-		for (op = reg->options; op->name; op++) {
-			char **temp = list;
-			int ret;
-
-			ret = asprintf(&name, "%s:%s", op->plugin, op->name);
-			if (ret < 0)
-				goto err;
-
-			list = realloc(list, count + 2);
-			if (!list) {
-				list = temp;
-				free(name);
-				goto err;
-			}
-			list[count++] = name;
-			list[count] = NULL;
-		}
-	}
-	return list;
-
- err:
-	while (--count >= 0)
-		free(list[count]);
-	free(list);
-
-	return INVALID_PLUGIN_LIST_OPTION;
-}
-
-void tep_plugin_free_options_list(char **list)
-{
-	int i;
-
-	if (!list)
-		return;
-
-	if (list == INVALID_PLUGIN_LIST_OPTION)
-		return;
-
-	for (i = 0; list[i]; i++)
-		free(list[i]);
-
-	free(list);
-}
-
 static int
 update_option(const char *file, struct tep_plugin_option *option)
 {
@@ -377,40 +312,31 @@ int tep_plugin_add_option(const char *name, const char *val)
 	return -ENOMEM;
 }
 
-static void print_op_data(struct trace_seq *s, const char *name,
-			  const char *op)
-{
-	if (op)
-		trace_seq_printf(s, "%8s:\t%s\n", name, op);
-}
-
 /**
- * tep_plugin_print_options - print out the registered plugin options
- * @s: The trace_seq descriptor to write the plugin options into
+ * tep_plugin_walk_options - walk through all registered plugin options
+ * @callback: a user function, called on each registered plugin option
+ * @context: user data, passed to @callback function
  *
- * Writes a list of options into trace_seq @s.
+ * If the @callback returns non zero, the iteration stops.
  */
-void tep_plugin_print_options(struct trace_seq *s)
+void tep_plugin_walk_options(int (*callback)(struct tep_plugin_option *op,
+					     void *context),
+			     void *context)
 {
 	struct registered_plugin_options *reg;
 	struct tep_plugin_option *op;
 
+	if (!callback)
+		return;
+
 	for (reg = registered_options; reg; reg = reg->next) {
-		if (reg != registered_options)
-			trace_seq_printf(s, "============\n");
 		for (op = reg->options; op->name; op++) {
-			if (op != reg->options)
-				trace_seq_printf(s, "------------\n");
-			print_op_data(s, "plugin", op->plugin);
-			print_op_data(s, "option", op->name);
-			print_op_data(s, "desc", op->description);
-			print_op_data(s, "value", op->value);
-			trace_seq_printf(s, "%8s:\t%d\n", "set", op->set);
+			if (callback(op, context))
+				break;
 		}
 	}
 }
 
-
 /**
  * tep_print_plugins - print out the list of plugins loaded
  * @s: the trace_seq descripter to write to
diff --git a/tracecmd/trace-list.c b/tracecmd/trace-list.c
index 6b59117..496a83b 100644
--- a/tracecmd/trace-list.c
+++ b/tracecmd/trace-list.c
@@ -304,6 +304,47 @@ static void show_buffers(void)
 		printf("No buffer instances defined\n");
 }
 
+struct plugin_options_context {
+	struct trace_seq *s;
+	char *plugin;
+};
+
+static void print_op_data(struct trace_seq *s, const char *name,
+			  const char *op)
+{
+	if (op)
+		trace_seq_printf(s, "%8s:\t%s\n", name, op);
+}
+
+static int print_options_walk(struct tep_plugin_option *op, void *context)
+{
+	struct plugin_options_context *data = (struct plugin_options_context *)context;
+
+	if (!data->plugin || strcmp(data->plugin, op->plugin)) {
+		trace_seq_printf(data->s, "============\n");
+		free(data->plugin);
+		data->plugin = strdup(op->plugin);
+	}
+	print_op_data(data->s, "plugin", op->plugin);
+	print_op_data(data->s, "option", op->name);
+	print_op_data(data->s, "desc", op->description);
+	print_op_data(data->s, "value", op->value);
+	trace_seq_printf(data->s, "%8s:\t%d\n", "set", op->set);
+	trace_seq_printf(data->s, "------------\n");
+
+	return 0;
+}
+
+static void plugin_options_print(struct trace_seq *s)
+{
+	struct plugin_options_context context;
+
+	memset(&context, 0, sizeof(context));
+	context.s = s;
+	tep_plugin_walk_options(print_options_walk, &context);
+	free(context.plugin);
+}
+
 
 static void show_plugin_options(void)
 {
@@ -320,7 +361,7 @@ static void show_plugin_options(void)
 	trace_seq_init(&s);
 
 	list = trace_load_plugins(pevent);
-	tep_plugin_print_options(&s);
+	plugin_options_print(&s);
 	trace_seq_do_printf(&s);
 	tep_unload_plugins(list, pevent);
 	tep_free(pevent);
-- 
2.24.1




[Index of Archives]     [Linux USB Development]     [Linux USB Development]     [Linux Audio Users]     [Yosemite Hiking]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux