[PATCH 04/13] perf tools: Add bpf__compile function
From: Jiri Olsa
Date: Mon Mar 12 2018 - 05:45:59 EST
Adding bpf__compile function to have interface for
ebpf object compilation.
Link: http://lkml.kernel.org/n/tip-3kus9vjbiven8par9j1ju9u5@xxxxxxxxxxxxxx
Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
tools/perf/util/bpf-loader.c | 46 +++++++++++++++++++++++++++++++++-----------
tools/perf/util/bpf-loader.h | 2 ++
2 files changed, 37 insertions(+), 11 deletions(-)
diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index af7ad814b2c3..e59168153375 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -74,6 +74,38 @@ bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)
return obj;
}
+static int compile(const char *filename, void *obj_buf, size_t *obj_buf_sz)
+{
+ int err;
+
+ perf_clang__init();
+ err = perf_clang__compile_bpf(filename, obj_buf, obj_buf_sz);
+ perf_clang__cleanup();
+ if (err) {
+ pr_debug("bpf: builtin compilation failed: %d, try external compiler\n", err);
+ err = llvm__compile_bpf(filename, obj_buf, obj_buf_sz);
+ if (err)
+ return -1;
+ } else {
+ pr_debug("bpf: successfull builtin compilation\n");
+ }
+
+ return 0;
+}
+
+int bpf__compile(const char *filename)
+{
+ void *obj_buf;
+ size_t obj_buf_sz;
+
+ if (compile(filename, &obj_buf, &obj_buf_sz))
+ return -1;
+
+ /* make llvm__dump_obj to return error value */
+ llvm__dump_obj(filename, obj_buf, obj_buf_sz);
+ return 0;
+}
+
struct bpf_object *bpf__prepare_load(const char *filename, bool source)
{
struct bpf_object *obj;
@@ -86,20 +118,12 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
}
if (source) {
- int err;
void *obj_buf;
size_t obj_buf_sz;
- perf_clang__init();
- err = perf_clang__compile_bpf(filename, &obj_buf, &obj_buf_sz);
- perf_clang__cleanup();
- if (err) {
- pr_debug("bpf: builtin compilation failed: %d, try external compiler\n", err);
- err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz);
- if (err)
- return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
- } else
- pr_debug("bpf: successfull builtin compilation\n");
+ if (compile(filename, &obj_buf, &obj_buf_sz))
+ return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
+
obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
if (!IS_ERR(obj) && llvm_param.dump_obj)
diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h
index 5d3aefd6fae7..8c2a494cab53 100644
--- a/tools/perf/util/bpf-loader.h
+++ b/tools/perf/util/bpf-loader.h
@@ -58,6 +58,8 @@ int bpf__strerror_prepare_load(const char *filename, bool source,
struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
const char *name);
+int bpf__compile(const char *filename);
+
void bpf__clear(void);
int bpf__probe(struct bpf_object *obj);
--
2.13.6