[RFC PATCH v2 19/26] perf tools: Register basic uBPF helpers

From: He Kuang
Date: Sun Jun 26 2016 - 07:25:44 EST


From: Wang Nan <wangnan0@xxxxxxxxxx>

Reigster basic extern functions for uBPF programs.

Signed-off-by: Wang Nan <wangnan0@xxxxxxxxxx>
Cc: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Cc: Alexei Starovoitov <ast@xxxxxxxxxx>
Cc: Brendan Gregg <brendan.d.gregg@xxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Li Zefan <lizefan@xxxxxxxxxx>
Signed-off-by: He Kuang <hekuang@xxxxxxxxxx>
---
tools/perf/perf.c | 3 ++
tools/perf/util/Build | 1 +
tools/perf/util/ubpf-helpers-list.h | 11 ++++++
tools/perf/util/ubpf-helpers.c | 73 +++++++++++++++++++++++++++++++++++++
tools/perf/util/ubpf-helpers.h | 21 +++++++++++
5 files changed, 109 insertions(+)
create mode 100644 tools/perf/util/ubpf-helpers-list.h
create mode 100644 tools/perf/util/ubpf-helpers.c
create mode 100644 tools/perf/util/ubpf-helpers.h

diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index 8f21922..f4c93c0 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -16,6 +16,7 @@
#include "util/parse-events.h"
#include <subcmd/parse-options.h>
#include "util/bpf-loader.h"
+#include "util/ubpf-helpers.h"
#include "util/debug.h"
#include <api/fs/fs.h>
#include <api/fs/tracing_path.h>
@@ -594,6 +595,8 @@ int main(int argc, const char **argv)

perf_debug_setup();

+ register_ubpf_helpers();
+
while (1) {
static int done_help;
int was_alias = run_argv(&argc, &argv);
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 1d24d94..d12ef7d 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -87,6 +87,7 @@ libperf-y += mem-events.o

libperf-$(CONFIG_LIBBPF) += bpf-loader.o
libperf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o
+libperf-$(CONFIG_UBPF) += ubpf-helpers.o
libperf-$(CONFIG_LIBELF) += symbol-elf.o
libperf-$(CONFIG_LIBELF) += probe-file.o
libperf-$(CONFIG_LIBELF) += probe-event.o
diff --git a/tools/perf/util/ubpf-helpers-list.h b/tools/perf/util/ubpf-helpers-list.h
new file mode 100644
index 0000000..02ad517
--- /dev/null
+++ b/tools/perf/util/ubpf-helpers-list.h
@@ -0,0 +1,11 @@
+DEF_UBPF_HELPER(int, ubpf_memcmp, (void *s1, void *s2, unsigned int n))
+DEF_UBPF_HELPER(void, ubpf_memcpy, (void *d, void *s, unsigned int size))
+DEF_UBPF_HELPER(int, ubpf_strcmp, (char *s1, char *s2))
+DEF_UBPF_HELPER(int, ubpf_printf, (char *fmt, ...))
+DEF_UBPF_HELPER(int, ubpf_map_lookup_elem,
+ (void *map_desc, void *key, void *value))
+DEF_UBPF_HELPER(int, ubpf_map_update_elem,
+ (void *map_desc, void *key, void *value,
+ unsigned long long flags))
+DEF_UBPF_HELPER(int, ubpf_map_get_next_key,
+ (void *map_desc, void *key, void *next_key))
diff --git a/tools/perf/util/ubpf-helpers.c b/tools/perf/util/ubpf-helpers.c
new file mode 100644
index 0000000..8b83fc8
--- /dev/null
+++ b/tools/perf/util/ubpf-helpers.c
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2016, Wang Nan <wangnan0@xxxxxxxxxx>
+ * Copyright (C) 2016, Huawei Inc.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+
+#include <bpf/libbpf.h>
+#include <bpf/bpf.h>
+#include <ubpf.h>
+
+#include "ubpf-helpers.h"
+
+static int __maybe_unused
+ubpf_memcmp(void *s1, void *s2, unsigned int n)
+{
+ return memcmp(s1, s2, n);
+}
+
+static void __maybe_unused
+ubpf_memcpy(void *d, void *s, unsigned int n)
+{
+ memcpy(d, s, n);
+}
+
+static int __maybe_unused
+ubpf_strcmp(char *s1, char *s2)
+{
+ return strcmp(s1, s2);
+}
+
+static int __maybe_unused
+ubpf_printf(char *fmt, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, fmt);
+ ret = vprintf(fmt, ap);
+ va_end(ap);
+
+ return ret;
+}
+
+static int __maybe_unused
+ubpf_map_lookup_elem(int map_fd, void *key, void *value)
+{
+ return bpf_map_lookup_elem(map_fd, key, value);
+}
+
+static int __maybe_unused
+ubpf_map_update_elem(int map_fd, void *key, void *value,
+ unsigned long long flags)
+{
+ return bpf_map_update_elem(map_fd, key, value, (u64)flags);
+}
+
+static int __maybe_unused
+ubpf_map_get_next_key(int map_fd, void *key, void *next_key)
+{
+ return bpf_map_get_next_key(map_fd, key, next_key);
+}
+
+void register_ubpf_helpers(void)
+{
+#define DEF_UBPF_HELPER(type, name, param) \
+ libbpf_set_ubpf_func(UBPF_FUNC_##name, #name, name);
+#include "ubpf-helpers-list.h"
+#undef DEF_UBPF_HELPER
+}
diff --git a/tools/perf/util/ubpf-helpers.h b/tools/perf/util/ubpf-helpers.h
new file mode 100644
index 0000000..7432715
--- /dev/null
+++ b/tools/perf/util/ubpf-helpers.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2016, Wang Nan <wangnan0@xxxxxxxxxx>
+ * Copyright (C) 2016, Huawei Inc.
+ */
+#ifndef __UBPF_HELPERS_H
+#define __UBPF_HELPERS_H
+
+#define DEF_UBPF_HELPER(type, name, param) UBPF_FUNC_##name,
+enum {
+ UBPF_FUNC_unspec = 0,
+#include "ubpf-helpers-list.h"
+ UBPF_FUNC_MAX
+};
+#undef DEF_UBPF_HELPER
+
+#ifdef HAVE_UBPF_SUPPORT
+void register_ubpf_helpers(void);
+#else
+static inline void register_ubpf_helpers(void) {};
+#endif
+#endif
--
1.8.5.2