From: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH 03/12] libtracefs: New kprobes APIs
Date: Thu, 28 Oct 2021 15:08:58 +0300 [thread overview]
Message-ID: <20211028120907.101847-4-tz.stoyanov@gmail.com> (raw)
In-Reply-To: <20211028120907.101847-1-tz.stoyanov@gmail.com>
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.
Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
include/tracefs.h | 8 ++
src/tracefs-kprobes.c | 169 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 177 insertions(+)
diff --git a/include/tracefs.h b/include/tracefs.h
index 0240aef..e7d545c 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -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,
diff --git a/src/tracefs-kprobes.c b/src/tracefs-kprobes.c
index d4c5f9e..80dc327 100644
--- a/src/tracefs-kprobes.c
+++ b/src/tracefs-kprobes.c
@@ -20,6 +20,135 @@
#define KPROBE_EVENTS "kprobe_events"
#define KPROBE_DEFAULT_GROUP "kprobes"
+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, ':');
+ if (tmp)
+ *tmp = '\0';
+ }
+
+ kp = dynevent_alloc(type, sys, ename, addr, format);
+ if (!event)
+ free((char *)ename);
+ if (!kp)
+ return NULL;
+ return kp;
+}
+
+
+/**
+ * tracefs_kprobe_alloc - Allocate new kprobe context
+ * @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_kprobe_free()
+ *
+ * errno will be set to EBADMSG if addr or format is NULL.
+ */
+struct tracefs_dynevent *
+tracefs_kprobe_alloc(const char *system, const char *event, const char *addr, const char *format)
+
+{
+ return kprobe_alloc(TRACE_DYNEVENT_KPROBE, system, event, addr, format);
+}
+
+/**
+ * tracefs_kretprobe_alloc - Allocate new kretprobe context
+ * @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.
+ *
+ * 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_kprobe_free()
+ *
+ * errno will be set to EBADMSG if addr or format 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(TRACE_DYNEVENT_KRETPROBE, system, event, addr, format);
+ if (!kp)
+ return NULL;
+ if (max) {
+ free(kp->prefix);
+ kp->prefix = NULL;
+ ret = asprintf(&kp->prefix, "r%d:", max);
+ if (ret < 0)
+ goto error;
+ }
+
+ return kp;
+error:
+ dynevent_free(kp);
+ return NULL;
+}
+
+/**
+ * tracefs_kprobe_create - Create a kprobe or kretprobe in the system
+ * @kprobe: Pointer to a kprobe context, describing the probe
+ *
+ * Return 0 on success, or -1 on error.
+ */
+int tracefs_kprobe_create(struct tracefs_dynevent *kprobe)
+{
+ return dynevent_create(kprobe);
+}
+
+/**
+ * tracefs_kprobe_free - Free a kprobe context
+ * @kprobe: Pointer to a kprobe context
+ *
+ * The kprobe/kretprobe, described by this context, is not
+ * removed from the system by this API. It only frees the memory.
+ */
+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)
@@ -474,3 +603,43 @@ int tracefs_kprobe_clear_probe(const char *system, const char *event, bool force
return ret < 0 ? -1 : 0;
}
+
+/**
+ * tracefs_kprobe_destroy - Remove a kprobe or kretprobe from the system
+ * @kprobe: A kprobe context, describing the kprobe that will be deleted.
+ * If NULL, all kprobes and kretprobes in the system will be deleted
+ * @force: Will attempt to disable all kprobe events and clear them
+ *
+ * The kprobe/kretprobe context is not freed by this API.
+ * It only removes the probe from the system.
+ *
+ * Return 0 on success, or -1 on error.
+ */
+int tracefs_kprobe_destroy(struct tracefs_dynevent *kprobe, bool force)
+{
+ char **instance_list;
+ int ret;
+
+ if (!kprobe) {
+ if (tracefs_instance_file_clear(NULL, KPROBE_EVENTS) == 0)
+ return 0;
+ if (!force)
+ return -1;
+ /* Attempt to disable all kprobe events */
+ return kprobe_clear_probes(NULL, force);
+ }
+
+ /*
+ * Since we know we are disabling a specific event, try
+ * to disable it first before clearing it.
+ */
+ if (force) {
+ instance_list = tracefs_instances(NULL);
+ disable_events(kprobe->system, kprobe->event, instance_list);
+ tracefs_list_free(instance_list);
+ }
+
+ ret = dynevent_destroy(kprobe);
+
+ return ret < 0 ? -1 : 0;
+}
--
2.31.1
next prev parent reply other threads:[~2021-10-28 12:09 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-28 12:08 [PATCH 00/12] libtracefs dynamic events support Tzvetomir Stoyanov (VMware)
2021-10-28 12:08 ` [PATCH 01/12] libtracefs: Add new internal APIs for dynamic events Tzvetomir Stoyanov (VMware)
2021-10-28 21:41 ` Steven Rostedt
2021-10-29 2:46 ` Tzvetomir Stoyanov
2021-10-29 3:09 ` Steven Rostedt
2021-10-28 12:08 ` [PATCH 02/12] libtracefs: Rename tracefs_get_kprobes API Tzvetomir Stoyanov (VMware)
2021-10-28 12:08 ` Tzvetomir Stoyanov (VMware) [this message]
2021-10-29 2:55 ` [PATCH 03/12] libtracefs: New kprobes APIs Steven Rostedt
2021-10-28 12:08 ` [PATCH 04/12] libtracefs: Remove redundant " Tzvetomir Stoyanov (VMware)
2021-10-28 12:09 ` [PATCH 05/12] libtracefs: Reimplement tracefs_kprobes_get API Tzvetomir Stoyanov (VMware)
2021-10-29 3:01 ` Steven Rostedt
2021-10-28 12:09 ` [PATCH 06/12] libtracefs: Change tracefs_kprobe_info API Tzvetomir Stoyanov (VMware)
2021-10-28 12:09 ` [PATCH 07/12] libtracefs: Reimplement kprobe raw APIs Tzvetomir Stoyanov (VMware)
2021-10-28 12:09 ` [PATCH 08/12] libtracefs: Extend kprobes unit test Tzvetomir Stoyanov (VMware)
2021-10-28 12:09 ` [PATCH 09/12] libtracefs: Update kprobes man pages Tzvetomir Stoyanov (VMware)
2021-10-28 12:09 ` [PATCH 10/12] libtracefs: Rename tracefs_synth_init API Tzvetomir Stoyanov (VMware)
2021-10-28 12:09 ` [PATCH 11/12] libtracefs: Use the internal dynamic events API when creating synthetic events Tzvetomir Stoyanov (VMware)
2021-10-28 12:09 ` [PATCH 12/12] libtracefs: Document tracefs_dynevent_list_free() API 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=20211028120907.101847-4-tz.stoyanov@gmail.com \
--to=tz.stoyanov@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).