[DTrace-devel] [PATCH 20/26] libcommon: add uprobe_name() and uprobe_delete()

Kris Van Hees kris.van.hees at oracle.com
Fri Oct 21 18:23:46 UTC 2022


Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
 libcommon/uprobes.c | 84 +++++++++++++++++++++++++++++++++------------
 libcommon/uprobes.h |  2 ++
 2 files changed, 64 insertions(+), 22 deletions(-)

diff --git a/libcommon/uprobes.c b/libcommon/uprobes.c
index 7e182797..bc4d1b2d 100644
--- a/libcommon/uprobes.c
+++ b/libcommon/uprobes.c
@@ -209,8 +209,21 @@ uprobe_decode_name(const char *name)
 	return decoded;
 }
 
+char *
+uprobe_name(dev_t dev, ino_t ino, uint64_t addr, int isret)
+{
+	char	*name;
+
+	if (asprintf(&name, "dt_pid/%c_%llx_%llx_%lx", isret ? 'r' : 'p',
+		     (unsigned long long)dev, (unsigned long long)ino,
+		     (unsigned long)addr) < 0)
+		return NULL;
+
+	return name;
+}
+
 /*
- * Create a uprobe for a given mapping, address, and spec: the uprobe may be a
+ * Create a uprobe for a given device, address, and spec: the uprobe may be a
  * uretprobe.  Return the probe's name as a new dynamically-allocated string, or
  * NULL on error.  If prv/mod/fun/prb are all set, they are passed down as the
  * name of the corresponding DTrace probe.
@@ -220,16 +233,9 @@ uprobe_create_named(dev_t dev, ino_t ino, uint64_t addr, const char *spec, int i
 		    const char *prv, const char *mod, const char *fun,
 		    const char *prb)
 {
-	int fd;
-	int rc;
-	char *name, *args = "";
-
-	if (asprintf(&name, "dt_pid/%c_%llx_%llx_%lx",
-		isret ? 'r' : 'p',
-		(unsigned long long) dev,
-		(unsigned long long) ino,
-		(unsigned long) addr) < 0)
-		return NULL;
+	int	fd = -1;
+	int	rc = -1;
+	char	*name, *args = NULL;
 
 	if (prv && mod && fun && prb) {
 		char *eprv, *emod, *efun, *eprb;
@@ -244,8 +250,7 @@ uprobe_create_named(dev_t dev, ino_t ino, uint64_t addr, const char *spec, int i
 			if (asprintf(&args, "P%s=\\1 M%s=\\2 F%s=\\3 N%s=\\4",
 				     eprv, emod, efun, eprb) < 0)
 				failed = 1;
-		}
-		else
+		} else
 			failed = 1;
 
 		free(eprv);
@@ -257,17 +262,24 @@ uprobe_create_named(dev_t dev, ino_t ino, uint64_t addr, const char *spec, int i
 			return NULL;
 	}
 
+	name = uprobe_name(dev, ino, addr, isret);
+	if (!name)
+		goto out;
+
 	/* Add the uprobe. */
 	fd = open(TRACEFS "uprobe_events", O_WRONLY | O_APPEND);
-	if (fd != -1) {
-		rc = dprintf(fd, "%c:%s %s %s\n", isret ? 'r' : 'p',
-			     name, spec, args);
+	if (fd == -1)
+		goto out;
 
-		/* TODO: error reporting, probably via a callback */
-		close(fd);
-	}
+	rc = dprintf(fd, "%c:%s %s %s\n", isret ? 'r' : 'p', name, spec,
+		     args ? args : "");
 
-	if (fd == -1 || rc == -1) {
+out:
+	if (fd == -1)
+		close(fd);
+	free(args);
+	if (rc < 0) {
+		free(name);
 		return NULL;
 	}
 
@@ -283,8 +295,8 @@ uprobe_create_named(dev_t dev, ino_t ino, uint64_t addr, const char *spec, int i
 char *
 uprobe_create(dev_t dev, ino_t ino, uint64_t addr, const char *spec, int isret)
 {
-    return uprobe_create_named(dev, ino, addr, spec, isret,
-			       NULL, NULL, NULL, NULL);
+	return uprobe_create_named(dev, ino, addr, spec, isret,
+				   NULL, NULL, NULL, NULL);
 }
 
 /*
@@ -309,3 +321,31 @@ uprobe_create_from_addr(pid_t pid, uint64_t addr, const char *prv,
 	free(spec);
 	return name;
 }
+
+/*
+ * Destroy a uprobe for a given device, address, and spec.
+ */
+int
+uprobe_delete(dev_t dev, ino_t ino, uint64_t addr, int isret)
+{
+	int	fd = -1;
+	int	rc = -1;
+	char	*name;
+
+	name = uprobe_name(dev, ino, addr, isret);
+	if (!name)
+		goto out;
+
+	fd = open(TRACEFS "uprobe_events", O_WRONLY | O_APPEND);
+	if (fd == -1)
+		goto out;
+
+	rc = dprintf(fd, "-:%s\n", name);
+
+out:
+	if (fd != -1)
+		close(fd);
+	free(name);
+
+	return rc < 0 ? -1 : 0;
+}
diff --git a/libcommon/uprobes.h b/libcommon/uprobes.h
index d99320cc..fde6ba40 100644
--- a/libcommon/uprobes.h
+++ b/libcommon/uprobes.h
@@ -15,6 +15,7 @@
 
 extern char *uprobe_spec_by_addr(pid_t pid, ps_prochandle *P, uint64_t addr,
 				 prmap_t *mapp);
+extern char *uprobe_name(dev_t dev, ino_t ino, uint64_t addr, int isret);
 extern char *uprobe_create_named(dev_t dev, ino_t ino, uint64_t addr,
 				 const char *spec, int isret, const char *prv,
 				 const char *mod, const char *fun,
@@ -24,6 +25,7 @@ extern char *uprobe_create(dev_t dev, ino_t ino, uint64_t addr, const char *spec
 extern char *uprobe_create_from_addr(pid_t pid, uint64_t addr, const char *prv,
 				     const char *mod, const char *fun,
 				     const char *prb);
+extern int uprobe_delete(dev_t dev, ino_t ino, uint64_t addr, int isret);
 extern char *uprobe_encode_name(const char *);
 extern char *uprobe_decode_name(const char *);
 
-- 
2.31.1




More information about the DTrace-devel mailing list