Linux-Trace-Devel Archive on lore.kernel.org
 help / color / Atom feed
From: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH 5/7] trace-cmd,libtraceevent: Remove API for plugin options print
Date: Wed, 22 Jan 2020 17:00:00 +0200
Message-ID: <20200122150002.763233-6-tz.stoyanov@gmail.com> (raw)
In-Reply-To: <20200122150002.763233-1-tz.stoyanov@gmail.com>

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@gmail.com>
---
 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


  parent reply index

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-22 14:59 [PATCH 0/7] trace-cmd,libtraceevent: Rework of plugin options APIs Tzvetomir Stoyanov (VMware)
2020-01-22 14:59 ` [PATCH 1/7] trace-cmd,libtraceevent: Plugin options rework Tzvetomir Stoyanov (VMware)
2020-01-22 14:59 ` [PATCH 2/7] trace-cmd,libtraceevent: Remove TEP_PLUGIN_OPTIONS Tzvetomir Stoyanov (VMware)
2020-01-22 14:59 ` [PATCH 3/7] trace-cmd,libtraceevent: Check for plugin options duplication Tzvetomir Stoyanov (VMware)
2020-01-22 14:59 ` [PATCH 4/7] trace-cmd,libtraceevent: Check for plugin duplication Tzvetomir Stoyanov (VMware)
2020-01-22 15:00 ` Tzvetomir Stoyanov (VMware) [this message]
2020-01-22 15:00 ` [PATCH 6/7] trace-cmd,libtraceevent: Remove API for plugin print Tzvetomir Stoyanov (VMware)
2020-01-22 15:00 ` [PATCH 7/7] trace-cmd, kernelshark, libtraceevent: Changed tep_plugin_add_option() API Tzvetomir Stoyanov (VMware)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200122150002.763233-6-tz.stoyanov@gmail.com \
    --to=tz.stoyanov@gmail.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Trace-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-trace-devel/0 linux-trace-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-trace-devel linux-trace-devel/ https://lore.kernel.org/linux-trace-devel \
		linux-trace-devel@vger.kernel.org
	public-inbox-index linux-trace-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-trace-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git