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 2/3] libtracefs: Implement tracefs_kprobe_clear() to remove all kprobes
Date: Tue, 29 Jun 2021 16:53:25 -0400	[thread overview]
Message-ID: <20210629205326.117059-3-rostedt@goodmis.org> (raw)
In-Reply-To: <20210629205326.117059-1-rostedt@goodmis.org>

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

A call to tracefs_kprobe_clear() will attempt to disable all kprobes. If
any kprobe is set, and the @force parameter is set, it will fail with
errno set to EBUSY. If @force is set, then it will attempt to disable all
the defined kprobe events and then clear it.

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

diff --git a/include/tracefs.h b/include/tracefs.h
index 47b05b3f55e7..54c461e85a2b 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -212,5 +212,5 @@ void tracefs_trace_pipe_stop(struct tracefs_instance *instance);
 
 int tracefs_kprobe_raw(const char *system, const char *event,
 		       const char *addr, const char *format);
-
+int tracefs_kprobe_clear(bool force);
 #endif /* _TRACE_FS_H */
diff --git a/src/tracefs-kprobes.c b/src/tracefs-kprobes.c
index e4b28cff9f08..48a4f090f041 100644
--- a/src/tracefs-kprobes.c
+++ b/src/tracefs-kprobes.c
@@ -68,3 +68,139 @@ int tracefs_kprobe_raw(const char *system, const char *event,
 
 	return ret < 0 ? ret : 0;
 }
+
+struct instance_list {
+	struct instance_list	*next;
+	struct tracefs_instance	*instance;
+};
+
+static int disable_events(const char *system, const char *event,
+			  struct instance_list *list)
+{
+	int ret;
+
+	ret = tracefs_event_disable(NULL, system, event);
+	if (ret < 0)
+		return ret;
+
+	while (list) {
+		ret = tracefs_event_disable(list->instance, system, event);
+		if (ret < 0)
+			return ret;
+		list = list->next;
+	}
+
+	return 0;
+}
+
+static int build_instances(const char *name, void *data)
+{
+	struct instance_list ***ptr_next = (struct instance_list ***)data;
+	struct instance_list **next = *ptr_next;
+	struct tracefs_instance *instance;
+	struct instance_list *list;
+
+	instance = tracefs_instance_alloc(NULL, name);
+	if (!instance)
+		return -1;
+
+	list = malloc(sizeof(*list));
+	if (!list) {
+		tracefs_instance_free(instance);
+		return -1;
+	}
+
+	list->instance = instance;
+	list->next = *next;
+	*next = list;
+	*ptr_next = &list->next;
+	return 0;
+}
+
+static void free_instance_list(struct instance_list *list)
+{
+	struct instance_list *n;
+
+	while (list) {
+		n = list->next;
+		tracefs_instance_free(list->instance);
+		free(list);
+		list = n;
+	}
+}
+
+/**
+ * tracefs_kprobe_clear - clear kprobe events
+ * @force: Will attempt to disable all kprobe events and clear them
+ *
+ * Will remove all defined kprobe events. If any of them are enabled,
+ * and @force is not set, then it will error with -1 and errno to be
+ * EBUSY. If @force is set, then it will attempt to disable all the kprobe
+ * events in all instances, and try again.
+ *
+ * Returns zero on success, -1 otherwise.
+ */
+int tracefs_kprobe_clear(bool force)
+{
+	struct instance_list *list = NULL;
+	struct instance_list **plist = &list;
+	char *content;
+	char *saveptr;
+	char *system;
+	char *event;
+	char *p;
+	int ret;
+
+	ret = tracefs_instance_file_clear(NULL, KPROBE_EVENTS);
+	if (!ret)
+		return 0;
+
+	if (!force)
+		return -1;
+
+	/* Attempt to disable all kprobe events */
+	content = tracefs_instance_file_read(NULL, KPROBE_EVENTS, NULL);
+	if (!content)
+		return -1;
+
+	ret = tracefs_instances_walk(build_instances, &plist);
+	if (ret < 0)
+		goto out;
+
+	ret = -1;
+	p = strtok_r(content, ":", &saveptr);
+	if (!p)
+		goto out;
+
+	for (;;) {
+		p = strtok_r(NULL, "/", &saveptr);
+		if (!p)
+			goto out;
+
+		system = p;
+
+		p = strtok_r(NULL," ", &saveptr);
+		if (!p)
+			goto out;
+		event = p;
+
+		if (disable_events(system, event, list) < 0)
+			goto out;
+
+		ret = tracefs_instance_file_clear(NULL, KPROBE_EVENTS);
+		/* On success stop the loop */
+		if (!ret)
+			goto out;
+
+		ret = -1;
+		p = strtok_r(NULL, "\n", &saveptr);
+		if (!p)
+			goto out;
+
+		p = strtok_r(NULL, ":", &saveptr);
+	}
+ out:
+	free(content);
+	free_instance_list(list);
+	return ret;
+}
-- 
2.30.2


  parent reply	other threads:[~2021-06-29 20:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-29 20:53 [PATCH 0/3] libtracefs: Facilitate adding and removing kprobes Steven Rostedt
2021-06-29 20:53 ` [PATCH 1/3] libtracefs: Implement tracefs_kprobe_raw() Steven Rostedt
2021-06-29 20:53 ` Steven Rostedt [this message]
2021-06-29 20:53 ` [PATCH 3/3] libtracefs: Implement tracefs_kprobe_clear_probe() 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=20210629205326.117059-3-rostedt@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).