libbpf: Allow decimal offset for kprobes
authorJiri Olsa <jolsa@redhat.com>
Wed, 21 Jul 2021 21:58:09 +0000 (23:58 +0200)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 23 Jul 2021 03:09:16 +0000 (20:09 -0700)
Allow to specify decimal offset in SEC macro, like:
  SEC("kprobe/bpf_fentry_test7+5")

Add selftest for that.

Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
Tested-by: Alan Maguire <alan.maguire@oracle.com>
Link: https://lore.kernel.org/bpf/20210721215810.889975-3-jolsa@kernel.org
tools/lib/bpf/libbpf.c
tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c
tools/testing/selftests/bpf/progs/get_func_ip_test.c

index d46c2dd37be279ac72f002ded63744ed3d14d508..52f4f1d4f49501afd7a8860cf682cfb318271fb3 100644 (file)
@@ -10424,7 +10424,7 @@ static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec,
        func_name = prog->sec_name + sec->len;
        opts.retprobe = strcmp(sec->sec, "kretprobe/") == 0;
 
-       n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%lx", &func, &offset);
+       n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset);
        if (n < 1) {
                err = -EINVAL;
                pr_warn("kprobe name is invalid: %s\n", func_name);
index 088b3653610d28ce59bb31a5cc5d0ef2397bfc65..02a465f36d5941f7ae0d61651a299e053e292878 100644 (file)
@@ -17,6 +17,7 @@ void test_get_func_ip_test(void)
         */
 #ifndef __x86_64__
        bpf_program__set_autoload(skel->progs.test6, false);
+       bpf_program__set_autoload(skel->progs.test7, false);
 #endif
 
        err = get_func_ip_test__load(skel);
@@ -46,6 +47,7 @@ void test_get_func_ip_test(void)
        ASSERT_EQ(skel->bss->test5_result, 1, "test5_result");
 #ifdef __x86_64__
        ASSERT_EQ(skel->bss->test6_result, 1, "test6_result");
+       ASSERT_EQ(skel->bss->test7_result, 1, "test7_result");
 #endif
 
 cleanup:
index acd587b6e859d41aa894a2e069c93e91d6160f34..a587aeca5ae0848b759a2cfc95a22a0a564e87e5 100644 (file)
@@ -11,6 +11,7 @@ extern const void bpf_fentry_test3 __ksym;
 extern const void bpf_fentry_test4 __ksym;
 extern const void bpf_modify_return_test __ksym;
 extern const void bpf_fentry_test6 __ksym;
+extern const void bpf_fentry_test7 __ksym;
 
 __u64 test1_result = 0;
 SEC("fentry/bpf_fentry_test1")
@@ -71,3 +72,13 @@ int test6(struct pt_regs *ctx)
        test6_result = (const void *) addr == &bpf_fentry_test6 + 5;
        return 0;
 }
+
+__u64 test7_result = 0;
+SEC("kprobe/bpf_fentry_test7+5")
+int test7(struct pt_regs *ctx)
+{
+       __u64 addr = bpf_get_func_ip(ctx);
+
+       test7_result = (const void *) addr == &bpf_fentry_test7 + 5;
+       return 0;
+}