Skip to contentSkip to conversation

libtracefs: Facilitate adding and removing kprobes

PATCH Superseded Steven Rostedt 2 participants 8 messages Jun 30, 2021

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover libtracefs: Facilitate adding and removing kprobes

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

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 still coming after this).

Changes since v1:
- Added a tracefs_instances() to get the names of all instances
- Rewrote the tracefs_kprobe_clear*() to use the tracefs_instances, which simplified the code tremendously.
- Do not fail on failed: allocating various memory, disabling events, finding instances, etc, in the tracefs_kprobe_clear*() because it may still succeed, if the failed part were not what was causing it to not succeed.

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

 include/tracefs.h      |   6 ++
 src/Makefile           |   1 +
 src/tracefs-instance.c |  78 +++++++++++++++
 src/tracefs-kprobes.c  | 215 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 300 insertions(+)
 create mode 100644 src/tracefs-kprobes.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/4 libtracefs: Implement tracefs_instances()

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

Implement tracefs_instances() that will take a regex (or NULL for all) and return a list of instances in the system.

---
 include/tracefs.h      |  1 +
 src/tracefs-instance.c | 78 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)
include/tracefs.h +1 -0
index da8ad4189d4d..a21d2d2f22a6 100644
@@ -44,6 +44,7 @@ int tracefs_instance_file_read_number(struct tracefs_instance *instance,
 int tracefs_instance_file_open(struct tracefs_instance *instance,
 			       const char *file, int mode);
 int tracefs_instances_walk(int (*callback)(const char *, void *), void *context);
+char **tracefs_instances(const char *regex);
 
 bool tracefs_instance_exists(const char *name);
 bool tracefs_file_exists(struct tracefs_instance *instance, const char *name);
src/tracefs-instance.c +78 -0

90 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/4 libtracefs: Implement tracefs_kprobe_raw()

In reply to Steven Rostedt

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 a21d2d2f22a6..bc504bcb0188 100644
@@ -214,4 +214,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
#3 lore raw

Re: [PATCH v2 2/4] libtracefs: Implement tracefs_kprobe_raw()

Quoted 96 lines · Steven Rostedt

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 a21d2d2f22a6..bc504bcb0188 100644
@@ -214,4 +214,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 +51 -0
new file mode 100644
index 000000000000..e4b28cff9f08
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: LGPL-2.1
+/*
+ * Copyright (C) 2021 VMware Inc, Steven Rostedt <[email protected]>
+ *
+ * Updates:
+ * Copyright (C) 2021, VMware, Tzvetomir Stoyanov <[email protected]>
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <limits.h>
+
+#include "tracefs.h"
+#include "tracefs-local.h"
+
+#define KPROBE_EVENTS "kprobe_events"
+
+/**
+ * tracefs_kprobe_raw - Create a kprobe using raw format
+ * @system: The system name (NULL for the default kprobes)
+ * @event: The event to create (NULL to use @addr for the event)
+ * @addr: The function and offset (or address) to insert the probe
+ * @format: The raw format string to define the probe.
+ *
+ * Create a kprobe that will be in the @system group (or kprobes if
+ * @system is NULL). Have the name of @event (or @addr if @event is
+ * NULL). Will be inserted to @addr (function name, with or without
+ * offset, or a address). And the @format will define the raw format
+ * of the kprobe. See the Linux documentation file under:
+ * Documentation/trace/kprobetrace.rst
+ *
+ * Return 0 on success, or -1 on error.
+ *   If the syntex of @format was incorrect, running
+ *   tracefs_error_last(NULL) may show what went wrong.
+ *
+ * errno will be set to EBADMSG if addr or format is NULL.
+ */
+int tracefs_kprobe_raw(const char *system, const char *event,
+		       const char *addr, const char *format)
+{
+	char *str;
+	int ret;
+
+	errno = EBADMSG;
+	if (!addr || !format)
+		return -1;

src/tracefs-kprobes.c line 61

Quoted 10 lines
src/tracefs-kprobes.c new file +10 -0
+
+	if (!event)
+		event = addr;
+
+	if (system)
+		ret = asprintf(&str, "p:%s/%s %s %s\n",
+			       system, event, addr, format);
+	else
+		ret = asprintf(&str, "p:%s %s %s\n",
+			       event, addr, format);

I wonder what will be the way to register a "return value" ("r:") kprobe. Do you consider having a separate API for them?

Thanks!
Yordan

Quoted 10 lines
src/tracefs-kprobes.c new file +9 -0
+
+	if (ret < 0)
+		return -1;
+
+	ret = tracefs_instance_file_append(NULL, KPROBE_EVENTS, str);
+	free(str);
+
+	return ret < 0 ? ret : 0;
+}
#4 lore raw

Re: [PATCH v2 2/4] libtracefs: Implement tracefs_kprobe_raw()

Quoted 14 lines · On Wed, 30 Jun 2021 22:46:19 +0300 "Yordan Karadzhov (VMware)"…
Quoted 14 lines
src/tracefs-kprobes.c new file +14 -0
+int tracefs_kprobe_raw(const char *system, const char *event,
+		       const char *addr, const char *format)
+{
+	char *str;
+	int ret;
+
+	errno = EBADMSG;
+	if (!addr || !format)
+		return -1;
+
+	if (!event)
+		event = addr;
+
+	if (system)
Quoted 8 lines

src/tracefs-kprobes.c line 61

Quoted 5 lines
src/tracefs-kprobes.c new file +5 -0
+		ret = asprintf(&str, "p:%s/%s %s %s\n",
+			       system, event, addr, format);
+	else
+		ret = asprintf(&str, "p:%s %s %s\n",
+			       event, addr, format);  

I wonder what will be the way to register a "return value" ("r:") kprobe. Do you consider having a separate API for them?

Yes, I was thinking about having a "tracefs_kretprobe_raw()" as well.

I also thought of adding a flag in the parameter, but thought that a separate function would be better instead.

I wanted to play with this interface before creating the retprobe one. If you are happy with the current design, I can add that too.

-- Steve

#5 lore raw

Re: [PATCH v2 2/4] libtracefs: Implement tracefs_kprobe_raw()

Quoted 26 lines · Steven Rostedt
Quoted 14 lines · On Wed, 30 Jun 2021 22:46:19 +0300 "Yordan Karadzhov (VMware)"…
Quoted 14 lines
src/tracefs-kprobes.c new file +14 -0
+int tracefs_kprobe_raw(const char *system, const char *event,
+		       const char *addr, const char *format)
+{
+	char *str;
+	int ret;
+
+	errno = EBADMSG;
+	if (!addr || !format)
+		return -1;
+
+	if (!event)
+		event = addr;
+
+	if (system)
Quoted 8 lines

src/tracefs-kprobes.c line 61

Quoted 5 lines
src/tracefs-kprobes.c new file +5 -0
+		ret = asprintf(&str, "p:%s/%s %s %s\n",
+			       system, event, addr, format);
+	else
+		ret = asprintf(&str, "p:%s %s %s\n",
+			       event, addr, format);

I wonder what will be the way to register a "return value" ("r:") kprobe. Do you consider having a separate API for them?

Quoted 8 lines

Yes, I was thinking about having a "tracefs_kretprobe_raw()" as well.

I also thought of adding a flag in the parameter, but thought that a separate function would be better instead.

I wanted to play with this interface before creating the retprobe one. If you are happy with the current design, I can add that too.

The design looks good to me.

Thanks!
Y.

Quoted 2 lines

-- Steve

#6 lore raw

PATCH v2 3/4 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 | 116 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+), 1 deletion(-)
include/tracefs.h +1 -1
index bc504bcb0188..19b9b49d2dae 100644
@@ -217,5 +217,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 +116 -0

119 lines not on this page · raw message

Signature · 1 line
2.30.2
#7 lore raw

PATCH v2 4/4 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 | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
include/tracefs.h +1 -0
index 19b9b49d2dae..d7980c773b3d 100644
@@ -218,4 +218,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 +29 -0
index a6faf69fc010..6f3fbac35d76 100644
@@ -184,3 +184,32 @@ int tracefs_kprobe_clear(bool force)
 	free(content);
 	return ret;
 }
+
+int tracefs_kprobe_clear_probe(const char *system, const char *event, bool force)
+{
+	char **instance_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) {
+		instance_list = tracefs_instances(NULL);
+		disable_events(system, event, instance_list);
+		tracefs_list_free(instance_list);
+	}
+
+	ret = tracefs_instance_file_append(NULL, KPROBE_EVENTS, content);
+	free(content);
+
+	return ret < 0 ? -1 : 0;
+}
Signature · 1 line
2.30.2