Re: [PATCH v3 03/30] tools lib bpf: Retrive bpf_map through offset of bpf_map_def

2016-11-26 Thread Alexei Starovoitov
On Sat, Nov 26, 2016 at 07:03:27AM +, Wang Nan wrote:
> Add a new API to libbpf, caller is able to get bpf_map through the
> offset of bpf_map_def to 'maps' section.
> 
> The API will be used to help jitted perf hook code find fd of a map.
> 
> Signed-off-by: Wang Nan 
...
> +struct bpf_map *
> +bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
> +{
> + int i;
> +
> + for (i = 0; i < obj->nr_maps; i++) {
> + if (obj->maps[i].offset == offset)
> + return &obj->maps[i];
> + }
> + return ERR_PTR(-ENOENT);
> +}

Acked-by: Alexei Starovoitov 



[PATCH v3 03/30] tools lib bpf: Retrive bpf_map through offset of bpf_map_def

2016-11-25 Thread Wang Nan
Add a new API to libbpf, caller is able to get bpf_map through the
offset of bpf_map_def to 'maps' section.

The API will be used to help jitted perf hook code find fd of a map.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/lib/bpf/libbpf.c | 12 
 tools/lib/bpf/libbpf.h |  8 
 2 files changed, 20 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 866d5cd..2e97459 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1524,3 +1524,15 @@ bpf_object__find_map_by_name(struct bpf_object *obj, 
const char *name)
}
return NULL;
 }
+
+struct bpf_map *
+bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
+{
+   int i;
+
+   for (i = 0; i < obj->nr_maps; i++) {
+   if (obj->maps[i].offset == offset)
+   return &obj->maps[i];
+   }
+   return ERR_PTR(-ENOENT);
+}
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 0c0b012..a5a8b86 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -24,6 +24,7 @@
 #include 
 #include 
 #include 
+#include   // for size_t
 
 enum libbpf_errno {
__LIBBPF_ERRNO__START = 4000,
@@ -200,6 +201,13 @@ struct bpf_map;
 struct bpf_map *
 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
 
+/*
+ * Get bpf_map through the offset of corresponding struct bpf_map_def
+ * in the bpf object file.
+ */
+struct bpf_map *
+bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
+
 struct bpf_map *
 bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
 #define bpf_map__for_each(pos, obj)\
-- 
2.10.1