linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-trace-devel@vger.kernel.org
Cc: "Steven Rostedt (VMware)" <rostedt@goodmis.org>
Subject: [PATCH v7 05/10] libtracefs: Add helper function to parse kprobes
Date: Fri,  2 Jul 2021 16:17:22 -0400	[thread overview]
Message-ID: <20210702201727.169080-6-rostedt@goodmis.org> (raw)
In-Reply-To: <20210702201727.169080-1-rostedt@goodmis.org>

From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

Add a helper function to parse kprobes and use that for
tracefs_get_kprobes(). This will allow other functions to easily parse the
kprobe_events file without having to duplicate the parsing code.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 src/tracefs-kprobes.c | 76 +++++++++++++++++++++++++++++++++----------
 1 file changed, 59 insertions(+), 17 deletions(-)

diff --git a/src/tracefs-kprobes.c b/src/tracefs-kprobes.c
index 3bba3683f29b..713715e96f4f 100644
--- a/src/tracefs-kprobes.c
+++ b/src/tracefs-kprobes.c
@@ -104,6 +104,58 @@ int tracefs_kretprobe_raw(const char *system, const char *event,
 	return insert_kprobe("r", system, event, addr, format);
 }
 
+/*
+ * Helper function to parse kprobes.
+ * @content: The content of kprobe_events on the first iteration.
+ *           NULL on next iterations.
+ * @saveptr: Same as saveptr for strtok_r
+ * @type: Where to store the type (before ':')
+ * @system: Store the system of the kprobe (NULL to have event contain
+ *          both system and event, as in "kprobes/myprobe").
+ * @event: Where to store the event.
+ * @addr: Where to store the addr (may be NULL to ignore)
+ * @format: Where to store the format (may be NULL to ignore)
+ */
+static int parse_kprobe(char *content, char **saveptr,
+			char **type, char **system, char **event,
+			char **addr, char **format)
+{
+	char *p;
+
+	p = strtok_r(content, ":", saveptr);
+	if (!p)
+		return 1; /* eof */
+	*type = p;
+
+	if (system) {
+		p = strtok_r(NULL, "/", saveptr);
+		if (!p)
+			return -1;
+		*system = p;
+	}
+
+	p = strtok_r(NULL, " ", saveptr);
+	if (!p)
+		return -1;
+	*event = p;
+
+	if (addr || format) {
+		p = strtok_r(NULL, " ", saveptr);
+		if (!p)
+			return -1;
+		if (addr)
+			*addr = p;
+	}
+
+	p = strtok_r(NULL, "\n", saveptr);
+	if (!p)
+		return -1;
+	if (format)
+		*format = p;
+
+	return 0;
+}
+
 /**
  * tracefs_get_kprobes - return a list kprobes (by group/event name)
  * @type: The type of kprobes to return.
@@ -127,8 +179,9 @@ char **tracefs_get_kprobes(enum tracefs_kprobe_type type)
 	char *content;
 	char *saveptr;
 	char *event;
-	char *p;
+	char *ktype;
 	int cnt = 0;
+	int ret;
 
 	errno = 0;
 	content = tracefs_instance_file_read(NULL, KPROBE_EVENTS, NULL);
@@ -140,13 +193,13 @@ char **tracefs_get_kprobes(enum tracefs_kprobe_type type)
 		return list;
 	}
 
-	p = strtok_r(content, ":", &saveptr);
+	ret = parse_kprobe(content, &saveptr, &ktype, NULL, &event, NULL, NULL);
 
-	while (p) {
+	while (!ret) {
 		char **tmp;
 
 		if (type != TRACEFS_ALL_KPROBES) {
-			switch (*p) {
+			switch (*ktype) {
 			case 'p':
 				if (type != TRACEFS_KPROBE)
 					goto next;
@@ -160,12 +213,7 @@ char **tracefs_get_kprobes(enum tracefs_kprobe_type type)
 			}
 		}
 
-		/* Failed parsing always return a failure */
-		p = strtok_r(NULL, " ", &saveptr);
-		if (!p)
-			break;
-
-		event = strdup(p);
+		event = strdup(event);
 		if (!event)
 			goto fail;
 
@@ -177,13 +225,7 @@ char **tracefs_get_kprobes(enum tracefs_kprobe_type type)
 		list[cnt++] = event;
 		list[cnt] = NULL;
  next:
-		p = strtok_r(NULL, "\n", &saveptr);
-		/* Could be end of content */
-		if (!p)
-			break;
-
-		/* p is NULL on end of content */
-		p = strtok_r(NULL, ":", &saveptr);
+		ret = parse_kprobe(NULL, &saveptr, &ktype, NULL, &event, NULL, NULL);
 	}
 
 	if (!list)
-- 
2.30.2


  parent reply	other threads:[~2021-07-02 20:17 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-02 20:17 [PATCH v7 00/10] libtracefs: Facilitate adding and removing kprobes Steven Rostedt
2021-07-02 20:17 ` [PATCH v7 01/10] libtracefs: Implement tracefs_instances() Steven Rostedt
2021-07-02 20:17 ` [PATCH v7 02/10] libtracefs: Implement tracefs_kprobe_raw() Steven Rostedt
2021-07-02 20:17 ` [PATCH v7 03/10] libtracefs: Implement tracefs_kretprobe_raw() Steven Rostedt
2021-07-02 20:17 ` [PATCH v7 04/10] libtracefs: Implement tracefs_get_kprobes() Steven Rostedt
2021-07-02 20:17 ` Steven Rostedt [this message]
2021-07-02 20:17 ` [PATCH v7 06/10] libtracefs: Implement tracefs_kprobe_info() Steven Rostedt
2021-07-02 20:17 ` [PATCH v7 07/10] libtracefs: Implement tracefs_kprobe_clear_all() to remove all kprobes Steven Rostedt
2021-07-02 20:17 ` [PATCH v7 08/10] libtracefs: Implement tracefs_kprobe_clear_probe() Steven Rostedt
2021-07-02 20:17 ` [PATCH v7 09/10] libtracefs: Add man pages for kprobe functions Steven Rostedt
2021-07-02 20:17 ` [PATCH v7 10/10] libtracefs: Update the unit tests to use the kprobe API instead Steven Rostedt

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=20210702201727.169080-6-rostedt@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --subject='Re: [PATCH v7 05/10] libtracefs: Add helper function to parse kprobes' \
    /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

This is a public inbox, see mirroring instructions
on how to clone and mirror all data and code used for this inbox