[RFC PATCH 10/22] perf bpf: collect config section in object.

Wang Nan
2015-04-30T10:53:38Z

eBPF programs are allowed to include 'config' sections, which contain
strings describe each bpf program. Config strings have identical format
as 'perf probe' defined. They describe positions and arguments used by
bpf programs.

Defining config strings separatly is allowed. For example:

#define SEC(NAME) __attribute__((section(NAME), used))
char _bpf_prog1_config[] SEC("config") = "bpf_prog1=kmem_cache_free%return";
SEC("bpf_prog1")
int bpf_prog1(struct pt_rets *ctx) {
....
}
char _bpf_prog2_config[] SEC("config") = "bpf_prog2=kmem_cache_free";
SEC("bpf_prog2")
int bpf_prog2(struct pt_rets *ctx) {
....
}
char other_config[] SEC("config") = "bpf_prog3=kmem_cache_alloc\n"
"bpf_prog4=__alloc_pages_nodemask%return";

To make further processing easiler, this patch converts '\0' in the
whole config strings into '\n'

Signed-off-by: Wang Nan <wangnan0@huawei.com>
---
tools/perf/util/bpf-loader.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
tools/perf/util/bpf-loader.h | 1 +
2 files changed, 45 insertions(+)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index bf3b793..b913d6f 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -67,6 +67,8 @@ static void bpf_obj_close(struct bpf_obj *obj)
free(obj->path);
if (obj->maps)
free(obj->maps);
+ if (obj->config_str)
+ free(obj->config_str);
free(obj);
}

@@ -211,6 +213,45 @@ static int bpf_obj_maps_init(struct bpf_obj *obj, void *data,
return 0;
}

+static int bpf_obj_config_init(struct bpf_obj *obj, void *data,
+ size_t size)
+{
+ char *config_str;
+ char *p, *pend;
+
+ if (size == 0) {
+ pr_debug("bpf: config section in %s empty\n",
+ obj->path);
+ return 0;
+ }
+ if (obj->config_str) {
+ pr_err("bpf: multiple config section in %s\n",
+ obj->path);
+ return -EEXIST;
+ }
+
+ config_str = malloc(size + 1);
+ if (!config_str) {
+ pr_err("bpf: malloc config string failed\n");
+ return -ENOMEM;
+ }
+
+ memcpy(config_str, data, size);
+
+ /*
+ * It is possible that config section contains multiple
+ * Make it a big string by converting all '\0' to '\n' and
+ * append final '\0'.
+ */
+ pend = config_str + size;
+ for (p = config_str; p < pend; p++)
+ *p == '\0' ? *p = '\n' : 0 ;
+ *pend = '\0';
+
+ obj->config_str = config_str;
+ return 0;
+}
+
static int bpf_obj_elf_collect(struct bpf_obj *obj)
{
Elf *elf = obj->elf.elf;
@@ -268,6 +309,9 @@ static int bpf_obj_elf_collect(struct bpf_obj *obj)
else if (strcmp(name, "maps") == 0)
err = bpf_obj_maps_init(obj, data->d_buf,
data->d_size);
+ else if (strcmp(name, "config") == 0)
+ err = bpf_obj_config_init(obj, data->d_buf,
+ data->d_size);
if (err)
goto out;
}
diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h
index 6c5c8d6..086f28d 100644
--- a/tools/perf/util/bpf-loader.h
+++ b/tools/perf/util/bpf-loader.h
@@ -28,6 +28,7 @@ struct bpf_obj {
u32 kern_version;
struct bpf_map_def *maps;
size_t nr_maps;
+ char *config_str;

/*
* Information when doing elf related work. Only valid if fd
--
1.8.3.4