Skip to contentSkip to conversation

libtracefs dynamic events support

PATCH Superseded Tzvetomir Stoyanov (VMware) 2 participants 18 messages Oct 28, 2021 – Oct 29, 2021 Documentation

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover libtracefs dynamic events support

The libtracefs logic that works with ftrace dynamic events is unified and capsulated into internal APIs. This change makes the code more consistent and reusable. Also, adding future libtracefs support for uprobes and eprobes dynamic events is simplified.
The existing library APIs for kprobes and synthetic events are reimplemented using the new dynamic events helpers.

Tzvetomir Stoyanov (VMware) (12):
libtracefs: Add new internal APIs for dynamic events
libtracefs: Rename tracefs_get_kprobes API
libtracefs: New kprobes APIs
libtracefs: Remove redundant kprobes APIs
libtracefs: Reimplement tracefs_kprobes_get API
libtracefs: Change tracefs_kprobe_info API
libtracefs: Reimplement kprobe raw APIs
libtracefs: Extend kprobes unit test
libtracefs: Update kprobes man pages
libtracefs: Rename tracefs_synth_init API
libtracefs: Use the internal dynamic events API when creating
synthetic events
libtracefs: Document tracefs_dynevent_list_free() API

 Documentation/libtracefs-kprobes.txt | 135 ++++---
 Documentation/libtracefs-synth.txt   |  20 +-
 Documentation/libtracefs-synth2.txt  |  10 +-
 Documentation/libtracefs-utils.txt   |   6 +-
 include/tracefs-local.h              |  34 ++
 include/tracefs.h                    |  38 +-
 src/Makefile                         |   1 +
 src/tracefs-dynevents.c              | 463 ++++++++++++++++++++++++
 src/tracefs-hist.c                   | 110 +++---
 src/tracefs-kprobes.c                | 523 +++++++++++++--------------
 src/tracefs-sqlhist.c                |   6 +-
 utest/tracefs-utest.c                | 414 ++++++++++++---------
 12 files changed, 1169 insertions(+), 591 deletions(-)
 create mode 100644 src/tracefs-dynevents.c
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/12 libtracefs: Add new internal APIs for dynamic events

Ftrace supports dynamic events, created by the user - kprobes, uprobes, eprobes and synthetic events. There are two interfaces for managing these events - new common "dynamic_events" file and event specific "kprobe_events", "uprobe_events", "synthetic_events" files. The configuration syntax for all dynamic events is almost the same. To simplify support of dynamic events in thw tracefs library, a new internal helper layer is implemented. It handles both configuration interfaces - the common "dynamic_events" file is preferred, if available. On the old kernels, where this file is missing, the event specific files are used. The new helper layer can be used to create, delete and get ftrace dynamic events form any type. Most of the APIs are internal, not exposed to the library users. Only one API is exposed publicly:
tracefs_dynevent_list_free()
This new logic is designed to be used internally within the library, from the APIs that implement kprobes, uprobes, eprobes and synthetic events support.

---
 include/tracefs-local.h |  34 +++
 include/tracefs.h       |   3 +
 src/Makefile            |   1 +
 src/tracefs-dynevents.c | 463 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 501 insertions(+)
 create mode 100644 src/tracefs-dynevents.c
include/tracefs-local.h +34 -0
index 684eccf..f15896f 100644
@@ -94,4 +94,38 @@ int synth_add_start_field(struct tracefs_synth *synth,
 			  const char *start_field,
 			  const char *name,
 			  enum tracefs_hist_key_type type);
+
+/* Internal interface for ftrace dynamic events */
+#define DYNEVENT_ADD_BIT(M, B)		((M) |= (1ULL<<(B)))
+#define DYNEVENT_CHECK_BIT(M, B)	((M)&(1ULL<<(B)))
+
+enum trace_dynevent_type {
+	TRACE_DYNEVENT_KPROBE = 0,
+	TRACE_DYNEVENT_KRETPROBE,
+	TRACE_DYNEVENT_UPROBE,
+	TRACE_DYNEVENT_URETPROBE,
+	TRACE_DYNEVENT_EPROBE,
+	TRACE_DYNEVENT_SYNTH,
+	TRACE_DYNEVENT_MAX,
+};
+
+struct tracefs_dynevent {
+	char *trace_file;
+	char *prefix;
+	char *system;
+	char *event;
+	char *address;
+	char *format;
+	enum trace_dynevent_type type;
+};
+
+struct tracefs_dynevent *
+dynevent_alloc(enum trace_dynevent_type type, const char *system,
+	       const char *event, const char *address, const char *format);
+void dynevent_free(struct tracefs_dynevent *devent);
+int dynevent_create(struct tracefs_dynevent *devent);
+int dynevent_destroy(struct tracefs_dynevent *devent);
+int dynevent_get_all(unsigned long type_mask, const char *system,
+		     struct tracefs_dynevent ***ret_events);
+
 #endif /* _TRACE_FS_LOCAL_H */
include/tracefs.h +3 -0
index a2cda30..4020551 100644
@@ -238,6 +238,9 @@ ssize_t tracefs_trace_pipe_stream(int fd, struct tracefs_instance *instance, int
 ssize_t tracefs_trace_pipe_print(struct tracefs_instance *instance, int flags);
 void tracefs_trace_pipe_stop(struct tracefs_instance *instance);
 
+struct tracefs_dynevent;
+void tracefs_dynevent_list_free(struct tracefs_dynevent ***events);
+
 enum tracefs_kprobe_type {
 	TRACEFS_ALL_KPROBES,
 	TRACEFS_KPROBE,
src/Makefile +1 -0
index 4e38d98..99cd7da 100644
@@ -11,6 +11,7 @@ OBJS += tracefs-marker.o
 OBJS += tracefs-kprobes.o
 OBJS += tracefs-hist.o
 OBJS += tracefs-filter.o
+OBJS += tracefs-dynevents.o
 
 # Order matters for the the three below
 OBJS += sqlhist-lex.o
src/tracefs-dynevents.c new file +463 -0

463 lines not on this page · raw message

Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH 01/12] libtracefs: Add new internal APIs for dynamic events

Quoted 32 lines · On Thu, 28 Oct 2021 15:08:56 +0300 "Tzvetomir Stoyanov (VMware)"…

Ftrace supports dynamic events, created by the user - kprobes, uprobes, eprobes and synthetic events. There are two interfaces for managing these events - new common "dynamic_events" file and event specific "kprobe_events", "uprobe_events", "synthetic_events" files. The configuration syntax for all dynamic events is almost the same. To simplify support of dynamic events in thw tracefs library, a new internal helper layer is implemented. It handles both configuration interfaces - the common "dynamic_events" file is preferred, if available. On the old kernels, where this file is missing, the event specific files are used. The new helper layer can be used to create, delete and get ftrace dynamic events form any type. Most of the APIs are internal, not exposed to the library users. Only one API is exposed publicly:
tracefs_dynevent_list_free()
This new logic is designed to be used internally within the library, from the APIs that implement kprobes, uprobes, eprobes and synthetic events support.

---
 include/tracefs-local.h |  34 +++
 include/tracefs.h       |   3 +
 src/Makefile            |   1 +
 src/tracefs-dynevents.c | 463 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 501 insertions(+)
 create mode 100644 src/tracefs-dynevents.c
include/tracefs-local.h +0 -0
index 684eccf..f15896f 100644

include/tracefs-local.h int synth_add_start_field(struct tracefs_synth *synth, · line 100

Quoted 7 lines
include/tracefs-local.h +4 -0
@@ -94,4 +94,38 @@ int synth_add_start_field(struct tracefs_synth *synth,
 			  const char *start_field,
 			  const char *name,
 			  enum tracefs_hist_key_type type);
+
+/* Internal interface for ftrace dynamic events */
+#define DYNEVENT_ADD_BIT(M, B)		((M) |= (1ULL<<(B)))
+#define DYNEVENT_CHECK_BIT(M, B)	((M)&(1ULL<<(B)))

The above macros are generic enough that they don't need DYNEVENT in the name. Just call them:

#define SET_BIT(M, B) do { (M) |= (1ULL << (B)); } while (0)
#define TEST_BIT(M, B) ((M) & (1ULL << (B)))

And might as well add for completeness:

#define CLEAR_BIT(M,B) do { (M) &= (1ULL << (B)); } while (0)

It is common to use the terminology of "set,clear,test" for bitmasks. Also, it is best to add the "do { } while (0)" notation for code, as that helps with some side effects macros can have.

include/tracefs-local.h int synth_add_start_field(struct tracefs_synth *synth, · line 103

Quoted 3 lines
include/tracefs-local.h +3 -0
+
+enum trace_dynevent_type {
+	TRACE_DYNEVENT_KPROBE = 0,

No real need for the "= 0" but I'm OK if you want to leave it in. That's because the C standard states that the first enum is zero.

Quoted 16 lines
include/tracefs-local.h +16 -0
+	TRACE_DYNEVENT_KRETPROBE,
+	TRACE_DYNEVENT_UPROBE,
+	TRACE_DYNEVENT_URETPROBE,
+	TRACE_DYNEVENT_EPROBE,
+	TRACE_DYNEVENT_SYNTH,
+	TRACE_DYNEVENT_MAX,
+};
+
+struct tracefs_dynevent {
+	char *trace_file;
+	char *prefix;
+	char *system;
+	char *event;
+	char *address;
+	char *format;
+	enum trace_dynevent_type type;

include/tracefs-local.h int synth_add_start_field(struct tracefs_synth *synth, · line 129

Quoted 10 lines
include/tracefs-local.h +10 -0
+};
+
+struct tracefs_dynevent *
+dynevent_alloc(enum trace_dynevent_type type, const char *system,
+	       const char *event, const char *address, const char *format);
+void dynevent_free(struct tracefs_dynevent *devent);
+int dynevent_create(struct tracefs_dynevent *devent);
+int dynevent_destroy(struct tracefs_dynevent *devent);
+int dynevent_get_all(unsigned long type_mask, const char *system,
+		     struct tracefs_dynevent ***ret_events);

I usually prefer to have a list returned, and NULL on error, instead of passing it in by reference. The "***" gets a bit out of hand.

include/tracefs.h ssize_t tracefs_trace_pipe_stream(int fd, struct tracefs_instance *instance, int · line 242

Quoted 12 lines
include/tracefs-local.h +1 -0
+
 #endif /* _TRACE_FS_LOCAL_H */
include/tracefs.h +2 -0
index a2cda30..4020551 100644
@@ -238,6 +238,9 @@ ssize_t tracefs_trace_pipe_stream(int fd, struct tracefs_instance *instance, int
 ssize_t tracefs_trace_pipe_print(struct tracefs_instance *instance, int flags);
 void tracefs_trace_pipe_stop(struct tracefs_instance *instance);
 
+struct tracefs_dynevent;
+void tracefs_dynevent_list_free(struct tracefs_dynevent ***events);

Why are you passing in the address?

Just use: struct tracefs_dynevent **events

If it is to be consistent with the get_all() then that's another reason to not pass it in as a parameter. That would be like:

int malloc(int size, void **m);

and

void free(void **m)

Quoted 54 lines
include/tracefs.h +1 -0
+
 enum tracefs_kprobe_type {
 	TRACEFS_ALL_KPROBES,
 	TRACEFS_KPROBE,
src/Makefile +1 -0
index 4e38d98..99cd7da 100644
@@ -11,6 +11,7 @@ OBJS += tracefs-marker.o
 OBJS += tracefs-kprobes.o
 OBJS += tracefs-hist.o
 OBJS += tracefs-filter.o
+OBJS += tracefs-dynevents.o
 
 # Order matters for the the three below
 OBJS += sqlhist-lex.o
src/tracefs-dynevents.c new file +32 -0
new file mode 100644
index 0000000..68ee95a
@@ -0,0 +1,463 @@
+// 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 <fcntl.h>
+#include <limits.h>
+
+#include "tracefs.h"
+#include "tracefs-local.h"
+
+#define DYNEVENTS_EVENTS "dynamic_events"
+#define KPROBE_EVENTS "kprobe_events"
+#define UPROBE_EVENTS "uprobe_events"
+#define SYNTH_EVENTS "synthetic_events"
+#define DYNEVENTS_DEFAULT_GROUP "dynamic"
+#define RETPROBE_DEFAUL_PREFIX	"r:"
+
+struct dyn_events_desc;
+static int dyn_generic_parse(struct dyn_events_desc *,
+			     const char *, char *, struct tracefs_dynevent **);
+static int dyn_synth_parse(struct dyn_events_desc *,
+			   const char *, char *, struct tracefs_dynevent **);
+static int dyn_generic_del(struct dyn_events_desc *, struct tracefs_dynevent *);

src/tracefs-dynevents.c line 42

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+static int dyn_synth_del(struct dyn_events_desc *, struct tracefs_dynevent *);
+
+struct dyn_events_desc {
+	enum trace_dynevent_type type;
+	const char *file;
+	const char *prefix;
+	int (*dyn_events_del)(struct dyn_events_desc *desc, struct tracefs_dynevent *dyn);
+	int (*dyn_events_parse)(struct dyn_events_desc *desc, const char *group,
+				char *line, struct tracefs_dynevent **ret_dyn);
+} dynevents[TRACE_DYNEVENT_MAX] = {

You don't need the TRACE_DYNEVENT_MAX. Just have:

} dynevents[] = {

And the compiler will fill in the rest.

We could add somewhere in the code:

	BUILD_BUG_ON(ARRAY_SIZE(dynevents) != TRACE_DYNEVENT_MAX);

to make sure all are accounted for.

Quoted 32 lines
src/tracefs-dynevents.c new file +32 -0
+	{TRACE_DYNEVENT_KPROBE, NULL, "p:", dyn_generic_del, dyn_generic_parse},
+	{TRACE_DYNEVENT_KRETPROBE, NULL, "r", dyn_generic_del, dyn_generic_parse},
+	{TRACE_DYNEVENT_UPROBE, NULL, "p:", dyn_generic_del, dyn_generic_parse},
+	{TRACE_DYNEVENT_URETPROBE, NULL, "r", dyn_generic_del, dyn_generic_parse},
+	{TRACE_DYNEVENT_EPROBE, NULL, "e:", dyn_generic_del, dyn_generic_parse},
+	{TRACE_DYNEVENT_SYNTH, NULL, "s:", dyn_synth_del, dyn_synth_parse},
+};
+
+int dyn_generic_del(struct dyn_events_desc *desc, struct tracefs_dynevent *dyn)
+{
+	char *str;
+	int ret;
+
+	if (dyn->system)
+		ret = asprintf(&str, "-:%s/%s", dyn->system, dyn->event);
+	else
+		ret = asprintf(&str, "-:%s", dyn->event);
+
+	if (ret < 0)
+		return -1;
+
+	ret = tracefs_instance_file_append(NULL, desc->file, str);
+	free(str);
+
+	return ret < 0 ? ret : 0;
+}
+
+__hidden void dynevent_free(struct tracefs_dynevent *devent)
+{
+	if (!devent)
+		return;
+	free(devent->system);

src/tracefs-dynevents.c line 84

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+	free(devent->event);
+	free(devent->address);
+	free(devent->format);
+	free(devent->prefix);
+	free(devent->trace_file);
+	free(devent);
+}
+
+static int dyn_generic_parse(struct dyn_events_desc *desc, const char *group,
+			     char *line, struct tracefs_dynevent **ret_dyn)

Again, I prefer to have the pointer returned, and not assigned as a parameter. All this returns is 0 on success or -1 on failure. That doesn't give us anything more than having ret_dyn allocated or not. (return NULL or non-NULL).

Quoted 13 lines
src/tracefs-dynevents.c new file +13 -0
+{
+	struct tracefs_dynevent *dyn;
+	char *address;
+	char *system;
+	char *format;
+	char *event;
+
+	if (strncmp(line, desc->prefix, strlen(desc->prefix)))
+		return -1;
+
+	system = strchr(line, ':');
+	if (!system)
+		return -1;

src/tracefs-dynevents.c line 107

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+	event = strchr(line, '/');
+	if (!event)
+		return -1;
+	address = strchr(line, ' ');
+	if (!address)
+		return -1;
+	format = strchr(address+1, ' ');
+
+	*address = '\0';
+	*event = '\0';

The above should use strtok_r.

	char *probe;
	char *sav;

	probe = strtok_r(line, ":", &sav);
	system = strtok_r(NULL, "/", &sav);
	event = strtok_r(NULL, " ", &sav);
	address = strtok_r(NULL, " ", &sav);
	format = strtok_r(NULL, "\n", &sav);

src/tracefs-dynevents.c line 114

Quoted 7 lines
src/tracefs-dynevents.c new file +7 -0
+
+	/* KPROBEs and UPROBEs share the same prefix, check the format */
+	if (desc->type == TRACE_DYNEVENT_UPROBE || desc->type == TRACE_DYNEVENT_URETPROBE) {
+		if (!strchr(format, '/'))
+			return -1;
+	}
+	if (group && strcmp(group, system+1) != 0)

No need for +1.

src/tracefs-dynevents.c line 122

Quoted 8 lines
src/tracefs-dynevents.c new file +8 -0
+		return -1;
+
+	if (!ret_dyn)
+		return 0;
+
+	dyn = calloc(1, sizeof(*dyn));
+	if (!dyn)
+		return -1;

add space here. All error checks should have a blank line after them.

Quoted 25 lines
src/tracefs-dynevents.c new file +21 -0
+	dyn->type = desc->type;
+	dyn->trace_file = strdup(desc->file);
+	if (!dyn->trace_file)
+		goto error;
 
+	dyn->system = strdup(system+1);
+	if (!dyn->system)
+		goto error;
 
+	*(system+1) = '\0';
+	/* Prefix of KRETPROBE can contain MAXACTIVE integer */
+	dyn->prefix = strdup(line);
+	if (!dyn->prefix)
+		goto error;
 
+	dyn->event = strdup(event+1);
+	if (!dyn->event)
+		goto error;
 
+	if (desc->type == TRACE_DYNEVENT_SYNTH) {
+		/* Synthetic events have no address */
+		dyn->format = strdup(address+1);
+		if (!dyn->format)
+			goto error;
+	} else {

src/tracefs-dynevents.c line 153

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+		if (format)
+			*format = '\0';
+		dyn->address = strdup(address+1);
+		if (!dyn->address)
+			goto error;
+		if (format) {
+			dyn->format = strdup(format+1);
+			if (!dyn->format)
+				goto error;
+		}

If strtok_r wasn't used, there's no reason to do all these "+1", you could have just done:

	system++;
	event++;
	address++;
	format++;

It would have made it much cleaner.

Quoted 33 lines
src/tracefs-dynevents.c new file +33 -0
+	}
+	*ret_dyn = dyn;
+	return 0;
+error:
+	dynevent_free(dyn);
+	return -1;
+}
+
+int dyn_synth_del(struct dyn_events_desc *desc, struct tracefs_dynevent *dyn)
+{
+	char *str;
+	int ret;
+
+	if (strcmp(desc->file, DYNEVENTS_EVENTS))
+		return dyn_generic_del(desc, dyn);
+
+	ret = asprintf(&str, "!%s", dyn->event);
+	if (ret < 0)
+		return -1;
+
+	ret = tracefs_instance_file_append(NULL, desc->file, str);
+	free(str);
+
+	return ret < 0 ? ret : 0;
+}
+
+static int dyn_synth_parse(struct dyn_events_desc *desc, const char *group,
+			   char *line, struct tracefs_dynevent **ret_dyn)
+{
+	struct tracefs_dynevent *dyn;
+	char *format;
+
+	if (strcmp(desc->file, DYNEVENTS_EVENTS))

src/tracefs-dynevents.c line 196

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+		return dyn_generic_parse(desc, group, line, ret_dyn);
+
+	/* synthetic_events file has slightly different syntax */
+	format = strchr(line, ' ');
+	if (!format)
+		return -1;
+	if (!ret_dyn)
+		return 0;
+
+	*format = '\0';

strtok_r is your friend.

Quoted 43 lines
src/tracefs-dynevents.c new file +43 -0
+	dyn = calloc(1, sizeof(*dyn));
+	if (!dyn)
+		return -1;
+	dyn->type = desc->type;
+	dyn->trace_file = strdup(desc->file);
+	if (!dyn->trace_file)
+		goto error;
+
+	dyn->event = strdup(line);
+	if (!dyn->event)
+		goto error;
+	dyn->format = strdup(format+1);
+	if (!dyn->format)
+		goto error;
+
+	*ret_dyn = dyn;
+	return 0;
+error:
+	dynevent_free(dyn);
+	return -1;
+}
+
+static struct dyn_events_desc *get_devent_desc(enum trace_dynevent_type type)
+{
+	static bool init;
+	int i;
+
+	if (init)
+		goto out;
+
+	init = true;
+	/* Use  ftrace dynamic_events, if available */
+	if (tracefs_file_exists(NULL, DYNEVENTS_EVENTS)) {
+		for (i = 0; i < TRACE_DYNEVENT_MAX; i++)
+			dynevents[i].file = DYNEVENTS_EVENTS;
+		goto out;
+	}
+
+	if (tracefs_file_exists(NULL, KPROBE_EVENTS)) {
+		dynevents[TRACE_DYNEVENT_KPROBE].file = KPROBE_EVENTS;
+		dynevents[TRACE_DYNEVENT_KRETPROBE].file = KPROBE_EVENTS;
+	}
+	if (tracefs_file_exists(NULL, UPROBE_EVENTS)) {

src/tracefs-dynevents.c line 249

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+		dynevents[TRACE_DYNEVENT_UPROBE].file = UPROBE_EVENTS;
+		dynevents[TRACE_DYNEVENT_URETPROBE].file = UPROBE_EVENTS;
+	}
+	if (tracefs_file_exists(NULL, SYNTH_EVENTS)) {
+		dynevents[TRACE_DYNEVENT_SYNTH].file = SYNTH_EVENTS;
+		dynevents[TRACE_DYNEVENT_SYNTH].prefix = "";
+	}
+
+out:
+	return &dynevents[type];

I would break up the above into two functions.

static void init_devent_desc(void)

{
	int i;

	/* Use  ftrace dynamic_events, if available */
	if (tracefs_file_exists(NULL, DYNEVENTS_EVENTS)) {
		for (i = 0; i < TRACE_DYNEVENT_MAX; i++)
			dynevents[i].file = DYNEVENTS_EVENTS;
		goto out;
	}

	if (tracefs_file_exists(NULL, KPROBE_EVENTS)) {
		dynevents[TRACE_DYNEVENT_KPROBE].file = KPROBE_EVENTS;
		dynevents[TRACE_DYNEVENT_KRETPROBE].file = KPROBE_EVENTS;
	}
	if (tracefs_file_exists(NULL, UPROBE_EVENTS)) {
		dynevents[TRACE_DYNEVENT_UPROBE].file = UPROBE_EVENTS;
		dynevents[TRACE_DYNEVENT_URETPROBE].file = UPROBE_EVENTS;
	}
	if (tracefs_file_exists(NULL, SYNTH_EVENTS)) {
		dynevents[TRACE_DYNEVENT_SYNTH].file = SYNTH_EVENTS;
		dynevents[TRACE_DYNEVENT_SYNTH].prefix = "";
	}
}

static struct dyn_events_desc *get_devent_desc(enum trace_dynevent_type type)

{
	static bool init;

	if (!init) {
		init_devent_desc();
		init = true;
	}
	return &dynevents[type];
}
Quoted 57 lines
src/tracefs-dynevents.c new file +57 -0
+}
+
+__hidden struct tracefs_dynevent *
+dynevent_alloc(enum trace_dynevent_type type, const char *system,
+	       const char *event, const char *address, const char *format)
+{
+	struct tracefs_dynevent *devent;
+	struct dyn_events_desc *desc;
+
+	if (!event) {
+		errno = EINVAL;
+		return NULL;
+	}
+
+	desc = get_devent_desc(type);
+	if (!desc || !desc->file) {
+		errno = ENOTSUP;
+		return NULL;
+	}
+
+	devent = calloc(1, sizeof(*devent));
+	if (!devent)
+		return NULL;
+
+	devent->type = type;
+	devent->trace_file = strdup(desc->file);
+	if (!devent->trace_file)
+		goto err;
+	if (!system)
+		system = DYNEVENTS_DEFAULT_GROUP;
+	devent->system = strdup(system);
+	if (!devent->system)
+		goto err;
+	devent->event = strdup(event);
+	if (!devent->event)
+		goto err;
+	if (type == TRACE_DYNEVENT_KRETPROBE || type == TRACE_DYNEVENT_URETPROBE)
+		devent->prefix = strdup(RETPROBE_DEFAUL_PREFIX);
+	else
+		devent->prefix = strdup(desc->prefix);
+
+	if (!devent->prefix)
+		goto err;
+	if (address) {
+		devent->address = strdup(address);
+		if (!devent->address)
+			goto err;
+	}
+	if (format) {
+		devent->format = strdup(format);
+		if (!devent->format)
+			goto err;
+	}
+
+	return devent;
+err:
+	dynevent_free(devent);

src/tracefs-dynevents.c line 316

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+	return NULL;
+}
+
+__hidden int dynevent_create(struct tracefs_dynevent *devent)
+{
+	char *str;
+	int ret;
+
+	if (!devent)
+		return -1;

Again, space after error paths.

src/tracefs-dynevents.c line 320

Quoted 4 lines
src/tracefs-dynevents.c new file +4 -0
+	if (devent->system && devent->system[0])
+		ret = asprintf(&str, "%s%s/%s %s %s\n",
+				devent->prefix, devent->system, devent->event,
+				devent->address?devent->address:"",

Add spaces around "?" and ":" and for the ones below.

Quoted 11 lines
src/tracefs-dynevents.c new file +10 -0
+				devent->format?devent->format:"");
+	else
+		ret = asprintf(&str, "%s%s %s %s\n",
+				devent->prefix, devent->event,
+				devent->address?devent->address:"",
+				devent->format?devent->format:"");
+	if (ret < 0)
+		return -1;
 
+	ret = tracefs_instance_file_append(NULL, devent->trace_file, str);
+	free(str);

src/tracefs-dynevents.c line 340

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+
+	return ret < 0 ? ret : 0;
+}
+
+__hidden int dynevent_destroy(struct tracefs_dynevent *devent)
+{
+	struct dyn_events_desc *desc;
+
+	if (!devent)
+		return -1;

space.

src/tracefs-dynevents.c line 343

Quoted 3 lines
src/tracefs-dynevents.c new file +3 -0
+	desc = get_devent_desc(devent->type);
+	if (!desc)
+		return -1;

space.

Quoted 11 lines
src/tracefs-dynevents.c new file +11 -0
+	return desc->dyn_events_del(desc, devent);
+}
+
+static int get_all_type(enum trace_dynevent_type type, const char *system,
+			struct tracefs_dynevent ***ret_all)
+{
+	struct dyn_events_desc *desc;
+	struct tracefs_dynevent *devent, **tmp, **all = NULL;
+	char *content;
+	int count = 0;
+	char *line;

src/tracefs-dynevents.c line 364

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+	char *next;
+	int ret;
+
+	desc = get_devent_desc(type);
+	if (!desc)
+		return -1;
+
+	content = tracefs_instance_file_read(NULL, desc->file, NULL);
+	if (!content)
+		return -1;

space.

src/tracefs-dynevents.c line 370

Quoted 6 lines
src/tracefs-dynevents.c new file +6 -0
+	line = content;
+	do {
+		next = strchr(line, '\n');
+		if (next)
+			*next = '\0';
+		ret = desc->dyn_events_parse(desc, system, line, ret_all ? &devent : NULL);

So if ret_all is NULL, then we allocate a bunch of devent and lose them?

Quoted 6 lines
src/tracefs-dynevents.c new file +6 -0
+		if (!ret) {
+			if (ret_all) {
+				tmp = realloc(all, (count+1)*sizeof(struct tracefs_dynevent *));
+				if (!tmp)
+					goto error;
+				all = tmp;

src/tracefs-dynevents.c line 386

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+				all[count] = devent;
+			}
+			count++;
+		}
+		line = next + 1;
+	} while (next);
+
+	free(content);
+	if (ret_all)
+		*ret_all = all;

We really should just return all, and if we want a count, we can pass a pointer to an integer, and put the count in there. Not the other way around.

Quoted 4 lines
src/tracefs-dynevents.c new file +4 -0
+	return count;
+
+error:
+	free(content);

src/tracefs-dynevents.c line 400

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+	free(all);
+	return -1;
+}
+
+/**
+ * tracefs_dynevent_list_free - Deletes an array of pointers to dynamic event contexts
+ * @events: An array of pointers to dynamic event contexts. The last element of the array
+ *	    must be a NULL pointer.
+ */
+void tracefs_dynevent_list_free(struct tracefs_dynevent ***events)

Again, just pass in ** not ***.

Quoted 14 lines
src/tracefs-dynevents.c new file +14 -0
+{
+	struct tracefs_dynevent **devent;
+	int i = 0;
+
+	if (!events || !(*events))
+		return;
+
+	devent = *events;
+	while (devent[i])
+		dynevent_free(devent[i++]);
+
+	free(*events);
+}
+

src/tracefs-dynevents.c line 424

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+__hidden int dynevent_get_all(unsigned long type_mask, const char *system,
+			      struct tracefs_dynevent ***ret_events)
+{
+	struct tracefs_dynevent **events, **tmp, **all_events = NULL;
+	int count, all = 0;
+	int i;
+
+	for (i = 0; i < TRACE_DYNEVENT_MAX; i++) {
+		if (!DYNEVENT_CHECK_BIT(type_mask, i))
+			continue;

space.

Quoted 29 lines
src/tracefs-dynevents.c new file +29 -0
+		count = get_all_type(i, system, ret_events ? &events : NULL);
+		if (count > 0) {
+			if (ret_events) {
+				tmp = realloc(all_events,
+					     (all + count)*sizeof(struct tracefs_dynevent *));
+				if (!tmp)
+					goto error;
+				all_events = tmp;
+				memcpy(all_events + all, events,
+				       count*sizeof(struct tracefs_dynevent *));
+			}
+			all += count;
+		}
+
+	}
+
+	if (ret_events) {
+		/* Add a NULL pointer at the end */
+		if (all > 0) {
+			tmp = realloc(all_events,
+				     (all + 1)*sizeof(struct tracefs_dynevent *));
+			if (!tmp)
+				goto error;
+			all_events = tmp;
+			all_events[all] = NULL;
+		}
+		*ret_events = all_events;
+	}
+

src/tracefs-dynevents.c line 463

Quoted 10 lines
src/tracefs-dynevents.c new file +10 -0
+	return all;
+
+error:
+	if (all_events) {
+		for (i = 0; i < all; i++)
+			free(all_events[i]);
+		free(all_events);
+	}
+	return -1;
+}

-- Steve

#3 lore raw

Re: [PATCH 01/12] libtracefs: Add new internal APIs for dynamic events

Quoted 4 lines · Steven Rostedt

On Thu, 28 Oct 2021 15:08:56 +0300
"Tzvetomir Stoyanov (VMware)" <[email protected]> wrote:

[...]

Quoted 4 lines
Quoted 4 lines
include/tracefs-local.h +4 -0
+
+struct tracefs_dynevent *
+dynevent_alloc(enum trace_dynevent_type type, const char *system,
+            const char *event, const char *address, const char *format);
Quoted 9 lines

include/tracefs-local.h int synth_add_start_field(struct tracefs_synth *synth, · line 129

Quoted 5 lines
include/tracefs-local.h +5 -0
+void dynevent_free(struct tracefs_dynevent *devent);
+int dynevent_create(struct tracefs_dynevent *devent);
+int dynevent_destroy(struct tracefs_dynevent *devent);
+int dynevent_get_all(unsigned long type_mask, const char *system,
+                  struct tracefs_dynevent ***ret_events);

I usually prefer to have a list returned, and NULL on error, instead of passing it in by reference. The "***" gets a bit out of hand.

The ret_events argument is optional, that's why it is passed as a parameter. If ret_events is NULL, the API will return only the count of the requested events. No array with events will be allocated in that case.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +1 -0
+
 #endif /* _TRACE_FS_LOCAL_H */
include/tracefs.h +0 -0
index a2cda30..4020551 100644

[ ... ]

Quoted 9 lines

include/tracefs.h

Quoted 3 lines
include/tracefs.h +3 -0
+
+static int dyn_generic_parse(struct dyn_events_desc *desc, const char *group,
+                          char *line, struct tracefs_dynevent **ret_dyn)

Again, I prefer to have the pointer returned, and not assigned as a parameter. All this returns is 0 on success or -1 on failure. That doesn't give us anything more than having ret_dyn allocated or not. (return NULL or non-NULL).

The ret_dyn is optional. If it is NULL, the function only checks if the line contains an event of the requested type, without allocating any memory.

Quoted 27 lines
Quoted 13 lines
include/tracefs.h +13 -0
+{
+     struct tracefs_dynevent *dyn;
+     char *address;
+     char *system;
+     char *format;
+     char *event;
+
+     if (strncmp(line, desc->prefix, strlen(desc->prefix)))
+             return -1;
+
+     system = strchr(line, ':');
+     if (!system)
+             return -1;

include/tracefs.h

Quoted 10 lines
include/tracefs.h +10 -0
+     event = strchr(line, '/');
+     if (!event)
+             return -1;
+     address = strchr(line, ' ');
+     if (!address)
+             return -1;
+     format = strchr(address+1, ' ');
+
+     *address = '\0';
+     *event = '\0';

The above should use strtok_r.

Quoted 9 lines
        char *probe;
        char *sav;

        probe = strtok_r(line, ":", &sav);
        system = strtok_r(NULL, "/", &sav);
        event = strtok_r(NULL, " ", &sav);
        address = strtok_r(NULL, " ", &sav);
        format = strtok_r(NULL, "\n", &sav);

I used strchr() instead of strtok_r() here, because my original idea was that this function should not modify the original line string, if the event is not of the requested type. When parsing the "dynamic_events" file, it contains events from any types. My initial design was to pass the same line string to all parse functions, until there is a match. But the design was changed, now a new copy of the file is used for each event type. I'll change that code to use strtok_r().

Quoted 9 lines
Quoted 9 lines
include/tracefs.h +6 -0
+
+     /* KPROBEs and UPROBEs share the same prefix, check the format */
+     if (desc->type == TRACE_DYNEVENT_UPROBE || desc->type == TRACE_DYNEVENT_URETPROBE) {
+             if (!strchr(format, '/'))
+                     return -1;
+     }

[ ... ]

Quoted 22 lines
Quoted 9 lines
include/tracefs.h +9 -0
+
+static int get_all_type(enum trace_dynevent_type type, const char *system,
+                     struct tracefs_dynevent ***ret_all)
+{
+     struct dyn_events_desc *desc;
+     struct tracefs_dynevent *devent, **tmp, **all = NULL;
+     char *content;
+     int count = 0;
+     char *line;

include/tracefs.h

Quoted 10 lines
include/tracefs.h +10 -0
+     char *next;
+     int ret;
+
+     desc = get_devent_desc(type);
+     if (!desc)
+             return -1;
+
+     content = tracefs_instance_file_read(NULL, desc->file, NULL);
+     if (!content)
+             return -1;

space.

Quoted 9 lines

include/tracefs.h

Quoted 6 lines
include/tracefs.h +6 -0
+     line = content;
+     do {
+             next = strchr(line, '\n');
+             if (next)
+                     *next = '\0';
+             ret = desc->dyn_events_parse(desc, system, line, ret_all ? &devent : NULL);

So if ret_all is NULL, then we allocate a bunch of devent and lose them?

Hm, no devents are allocated if ret_all is NULL. That's the idea of that API design - ret_all is an optional input parameter. Without it, the functions can be used only to get the count of the requested events, without any memory allocation. I would like to keep that functionality, even though it is not used at the moment.

Quoted 98 lines
Quoted 6 lines
include/tracefs.h +6 -0
+             if (!ret) {
+                     if (ret_all) {
+                             tmp = realloc(all, (count+1)*sizeof(struct tracefs_dynevent *));
+                             if (!tmp)
+                                     goto error;
+                             all = tmp;

include/tracefs.h

Quoted 10 lines
include/tracefs.h +10 -0
+                             all[count] = devent;
+                     }
+                     count++;
+             }
+             line = next + 1;
+     } while (next);
+
+     free(content);
+     if (ret_all)
+             *ret_all = all;

We really should just return all, and if we want a count, we can pass a pointer to an integer, and put the count in there. Not the other way around.

Quoted 4 lines
include/tracefs.h +4 -0
+     return count;
+
+error:
+     free(content);

include/tracefs.h

Quoted 10 lines
include/tracefs.h +10 -0
+     free(all);
+     return -1;
+}
+
+/**
+ * tracefs_dynevent_list_free - Deletes an array of pointers to dynamic event contexts
+ * @events: An array of pointers to dynamic event contexts. The last element of the array
+ *       must be a NULL pointer.
+ */
+void tracefs_dynevent_list_free(struct tracefs_dynevent ***events)

Again, just pass in ** not ***.

Quoted 14 lines
include/tracefs.h +14 -0
+{
+     struct tracefs_dynevent **devent;
+     int i = 0;
+
+     if (!events || !(*events))
+             return;
+
+     devent = *events;
+     while (devent[i])
+             dynevent_free(devent[i++]);
+
+     free(*events);
+}
+

include/tracefs.h

Quoted 10 lines
include/tracefs.h +10 -0
+__hidden int dynevent_get_all(unsigned long type_mask, const char *system,
+                           struct tracefs_dynevent ***ret_events)
+{
+     struct tracefs_dynevent **events, **tmp, **all_events = NULL;
+     int count, all = 0;
+     int i;
+
+     for (i = 0; i < TRACE_DYNEVENT_MAX; i++) {
+             if (!DYNEVENT_CHECK_BIT(type_mask, i))
+                     continue;

space.

Quoted 34 lines
include/tracefs.h +34 -0
+             count = get_all_type(i, system, ret_events ? &events : NULL);
+             if (count > 0) {
+                     if (ret_events) {
+                             tmp = realloc(all_events,
+                                          (all + count)*sizeof(struct tracefs_dynevent *));
+                             if (!tmp)
+                                     goto error;
+                             all_events = tmp;
+                             memcpy(all_events + all, events,
+                                    count*sizeof(struct tracefs_dynevent *));
+                     }
+                     all += count;
+             }
+
+     }
+
+     if (ret_events) {
+             /* Add a NULL pointer at the end */
+             if (all > 0) {
+                     tmp = realloc(all_events,
+                                  (all + 1)*sizeof(struct tracefs_dynevent *));
+                     if (!tmp)
+                             goto error;
+                     all_events = tmp;
+                     all_events[all] = NULL;
+             }
+             *ret_events = all_events;
+     }
+
+     return all;
+
+error:
+     if (all_events) {
+             for (i = 0; i < all; i++)
Quoted 8 lines

include/tracefs.h

Quoted 5 lines
include/tracefs.h +5 -0
+                     free(all_events[i]);
+             free(all_events);
+     }
+     return -1;
+}

-- Steve

Thanks Steven! I'll address these comments in the next version, but I really want to keep the dynevent_get_all() API more flexible - to have a way to get only the count, without allocating an array with the events.

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#4 lore raw

Re: [PATCH 01/12] libtracefs: Add new internal APIs for dynamic events

Quoted 20 lines · On Fri, 29 Oct 2021 05:46:42 +0300 Tzvetomir Stoyanov <[email protected]>…
Quoted 4 lines · Steven Rostedt

On Thu, 28 Oct 2021 15:08:56 +0300
"Tzvetomir Stoyanov (VMware)" <[email protected]> wrote:

[...]

Quoted 4 lines
Quoted 4 lines
include/tracefs-local.h +4 -0
+
+struct tracefs_dynevent *
+dynevent_alloc(enum trace_dynevent_type type, const char *system,
+            const char *event, const char *address, const char *format);
Quoted 9 lines

include/tracefs-local.h int synth_add_start_field(struct tracefs_synth *synth, · line 129

Quoted 5 lines
include/tracefs-local.h +5 -0
+void dynevent_free(struct tracefs_dynevent *devent);
+int dynevent_create(struct tracefs_dynevent *devent);
+int dynevent_destroy(struct tracefs_dynevent *devent);
+int dynevent_get_all(unsigned long type_mask, const char *system,
+                  struct tracefs_dynevent ***ret_events);  

I usually prefer to have a list returned, and NULL on error, instead of passing it in by reference. The "***" gets a bit out of hand.

Quoted 5 lines

The ret_events argument is optional, that's why it is passed as a parameter. If ret_events is NULL, the API will return only the count of the requested events. No array with events will be allocated in that case.

I would make a separate API for just the count, and not have this function server two functionalities.

Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +1 -0
+
 #endif /* _TRACE_FS_LOCAL_H */
include/tracefs.h +0 -0
index a2cda30..4020551 100644  

[ ... ]

Quoted 9 lines

include/tracefs.h

Quoted 3 lines
include/tracefs.h +3 -0
+
+static int dyn_generic_parse(struct dyn_events_desc *desc, const char *group,
+                          char *line, struct tracefs_dynevent **ret_dyn)  

Again, I prefer to have the pointer returned, and not assigned as a parameter. All this returns is 0 on success or -1 on failure. That doesn't give us anything more than having ret_dyn allocated or not. (return NULL or non-NULL).

Quoted 3 lines

The ret_dyn is optional. If it is NULL, the function only checks if the line contains an event of the requested type, without allocating any memory.

I think we should make a separate API for that as well.

dyn_generic_parse_check()?

Quoted 86 lines
Quoted 28 lines
Quoted 14 lines
include/tracefs.h +13 -0
+{
+     struct tracefs_dynevent *dyn;
+     char *address;
+     char *system;
+     char *format;
+     char *event;
+
+     if (strncmp(line, desc->prefix, strlen(desc->prefix)))
+             return -1;
+
+     system = strchr(line, ':');
+     if (!system)
+             return -1;

include/tracefs.h

Quoted 10 lines
include/tracefs.h +10 -0
+     event = strchr(line, '/');
+     if (!event)
+             return -1;
+     address = strchr(line, ' ');
+     if (!address)
+             return -1;
+     format = strchr(address+1, ' ');
+
+     *address = '\0';
+     *event = '\0';  

The above should use strtok_r.

Quoted 9 lines
        char *probe;
        char *sav;

        probe = strtok_r(line, ":", &sav);
        system = strtok_r(NULL, "/", &sav);
        event = strtok_r(NULL, " ", &sav);
        address = strtok_r(NULL, " ", &sav);
        format = strtok_r(NULL, "\n", &sav);

I used strchr() instead of strtok_r() here, because my original idea was that this function should not modify the original line string, if the event is not of the requested type. When parsing the "dynamic_events" file, it contains events from any types. My initial design was to pass the same line string to all parse functions, until there is a match. But the design was changed, now a new copy of the file is used for each event type. I'll change that code to use strtok_r().

Quoted 9 lines
Quoted 9 lines
include/tracefs.h +6 -0
+
+     /* KPROBEs and UPROBEs share the same prefix, check the format */
+     if (desc->type == TRACE_DYNEVENT_UPROBE || desc->type == TRACE_DYNEVENT_URETPROBE) {
+             if (!strchr(format, '/'))
+                     return -1;
+     }  

[ ... ]

Quoted 29 lines
Quoted 9 lines
include/tracefs.h +9 -0
+
+static int get_all_type(enum trace_dynevent_type type, const char *system,
+                     struct tracefs_dynevent ***ret_all)
+{
+     struct dyn_events_desc *desc;
+     struct tracefs_dynevent *devent, **tmp, **all = NULL;
+     char *content;
+     int count = 0;
+     char *line;

include/tracefs.h

Quoted 10 lines
include/tracefs.h +10 -0
+     char *next;
+     int ret;
+
+     desc = get_devent_desc(type);
+     if (!desc)
+             return -1;
+
+     content = tracefs_instance_file_read(NULL, desc->file, NULL);
+     if (!content)
+             return -1;  

space.

Quoted 6 lines
include/tracefs.h +6 -0
+     line = content;
+     do {
+             next = strchr(line, '\n');
+             if (next)
+                     *next = '\0';
+             ret = desc->dyn_events_parse(desc, system, line, ret_all ? &devent : NULL);  
Quoted 8 lines
Quoted 2 lines

So if ret_all is NULL, then we allocate a bunch of devent and lose them?

Hm, no devents are allocated if ret_all is NULL. That's the idea of that API design - ret_all is an optional input parameter. Without it, the functions can be used only to get the count of the requested events, without any memory allocation. I would like to keep that functionality, even though it is not used at the moment.

Ah. But still, this is overly complex to try to have this server two functionalities. It becomes confusing.

Quoted 5 lines

Thanks Steven! I'll address these comments in the next version, but I really want to keep the dynevent_get_all() API more flexible - to have a way to get only the count, without allocating an array with the events.

I really think you should have that as two separate functions, otherwise it becomes overly complex.

-- Steve

#5 lore raw

PATCH 2/12 libtracefs: Rename tracefs_get_kprobes API

In reply to Tzvetomir Stoyanov (VMware)

The names of tracefs API should follow the same pattern:
tracefs_XXX_<action>
Rename tracefs_get_kprobes() to tracefs_kprobes_get() to be consistent with that pattern.

---
 Documentation/libtracefs-kprobes.txt |  8 ++++----
 include/tracefs.h                    |  2 +-
 src/tracefs-kprobes.c                |  6 +++---
 utest/tracefs-utest.c                | 10 +++++-----
 4 files changed, 13 insertions(+), 13 deletions(-)
Documentation/libtracefs-kprobes.txt +4 -4
index c10576e..ca48fa9 100644
@@ -3,7 +3,7 @@ libtracefs(3)
 
 NAME
 ----
-tracefs_kprobe_raw, tracefs_kretprobe_raw, tracefs_get_kprobes, tracefs_kprobe_info, tracefs_kprobe_clear_all, tracefs_kprobe_clear_probe - Create, list, and destroy kprobes
+tracefs_kprobe_raw, tracefs_kretprobe_raw, tracefs_kprobes_get, tracefs_kprobe_info, tracefs_kprobe_clear_all, tracefs_kprobe_clear_probe - Create, list, and destroy kprobes
 
 SYNOPSIS
 --------
@@ -13,7 +13,7 @@ SYNOPSIS
 
 int tracefs_kprobe_raw(const char pass:[*]system, const char pass:[*]event, const char pass:[*]addr, const char pass:[*]format);
 int tracefs_kretprobe_raw(const char pass:[*]system, const char pass:[*]event, const char pass:[*]addr, const char pass:[*]format);
-char pass:[*]pass:[*]tracefs_get_kprobes(enum tracefs_kprobe_type type);
+char pass:[*]pass:[*]tracefs_kprobes_get(enum tracefs_kprobe_type type);
 enum tracefs_kprobe_type tracefs_kprobe_info(const char pass:[*]group, const char pass:[*]event,
 					     char pass:[*]pass:[*]type, char pass:[*]pass:[*]addr, char pass:[*]pass:[*]format);
 enum tracefs_kprobe_type tracefs_kprobe_type(const char pass:[*]group, const char pass:[*]event)
@@ -36,7 +36,7 @@ document.
 creates a kretprobe instead of a kprobe. The difference is also described
 in the Linux kernel source in the Documentation/trace/kprobetrace.rst file.
 
-*tracefs_get_kprobes*() returns an array of strings (char pass:[*]) that contain
+*tracefs_kprobes_get*() returns an array of strings (char pass:[*]) that contain
 the registered kprobes and kretprobes depending on the given _type_. If _type_ is
 TRACEFS_ALL_KPROBES, then all kprobes found are returned. If _type_ is
 TRACEFS_KPROBE, then only normal kprobes are returned. If _type_ is
@@ -76,7 +76,7 @@ If a parsing error occurs on *tracefs_kprobe_raw*() or *tracefs_kretprobe_raw*()
 then *tracefs_error_last*(3) may be used to retrieve the error message explaining
 the parsing issue.
 
-*tracefs_get_kprobes*() returns an allocate string list of allocated strings
+*tracefs_kprobes_get*() returns an allocate string list of allocated strings
 on success that must be freed with *tracefs_list_free*(3) and returns
 NULL on error.
 
include/tracefs.h +1 -1
index 4020551..0240aef 100644
@@ -251,7 +251,7 @@ int tracefs_kprobe_raw(const char *system, const char *event,
 		       const char *addr, const char *format);
 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);
+char **tracefs_kprobes_get(enum tracefs_kprobe_type type);
 enum tracefs_kprobe_type tracefs_kprobe_info(const char *group, const char *event,
 					     char **type, char **addr, char **format);
 int tracefs_kprobe_clear_all(bool force);
src/tracefs-kprobes.c +3 -3
index 6fdd8f9..d4c5f9e 100644
@@ -158,7 +158,7 @@ static int parse_kprobe(char *content, char **saveptr,
 }
 
 /**
- * tracefs_get_kprobes - return a list kprobes (by group/event name)
+ * tracefs_kprobes_get - return a list kprobes (by group/event name)
  * @type: The type of kprobes to return.
  *
  * If @type is TRACEFS_ALL_KPROBES all kprobes in the kprobe_events
@@ -174,7 +174,7 @@ static int parse_kprobe(char *content, char **saveptr,
  * only a NULL pointer.
  * On error, NULL is returned.
  */
-char **tracefs_get_kprobes(enum tracefs_kprobe_type type)
+char **tracefs_kprobes_get(enum tracefs_kprobe_type type)
 {
 	char **list = NULL;
 	char *content;
@@ -357,7 +357,7 @@ static int kprobe_clear_probes(const char *group, bool force)
 	int ret;
 	int i;
 
-	kprobe_list = tracefs_get_kprobes(TRACEFS_ALL_KPROBES);
+	kprobe_list = tracefs_kprobes_get(TRACEFS_ALL_KPROBES);
 	if (!kprobe_list)
 		return -1;
 
utest/tracefs-utest.c +5 -5
index 09bb8f2..7c1a84e 100644
@@ -583,7 +583,7 @@ static void test_instance_file(void)
 	free(kaddr);
 	free(kformat);
 
-	kprobes = tracefs_get_kprobes(TRACEFS_ALL_KPROBES);
+	kprobes = tracefs_kprobes_get(TRACEFS_ALL_KPROBES);
 	CU_TEST(kprobes != NULL);
 
 	for (i = 0; kprobes[i]; i++) {
@@ -612,7 +612,7 @@ static void test_instance_file(void)
 	tracefs_list_free(kprobes);
 	CU_TEST(i == 3);
 
-	kprobes = tracefs_get_kprobes(TRACEFS_KPROBE);
+	kprobes = tracefs_kprobes_get(TRACEFS_KPROBE);
 	CU_TEST(kprobes != NULL);
 
 	for (i = 0; kprobes[i]; i++) {
@@ -631,7 +631,7 @@ static void test_instance_file(void)
 	tracefs_list_free(kprobes);
 	CU_TEST(i == 2);
 
-	kprobes = tracefs_get_kprobes(TRACEFS_KRETPROBE);
+	kprobes = tracefs_kprobes_get(TRACEFS_KRETPROBE);
 	CU_TEST(kprobes != NULL);
 
 	for (i = 0; kprobes[i]; i++) {
@@ -663,7 +663,7 @@ static void test_instance_file(void)
 	ret = tracefs_kprobe_clear_probe(KPROBE_2_GROUP, NULL, true);
 	CU_TEST(ret == 0);
 
-	kprobes = tracefs_get_kprobes(TRACEFS_ALL_KPROBES);
+	kprobes = tracefs_kprobes_get(TRACEFS_ALL_KPROBES);
 	CU_TEST(kprobes != NULL);
 
 	for (i = 0; kprobes[i]; i++) {
@@ -682,7 +682,7 @@ static void test_instance_file(void)
 	ret = tracefs_kprobe_clear_all(true);
 	CU_TEST(ret == 0);
 
-	kprobes = tracefs_get_kprobes(TRACEFS_ALL_KPROBES);
+	kprobes = tracefs_kprobes_get(TRACEFS_ALL_KPROBES);
 	CU_TEST(kprobes != NULL);
 
 	CU_TEST(kprobes[0] == NULL);
Signature · 1 line
2.31.1
#6 lore raw

PATCH 3/12 libtracefs: New kprobes APIs

In reply to Tzvetomir Stoyanov (VMware)

In order to be consistent with the other APIs, a new set of kprobe APIs is introduced:
tracefs_kprobe_alloc();
tracefs_kretprobe_alloc();
tracefs_kprobe_create();
tracefs_kprobe_destroy();
tracefs_kprobe_free();
These APIs work with kprobe context, represented by the tracefs_dynevent structure.

---
 include/tracefs.h     |   8 ++
 src/tracefs-kprobes.c | 169 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 177 insertions(+)
include/tracefs.h +8 -0
index 0240aef..e7d545c 100644
@@ -247,6 +247,14 @@ enum tracefs_kprobe_type {
 	TRACEFS_KRETPROBE,
 };
 
+struct tracefs_dynevent *
+tracefs_kprobe_alloc(const char *system, const char *event, const char *addr, const char *format);
+struct tracefs_dynevent *
+tracefs_kretprobe_alloc(const char *system, const char *event,
+			const char *addr, const char *format, int max);
+int tracefs_kprobe_create(struct tracefs_dynevent *kprobe);
+int tracefs_kprobe_destroy(struct tracefs_dynevent *kprobe, bool force);
+void tracefs_kprobe_free(struct tracefs_dynevent *kprobe);
 int tracefs_kprobe_raw(const char *system, const char *event,
 		       const char *addr, const char *format);
 int tracefs_kretprobe_raw(const char *system, const char *event,
src/tracefs-kprobes.c +169 -0

178 lines not on this page · raw message

Signature · 1 line
2.31.1
#7 lore raw

Re: [PATCH 03/12] libtracefs: New kprobes APIs

Quoted 21 lines · On Thu, 28 Oct 2021 15:08:58 +0300 "Tzvetomir Stoyanov (VMware)"…
src/tracefs-kprobes.c +21 -0
+static struct tracefs_dynevent *
+kprobe_alloc(enum trace_dynevent_type type, const char *system, const char *event,
+	     const char *addr, const char *format)
+{
+	struct tracefs_dynevent *kp;
+	const char *sys = system;
+	const char *ename = event;
+	char *tmp;
+
+	if (!addr || !format) {
+		errno = EBADMSG;
+		return NULL;
+	}
+	if (!sys)
+		sys = KPROBE_DEFAULT_GROUP;
+
+	if (!event) {
+		ename = strdup(addr);
+		if (!ename)
+			return NULL;
+		tmp = strchr(ename, ':');

src/tracefs-kprobes.c line 53

Quoted 10 lines
src/tracefs-kprobes.c +10 -0
+		if (tmp)
+			*tmp = '\0';
+	}
+
+	kp = dynevent_alloc(type, sys, ename, addr, format);
+	if (!event)
+		free((char *)ename);
+	if (!kp)
+		return NULL;
+	return kp;

If !kp is true, then return kp is the same as return NULL;

Nuke the "if (!kp)" code.

src/tracefs-kprobes.c line 47

Quoted 3 lines
src/tracefs-kprobes.c +3 -0
+}
+
+

-- Steve

#8 lore raw

PATCH 4/12 libtracefs: Remove redundant kprobes APIs

In reply to Tzvetomir Stoyanov (VMware)

The newly introduced API tracefs_kprobe_destroy() can be used to clear a specific kprobe or all kprobes from the system. These legacy APIs are removed as redundant:
tracefs_kprobe_clear_all();
tracefs_kprobe_clear_probe();

There is one functionality, missing in the new API - clearing all kprobes from specific system only. If this is needed, tracefs_kprobe_destroy() can be extended with that use case.

---
 include/tracefs.h     |  2 --
 src/tracefs-kprobes.c | 62 -------------------------------------------
 2 files changed, 64 deletions(-)
include/tracefs.h +0 -2
index e7d545c..c7b9179 100644
@@ -262,8 +262,6 @@ int tracefs_kretprobe_raw(const char *system, const char *event,
 char **tracefs_kprobes_get(enum tracefs_kprobe_type type);
 enum tracefs_kprobe_type tracefs_kprobe_info(const char *group, const char *event,
 					     char **type, char **addr, char **format);
-int tracefs_kprobe_clear_all(bool force);
-int tracefs_kprobe_clear_probe(const char *system, const char *event, bool force);
 
 enum tracefs_hist_key_type {
 	TRACEFS_HIST_KEY_NORMAL = 0,
src/tracefs-kprobes.c +0 -62

68 lines not on this page · raw message

Signature · 1 line
2.31.1
#9 lore raw

PATCH 5/12 libtracefs: Reimplement tracefs_kprobes_get API

In reply to Tzvetomir Stoyanov (VMware)

In order to unify the code and reuse the internal helpers for dynamic events helpers, the tracefs_kprobes_get() API is reimplemented using the new dynamic events APIs.

---
 include/tracefs.h     |   2 +-
 src/tracefs-kprobes.c | 127 +++++++++++-------------------------------
 2 files changed, 33 insertions(+), 96 deletions(-)
include/tracefs.h +1 -1
index c7b9179..d4ac8fb 100644
@@ -259,7 +259,7 @@ int tracefs_kprobe_raw(const char *system, const char *event,
 		       const char *addr, const char *format);
 int tracefs_kretprobe_raw(const char *system, const char *event,
 			  const char *addr, const char *format);
-char **tracefs_kprobes_get(enum tracefs_kprobe_type type);
+int tracefs_kprobes_get(enum tracefs_kprobe_type type, struct tracefs_dynevent ***kprobes);
 enum tracefs_kprobe_type tracefs_kprobe_info(const char *group, const char *event,
 					     char **type, char **addr, char **format);
 
src/tracefs-kprobes.c +32 -95

172 lines not on this page · raw message

Signature · 1 line
2.31.1
#10 lore raw

Re: [PATCH 05/12] libtracefs: Reimplement tracefs_kprobes_get API

Quoted 15 lines · On Thu, 28 Oct 2021 15:09:00 +0300 "Tzvetomir Stoyanov (VMware)"…

In order to unify the code and reuse the internal helpers for dynamic events helpers, the tracefs_kprobes_get() API is reimplemented using the new dynamic events APIs.

---
 include/tracefs.h     |   2 +-
 src/tracefs-kprobes.c | 127 +++++++++++-------------------------------
 2 files changed, 33 insertions(+), 96 deletions(-)
include/tracefs.h +0 -0
index c7b9179..d4ac8fb 100644

include/tracefs.h int tracefs_kprobe_raw(const char *system, const char *event, · line 262

Quoted 5 lines
include/tracefs.h +1 -1
@@ -259,7 +259,7 @@ int tracefs_kprobe_raw(const char *system, const char *event,
 		       const char *addr, const char *format);
 int tracefs_kretprobe_raw(const char *system, const char *event,
 			  const char *addr, const char *format);
-char **tracefs_kprobes_get(enum tracefs_kprobe_type type);
+int tracefs_kprobes_get(enum tracefs_kprobe_type type, struct tracefs_dynevent ***kprobes);

Like I mentioned for the other patches. This should return the array, and not be passing in an object to allocate.

General rule is, if you need to have a parameter with three dereferences (***) you're doing it wrong. ;-)

src/tracefs-kprobes.c

Quoted 4 lines
include/tracefs.h +0 -0
 enum tracefs_kprobe_type tracefs_kprobe_info(const char *group, const char *event,
 					     char **type, char **addr, char **format);
 
src/tracefs-kprobes.c +0 -0

-- Steve

#11 lore raw

PATCH 6/12 libtracefs: Change tracefs_kprobe_info API

In reply to Tzvetomir Stoyanov (VMware)

In order to make kprobe APIs consistent with the other libtracefs APIs, the tracefs_kprobe_info() API is reimplemented and changed to work with the library kprobe context structure.

---
 include/tracefs.h     |   5 +-
 src/tracefs-kprobes.c | 175 ++++++++++++++++--------------------------
 2 files changed, 69 insertions(+), 111 deletions(-)
include/tracefs.h +3 -2
index d4ac8fb..c65f21f 100644
@@ -260,8 +260,9 @@ 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);
 int tracefs_kprobes_get(enum tracefs_kprobe_type type, struct tracefs_dynevent ***kprobes);
-enum tracefs_kprobe_type tracefs_kprobe_info(const char *group, const char *event,
-					     char **type, char **addr, char **format);
+enum tracefs_kprobe_type tracefs_kprobe_info(struct tracefs_dynevent *kprobe,
+					     char **system, char **event,
+					     char **prefix, char **addr, char **format);
 
 enum tracefs_hist_key_type {
 	TRACEFS_HIST_KEY_NORMAL = 0,
src/tracefs-kprobes.c +66 -109

198 lines not on this page · raw message

Signature · 1 line
2.31.1
#12 lore raw

PATCH 7/12 libtracefs: Reimplement kprobe raw APIs

In reply to Tzvetomir Stoyanov (VMware)

In order to unify the code and use the new dynamic event helpers, these kprobe APIs are rewritten:
kracefs_kprobe_raw()
kracefs_kretprobe_raw()
The definition and logic of the APIs remain the same.

---
 src/tracefs-kprobes.c | 44 +++++++++++++++++--------------------------
 1 file changed, 17 insertions(+), 27 deletions(-)
src/tracefs-kprobes.c +17 -27
index 083a3a9..84a9903 100644
@@ -148,38 +148,28 @@ void tracefs_kprobe_free(struct tracefs_dynevent *kprobe)
 	dynevent_free(kprobe);
 }
 
-
-static int insert_kprobe(const char *type, const char *system,
-			 const char *event, const char *addr,
-			 const char *format)
+static int kprobe_raw(enum trace_dynevent_type type, const char *system,
+		      const char *event, const char *addr, const char *format)
 {
-	char *str;
+	static struct tracefs_dynevent *kp;
 	int ret;
 
-	if (!tracefs_file_exists(NULL, KPROBE_EVENTS))
-		return -1;
-
-	errno = EBADMSG;
-	if (!addr || !format)
+	kp = kprobe_alloc(type, system, event, addr, format);
+	if (!kp)
 		return -1;
 
-	if (!event)
-		event = addr;
-
-	if (system)
-		ret = asprintf(&str, "%s:%s/%s %s %s\n",
-			       type, system, event, addr, format);
-	else
-		ret = asprintf(&str, "%s:%s %s %s\n",
-			       type, event, addr, format);
-
-	if (ret < 0)
-		return -1;
+	if (type == TRACE_DYNEVENT_KRETPROBE) {
+		free(kp->prefix);
+		kp->prefix = NULL;
+		ret = asprintf(&kp->prefix, "r:");
+		if (ret < 0)
+			return ret;
+	}
 
-	ret = tracefs_instance_file_append(NULL, KPROBE_EVENTS, str);
-	free(str);
+	ret = dynevent_create(kp);
+	dynevent_free(kp);
 
-	return ret < 0 ? ret : 0;
+	return ret;
 }
 
 /**
@@ -205,7 +195,7 @@ static int insert_kprobe(const char *type, const char *system,
 int tracefs_kprobe_raw(const char *system, const char *event,
 		       const char *addr, const char *format)
 {
-	return insert_kprobe("p", system, event, addr, format);
+	return kprobe_raw(TRACE_DYNEVENT_KPROBE, system, event, addr, format);
 }
 
 /**
@@ -231,7 +221,7 @@ 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)
 {
-	return insert_kprobe("r", system, event, addr, format);
+	return kprobe_raw(TRACE_DYNEVENT_KRETPROBE, system, event, addr, format);
 }
 
 /**
Signature · 1 line
2.31.1
#13 lore raw

PATCH 8/12 libtracefs: Extend kprobes unit test

In reply to Tzvetomir Stoyanov (VMware)

As there are a lot of changes in the libtracefs kprobes APIs, the unit test of that functionality should be updated. A new test section is added, for testing the all kprobes APIs.

---
 utest/tracefs-utest.c | 414 +++++++++++++++++++++++++-----------------
 1 file changed, 244 insertions(+), 170 deletions(-)
utest/tracefs-utest.c +244 -170
index 7c1a84e..0a17b82 100644
@@ -28,22 +28,6 @@
 #define TRACE_ON	"tracing_on"
 #define TRACE_CLOCK	"trace_clock"
 
-#define KPROBE_EVENTS	"kprobe_events"
-
-#define KPROBE_1_NAME	"mkdir"
-#define KPROBE_1_GROUP	"kprobes"
-#define KPROBE_1_ADDR	"do_mkdirat"
-#define KPROBE_1_FMT	"path=+u0($arg2):ustring"
-
-#define KPROBE_2_NAME	"open"
-#define KPROBE_2_GROUP	"myprobe"
-#define KPROBE_2_ADDR	"do_sys_openat2"
-#define KPROBE_2_FMT	"file=+u0($arg2):ustring flags=+0($arg3):x64"
-
-#define KRETPROBE_NAME	"retopen"
-#define KRETPROBE_ADDR	"do_sys_openat2"
-#define KRETPROBE_FMT	"ret=$retval"
-
 #define SQL_1_EVENT	"wakeup_1"
 #define SQL_1_SQL	"select sched_switch.next_pid as woke_pid, sched_waking.common_pid as waking_pid from sched_waking join sched_switch on sched_switch.next_pid = sched_waking.pid"
 
@@ -457,28 +441,264 @@ static void test_instance_file_read(struct tracefs_instance *inst, const char *f
 	free(file);
 }
 
+#define KPROBE_DEFAULT_GROUP "kprobes"
+struct kprobe_test {
+	enum tracefs_kprobe_type type;
+	char *prefix;
+	char *system;
+	char *event;
+	char *address;
+	char *format;
+};
+
+static bool check_kprobes(struct kprobe_test *kprobes, int count,
+			  struct tracefs_dynevent **devents, bool in_system)
+{
+
+	enum tracefs_kprobe_type ktype;
+	char *ename;
+	char *kaddress;
+	char *kevent;
+	char *ksystem;
+	char *kformat;
+	char *kprefix;
+	int found = 0;
+	int ret;
+	int i, j;
+
+	for (i = 0; devents[i]; i++) {
+		ktype = tracefs_kprobe_info(devents[i], &ksystem,
+					   &kevent, &kprefix, &kaddress, &kformat);
+		for (j = 0; j < count; j++) {
+			if (ktype != kprobes[j].type)
+				continue;
+			if (kprobes[j].event)
+				ename = kprobes[j].event;
+			else
+				ename = kprobes[j].address;
+			if (strcmp(ename, kevent))
+				continue;
+			if (kprobes[j].system) {
+				CU_TEST(strcmp(kprobes[j].system, ksystem) == 0);
+			} else {
+				CU_TEST(strcmp(KPROBE_DEFAULT_GROUP, ksystem) == 0);
+			}
+			CU_TEST(strcmp(kprobes[j].address, kaddress) == 0);
+			CU_TEST(strcmp(kprobes[j].format, kformat) == 0);
+			if (kprobes[j].prefix)
+				CU_TEST(strcmp(kprobes[j].prefix, kprefix) == 0);
+			ret = tracefs_event_enable(test_instance, ksystem, kevent);
+			if (in_system) {
+				CU_TEST(ret == 0);
+			} else {
+				CU_TEST(ret != 0);
+			}
+			ret = tracefs_event_disable(test_instance, ksystem, kevent);
+			if (in_system) {
+				CU_TEST(ret == 0);
+			} else {
+				CU_TEST(ret != 0);
+			}
+
+			found++;
+			break;
+		}
+		free(ksystem);
+		free(kevent);
+		free(kprefix);
+		free(kaddress);
+		free(kformat);
+	}
+
+	CU_TEST(found == count);
+	if (found != count)
+		return false;
+
+	return true;
+}
+
+#define KPROBE_COUNT	2
+#define KRETPROBE_COUNT	2
+static void test_kprobes(void)
+{
+	char *tmp;
+
+	struct kprobe_test ktests[KPROBE_COUNT] = {
+		{ TRACEFS_KPROBE, "p:", NULL, "mkdir", "do_mkdirat", "path=+u0($arg2):ustring" },
+		{ TRACEFS_KPROBE, "p:", "ptest", "open", "do_sys_openat2",
+				  "file=+u0($arg2):ustring flags=+0($arg3):x64" },
+	};
+	struct kprobe_test kretests[KRETPROBE_COUNT] = {
+		{ TRACEFS_KRETPROBE, NULL, NULL, "retopen", "do_sys_openat2", "ret=$retval" },
+		{ TRACEFS_KRETPROBE, NULL, NULL, NULL, "do_sys_open", "ret=$retval" },
+	};
+	struct tracefs_dynevent *dkretprobe[KRETPROBE_COUNT + 1];
+	struct tracefs_dynevent *dkprobe[KPROBE_COUNT + 1];
+	struct tracefs_dynevent **devents;
+	int ret;
+	int i;
+
+	/* Invalid parameters */
+	CU_TEST(tracefs_kprobe_alloc("test", NULL, NULL, "test") == NULL);
+	CU_TEST(tracefs_kretprobe_alloc("test", NULL, NULL, "test", 0) == NULL);
+	CU_TEST(tracefs_kprobe_alloc("test", NULL, "test", NULL) == NULL);
+	CU_TEST(tracefs_kretprobe_alloc("test", NULL, "test", NULL, 0) == NULL);
+	CU_TEST(tracefs_kprobe_create(NULL) != 0);
+	CU_TEST(tracefs_kprobe_info(NULL, &tmp, &tmp, &tmp, &tmp, &tmp) == TRACEFS_ALL_KPROBES);
+	CU_TEST(tracefs_kprobe_raw("test", "test", NULL, "test") != 0);
+	CU_TEST(tracefs_kretprobe_raw("test", "test", NULL, "test") != 0);
+	CU_TEST(tracefs_kprobe_raw("test", "test", "test", NULL) != 0);
+	CU_TEST(tracefs_kretprobe_raw("test", "test", "test", NULL) != 0);
+
+	/* kprobes APIs */
+	ret = tracefs_kprobe_destroy(NULL, true);
+	CU_TEST(ret == 0);
+	ret = tracefs_kprobes_get(TRACEFS_ALL_KPROBES, &devents);
+	CU_TEST(ret == 0);
+	CU_TEST(devents == NULL);
+
+	for (i = 0; i < KPROBE_COUNT; i++) {
+		dkprobe[i] = tracefs_kprobe_alloc(ktests[i].system, ktests[i].event,
+						  ktests[i].address, ktests[i].format);
+		CU_TEST(dkprobe[i] != NULL);
+	}
+	dkprobe[i] = NULL;
+	ret = tracefs_kprobes_get(TRACEFS_ALL_KPROBES, &devents);
+	CU_TEST(ret == 0);
+	CU_TEST(devents == NULL);
+	CU_TEST(check_kprobes(ktests, KPROBE_COUNT, dkprobe, false));
+
+	for (i = 0; i < KRETPROBE_COUNT; i++) {
+		dkretprobe[i] = tracefs_kretprobe_alloc(kretests[i].system, kretests[i].event,
+							kretests[i].address, kretests[i].format, 0);
+		CU_TEST(dkretprobe[i] != NULL);
+	}
+	dkretprobe[i] = NULL;
+	ret = tracefs_kprobes_get(TRACEFS_ALL_KPROBES, &devents);
+	CU_TEST(ret == 0);
+	CU_TEST(devents == NULL);
+	CU_TEST(check_kprobes(kretests, KRETPROBE_COUNT, dkretprobe, false));
+
+	for (i = 0; i < KPROBE_COUNT; i++) {
+		CU_TEST(tracefs_kprobe_create(dkprobe[i]) == 0);
+	}
+	ret = tracefs_kprobes_get(TRACEFS_ALL_KPROBES, &devents);
+	CU_TEST(ret == KPROBE_COUNT);
+	CU_TEST(devents != NULL);
+	CU_TEST(check_kprobes(ktests, KPROBE_COUNT, devents, true));
+	CU_TEST(check_kprobes(kretests, KRETPROBE_COUNT, dkretprobe, false));
+	tracefs_dynevent_list_free(&devents);
+	devents = NULL;
+
+	for (i = 0; i < KRETPROBE_COUNT; i++) {
+		CU_TEST(tracefs_kprobe_create(dkretprobe[i]) == 0);
+	}
+	ret = tracefs_kprobes_get(TRACEFS_ALL_KPROBES, &devents);
+	CU_TEST(ret == (KPROBE_COUNT + KRETPROBE_COUNT));
+	CU_TEST(devents != NULL);
+	CU_TEST(check_kprobes(ktests, KPROBE_COUNT, devents, true));
+	CU_TEST(check_kprobes(kretests, KRETPROBE_COUNT, devents, true));
+	tracefs_dynevent_list_free(&devents);
+	devents = NULL;
+
+	for (i = 0; i < KRETPROBE_COUNT; i++) {
+		CU_TEST(tracefs_kprobe_destroy(dkretprobe[i], false) == 0);
+	}
+	ret = tracefs_kprobes_get(TRACEFS_ALL_KPROBES, &devents);
+	CU_TEST(ret == KPROBE_COUNT);
+	CU_TEST(devents != NULL);
+	CU_TEST(check_kprobes(ktests, KPROBE_COUNT, devents, true));
+	CU_TEST(check_kprobes(kretests, KRETPROBE_COUNT, dkretprobe, false));
+	tracefs_dynevent_list_free(&devents);
+	devents = NULL;
+
+	for (i = 0; i < KPROBE_COUNT; i++) {
+		CU_TEST(tracefs_kprobe_destroy(dkprobe[i], false) == 0);
+	}
+	ret = tracefs_kprobes_get(TRACEFS_ALL_KPROBES, &devents);
+	CU_TEST(ret == 0);
+	CU_TEST(devents == NULL);
+	CU_TEST(check_kprobes(ktests, KPROBE_COUNT, dkprobe, false));
+	CU_TEST(check_kprobes(kretests, KRETPROBE_COUNT, dkretprobe, false));
+	tracefs_dynevent_list_free(&devents);
+	devents = NULL;
+
+	for (i = 0; i < KPROBE_COUNT; i++)
+		tracefs_kprobe_free(dkprobe[i]);
+	for (i = 0; i < KRETPROBE_COUNT; i++)
+		tracefs_kprobe_free(dkretprobe[i]);
+
+	/* kprobes raw APIs */
+	ret = tracefs_kprobe_destroy(NULL, true);
+	CU_TEST(ret == 0);
+	ret = tracefs_kprobes_get(TRACEFS_ALL_KPROBES, &devents);
+	CU_TEST(ret == 0);
+	CU_TEST(devents == NULL);
+
+	for (i = 0; i < KPROBE_COUNT; i++) {
+		ret = tracefs_kprobe_raw(ktests[i].system, ktests[i].event,
+					 ktests[i].address, ktests[i].format);
+		CU_TEST(ret == 0);
+	}
+
+	ret = tracefs_kprobes_get(TRACEFS_KPROBE, &devents);
+	CU_TEST(ret == KPROBE_COUNT);
+	CU_TEST(devents != NULL);
+	CU_TEST(check_kprobes(ktests, KPROBE_COUNT, devents, true));
+	tracefs_dynevent_list_free(&devents);
+	devents = NULL;
+
+	for (i = 0; i < KRETPROBE_COUNT; i++) {
+		ret = tracefs_kretprobe_raw(kretests[i].system, kretests[i].event,
+					    kretests[i].address, kretests[i].format);
+		CU_TEST(ret == 0);
+	}
+
+	ret = tracefs_kprobes_get(TRACEFS_KPROBE, &devents);
+	CU_TEST(ret == KPROBE_COUNT);
+	CU_TEST(devents != NULL);
+	CU_TEST(check_kprobes(ktests, KPROBE_COUNT, devents, true));
+	tracefs_dynevent_list_free(&devents);
+	devents = NULL;
+
+	ret = tracefs_kprobes_get(TRACEFS_KRETPROBE, &devents);
+	CU_TEST(ret == KRETPROBE_COUNT);
+	CU_TEST(devents != NULL);
+	CU_TEST(check_kprobes(kretests, KRETPROBE_COUNT, devents, true));
+	tracefs_dynevent_list_free(&devents);
+	devents = NULL;
+
+	ret = tracefs_kprobes_get(TRACEFS_ALL_KPROBES, &devents);
+	CU_TEST(ret == (KPROBE_COUNT + KRETPROBE_COUNT));
+	CU_TEST(devents != NULL);
+	CU_TEST(check_kprobes(ktests, KPROBE_COUNT, devents, true));
+	CU_TEST(check_kprobes(kretests, KRETPROBE_COUNT, devents, true));
+	tracefs_dynevent_list_free(&devents);
+	devents = NULL;
+
+	ret = tracefs_kprobe_destroy(NULL, true);
+	CU_TEST(ret == 0);
+	ret = tracefs_kprobes_get(TRACEFS_ALL_KPROBES, &devents);
+	CU_TEST(ret == 0);
+	CU_TEST(devents == NULL);
+}
+
 static void test_instance_file(void)
 {
 	struct tracefs_instance *instance = NULL;
 	struct tracefs_instance *second = NULL;
-	enum tracefs_kprobe_type type;
 	const char *name = get_rand_str();
 	const char *inst_name = NULL;
 	const char *tdir;
 	char *inst_file;
 	char *inst_dir;
 	struct stat st;
-	char **kprobes;
-	char *kformat;
-	char *ktype;
-	char *kaddr;
-	char *fname;
 	char *file1;
 	char *file2;
 	char *tracer;
+	char *fname;
 	int size;
 	int ret;
-	int i;
 
 	tdir  = tracefs_tracing_dir();
 	CU_TEST(tdir != NULL);
@@ -541,153 +761,6 @@ static void test_instance_file(void)
 	free(file1);
 	free(file2);
 
-	ret = tracefs_kprobe_clear_all(true);
-	CU_TEST(ret == 0);
-	ret = tracefs_kprobe_raw(NULL, KPROBE_1_NAME, KPROBE_1_ADDR, KPROBE_1_FMT);
-	CU_TEST(ret == 0);
-	ret = tracefs_kprobe_raw(KPROBE_2_GROUP, KPROBE_2_NAME, KPROBE_2_ADDR,
-				 KPROBE_2_FMT);
-	CU_TEST(ret == 0);
-
-	ret = tracefs_kretprobe_raw(KPROBE_2_GROUP, KRETPROBE_NAME, KRETPROBE_ADDR,
-				 KRETPROBE_FMT);
-	CU_TEST(ret == 0);
-
-	type = tracefs_kprobe_info(KPROBE_1_GROUP, KPROBE_1_NAME, &ktype,
-				   &kaddr, &kformat);
-	CU_TEST(type == TRACEFS_KPROBE);
-	CU_TEST(ktype && *ktype == 'p');
-	CU_TEST(kaddr && !strcmp(kaddr, KPROBE_1_ADDR));
-	CU_TEST(kformat && !strcmp(kformat, KPROBE_1_FMT));
-	free(ktype);
-	free(kaddr);
-	free(kformat);
-
-	type = tracefs_kprobe_info(KPROBE_2_GROUP, KPROBE_2_NAME, &ktype,
-				   &kaddr, &kformat);
-	CU_TEST(type == TRACEFS_KPROBE);
-	CU_TEST(ktype && *ktype == 'p');
-	CU_TEST(kaddr && !strcmp(kaddr, KPROBE_2_ADDR));
-	CU_TEST(kformat && !strcmp(kformat, KPROBE_2_FMT));
-	free(ktype);
-	free(kaddr);
-	free(kformat);
-
-	type = tracefs_kprobe_info(KPROBE_2_GROUP, KRETPROBE_NAME, &ktype,
-				   &kaddr, &kformat);
-	CU_TEST(type == TRACEFS_KRETPROBE);
-	CU_TEST(ktype && *ktype == 'r');
-	CU_TEST(kaddr && !strcmp(kaddr, KRETPROBE_ADDR));
-	CU_TEST(kformat && !strcmp(kformat, KRETPROBE_FMT));
-	free(ktype);
-	free(kaddr);
-	free(kformat);
-
-	kprobes = tracefs_kprobes_get(TRACEFS_ALL_KPROBES);
-	CU_TEST(kprobes != NULL);
-
-	for (i = 0; kprobes[i]; i++) {
-		char *system = strtok(kprobes[i], "/");
-		char *event = strtok(NULL, "");
-		bool found = false;
-		if (!strcmp(system, KPROBE_1_GROUP)) {
-			CU_TEST(!strcmp(event, KPROBE_1_NAME));
-			found = true;
-		} else if (!strcmp(system, KPROBE_2_GROUP)) {
-			switch (tracefs_kprobe_info(system, event, NULL, NULL, NULL)) {
-			case TRACEFS_KPROBE:
-				CU_TEST(!strcmp(event, KPROBE_2_NAME));
-				found = true;
-				break;
-			case TRACEFS_KRETPROBE:
-				CU_TEST(!strcmp(event, KRETPROBE_NAME));
-				found = true;
-				break;
-			default:
-				break;
-			}
-		}
-		CU_TEST(found);
-	}
-	tracefs_list_free(kprobes);
-	CU_TEST(i == 3);
-
-	kprobes = tracefs_kprobes_get(TRACEFS_KPROBE);
-	CU_TEST(kprobes != NULL);
-
-	for (i = 0; kprobes[i]; i++) {
-		char *system = strtok(kprobes[i], "/");
-		char *event = strtok(NULL, "");
-		bool found = false;
-		if (!strcmp(system, KPROBE_1_GROUP)) {
-			CU_TEST(!strcmp(event, KPROBE_1_NAME));
-			found = true;
-		} else if (!strcmp(system, KPROBE_2_GROUP)) {
-			CU_TEST(!strcmp(event, KPROBE_2_NAME));
-			found = true;
-		}
-		CU_TEST(found);
-	}
-	tracefs_list_free(kprobes);
-	CU_TEST(i == 2);
-
-	kprobes = tracefs_kprobes_get(TRACEFS_KRETPROBE);
-	CU_TEST(kprobes != NULL);
-
-	for (i = 0; kprobes[i]; i++) {
-		char *system = strtok(kprobes[i], "/");
-		char *event = strtok(NULL, "");
-		bool found = false;
-		if (!strcmp(system, KPROBE_2_GROUP)) {
-			CU_TEST(!strcmp(event, KRETPROBE_NAME));
-			found = true;
-		}
-		CU_TEST(found);
-	}
-	tracefs_list_free(kprobes);
-	CU_TEST(i == 1);
-
-	ret = tracefs_event_enable(instance, KPROBE_1_GROUP, KPROBE_1_NAME);
-	CU_TEST(ret == 0);
-	ret = tracefs_event_enable(instance, KPROBE_2_GROUP, KPROBE_2_NAME);
-	CU_TEST(ret == 0);
-	ret = tracefs_event_enable(instance, KPROBE_2_GROUP, KRETPROBE_NAME);
-	CU_TEST(ret == 0);
-
-	ret = tracefs_kprobe_clear_all(false);
-	CU_TEST(ret < 0);
-
-	ret = tracefs_kprobe_clear_probe(KPROBE_2_GROUP, NULL, false);
-	CU_TEST(ret < 0);
-
-	ret = tracefs_kprobe_clear_probe(KPROBE_2_GROUP, NULL, true);
-	CU_TEST(ret == 0);
-
-	kprobes = tracefs_kprobes_get(TRACEFS_ALL_KPROBES);
-	CU_TEST(kprobes != NULL);
-
-	for (i = 0; kprobes[i]; i++) {
-		char *system = strtok(kprobes[i], "/");
-		char *event = strtok(NULL, "");
-		bool found = false;
-		if (!strcmp(system, KPROBE_1_GROUP)) {
-			CU_TEST(!strcmp(event, KPROBE_1_NAME));
-			found = true;
-		}
-		CU_TEST(found);
-	}
-	tracefs_list_free(kprobes);
-	CU_TEST(i == 1);
-
-	ret = tracefs_kprobe_clear_all(true);
-	CU_TEST(ret == 0);
-
-	kprobes = tracefs_kprobes_get(TRACEFS_ALL_KPROBES);
-	CU_TEST(kprobes != NULL);
-
-	CU_TEST(kprobes[0] == NULL);
-	tracefs_list_free(kprobes);
-
 	tracefs_put_tracing_file(inst_file);
 	free(fname);
 
@@ -1442,4 +1515,5 @@ void test_tracefs_lib(void)
 		    test_custom_trace_dir);
 	CU_add_test(suite, "ftrace marker",
 		    test_ftrace_marker);
+	CU_add_test(suite, "kprobes", test_kprobes);
 }
Signature · 1 line
2.31.1
#14 lore raw

PATCH 9/12 libtracefs: Update kprobes man pages

In reply to Tzvetomir Stoyanov (VMware)

As there are a lot of changes in the libtracefs kprobes APIs, the documentation of library should be updated.

---
 Documentation/libtracefs-kprobes.txt | 135 ++++++++++++++++-----------
 1 file changed, 79 insertions(+), 56 deletions(-)
Documentation/libtracefs-kprobes.txt +79 -56
index ca48fa9..c11cd5c 100644
@@ -3,7 +3,10 @@ libtracefs(3)
 
 NAME
 ----
-tracefs_kprobe_raw, tracefs_kretprobe_raw, tracefs_kprobes_get, tracefs_kprobe_info, tracefs_kprobe_clear_all, tracefs_kprobe_clear_probe - Create, list, and destroy kprobes
+tracefs_kprobe_alloc, tracefs_kretprobe_alloc, tracefs_kprobe_create,
+tracefs_kprobe_destroy, tracefs_kprobe_free, tracefs_kprobes_get,
+tracefs_kprobe_info, tracefs_kprobe_raw, tracefs_kretprobe_raw - Create, get, and destroy kprobes
+
 
 SYNOPSIS
 --------
@@ -11,18 +14,67 @@ SYNOPSIS
 --
 *#include <tracefs.h>*
 
-int tracefs_kprobe_raw(const char pass:[*]system, const char pass:[*]event, const char pass:[*]addr, const char pass:[*]format);
-int tracefs_kretprobe_raw(const char pass:[*]system, const char pass:[*]event, const char pass:[*]addr, const char pass:[*]format);
-char pass:[*]pass:[*]tracefs_kprobes_get(enum tracefs_kprobe_type type);
-enum tracefs_kprobe_type tracefs_kprobe_info(const char pass:[*]group, const char pass:[*]event,
-					     char pass:[*]pass:[*]type, char pass:[*]pass:[*]addr, char pass:[*]pass:[*]format);
-enum tracefs_kprobe_type tracefs_kprobe_type(const char pass:[*]group, const char pass:[*]event)
-int tracefs_kprobe_clear_all(bool force);
-int tracefs_kprobe_clear_probe(const char pass:[*]system, const char pass:[*]event, bool force);
+struct tracefs_dynevent pass:[*]
+*tracefs_kprobe_alloc*(const char pass:[*]_system_, const char pass:[*]_event_,
+		       const char pass:[*]_addr_, const char pass:[*]_format_);
+struct tracefs_dynevent pass:[*]
+*tracefs_kretprobe_alloc*(const char pass:[*]_system_, const char pass:[*]_event_,
+			  const char pass:[*]_addr_, const char pass:[*]_format_, int _max_);
+void *tracefs_kprobe_free*(struct tracefs_dynevent pass:[*]_kprobe_);
+int *tracefs_kprobe_create*(struct tracefs_dynevent pass:[*]_kprobe_);
+int *tracefs_kprobe_destroy*(struct tracefs_dynevent pass:[*]_kprobe_, bool _force_);
+int *tracefs_kprobes_get*(enum tracefs_kprobe_type _type_,
+			  struct tracefs_dynevent pass:[*]pass:[*]pass:[*]_kprobes_);
+enum tracefs_kprobe_type *tracefs_kprobe_info*(struct tracefs_dynevent pass:[*]_kprobe_,
+					       char pass:[*]pass:[*]_system_, char pass:[*]pass:[*]_event_,
+					       char pass:[*]pass:[*]_prefix_, char pass:[*]pass:[*]_addr_,
+					       char pass:[*]pass:[*]_format_);
+int tracefs_kprobe_raw(const char pass:[*]system, const char pass:[*]event,
+		       const char pass:[*]addr, const char pass:[*]format);
+int tracefs_kretprobe_raw(const char pass:[*]system, const char pass:[*]event,
+			  const char pass:[*]addr, const char pass:[*]format);
 --
 
 DESCRIPTION
 -----------
+*tracefs_kprobe_alloc*() allocates a new kprobe context. The kbrobe is not configured in the system.
+The new kprobe 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 format of the kprobe. See the Linux
+documentation file under: Documentation/trace/kprobetrace.rst
+
+*tracefs_kretprobe_alloc*() is the same as *tracefs_kprobe_alloc*, but allocates context for
+kretprobe. It has one additional parameter, which is optional, _max_ - maxactive count.
+See description of kretprobes in the Documentation/trace/kprobetrace.rst file.
+
+*tracefs_kprobe_free*() frees a kprobe context, allocated with *tracefs_kprobe_alloc*() or
+*tracefs_kretprobe_alloc*(). The kprobe is not removed from the system.
+
+*tracefs_kprobe_create*() creates a kprobe or kretprobe in the system. The probe is described by the
+given _kprobe_ context.
+
+*tracefs_kprobe_destroy*() will try to remove the specified _kprobe_ from the system. If _kprobe_
+is NULL, all kprobes and kretprobes in the system will be removed. If the _force_ flag is set, then
+it will disable the given kprobe events before removing them.
+
+*tracefs_kprobes_get*() returns the count of the registered kprobes and kretprobes in the system,
+depending on the given _type_. If _type_ is TRACEFS_ALL_KPROBES, then all kprobes found are
+counted. If _type_ is TRACEFS_KPROBE, then only normal kprobes are counted. If _type_ is
+TRACEFS_KRETPROBE, then only kretprobes are counted. If the _kprobes_ parameter in not NULL, then
+an array of pointers to kprobe contexts is allocated and passed back via _kprobes_. The size of this
+array is the returned count + 1, as the last element is a NULL pointer. The array must be freed with
+*tracefs_dynevent_list_free*(3).
+
+*tracefs_kprobe_info*() returns the type and information of a given _kprobe_. If any of the
+_system_, _event_, _prefix_, _addr_ or _format_ arguments are not NULL, then strings are allocated
+and returned back via these arguments. The _system_ and _event_ holds the system and the name of the
+kprobe. If _prefix_ is non NULL, then it will hold an allocated string that holds the prefix portion
+of the kprobe in the kprobe_events file (the content up to the ":", including it). Note that for
+kretprobes, the max active count is encoded in the prefix srting. If _addr_ is non NULL, it will
+hold the address or function that the kprobe is attached to. If _format_ is non NULL, it will hold
+the format string of the kprobe. Note, that the content in _group_, _event_, _prefix_, _addr_, and
+_format_ must be freed with free(3) if they are set.
+
 *tracefs_kprobe_raw*() will create a kprobe event. If _system_ is NULL, then
 the default "kprobes" is used for the group (event system). Otherwise if _system_
 is specified then the kprobe will be created under the group by that name. The
@@ -36,55 +88,25 @@ document.
 creates a kretprobe instead of a kprobe. The difference is also described
 in the Linux kernel source in the Documentation/trace/kprobetrace.rst file.
 
-*tracefs_kprobes_get*() returns an array of strings (char pass:[*]) that contain
-the registered kprobes and kretprobes depending on the given _type_. If _type_ is
-TRACEFS_ALL_KPROBES, then all kprobes found are returned. If _type_ is
-TRACEFS_KPROBE, then only normal kprobes are returned. If _type_ is
-TRACEFS_KRETPROBE, then only kretprobes are returned.
-The names are in the "system/event" format.
-That is, one string holds both the kprobe's name as well as the group it is
-defined under. These strings are allocated and may be modified with the
-*strtok*(3) and *strtok_r*(3) functions. The string returned must be freed with
-*tracefs_list_free*(3).
-
-*tracefs_kprobe_info*() returns the type of the given kprobe. If _group_ is
-NULL, then the default "kprobes" is used. If _type_ is non NULL, then it will
-hold an allocated string that holds the type portion of the kprobe in the
-kprobe_events file (the content before the ":"). If _addr_ is non NULL, it will
-hold the address or function that the kprobe is attached to. If _format_ is non
-NULL, it will hold the format string of the kprobe. Note, that the content in
-_type_, _addr_, and _format_ must be freed with free(3) if they are set. Even
-in the case of an error, as they may hold information of what caused the error.
-
-*tracefs_kprobe_clear_all*() will try to remove all kprobes that have been
-registered. If the @force flag is set, it will then disable those kprobe events
-if they are enabled and then try to clear the kprobes.
-
-*tracefs_kprobe_clear_probe*() will try to clear specified kprobes. If _system_
-is NULL, then it will only clear the default kprobes under the "kprobes" group.
-If _event_ is NULL, it will clear all kprobes under the given _system_. If the
-_force_ flag is set, then it will disable the given kprobe events before clearing
-them.
-
 RETURN VALUE
 ------------
 
-*tracefs_kprobe_raw*(), *tracefs_kretprobe_raw*(), *tracefs_kprobe_clear_all*(),
-and *tracefs_kprobe_clear_probe*() return 0 on success, or -1 on error.
+*tracefs_kprobe_raw*(), *tracefs_kretprobe_raw*(), *tracefs_kprobe_create*(),
+and *tracefs_kprobe_destroy*() return 0 on success, or -1 on error.
 
-If a parsing error occurs on *tracefs_kprobe_raw*() or *tracefs_kretprobe_raw*()
-then *tracefs_error_last*(3) may be used to retrieve the error message explaining
-the parsing issue.
+If a parsing error occurs on *tracefs_kprobe_raw*(), *tracefs_kretprobe_raw*() or
+*tracefs_kprobe_create*() then *tracefs_error_last*(3) may be used to retrieve the error message
+explaining the parsing issue.
 
-*tracefs_kprobes_get*() returns an allocate string list of allocated strings
-on success that must be freed with *tracefs_list_free*(3) and returns
-NULL on error.
+*tracefs_kprobes_get*() returns a count of the requested krpobe types, or -1 in case of an error.
+If the _kprobes_ parameter in not NULL, then an array of pointers to kprobe contexts is allocated
+and passed back via _kprobes_. The size of this array is the returned count + 1, as the last element
+is a NULL pointer. The array must be freed with *tracefs_dynevent_list_free*(3).
 
-*tracefs_kprobe_info*() returns the type of the given kprobe. It returns
-TRACEFS_KPROBE for normal kprobes, TRACEFS_KRETPROBE for kretprobes, and
-on error, or if the kprobe is not found TRACEFS_ALL_KPROBES is returned.
-If _type_, _addr_, or _format_ are non NULL, they will contain allocated
-strings that must be freed by free(3) even in the case of error.
+*tracefs_kprobe_info*() returns the type of the given kprobe. It returns TRACEFS_KPROBE for normal
+kprobes, TRACEFS_KRETPROBE for kretprobes or TRACEFS_ALL_KPROBES on error. If _system_, _event_,
+_prefix_, _addr_, or _format_ are non NULL, they will contain allocated strings that must be freed
+by free(3).
 
 ERRORS
 ------
@@ -96,7 +118,8 @@ The following errors are for all the above calls:
 
 *ENOMEM* Memory allocation error.
 
-*tracefs_kprobe_raw*(), *tracefs_kretprobe_raw*() can fail with the following errors:
+*tracefs_kprobe_raw*(), *tracefs_kretprobe_raw*(), *tracefs_kprobe_alloc*(),
+*tracefs_kretprobe_alloc*() and *tracefs_kprobe_create*() can fail with the following errors:
 
 *EBADMSG* Either _addr_ or _format_ are NULL.
 
@@ -217,7 +240,7 @@ int main (int argc, char **argv, char **env)
 		exit(-1);
 	}
 
-	tracefs_kprobe_clear_probe(mykprobe, NULL, true);
+	tracefs_kprobe_destroy(NULL, true);
 
 	kprobe_create("open", "do_sys_openat2",
 		      "file=+0($arg2):ustring flags=+0($arg3):x64 mode=+8($arg3):x64\n");
@@ -247,7 +270,7 @@ int main (int argc, char **argv, char **env)
 	} while (waitpid(pid, NULL, WNOHANG) != pid);
 
 	/* Will disable the events */
-	tracefs_kprobe_clear_probe(mykprobe, NULL, true);
+	tracefs_kprobe_destroy(NULL, true);
 	tracefs_instance_destroy(instance);
 	tep_free(tep);
 
@@ -293,5 +316,5 @@ https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/
 
 COPYING
 -------
-Copyright \(C) 2020 VMware, Inc. Free use of this software is granted under
+Copyright \(C) 2021 VMware, Inc. Free use of this software is granted under
 the terms of the GNU Public License (GPL).
Signature · 1 line
2.31.1
#15 lore raw

PATCH 10/12 libtracefs: Rename tracefs_synth_init API

In reply to Tzvetomir Stoyanov (VMware)

In order to be consistent with the others library APIs, the tracefs_synth_init() is renamed to tracefs_synth_alloc().

---
 Documentation/libtracefs-synth.txt  | 20 ++++++++++----------
 Documentation/libtracefs-synth2.txt | 10 +++++-----
 include/tracefs.h                   | 18 +++++++++---------
 src/tracefs-hist.c                  | 22 +++++++++++-----------
 src/tracefs-sqlhist.c               |  6 +++---
 5 files changed, 38 insertions(+), 38 deletions(-)
Documentation/libtracefs-synth.txt +10 -10
index 77076e1..2fd9019 100644
@@ -3,7 +3,7 @@ libtracefs(3)
 
 NAME
 ----
-tracefs_synth_init, tracefs_synth_add_match_field, tracefs_synth_add_compare_field, tracefs_synth_add_start_field,
+tracefs_synth_alloc, tracefs_synth_add_match_field, tracefs_synth_add_compare_field, tracefs_synth_add_start_field,
 tracefs_synth_add_end_field, tracefs_synth_append_start_filter, tracefs_synth_append_end_filter, tracefs_synth_free,
 - Creation of a synthetic event descriptor
 
@@ -13,7 +13,7 @@ SYNOPSIS
 --
 *#include <tracefs.h>*
 
-struct tracefs_synth pass:[*]tracefs_synth_init(struct tep_handle pass:[*]tep,
+struct tracefs_synth pass:[*]tracefs_synth_alloc(struct tep_handle pass:[*]tep,
 					 const char pass:[*]name,
 					 const char pass:[*]start_system,
 					 const char pass:[*]start_event,
@@ -69,7 +69,7 @@ as a field for both events to calculate the delta in nanoseconds, or use
 *TRACEFS_TIMESTAMP_USECS" as the compare fields for both events to calculate the
 delta in microseconds. This is used as the example below.
 
-*tracefs_synth_init*() allocates and initializes a synthetic event.
+*tracefs_synth_alloc*() allocates and initializes a synthetic event.
 It does not create the synthetic event, but supplies the minimal information
 to do so. See *tracefs_synth_create*(3) for how to create the synthetic
 event in the system. It requires a _tep_ handler that can be created by
@@ -156,11 +156,11 @@ _field_, _compare_, and _val_ are ignored unless _type_ is equal to
 filters on the ending event.
 
 *tracefs_synth_free*() frees the allocated descriptor returned by
-*tracefs_synth_init*().
+*tracefs_synth_alloc*().
 
 RETURN VALUE
 ------------
-*tracefs_synth_init*() returns an allocated struct tracefs_synth descriptor
+*tracefs_synth_alloc*() returns an allocated struct tracefs_synth descriptor
 on sucess or NULL on error.
 
 All other functions that return an integer returns zero on success or -1
@@ -209,11 +209,11 @@ static void make_event(void)
 	tep = tracefs_local_events(NULL);
 
 	/* Initialize the synthetic event */
-	synth = tracefs_synth_init(tep, "wakeup_lat",
-				   NULL, start_event,
-				   NULL, end_event,
-				   start_field, end_field,
-				   match_name);
+	synth = tracefs_synth_alloc(tep, "wakeup_lat",
+				    NULL, start_event,
+				    NULL, end_event,
+				    start_field, end_field,
+				    match_name);
 
 	/* The tep is no longer needed */
 	tep_free(tep);
Documentation/libtracefs-synth2.txt +5 -5
index 4c44253..f734b44 100644
@@ -142,11 +142,11 @@ static void make_event(void)
 	tep = tracefs_local_events(NULL);
 
 	/* Initialize the synthetic event */
-	synth = tracefs_synth_init(tep, "wakeup_lat",
-				   NULL, start_event,
-				   NULL, end_event,
-				   start_field, end_field,
-				   match_name);
+	synth = tracefs_synth_alloc(tep, "wakeup_lat",
+				    NULL, start_event,
+				    NULL, end_event,
+				    start_field, end_field,
+				    match_name);
 
 	/* The tep is no longer needed */
 	tep_free(tep);
include/tracefs.h +9 -9
index c65f21f..418725e 100644
@@ -472,15 +472,15 @@ enum tracefs_synth_handler {
 	TRACEFS_SYNTH_HANDLE_CHANGE,
 };
 
-struct tracefs_synth *tracefs_synth_init(struct tep_handle *tep,
-					 const char *name,
-					 const char *start_system,
-					 const char *start_event,
-					 const char *end_system,
-					 const char *end_event,
-					 const char *start_match_field,
-					 const char *end_match_field,
-					 const char *match_name);
+struct tracefs_synth *tracefs_synth_alloc(struct tep_handle *tep,
+					  const char *name,
+					  const char *start_system,
+					  const char *start_event,
+					  const char *end_system,
+					  const char *end_event,
+					  const char *start_match_field,
+					  const char *end_match_field,
+					  const char *match_name);
 int tracefs_synth_add_match_field(struct tracefs_synth *synth,
 				  const char *start_match_field,
 				  const char *end_match_field,
src/tracefs-hist.c +11 -11
index 27bab00..9009dba 100644
@@ -692,7 +692,7 @@ static void action_free(struct action *action)
  * @synth: The tracefs_synth descriptor
  *
  * Frees the resources allocated for a @synth created with
- * tracefs_synth_init(). It does not touch the system. That is,
+ * tracefs_synth_alloc(). It does not touch the system. That is,
  * any synthetic event created, will not be destroyed by this
  * function.
  */
@@ -890,7 +890,7 @@ synth_init_from(struct tep_handle *tep, const char *start_system,
 }
 
 /**
- * tracefs_synth_init - create a new tracefs_synth instance
+ * tracefs_synth_alloc - create a new tracefs_synth instance
  * @tep: The tep handle that holds the events to work on
  * @name: The name of the synthetic event being created
  * @start_system: The name of the system of the start event (can be NULL)
@@ -933,15 +933,15 @@ synth_init_from(struct tep_handle *tep, const char *start_system,
  * event on the system is not created. That needs to be done with
  * tracefs_synth_create().
  */
-struct tracefs_synth *tracefs_synth_init(struct tep_handle *tep,
-					 const char *name,
-					 const char *start_system,
-					 const char *start_event_name,
-					 const char *end_system,
-					 const char *end_event_name,
-					 const char *start_match_field,
-					 const char *end_match_field,
-					 const char *match_name)
+struct tracefs_synth *tracefs_synth_alloc(struct tep_handle *tep,
+					  const char *name,
+					  const char *start_system,
+					  const char *start_event_name,
+					  const char *end_system,
+					  const char *end_event_name,
+					  const char *start_match_field,
+					  const char *end_match_field,
+					  const char *match_name)
 {
 	struct tep_event *end_event;
 	struct tracefs_synth *synth;
src/tracefs-sqlhist.c +3 -3
index d77ce86..016f3eb 100644
@@ -1419,9 +1419,9 @@ static struct tracefs_synth *build_synth(struct tep_handle *tep,
 	assign_match(start_system, start_event, match,
 		     &start_match, &end_match);
 
-	synth = tracefs_synth_init(tep, name, start_system,
-				   start_event, end_system, end_event,
-				   start_match, end_match, NULL);
+	synth = tracefs_synth_alloc(tep, name, start_system,
+				    start_event, end_system, end_event,
+				    start_match, end_match, NULL);
 	if (!synth)
 		return synth_init_error(tep, table);
 
Signature · 1 line
2.31.1
#16 lore raw

PATCH 11/12 libtracefs: Use the internal dynamic events API when creating synthetic events

In reply to Tzvetomir Stoyanov (VMware)

Synthetic events are type of ftrace dynamic events. The tracefs library has an internal helper APIs to manage dynamic events of all types. In order the code to be consistent, the creation of synthetic events inside the library is reimplemented with these new dynamic events APIs.

---
 src/tracefs-hist.c | 88 +++++++++++++++++-----------------------------
 1 file changed, 33 insertions(+), 55 deletions(-)
src/tracefs-hist.c +33 -55
index 9009dba..7ac0023 100644
@@ -661,6 +661,7 @@ struct tracefs_synth {
 	struct tep_event	*end_event;
 	struct action		*actions;
 	struct action		**next_action;
+	struct tracefs_dynevent	*dyn_event;
 	char			*name;
 	char			**synthetic_fields;
 	char			**synthetic_args;
@@ -719,6 +720,7 @@ void tracefs_synth_free(struct tracefs_synth *synth)
 		synth->actions = action->next;
 		action_free(action);
 	}
+	dynevent_free(synth->dyn_event);
 
 	free(synth);
 }
@@ -889,6 +891,28 @@ synth_init_from(struct tep_handle *tep, const char *start_system,
 	return synth;
 }
 
+static int alloc_synthetic_event(struct tracefs_synth *synth)
+{
+	char *synthetic_format;
+	const char *field;
+	int i;
+
+	synthetic_format = strdup("");
+	if (!synthetic_format)
+		return -1;
+
+	for (i = 0; synth->synthetic_fields && synth->synthetic_fields[i]; i++) {
+		field = synth->synthetic_fields[i];
+		synthetic_format = append_string(synthetic_format, " ", field);
+	}
+
+	synth->dyn_event = dynevent_alloc(TRACE_DYNEVENT_SYNTH, NULL,
+					  synth->name, NULL, synthetic_format);
+	free(synthetic_format);
+
+	return synth->dyn_event ? 0 : -1;
+}
+
 /**
  * tracefs_synth_alloc - create a new tracefs_synth instance
  * @tep: The tep handle that holds the events to work on
@@ -1609,38 +1633,6 @@ int tracefs_synth_save(struct tracefs_synth *synth,
 	return 0;
 }
 
-static char *create_synthetic_event(struct tracefs_synth *synth)
-{
-	char *synthetic_event;
-	const char *field;
-	int i;
-
-	synthetic_event = strdup(synth->name);
-	if (!synthetic_event)
-		return NULL;
-
-	for (i = 0; synth->synthetic_fields && synth->synthetic_fields[i]; i++) {
-		field = synth->synthetic_fields[i];
-		synthetic_event = append_string(synthetic_event, " ", field);
-	}
-
-	return synthetic_event;
-}
-
-static int remove_synthetic(const char *synthetic)
-{
-	char *str;
-	int ret;
-
-	ret = asprintf(&str, "!%s", synthetic);
-	if (ret < 0)
-		return -1;
-
-	ret = tracefs_instance_file_append(NULL, "synthetic_events", str);
-	free(str);
-	return ret < 0 ? -1 : 0;
-}
-
 static int remove_hist(struct tracefs_instance *instance,
 		       struct tep_event *event, const char *hist)
 {
@@ -1919,7 +1911,6 @@ tracefs_synth_get_start_hist(struct tracefs_synth *synth)
 int tracefs_synth_create(struct tracefs_instance *instance,
 			 struct tracefs_synth *synth)
 {
-	char *synthetic_event;
 	char *start_hist = NULL;
 	char *end_hist = NULL;
 	int ret;
@@ -1937,14 +1928,10 @@ int tracefs_synth_create(struct tracefs_instance *instance,
 	if (verify_state(synth) < 0)
 		return -1;
 
-	synthetic_event = create_synthetic_event(synth);
-	if (!synthetic_event)
+	if (!synth->dyn_event && alloc_synthetic_event(synth))
+		return -1;
+	if (!dynevent_create(synth->dyn_event))
 		return -1;
-
-	ret = tracefs_instance_file_append(NULL, "synthetic_events",
-					   synthetic_event);
-	if (ret < 0)
-		goto free_synthetic;
 
 	start_hist = create_hist(synth->start_keys, synth->start_vars);
 	start_hist = append_filter(start_hist, synth->start_filter,
@@ -1980,9 +1967,7 @@ int tracefs_synth_create(struct tracefs_instance *instance,
  remove_synthetic:
 	free(end_hist);
 	free(start_hist);
-	remove_synthetic(synthetic_event);
- free_synthetic:
-	free(synthetic_event);
+	dynevent_destroy(synth->dyn_event);
 	return -1;
 }
 
@@ -2007,7 +1992,6 @@ int tracefs_synth_create(struct tracefs_instance *instance,
 int tracefs_synth_destroy(struct tracefs_instance *instance,
 			  struct tracefs_synth *synth)
 {
-	char *synthetic_event;
 	char *hist;
 	int ret;
 
@@ -2041,11 +2025,7 @@ int tracefs_synth_destroy(struct tracefs_instance *instance,
 	ret = remove_hist(instance, synth->start_event, hist);
 	free(hist);
 
-	synthetic_event = create_synthetic_event(synth);
-	if (!synthetic_event)
-		return -1;
-
-	ret = remove_synthetic(synthetic_event);
+	ret = dynevent_destroy(synth->dyn_event);
 
 	return ret ? -1 : 0;
 }
@@ -2067,7 +2047,6 @@ int tracefs_synth_show(struct trace_seq *seq,
 		       struct tracefs_instance *instance,
 		       struct tracefs_synth *synth)
 {
-	char *synthetic_event = NULL;
 	char *hist = NULL;
 	char *path;
 	int ret = -1;
@@ -2082,16 +2061,16 @@ int tracefs_synth_show(struct trace_seq *seq,
 		return -1;
 	}
 
-	synthetic_event = create_synthetic_event(synth);
-	if (!synthetic_event)
+	if (!synth->dyn_event && alloc_synthetic_event(synth))
 		return -1;
 
 	path = trace_find_tracing_dir();
 	if (!path)
 		goto out_free;
 
-	trace_seq_printf(seq, "echo '%s' > %s/synthetic_events\n",
-			 synthetic_event, path);
+	trace_seq_printf(seq, "echo '%s%s %s' > %s/%s\n",
+			 synth->dyn_event->prefix, synth->dyn_event->event,
+			 synth->dyn_event->format, path, synth->dyn_event->trace_file);
 
 	tracefs_put_tracing_file(path);
 	path = tracefs_instance_get_dir(instance);
@@ -2118,7 +2097,6 @@ int tracefs_synth_show(struct trace_seq *seq,
 
 	ret = 0;
  out_free:
-	free(synthetic_event);
 	free(hist);
 	tracefs_put_tracing_file(path);
 	return ret;
Signature · 1 line
2.31.1
#17 lore raw

PATCH 12/12 libtracefs: Document tracefs_dynevent_list_free() API

In reply to Tzvetomir Stoyanov (VMware)

The newly added tracefs_dynevent_list_free() should be documented in the library man pages.

---
 Documentation/libtracefs-utils.txt | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Documentation/libtracefs-utils.txt +5 -1
index 41544ab..2908d41 100644
@@ -3,7 +3,7 @@ libtracefs(3)
 
 NAME
 ----
-tracefs_tracers, tracefs_get_clock, tracefs_list_free -
+tracefs_tracers, tracefs_get_clock, tracefs_list_free, tracefs_dynevent_list_free -
 Helper functions for working with trace file system.
 
 SYNOPSIS
@@ -15,6 +15,7 @@ SYNOPSIS
 char pass:[*]pass:[*]*tracefs_tracers*(const char pass:[*]_tracing_dir_);
 char pass:[*]*tracefs_get_clock*(struct tracefs_instance pass:[*]_instance_);
 void *tracefs_list_free*(char pass:[*]pass:[*]_list_);
+void *tracefs_dynevent_list_free*(struct tracefs_dynevent pass:[*]pass:[*]pass:[*]events);
 --
 
 DESCRIPTION
@@ -36,6 +37,9 @@ The _tracefs_list_free()_ function frees an array of strings, returned by
 _tracefs_event_systems()_, _tracefs_system_events()_ and _tracefs_tracers()_
 APIs.
 
+The _tracefs_dynevent_list_free()_ functions frees an array of pointers to dynamic event contexts,
+returned by_tracefs_kprobes_get()_ API.
+
 RETURN VALUE
 ------------
 The _tracefs_tracers()_ returns array of strings. The last element in that
Signature · 1 line
2.31.1