Commit 01af3bf0 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: Expose BPF program's function name

Add APIs to get BPF program function name, as opposed to bpf_program__title(),
which returns BPF program function's section name. Function name has a benefit
of being a valid C identifier and uniquely identifies a specific BPF program,
while section name can be duplicated across multiple independent BPF programs.

Add also bpf_object__find_program_by_name(), similar to
bpf_object__find_program_by_title(), to facilitate looking up BPF programs by
their C function names.

Convert one of selftests to new API for look up.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20191214014341.3442258-9-andriin@fb.com
parent 9f81654e
...@@ -209,8 +209,8 @@ static const char * const libbpf_type_to_btf_name[] = { ...@@ -209,8 +209,8 @@ static const char * const libbpf_type_to_btf_name[] = {
}; };
struct bpf_map { struct bpf_map {
int fd;
char *name; char *name;
int fd;
int sec_idx; int sec_idx;
size_t sec_offset; size_t sec_offset;
int map_ifindex; int map_ifindex;
...@@ -1384,7 +1384,7 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict, ...@@ -1384,7 +1384,7 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
} }
static int bpf_object__init_maps(struct bpf_object *obj, static int bpf_object__init_maps(struct bpf_object *obj,
struct bpf_object_open_opts *opts) const struct bpf_object_open_opts *opts)
{ {
const char *pin_root_path = OPTS_GET(opts, pin_root_path, NULL); const char *pin_root_path = OPTS_GET(opts, pin_root_path, NULL);
bool strict = !OPTS_GET(opts, relaxed_maps, false); bool strict = !OPTS_GET(opts, relaxed_maps, false);
...@@ -1748,6 +1748,19 @@ bpf_object__find_program_by_title(const struct bpf_object *obj, ...@@ -1748,6 +1748,19 @@ bpf_object__find_program_by_title(const struct bpf_object *obj,
return NULL; return NULL;
} }
struct bpf_program *
bpf_object__find_program_by_name(const struct bpf_object *obj,
const char *name)
{
struct bpf_program *prog;
bpf_object__for_each_program(prog, obj) {
if (!strcmp(prog->name, name))
return prog;
}
return NULL;
}
static bool bpf_object__shndx_is_data(const struct bpf_object *obj, static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
int shndx) int shndx)
{ {
...@@ -3894,7 +3907,7 @@ static int libbpf_find_attach_btf_id(const char *name, ...@@ -3894,7 +3907,7 @@ static int libbpf_find_attach_btf_id(const char *name,
__u32 attach_prog_fd); __u32 attach_prog_fd);
static struct bpf_object * static struct bpf_object *
__bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz, __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
struct bpf_object_open_opts *opts) const struct bpf_object_open_opts *opts)
{ {
struct bpf_program *prog; struct bpf_program *prog;
struct bpf_object *obj; struct bpf_object *obj;
...@@ -4003,7 +4016,7 @@ struct bpf_object *bpf_object__open(const char *path) ...@@ -4003,7 +4016,7 @@ struct bpf_object *bpf_object__open(const char *path)
} }
struct bpf_object * struct bpf_object *
bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts) bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts)
{ {
if (!path) if (!path)
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
...@@ -4015,7 +4028,7 @@ bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts) ...@@ -4015,7 +4028,7 @@ bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts)
struct bpf_object * struct bpf_object *
bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
struct bpf_object_open_opts *opts) const struct bpf_object_open_opts *opts)
{ {
if (!obj_buf || obj_buf_sz == 0) if (!obj_buf || obj_buf_sz == 0)
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
...@@ -4820,6 +4833,11 @@ void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex) ...@@ -4820,6 +4833,11 @@ void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
prog->prog_ifindex = ifindex; prog->prog_ifindex = ifindex;
} }
const char *bpf_program__name(const struct bpf_program *prog)
{
return prog->name;
}
const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy) const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy)
{ {
const char *title; const char *title;
......
...@@ -90,10 +90,10 @@ struct bpf_object_open_opts { ...@@ -90,10 +90,10 @@ struct bpf_object_open_opts {
LIBBPF_API struct bpf_object *bpf_object__open(const char *path); LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
LIBBPF_API struct bpf_object * LIBBPF_API struct bpf_object *
bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts); bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
LIBBPF_API struct bpf_object * LIBBPF_API struct bpf_object *
bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz, bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
struct bpf_object_open_opts *opts); const struct bpf_object_open_opts *opts);
/* deprecated bpf_object__open variants */ /* deprecated bpf_object__open variants */
LIBBPF_API struct bpf_object * LIBBPF_API struct bpf_object *
...@@ -132,6 +132,7 @@ struct bpf_object_load_attr { ...@@ -132,6 +132,7 @@ struct bpf_object_load_attr {
LIBBPF_API int bpf_object__load(struct bpf_object *obj); LIBBPF_API int bpf_object__load(struct bpf_object *obj);
LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr); LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
LIBBPF_API int bpf_object__unload(struct bpf_object *obj); LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj); LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj); LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
...@@ -142,6 +143,9 @@ LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj); ...@@ -142,6 +143,9 @@ LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
LIBBPF_API struct bpf_program * LIBBPF_API struct bpf_program *
bpf_object__find_program_by_title(const struct bpf_object *obj, bpf_object__find_program_by_title(const struct bpf_object *obj,
const char *title); const char *title);
LIBBPF_API struct bpf_program *
bpf_object__find_program_by_name(const struct bpf_object *obj,
const char *name);
LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev); LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
#define bpf_object__for_each_safe(pos, tmp) \ #define bpf_object__for_each_safe(pos, tmp) \
...@@ -185,6 +189,7 @@ LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog); ...@@ -185,6 +189,7 @@ LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog, LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
__u32 ifindex); __u32 ifindex);
LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog, LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog,
bool needs_copy); bool needs_copy);
......
...@@ -212,6 +212,8 @@ LIBBPF_0.0.6 { ...@@ -212,6 +212,8 @@ LIBBPF_0.0.6 {
LIBBPF_0.0.7 { LIBBPF_0.0.7 {
global: global:
btf_dump__emit_type_decl; btf_dump__emit_type_decl;
bpf_object__find_program_by_name;
bpf_program__attach; bpf_program__attach;
bpf_program__name;
btf__align_of; btf__align_of;
} LIBBPF_0.0.6; } LIBBPF_0.0.6;
...@@ -16,14 +16,11 @@ struct rdonly_map_subtest { ...@@ -16,14 +16,11 @@ struct rdonly_map_subtest {
void test_rdonly_maps(void) void test_rdonly_maps(void)
{ {
const char *prog_name_skip_loop = "raw_tracepoint/sys_enter:skip_loop";
const char *prog_name_part_loop = "raw_tracepoint/sys_enter:part_loop";
const char *prog_name_full_loop = "raw_tracepoint/sys_enter:full_loop";
const char *file = "test_rdonly_maps.o"; const char *file = "test_rdonly_maps.o";
struct rdonly_map_subtest subtests[] = { struct rdonly_map_subtest subtests[] = {
{ "skip loop", prog_name_skip_loop, 0, 0 }, { "skip loop", "skip_loop", 0, 0 },
{ "part loop", prog_name_part_loop, 3, 2 + 3 + 4 }, { "part loop", "part_loop", 3, 2 + 3 + 4 },
{ "full loop", prog_name_full_loop, 4, 2 + 3 + 4 + 5 }, { "full loop", "full_loop", 4, 2 + 3 + 4 + 5 },
}; };
int i, err, zero = 0, duration = 0; int i, err, zero = 0, duration = 0;
struct bpf_link *link = NULL; struct bpf_link *link = NULL;
...@@ -50,7 +47,7 @@ void test_rdonly_maps(void) ...@@ -50,7 +47,7 @@ void test_rdonly_maps(void)
if (!test__start_subtest(t->subtest_name)) if (!test__start_subtest(t->subtest_name))
continue; continue;
prog = bpf_object__find_program_by_title(obj, t->prog_name); prog = bpf_object__find_program_by_name(obj, t->prog_name);
if (CHECK(!prog, "find_prog", "prog '%s' not found\n", if (CHECK(!prog, "find_prog", "prog '%s' not found\n",
t->prog_name)) t->prog_name))
goto cleanup; goto cleanup;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment