bpf tools: New API to get name from a BPF object
authorWang Nan <wangnan0@huawei.com>
Thu, 27 Aug 2015 02:30:55 +0000 (02:30 +0000)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 31 Aug 2015 19:53:15 +0000 (16:53 -0300)
Before this patch there's no way to connect a loaded bpf object
to its source file. However, during applying perf's '--filter' to BPF
object, without this connection makes things harder, because perf loads
all programs together, but '--filter' setting is for each object.

The API of bpf_object__open_buffer() is changed to allow passing a name.
Fortunately, at this time there's only one user of it (perf test LLVM),
so we change it together.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1440742821-44548-2-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf.h
tools/perf/tests/llvm.c

index 4fa4bc4505f5b5ecec02758efe3af453f49ec01a..4252fc22f78f4f8fd4c204975b5464f5bbc192e0 100644 (file)
@@ -880,15 +880,26 @@ struct bpf_object *bpf_object__open(const char *path)
 }
 
 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
-                                          size_t obj_buf_sz)
+                                          size_t obj_buf_sz,
+                                          const char *name)
 {
+       char tmp_name[64];
+
        /* param validation */
        if (!obj_buf || obj_buf_sz <= 0)
                return NULL;
 
-       pr_debug("loading object from buffer\n");
+       if (!name) {
+               snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
+                        (unsigned long)obj_buf,
+                        (unsigned long)obj_buf_sz);
+               tmp_name[sizeof(tmp_name) - 1] = '\0';
+               name = tmp_name;
+       }
+       pr_debug("loading object '%s' from buffer\n",
+                name);
 
-       return __bpf_object__open("[buffer]", obj_buf, obj_buf_sz);
+       return __bpf_object__open(name, obj_buf, obj_buf_sz);
 }
 
 int bpf_object__unload(struct bpf_object *obj)
@@ -975,6 +986,14 @@ bpf_object__next(struct bpf_object *prev)
        return next;
 }
 
+const char *
+bpf_object__get_name(struct bpf_object *obj)
+{
+       if (!obj)
+               return NULL;
+       return obj->path;
+}
+
 struct bpf_program *
 bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
 {
index ea8adc206b6242ec08dfcc0659b839f7cc79145d..f16170c95ffdc2306aa02f836092661b2673408e 100644 (file)
@@ -28,12 +28,14 @@ struct bpf_object;
 
 struct bpf_object *bpf_object__open(const char *path);
 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
-                                          size_t obj_buf_sz);
+                                          size_t obj_buf_sz,
+                                          const char *name);
 void bpf_object__close(struct bpf_object *object);
 
 /* Load/unload object into/from kernel */
 int bpf_object__load(struct bpf_object *obj);
 int bpf_object__unload(struct bpf_object *obj);
+const char *bpf_object__get_name(struct bpf_object *obj);
 
 struct bpf_object *bpf_object__next(struct bpf_object *prev);
 #define bpf_object__for_each_safe(pos, tmp)                    \
index a337356fd979002eb94a657f52feb1122e360722..52d55971f66f6f569db38db00b87101e7aa61507 100644 (file)
@@ -26,7 +26,7 @@ static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
 {
        struct bpf_object *obj;
 
-       obj = bpf_object__open_buffer(obj_buf, obj_buf_sz);
+       obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
        if (!obj)
                return -1;
        bpf_object__close(obj);