From: fallentree <fallentree@xxxxxx> Tracking: libbpf/libbpf#291 Signed-off-by: Yucong Sun <fallentree@xxxxxx> --- tools/lib/bpf/libbpf.c | 10 ++++++++++ tools/lib/bpf/libbpf.h | 1 + tools/lib/bpf/libbpf.map | 1 + .../selftests/bpf/prog_tests/reference_tracking.c | 7 +++++++ 4 files changed, 19 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index a1ca6fb0c6d8..654fa638743f 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -7545,6 +7545,16 @@ const char *bpf_object__name(const struct bpf_object *obj) return obj ? obj->name : libbpf_err_ptr(-EINVAL); } +int bpf_object__set_name(struct bpf_object *obj, const char* name) +{ + if (!name) + return libbpf_err(-EINVAL); + + strncpy(obj->name, name, sizeof(obj->name) - 1); + + return 0; +} + unsigned int bpf_object__kversion(const struct bpf_object *obj) { return obj ? obj->kern_version : 0; diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 1271d99bb7aa..7d28c7c600d4 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -161,6 +161,7 @@ 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 const char *bpf_object__name(const struct bpf_object *obj); +LIBBPF_API int bpf_object__set_name(struct bpf_object *obj, const char* name); LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj); LIBBPF_API int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index c240d488eb5e..3c15aefeb6e0 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -377,4 +377,5 @@ LIBBPF_0.5.0 { bpf_object__gen_loader; btf_dump__dump_type_data; libbpf_set_strict_mode; + bpf_object__set_name; } LIBBPF_0.4.0; diff --git a/tools/testing/selftests/bpf/prog_tests/reference_tracking.c b/tools/testing/selftests/bpf/prog_tests/reference_tracking.c index de2688166696..4d3d0a4aec03 100644 --- a/tools/testing/selftests/bpf/prog_tests/reference_tracking.c +++ b/tools/testing/selftests/bpf/prog_tests/reference_tracking.c @@ -5,6 +5,7 @@ void test_reference_tracking(void) { const char *file = "test_sk_lookup_kern.o"; const char *obj_name = "ref_track"; + const char *obj_name2 = "ref_track2"; DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts, .object_name = obj_name, .relaxed_maps = true, @@ -23,6 +24,12 @@ void test_reference_tracking(void) bpf_object__name(obj), obj_name)) goto cleanup; + bpf_object__set_name(obj, obj_name2); + if (CHECK(strcmp(bpf_object__name(obj), obj_name2), "obj_name", + "wrong obj name '%s', expected '%s'\n", + bpf_object__name(obj), obj_name2)) + goto cleanup; + bpf_object__for_each_program(prog, obj) { const char *title; -- 2.30.2