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 06/10] libtracefs: Implement tracefs_kprobe_info()
Date: Fri,  2 Jul 2021 16:17:23 -0400	[thread overview]
Message-ID: <20210702201727.169080-7-rostedt@goodmis.org> (raw)
In-Reply-To: <20210702201727.169080-1-rostedt@goodmis.org>

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

Add tracefs_kprobe_info() that returns the type of a given probe, and may
also return the content of the parsed kprobe_events file.

If @type is non NULL, it will hold the content of the kprobe before the
":'.

If @addr is non NULL, it will hold the content of the kprobe's address or
function that it is attached to.

If @format is non NULL, it will hold the format string of the kprobe.

It returns:

    TRACEFS_ALL_KPROBES if an error occurs or the kprobe is not found,
             or the probe is of an unknown type.
             Having @type set, can help debug an unknown type.
    TRACEFS_KPROBE if the type of kprobe found is a normal kprobe.
    TRACEFS_KRETPROBE if the type of kprobe found is a kretprobe.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 include/tracefs.h     |  2 ++
 src/tracefs-kprobes.c | 72 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+)

diff --git a/include/tracefs.h b/include/tracefs.h
index e8a2008d9714..f0ab7c96e4ab 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -226,4 +226,6 @@ int tracefs_kprobe_raw(const char *system, const char *event,
 int tracefs_kretprobe_raw(const char *system, const char *event,
 			  const char *addr, const char *format);
 char **tracefs_get_kprobes(enum tracefs_kprobe_type type);
+enum tracefs_kprobe_type tracefs_kprobe_info(const char *group, const char *event,
+					     char **type, char **addr, char **format);
 #endif /* _TRACE_FS_H */
diff --git a/src/tracefs-kprobes.c b/src/tracefs-kprobes.c
index 713715e96f4f..5442b6c17638 100644
--- a/src/tracefs-kprobes.c
+++ b/src/tracefs-kprobes.c
@@ -18,6 +18,7 @@
 #include "tracefs-local.h"
 
 #define KPROBE_EVENTS "kprobe_events"
+#define KPROBE_DEFAULT_GROUP "kprobes"
 
 static int insert_kprobe(const char *type, const char *system,
 			 const char *event, const char *addr,
@@ -238,3 +239,74 @@ char **tracefs_get_kprobes(enum tracefs_kprobe_type type)
 	list = NULL;
 	goto out;
 }
+
+/**
+ * tracefs_kprobe_info - return the type of kprobe specified.
+ * @group: The group the kprobe is in (NULL for the default "kprobes")
+ * @event: The name of the kprobe to find.
+ * @type: String to return kprobe type (before ':') NULL to ignore.
+ * @addr: String to return address kprobe is attached to. NULL to ignore.
+ * @format: String to return kprobe format. NULL to ignore.
+ *
+ * If @type, @addr, or @format is non NULL, then the returned string
+ * must be freed with free(). They will also be set to NULL, and
+ * even on error, they may contain strings to be freed. If they are
+ * not NULL, then they still need to be freed.
+ *
+ * Returns TRACEFS_ALL_KPROBES if an error occurs or the kprobe is not found,
+ *            or the probe is of an unknown type.
+ * TRACEFS_KPROBE if the type of kprobe found is a normal kprobe.
+ * TRACEFS_KRETPROBE if the type of kprobe found is a kretprobe.
+ */
+enum tracefs_kprobe_type tracefs_kprobe_info(const char *group, const char *event,
+					     char **type, char **addr, char **format)
+{
+	enum tracefs_kprobe_type rtype = TRACEFS_ALL_KPROBES;
+	char *saveptr;
+	char *content;
+	char *system;
+	char *probe;
+	char *ktype;
+	char *kaddr;
+	char *kfmt;
+	int ret;
+
+	if (!group)
+		group = KPROBE_DEFAULT_GROUP;
+
+	if (type)
+		*type = NULL;
+	if (addr)
+		*addr = NULL;
+	if (format)
+		*format = NULL;
+
+	content = tracefs_instance_file_read(NULL, KPROBE_EVENTS, NULL);
+	if (!content)
+		return rtype;
+
+	ret = parse_kprobe(content, &saveptr, &ktype, &system, &probe,
+			   &kaddr, &kfmt);
+
+	while (!ret) {
+
+		if (!strcmp(system, group) && !strcmp(probe, event)) {
+			if (type)
+				*type = strdup(ktype);
+			if (addr)
+				*addr = strdup(kaddr);
+			if (format)
+				*format = strdup(kfmt);
+
+			switch (*ktype) {
+			case 'p': rtype = TRACEFS_KPROBE; break;
+			case 'r': rtype = TRACEFS_KRETPROBE; break;
+			}
+			break;
+		}
+		ret = parse_kprobe(NULL, &saveptr, &ktype, &system, &probe,
+				   &kaddr, &kfmt);
+	}
+	free(content);
+	return rtype;
+}
-- 
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 ` [PATCH v7 05/10] libtracefs: Add helper function to parse kprobes Steven Rostedt
2021-07-02 20:17 ` Steven Rostedt [this message]
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-7-rostedt@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --subject='Re: [PATCH v7 06/10] libtracefs: Implement tracefs_kprobe_info()' \
    /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