From: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
To: rostedt@goodmis.org, y.karadz@gmail.com
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH v4 02/10] libtracefs: New APIs for kprobe allocation
Date: Thu,  4 Nov 2021 13:10:39 +0200	[thread overview]
Message-ID: <20211104111047.302660-3-tz.stoyanov@gmail.com> (raw)
In-Reply-To: <20211104111047.302660-1-tz.stoyanov@gmail.com>
In order to be consistent with the other APIs, new APIs for kprobe
allocation are introduced:
 tracefs_kprobe_alloc();
 tracefs_kretprobe_alloc();
These APIs allocate new kpobe dynamic event, that can be used with
tracefs_dynevent_... set of APIs.
Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 include/tracefs.h     |   5 ++
 src/tracefs-kprobes.c | 109 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 114 insertions(+)
diff --git a/include/tracefs.h b/include/tracefs.h
index 81f22af..3b2fc50 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -263,6 +263,11 @@ 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_raw(const char *system, const char *event,
 		       const char *addr, const char *format);
 int tracefs_kretprobe_raw(const char *system, const char *event,
diff --git a/src/tracefs-kprobes.c b/src/tracefs-kprobes.c
index 6fdd8f9..b2dcb0e 100644
--- a/src/tracefs-kprobes.c
+++ b/src/tracefs-kprobes.c
@@ -20,6 +20,115 @@
 #define KPROBE_EVENTS "kprobe_events"
 #define KPROBE_DEFAULT_GROUP "kprobes"
 
+static struct tracefs_dynevent *
+kprobe_alloc(enum tracefs_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) {
+		errno = EBADMSG;
+		return NULL;
+	}
+	if (!sys)
+		sys = KPROBE_DEFAULT_GROUP;
+
+	if (!event) {
+		ename = strdup(addr);
+		if (!ename)
+			return NULL;
+		tmp = strchr(ename, ':');
+		if (tmp)
+			*tmp = '\0';
+	}
+
+	kp = dynevent_alloc(type, sys, ename, addr, format);
+	if (!event)
+		free((char *)ename);
+
+	return kp;
+}
+
+/**
+ * tracefs_kprobe_alloc - Allocate new kprobe
+ * @system: The system name (NULL for the default kprobes)
+ * @event: The event to create (NULL to use @addr for the event)
+ * @addr: The function and offset (or address) to insert the probe
+ * @format: The format string to define the probe.
+ *
+ * Allocate a kprobe context that will be in the @system group (or kprobes if
+ * @system is NULL). Have the name of @event (or @addr if @event is NULL). Will
+ * be inserted to @addr (function name, with or without offset, or a address).
+ * And the @format will define the format of the kprobe.
+ *
+ * See the Linux documentation file under:
+ *  Documentation/trace/kprobetrace.rst
+ *
+ * The kprobe is not created in the system.
+ *
+ * Return a pointer to a kprobe context on success, or NULL on error.
+ * The returned pointer must be freed with tracefs_dynevent_free()
+ *
+ * errno will be set to EBADMSG if addr is NULL.
+ */
+struct tracefs_dynevent *
+tracefs_kprobe_alloc(const char *system, const char *event, const char *addr, const char *format)
+
+{
+	return kprobe_alloc(TRACEFS_DYNEVENT_KPROBE, system, event, addr, format);
+}
+
+/**
+ * tracefs_kretprobe_alloc - Allocate new kretprobe
+ * @system: The system name (NULL for the default kprobes)
+ * @event: The event to create (NULL to use @addr for the event)
+ * @addr: The function and offset (or address) to insert the retprobe
+ * @format: The format string to define the retprobe.
+ * @max: Maximum number of instances of the specified function that
+ *	 can be probed simultaneously, or 0 for the default value.
+ *
+ * Allocate a kretprobe that will be in the @system group (or kprobes if
+ * @system is NULL). Have the name of @event (or @addr if @event is
+ * NULL). Will be inserted to @addr (function name, with or without
+ * offset, or a address). And the @format will define the raw format
+ * of the kprobe. See the Linux documentation file under:
+ * Documentation/trace/kprobetrace.rst
+ * The kretprobe is not created in the system.
+ *
+ * Return a pointer to a kprobe context on success, or NULL on error.
+ * The returned pointer must be freed with tracefs_dynevent_free()
+ *
+ * errno will be set to EBADMSG if addr is NULL.
+ */
+struct tracefs_dynevent *
+tracefs_kretprobe_alloc(const char *system, const char *event,
+			const char *addr, const char *format, int max)
+{
+	struct tracefs_dynevent *kp;
+	int ret;
+
+	kp = kprobe_alloc(TRACEFS_DYNEVENT_KRETPROBE, system, event, addr, format);
+	if (!kp)
+		return NULL;
+
+	if (!max)
+		return kp;
+
+	free(kp->prefix);
+	kp->prefix = NULL;
+	ret = asprintf(&kp->prefix, "r%d:", max);
+	if (ret < 0)
+		goto error;
+
+	return kp;
+error:
+	tracefs_dynevent_free(kp);
+	return NULL;
+}
+
 static int insert_kprobe(const char *type, const char *system,
 			 const char *event, const char *addr,
 			 const char *format)
-- 
2.31.1
next prev parent reply	other threads:[~2021-11-04 11:10 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-04 11:10 [PATCH v4 00/10] libtracefs dynamic events support Tzvetomir Stoyanov (VMware)
2021-11-04 11:10 ` [PATCH v4 01/10] libtracefs: New APIs for dynamic events Tzvetomir Stoyanov (VMware)
2021-11-04 13:58   ` Steven Rostedt
2021-11-04 14:37   ` Steven Rostedt
2021-11-04 14:54   ` Steven Rostedt
2021-11-04 11:10 ` Tzvetomir Stoyanov (VMware) [this message]
2021-11-04 16:29   ` [PATCH v4 02/10] libtracefs: New APIs for kprobe allocation Steven Rostedt
2021-11-04 11:10 ` [PATCH v4 03/10] libtracefs: Remove redundant kprobes APIs Tzvetomir Stoyanov (VMware)
2021-11-04 11:10 ` [PATCH v4 04/10] libtracefs: Change tracefs_kprobe_info API Tzvetomir Stoyanov (VMware)
2021-11-04 16:33   ` Steven Rostedt
2021-11-04 17:28     ` Tzvetomir Stoyanov
2021-11-04 17:57       ` Steven Rostedt
2021-11-04 11:10 ` [PATCH v4 05/10] libtracefs: Reimplement kprobe raw APIs Tzvetomir Stoyanov (VMware)
2021-11-04 11:10 ` [PATCH v4 06/10] libtracefs: Extend kprobes unit test Tzvetomir Stoyanov (VMware)
2021-11-04 11:10 ` [PATCH v4 07/10] libtracefs: Rename tracefs_synth_init API Tzvetomir Stoyanov (VMware)
2021-11-04 11:10 ` [PATCH v4 08/10] libtracefs: Use the internal dynamic events API when creating synthetic events Tzvetomir Stoyanov (VMware)
2021-11-04 12:47   ` Yordan Karadzhov
2021-11-04 13:21     ` Steven Rostedt
2021-11-04 17:13   ` Steven Rostedt
2021-11-05 12:15     ` Tzvetomir Stoyanov
2021-11-05 12:33       ` Steven Rostedt
2021-11-04 11:10 ` [PATCH v4 09/10] libtracefs: Update kprobes man pages Tzvetomir Stoyanov (VMware)
2021-11-04 20:28   ` Steven Rostedt
2021-11-04 11:10 ` [PATCH v4 10/10] libtracefs: Document dynamic events APIs Tzvetomir Stoyanov (VMware)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox
  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):
  git send-email \
    --in-reply-to=20211104111047.302660-3-tz.stoyanov@gmail.com \
    --to=tz.stoyanov@gmail.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=y.karadz@gmail.com \
    /path/to/YOUR_REPLY
  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
  Be sure your reply has a Subject: header at the top and a blank line
  before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).