[DTrace-devel] [PATCH 27/47] Code cleanup in provider modules
Kris Van Hees
kris.van.hees at oracle.com
Sun May 3 20:17:43 PDT 2020
The dtrace, FBT, SDT, and syscall providers contained some left-over
code from an earlier implementation. Provider-specific implementation
functions (static, exported through a dt_provimpl_t struct) were named
with the provider name as prefix which is unnecessary (and will lead to
a naming conflict once we implement a dtrace_probe_info() function for
the dtrace provider.)
This patch also provides some further cleanup of comments.
Orabug: 31220516
Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
Reviewed-by: Kris Van Hees <kris.van.hees at oracle.com>
Reviewed-by: Eugene Loh <eugene.loh at oracle.com>
---
libdtrace/dt_prov_dtrace.c | 50 +--------
libdtrace/dt_prov_fbt.c | 216 +++++++++---------------------------
libdtrace/dt_prov_sdt.c | 120 ++++----------------
libdtrace/dt_prov_syscall.c | 149 ++++++-------------------
4 files changed, 110 insertions(+), 425 deletions(-)
diff --git a/libdtrace/dt_prov_dtrace.c b/libdtrace/dt_prov_dtrace.c
index e323bc70..9c2a076f 100644
--- a/libdtrace/dt_prov_dtrace.c
+++ b/libdtrace/dt_prov_dtrace.c
@@ -22,7 +22,7 @@ static const dtrace_pattr_t pattr = {
{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
};
-static int dtrace_populate(dtrace_hdl_t *dtp)
+static int populate(dtrace_hdl_t *dtp)
{
dt_provider_t *prv;
int n = 0;
@@ -53,7 +53,7 @@ static int dtrace_populate(dtrace_hdl_t *dtp)
* function that implements tha compiled D clause. It returns the value that
* it gets back from that function.
*/
-static void dtrace_trampoline(dt_pcb_t *pcb, int haspred)
+static void trampoline(dt_pcb_t *pcb, int haspred)
{
int i;
dt_irlist_t *dlp = &pcb->pcb_ir;
@@ -200,51 +200,9 @@ static void dtrace_trampoline(dt_pcb_t *pcb, int haspred)
dt_irlist_append(dlp, dt_cg_node_alloc(lbl_exit, instr));
}
-#if 0
-#define EVENT_PREFIX "tracepoint/dtrace/"
-
-/*
- * Perform a probe lookup based on an event name (usually obtained from a BPF
- * ELF section name). We use an unused event group (dtrace) to be able to
- * fake a section name that libbpf will allow us to use.
- */
-static struct dt_probe *dtrace_resolve_event(const char *name)
-{
- struct dt_probe tmpl;
- struct dt_probe *probe;
-
- if (!name)
- return NULL;
-
- /* Exclude anything that is not a dtrace core tracepoint */
- if (strncmp(name, EVENT_PREFIX, sizeof(EVENT_PREFIX) - 1) != 0)
- return NULL;
- name += sizeof(EVENT_PREFIX) - 1;
-
- memset(&tmpl, 0, sizeof(tmpl));
- tmpl.prv_name = provname;
- tmpl.mod_name = NULL;
- tmpl.fun_name = NULL;
- tmpl.prb_name = name;
-
- probe = dt_probe_by_name(&tmpl);
-
- return probe;
-}
-
-static int dtrace_attach(const char *name, int bpf_fd)
-{
- return -1;
-}
-#endif
-
dt_provimpl_t dt_dtrace = {
.name = "dtrace",
.prog_type = BPF_PROG_TYPE_KPROBE,
- .populate = &dtrace_populate,
- .trampoline = &dtrace_trampoline,
-#if 0
- .resolve_event = &dtrace_resolve_event,
- .attach = &dtrace_attach,
-#endif
+ .populate = &populate,
+ .trampoline = &trampoline,
};
diff --git a/libdtrace/dt_prov_fbt.c b/libdtrace/dt_prov_fbt.c
index 3262449c..f63d4a79 100644
--- a/libdtrace/dt_prov_fbt.c
+++ b/libdtrace/dt_prov_fbt.c
@@ -17,16 +17,6 @@
* or
* <name> [<modname>] fbt:<modname>:<name>:entry
* fbt:<modname>:<name>:return
- *
- * Mapping from BPF section name to DTrace probe name:
- *
- * kprobe/<name> fbt:vmlinux:<name>:entry
- * kretprobe/<name> fbt:vmlinux:<name>:return
- *
- * (Note that the BPF section does not carry information about the module that
- * the function is found in. This means that BPF section name cannot be used
- * to distinguish between functions with the same name occurring in different
- * modules.)
*/
#include <assert.h>
#include <errno.h>
@@ -63,70 +53,11 @@ static const dtrace_pattr_t pattr = {
{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
};
-static int fbt_probe_info(dtrace_hdl_t *dtp, const dt_probe_t *prp,
- int *idp, int *argcp, dt_argdesc_t **argvp)
-{
- FILE *f;
- char fn[256];
- int rc = 0;
-
- *idp = -1;
-
- strcpy(fn, KPROBESFS);
- strcat(fn, prp->desc->fun);
- strcat(fn, "/format");
-
- /*
- * We check to see if the kprobe event already exists in the tracing
- * sub-system. If not, we try to register the probe with the tracing
- * sub-system, and try accessing it again.
- */
-again:
- f = fopen(fn, "r");
- if (f == NULL) {
- int fd;
- char c = 'p';
-
- if (rc)
- goto out;
-
- rc = -ENOENT;
-
- /*
- * The probe name component is either "entry" or "return" for
- * FBT probes.
- */
- if (prp->desc->prb[0] == 'r')
- c = 'r';
-
- /*
- * Register the kprobe with the tracing subsystem. This will
- * create a tracepoint event.
- */
- fd = open(KPROBE_EVENTS, O_WRONLY | O_APPEND);
- if (fd == -1)
- goto out;
-
- dprintf(fd, "%c:%s %s\n", c, prp->desc->fun, prp->desc->fun);
- close(fd);
-
- goto again;
- }
-
- *argcp = 0;
- *argvp = NULL;
- rc = tp_event_info(dtp, f, 0, idp, NULL, NULL);
- fclose(f);
-
-out:
- return rc;
-}
-
/*
* Scan the PROBE_LIST file and add entry and return probes for every function
* that is listed.
*/
-static int fbt_populate(dtrace_hdl_t *dtp)
+static int populate(dtrace_hdl_t *dtp)
{
dt_provider_t *prv;
FILE *f;
@@ -226,7 +157,7 @@ static int fbt_populate(dtrace_hdl_t *dtp)
* function that implements tha compiled D clause. It returns the value that
* it gets back from that function.
*/
-static void fbt_trampoline(dt_pcb_t *pcb, int haspred)
+static void trampoline(dt_pcb_t *pcb, int haspred)
{
int i;
dt_irlist_t *dlp = &pcb->pcb_ir;
@@ -373,112 +304,69 @@ static void fbt_trampoline(dt_pcb_t *pcb, int haspred)
dt_irlist_append(dlp, dt_cg_node_alloc(lbl_exit, instr));
}
-#if 0
-#define ENTRY_PREFIX "kprobe/"
-#define EXIT_PREFIX "kretprobe/"
-
-/*
- * Perform a probe lookup based on an event name (BPF ELF section name).
- */
-static struct dt_probe *fbt_resolve_event(const char *name)
+static int probe_info(dtrace_hdl_t *dtp, const dt_probe_t *prp,
+ int *idp, int *argcp, dt_argdesc_t **argvp)
{
- const char *prbname;
- struct dt_probe tmpl;
- struct dt_probe *probe;
-
- if (!name)
- return NULL;
-
- if (strncmp(name, ENTRY_PREFIX, sizeof(ENTRY_PREFIX) - 1) == 0) {
- name += sizeof(ENTRY_PREFIX) - 1;
- prbname = "entry";
- } else if (strncmp(name, EXIT_PREFIX, sizeof(EXIT_PREFIX) - 1) == 0) {
- name += sizeof(EXIT_PREFIX) - 1;
- prbname = "return";
- } else
- return NULL;
-
- memset(&tmpl, 0, sizeof(tmpl));
- tmpl.prv_name = provname;
- tmpl.mod_name = modname;
- tmpl.fun_name = name;
- tmpl.prb_name = prbname;
-
- probe = dt_probe_by_name(&tmpl);
-
- return probe;
-}
+ FILE *f;
+ char fn[256];
+ int rc = 0;
-/*
- * Attach the given BPF program (identified by its file descriptor) to the
- * kprobe identified by the given section name.
- *
- * TODO: This should somehow update the probe description with the event ID.
- */
-static int fbt_attach(const char *name, int bpf_fd)
-{
- char efn[256];
- char buf[256];
- int event_id, fd, rc;
+ *idp = -1;
- name += 7; /* skip "kprobe/" */
- snprintf(buf, sizeof(buf), "p:%s %s\n", name, name);
+ strcpy(fn, KPROBESFS);
+ strcat(fn, prp->desc->fun);
+ strcat(fn, "/format");
/*
- * Register the kprobe with the tracing subsystem. This will create
- * a tracepoint event.
+ * We check to see if the kprobe event already exists in the tracing
+ * sub-system. If not, we try to register the probe with the tracing
+ * sub-system, and try accessing it again.
*/
- fd = open(KPROBE_EVENTS, O_WRONLY | O_APPEND);
- if (fd < 0) {
- perror(KPROBE_EVENTS);
- return -1;
- }
- rc = write(fd, buf, strlen(buf));
- if (rc < 0) {
- perror(KPROBE_EVENTS);
- close(fd);
- return -1;
- }
- close(fd);
-
- /* Read the tracepoint event id for the kprobe we just registered. */
- strcpy(efn, EVENTSFS);
- strcat(efn, "kprobes/");
- strcat(efn, name);
- strcat(efn, "/id");
-
- fd = open(efn, O_RDONLY);
- if (fd < 0) {
- perror(efn);
- return -1;
- }
- rc = read(fd, buf, sizeof(buf));
- if (rc < 0 || rc >= sizeof(buf)) {
- perror(efn);
+again:
+ f = fopen(fn, "r");
+ if (f == NULL) {
+ int fd;
+ char c = 'p';
+
+ if (rc)
+ goto out;
+
+ rc = -ENOENT;
+
+ /*
+ * The probe name component is either "entry" or "return" for
+ * FBT probes.
+ */
+ if (prp->desc->prb[0] == 'r')
+ c = 'r';
+
+ /*
+ * Register the kprobe with the tracing subsystem. This will
+ * create a tracepoint event.
+ */
+ fd = open(KPROBE_EVENTS, O_WRONLY | O_APPEND);
+ if (fd == -1)
+ goto out;
+
+ dprintf(fd, "%c:%s %s\n", c, prp->desc->fun, prp->desc->fun);
close(fd);
- return -1;
+
+ goto again;
}
- close(fd);
- buf[rc] = '\0';
- event_id = atoi(buf);
- /*
- * Attaching a BPF program (by file descriptor) to an event (by ID) is
- * a generic operation provided by the BPF interface code.
- */
- return dt_bpf_attach(event_id, bpf_fd);
+ *argcp = 0;
+ *argvp = NULL;
+ rc = tp_event_info(dtp, f, 0, idp, NULL, NULL);
+ fclose(f);
+out:
+ return rc;
}
-#endif
dt_provimpl_t dt_fbt = {
.name = "fbt",
.prog_type = BPF_PROG_TYPE_KPROBE,
- .populate = &fbt_populate,
- .trampoline = &fbt_trampoline,
- .probe_info = &fbt_probe_info,
-#if 0
- .resolve_event = &fbt_resolve_event,
- .attach = &fbt_attach,
-#endif
+ .populate = &populate,
+ .trampoline = &trampoline,
+ .probe_info = &probe_info,
};
diff --git a/libdtrace/dt_prov_sdt.c b/libdtrace/dt_prov_sdt.c
index 1dcaf584..ad76d9f5 100644
--- a/libdtrace/dt_prov_sdt.c
+++ b/libdtrace/dt_prov_sdt.c
@@ -12,10 +12,6 @@
* Mapping from event name to DTrace probe name:
*
* <group>:<name> sdt:<group>::<name>
- *
- * Mapping from BPF section name to DTrace probe name:
- *
- * tracepoint/<group>/<name> sdt:<group>::<name>
*/
#include <assert.h>
#include <errno.h>
@@ -226,38 +222,13 @@ done:
return 0;
}
-static int sdt_probe_info(dtrace_hdl_t *dtp, const dt_probe_t *prp,
- int *idp, int *argcp, dt_argdesc_t **argvp)
-{
- FILE *f;
- char fn[256];
- int rc;
-
- *idp = -1;
-
- strcpy(fn, EVENTSFS);
- strcat(fn, prp->desc->mod);
- strcat(fn, "/");
- strcat(fn, prp->desc->prb);
- strcat(fn, "/format");
-
- f = fopen(fn, "r");
- if (!f)
- return -ENOENT;
-
- rc = tp_event_info(dtp, f, 0, idp, argcp, argvp);
- fclose(f);
-
- return rc;
-}
-
/*
* The PROBE_LIST file lists all tracepoints in a <group>:<name> format. When
* kprobes are registered on the system, they will appear in this list also as
* kprobes:<name>. We need to ignore them because DTrace already accounts for
* them as FBT probes.
*/
-static int sdt_populate(dtrace_hdl_t *dtp)
+static int populate(dtrace_hdl_t *dtp)
{
dt_provider_t *prv;
FILE *f;
@@ -313,7 +284,7 @@ static int sdt_populate(dtrace_hdl_t *dtp)
*
* FIXME: Currently, access to arguments of the tracepoint is not supported.
*/
-static void sdt_trampoline(dt_pcb_t *pcb, int haspred)
+static void trampoline(dt_pcb_t *pcb, int haspred)
{
int i;
dt_irlist_t *dlp = &pcb->pcb_ir;
@@ -425,82 +396,35 @@ static void sdt_trampoline(dt_pcb_t *pcb, int haspred)
dt_irlist_append(dlp, dt_cg_node_alloc(lbl_exit, instr));
}
-#if 0
-#define EVENT_PREFIX "tracepoint/"
-#define EVENT_SYSCALLS "syscalls/"
-#define EVENT_KPROBES "kprobes/"
-
-/*
- * Perform a probe lookup based on an event name (usually obtained from a BPF
- * ELF section name). Exclude syscalls and kprobes tracepoint events because
- * they are handled by their own individual providers.
- */
-static struct dt_probe *sdt_resolve_event(const char *name)
+static int probe_info(dtrace_hdl_t *dtp, const dt_probe_t *prp,
+ int *idp, int *argcp, dt_argdesc_t **argvp)
{
- char *str, *p;
- struct dt_probe tmpl;
- struct dt_probe *probe;
-
- if (!name)
- return NULL;
-
- /* Exclude anything that is not a tracepoint */
- if (strncmp(name, EVENT_PREFIX, sizeof(EVENT_PREFIX) - 1) != 0)
- return NULL;
- name += sizeof(EVENT_PREFIX) - 1;
-
- /* Exclude syscall tracepoints */
- if (strncmp(name, EVENT_SYSCALLS, sizeof(EVENT_SYSCALLS) - 1) == 0)
- return NULL;
-
- /* Exclude kprobes tracepoints */
- if (strncmp(name, EVENT_KPROBES, sizeof(EVENT_KPROBES) - 1) == 0)
- return NULL;
-
- str = strdup(name);
- if (!str)
- return NULL;
-
- p = strchr(str, '/');
- *p++ = '\0';
-
- memset(&tmpl, 0, sizeof(tmpl));
- tmpl.prv_name = provname;
- tmpl.mod_name = p ? str : modname;
- tmpl.fun_name = NULL;
- tmpl.prb_name = p;
-
- probe = dt_probe_by_name(&tmpl);
+ FILE *f;
+ char fn[256];
+ int rc;
- free(str);
+ *idp = -1;
- return probe;
-}
+ strcpy(fn, EVENTSFS);
+ strcat(fn, prp->desc->mod);
+ strcat(fn, "/");
+ strcat(fn, prp->desc->prb);
+ strcat(fn, "/format");
-static int sdt_attach(const char *name, int bpf_fd)
-{
- char efn[256];
- int len = 265 - strlen(EVENTSFS);
+ f = fopen(fn, "r");
+ if (!f)
+ return -ENOENT;
- name += 11; /* skip "tracepoint/" */
- strcpy(efn, EVENTSFS);
- strncat(efn, name, len);
- len -= strlen(name);
- strncat(efn, "/id", len);
-printf("[%s]\n", efn);
+ rc = tp_event_info(dtp, f, 0, idp, argcp, argvp);
+ fclose(f);
- return 0;
+ return rc;
}
-#endif
dt_provimpl_t dt_sdt = {
.name = "sdt",
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
- .populate = &sdt_populate,
- .trampoline = &sdt_trampoline,
- .probe_info = &sdt_probe_info,
-#if 0
- .resolve_event = &sdt_resolve_event,
- .attach = &sdt_attach,
-#endif
+ .populate = &populate,
+ .trampoline = &trampoline,
+ .probe_info = &probe_info,
};
diff --git a/libdtrace/dt_prov_syscall.c b/libdtrace/dt_prov_syscall.c
index 2cd9c6ef..fd5730f3 100644
--- a/libdtrace/dt_prov_syscall.c
+++ b/libdtrace/dt_prov_syscall.c
@@ -14,11 +14,6 @@
*
* syscalls:sys_enter_<name> syscall:vmlinux:<name>:entry
* syscalls:sys_exit_<name> syscall:vmlinux:<name>:return
- *
- * Mapping from BPF section name to DTrace probe name:
- *
- * tracepoint/syscalls/sys_enter_<name> syscall:vmlinux:<name>:entry
- * tracepoint/syscalls/sys_exit_<name> syscall:vmlinux:<name>:return
*/
#include <assert.h>
#include <ctype.h>
@@ -66,45 +61,14 @@ struct syscall_data {
#define SCD_ARG(n) offsetof(struct syscall_data, arg[n])
-static int syscall_probe_info(dtrace_hdl_t *dtp, const dt_probe_t *prp,
- int *idp, int *argcp, dt_argdesc_t **argvp)
-{
- FILE *f;
- char fn[256];
- int rc;
-
- *idp = -1;
-
- /*
- * We know that the probe name is either "entry" or "return", so we can
- * just check the first character.
- */
- strcpy(fn, SYSCALLSFS);
- if (prp->desc->prb[0] == 'e')
- strcat(fn, "sys_enter_");
- else
- strcat(fn, "sys_exit_");
- strcat(fn, prp->desc->fun);
- strcat(fn, "/format");
-
- f = fopen(fn, "r");
- if (!f)
- return -ENOENT;
-
- rc = tp_event_info(dtp, f, SKIP_EXTRA_FIELDS, idp, argcp, argvp);
- fclose(f);
-
- return rc;
-}
-
#define PROBE_LIST TRACEFS "available_events"
#define PROV_PREFIX "syscalls:"
#define ENTRY_PREFIX "sys_enter_"
#define EXIT_PREFIX "sys_exit_"
-/* can the PROBE_LIST file and add probes for any syscalls events. */
-static int syscall_populate(dtrace_hdl_t *dtp)
+/* Scan the PROBE_LIST file and add probes for any syscalls events. */
+static int populate(dtrace_hdl_t *dtp)
{
dt_provider_t *prv;
FILE *f;
@@ -178,7 +142,7 @@ static int syscall_populate(dtrace_hdl_t *dtp)
* function that implements the compiled D clause. It returns the value that
* it gets back from that function.
*/
-static void syscall_trampoline(dt_pcb_t *pcb, int haspred)
+static void trampoline(dt_pcb_t *pcb, int haspred)
{
int i;
dt_irlist_t *dlp = &pcb->pcb_ir;
@@ -303,90 +267,41 @@ static void syscall_trampoline(dt_pcb_t *pcb, int haspred)
dt_irlist_append(dlp, dt_cg_node_alloc(lbl_exit, instr));
}
-#if 0
-#define EVENT_PREFIX "tracepoint/syscalls/"
-
-/*
- * Perform a probe lookup based on an event name (BPF ELF section name).
- * Exclude syscalls and kprobes tracepoint events because they are handled by
- * their own individual providers.
- */
-static struct dt_probe *systrace_resolve_event(const char *name)
+static int probe_info(dtrace_hdl_t *dtp, const dt_probe_t *prp,
+ int *idp, int *argcp, dt_argdesc_t **argvp)
{
- const char *prbname;
- struct dt_probe tmpl;
- struct dt_probe *probe;
-
- if (!name)
- return NULL;
-
- /* Exclude anything that is not a syscalls tracepoint */
- if (strncmp(name, EVENT_PREFIX, sizeof(EVENT_PREFIX) - 1) != 0)
- return NULL;
- name += sizeof(EVENT_PREFIX) - 1;
-
- if (strncmp(name, ENTRY_PREFIX, sizeof(ENTRY_PREFIX) - 1) == 0) {
- name += sizeof(ENTRY_PREFIX) - 1;
- prbname = "entry";
- } else if (strncmp(name, EXIT_PREFIX, sizeof(EXIT_PREFIX) - 1) == 0) {
- name += sizeof(EXIT_PREFIX) - 1;
- prbname = "return";
- } else
- return NULL;
-
- memset(&tmpl, 0, sizeof(tmpl));
- tmpl.prv_name = provname;
- tmpl.mod_name = modname;
- tmpl.fun_name = name;
- tmpl.prb_name = prbname;
-
- probe = dt_probe_by_name(&tmpl);
-
- return probe;
-}
+ FILE *f;
+ char fn[256];
+ int rc;
-/*
- * Attach the given BPF program (identified by its file descriptor) to the
- * event identified by the given section name.
- */
-static int syscall_attach(const char *name, int bpf_fd)
-{
- char efn[256];
- char buf[256];
- int event_id, fd, rc;
-
- name += sizeof(EVENT_PREFIX) - 1;
- strcpy(efn, SYSCALLSFS);
- strcat(efn, name);
- strcat(efn, "/id");
-
- fd = open(efn, O_RDONLY);
- if (fd < 0) {
- perror(efn);
- return -1;
- }
- rc = read(fd, buf, sizeof(buf));
- if (rc < 0 || rc >= sizeof(buf)) {
- perror(efn);
- close(fd);
- return -1;
- }
- close(fd);
- buf[rc] = '\0';
- event_id = atoi(buf);
+ *idp = -1;
- return dt_bpf_attach(event_id, bpf_fd);
+ /*
+ * We know that the probe name is either "entry" or "return", so we can
+ * just check the first character.
+ */
+ strcpy(fn, SYSCALLSFS);
+ if (prp->desc->prb[0] == 'e')
+ strcat(fn, "sys_enter_");
+ else
+ strcat(fn, "sys_exit_");
+ strcat(fn, prp->desc->fun);
+ strcat(fn, "/format");
+
+ f = fopen(fn, "r");
+ if (!f)
+ return -ENOENT;
+
+ rc = tp_event_info(dtp, f, SKIP_EXTRA_FIELDS, idp, argcp, argvp);
+ fclose(f);
+
+ return rc;
}
-#endif
dt_provimpl_t dt_syscall = {
.name = "syscall",
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
- .populate = &syscall_populate,
- .trampoline = &syscall_trampoline,
- .probe_info = &syscall_probe_info,
-#if 0
- .resolve_event = &systrace_resolve_event,
- .attach = &syscall_attach,
-#endif
+ .populate = &populate,
+ .trampoline = &trampoline,
+ .probe_info = &probe_info,
};
--
2.26.0
More information about the DTrace-devel
mailing list