Skip to contentSkip to conversation

libtracefs: Facilitate adding and removing kprobes

PATCH Superseded Steven Rostedt 1 participant 4 messages Jun 29, 2021

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover libtracefs: Facilitate adding and removing kprobes

Add a tracefs_kprobe_raw() to facilitate adding kprobe events. It's denoted as "raw" because it still requires knowing the complex format of a kprobe, but at least it helps with other formats:

p:[[system/]event] addr fmt

The user only needs to know the "fmt" part above, and not worry about what file to open, or how to open it.

Also add a tracefs_kprobe_clear() to clear all kprobes and a tracefs_kprobe_clear_probe() to clear an individual kprobe. Both have a "force" parameter, that if set, will then try to disable the kprobe in all instances (including the top) and then remove the kprobe(s).

(Man pages coming after this).

Steven Rostedt (VMware) (3):
libtracefs: Implement tracefs_kprobe_raw()
libtracefs: Implement tracefs_kprobe_clear() to remove all kprobes
libtracefs: Implement tracefs_kprobe_clear_probe()

 include/tracefs.h     |   5 +
 src/Makefile          |   1 +
 src/tracefs-kprobes.c | 242 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 248 insertions(+)
 create mode 100644 src/tracefs-kprobes.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/3 libtracefs: Implement tracefs_kprobe_raw()

From: "Steven Rostedt (VMware)" <[email protected]>

Add a function to facilitate creating a kprobe event. It has the "raw" in its name because it still requires knowing the format of the kprobe. But does handle the kprobe naming better and writing to the kprobe_event file.

---
 include/tracefs.h     |  4 +++
 src/Makefile          |  1 +
 src/tracefs-kprobes.c | 70 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+)
 create mode 100644 src/tracefs-kprobes.c
include/tracefs.h +4 -0
index 048474240958..47b05b3f55e7 100644
@@ -209,4 +209,8 @@ int tracefs_tracer_clear(struct tracefs_instance *instance);
 ssize_t tracefs_trace_pipe_stream(int fd, struct tracefs_instance *instance, int flags);
 ssize_t tracefs_trace_pipe_print(struct tracefs_instance *instance, int flags);
 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);
+
 #endif /* _TRACE_FS_H */
src/Makefile +1 -0
index b4cff07efc50..0697a047f4bc 100644
@@ -8,6 +8,7 @@ OBJS += tracefs-instance.o
 OBJS += tracefs-events.o
 OBJS += tracefs-tools.o
 OBJS += tracefs-marker.o
+OBJS += tracefs-kprobes.o
 
 OBJS := $(OBJS:%.o=$(bdir)/%.o)
 DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d)
src/tracefs-kprobes.c new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/3 libtracefs: Implement tracefs_kprobe_clear() to remove all kprobes

In reply to Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

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.

---
 include/tracefs.h     |   2 +-
 src/tracefs-kprobes.c | 136 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+), 1 deletion(-)
include/tracefs.h +1 -1
index 47b05b3f55e7..54c461e85a2b 100644
@@ -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 */
src/tracefs-kprobes.c +136 -0

139 lines not on this page · raw message

Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/3 libtracefs: Implement tracefs_kprobe_clear_probe()

In reply to Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

Add the function tracefs_kprobe_clear_probe() that will remove a single kprobe. If the @force parameter is set, it will disable that probe in all instances (including the top level instance) before removing it.

---
 include/tracefs.h     |  1 +
 src/tracefs-kprobes.c | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
include/tracefs.h +1 -0
index 54c461e85a2b..447821e65ce2 100644
@@ -213,4 +213,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);
+int tracefs_kprobe_clear_probe(const char *system, const char *event, bool force);
 #endif /* _TRACE_FS_H */
src/tracefs-kprobes.c +36 -0
index 48a4f090f041..27d4c2181404 100644
@@ -204,3 +204,39 @@ int tracefs_kprobe_clear(bool force)
 	free_instance_list(list);
 	return ret;
 }
+
+int tracefs_kprobe_clear_probe(const char *system, const char *event, bool force)
+{
+	struct instance_list *list = NULL;
+	struct instance_list **plist = &list;
+	char *content;
+	int ret;
+
+	if (!system)
+		system = "kprobes";
+
+	ret = asprintf(&content, "-:%s/%s", system, event);
+	if (ret < 0)
+		return -1;
+
+	/*
+	 * Since we know we are disabling a specific event, try
+	 * to disable it first before clearing it.
+	 */
+	if (force) {
+		ret = tracefs_instances_walk(build_instances, &plist);
+		if (ret < 0)
+			goto out;
+
+		ret = disable_events(system, event, list);
+		if (ret < 0)
+			goto out;
+	}
+
+	ret = tracefs_instance_file_append(NULL, KPROBE_EVENTS, content);
+ out:
+	free(content);
+	free_instance_list(list);
+
+	return ret < 0 ? -1 : 0;
+}
Signature · 1 line
2.30.2