1 // SPDX-License-Identifier: GPL-2.0 2 #include <test_progs.h> 3 4 void test_stacktrace_build_id(void) 5 { 6 int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd; 7 const char *prog_name = "tracepoint/random/urandom_read"; 8 const char *file = "./test_stacktrace_build_id.o"; 9 int err, prog_fd, stack_trace_len; 10 __u32 key, previous_key, val, duration = 0; 11 struct bpf_program *prog; 12 struct bpf_object *obj; 13 struct bpf_link *link = NULL; 14 char buf[256]; 15 int i, j; 16 struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH]; 17 int build_id_matches = 0; 18 int retry = 1; 19 20 retry: 21 err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); 22 if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno)) 23 return; 24 25 prog = bpf_object__find_program_by_title(obj, prog_name); 26 if (CHECK(!prog, "find_prog", "prog '%s' not found\n", prog_name)) 27 goto close_prog; 28 29 link = bpf_program__attach_tracepoint(prog, "random", "urandom_read"); 30 if (CHECK(IS_ERR(link), "attach_tp", "err %ld\n", PTR_ERR(link))) 31 goto close_prog; 32 33 /* find map fds */ 34 control_map_fd = bpf_find_map(__func__, obj, "control_map"); 35 if (CHECK(control_map_fd < 0, "bpf_find_map control_map", 36 "err %d errno %d\n", err, errno)) 37 goto disable_pmu; 38 39 stackid_hmap_fd = bpf_find_map(__func__, obj, "stackid_hmap"); 40 if (CHECK(stackid_hmap_fd < 0, "bpf_find_map stackid_hmap", 41 "err %d errno %d\n", err, errno)) 42 goto disable_pmu; 43 44 stackmap_fd = bpf_find_map(__func__, obj, "stackmap"); 45 if (CHECK(stackmap_fd < 0, "bpf_find_map stackmap", "err %d errno %d\n", 46 err, errno)) 47 goto disable_pmu; 48 49 stack_amap_fd = bpf_find_map(__func__, obj, "stack_amap"); 50 if (CHECK(stack_amap_fd < 0, "bpf_find_map stack_amap", 51 "err %d errno %d\n", err, errno)) 52 goto disable_pmu; 53 54 if (CHECK_FAIL(system("dd if=/dev/urandom of=/dev/zero count=4 2> /dev/null"))) 55 goto disable_pmu; 56 if (CHECK_FAIL(system("./urandom_read"))) 57 goto disable_pmu; 58 /* disable stack trace collection */ 59 key = 0; 60 val = 1; 61 bpf_map_update_elem(control_map_fd, &key, &val, 0); 62 63 /* for every element in stackid_hmap, we can find a corresponding one 64 * in stackmap, and vise versa. 65 */ 66 err = compare_map_keys(stackid_hmap_fd, stackmap_fd); 67 if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap", 68 "err %d errno %d\n", err, errno)) 69 goto disable_pmu; 70 71 err = compare_map_keys(stackmap_fd, stackid_hmap_fd); 72 if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap", 73 "err %d errno %d\n", err, errno)) 74 goto disable_pmu; 75 76 err = extract_build_id(buf, 256); 77 78 if (CHECK(err, "get build_id with readelf", 79 "err %d errno %d\n", err, errno)) 80 goto disable_pmu; 81 82 err = bpf_map_get_next_key(stackmap_fd, NULL, &key); 83 if (CHECK(err, "get_next_key from stackmap", 84 "err %d, errno %d\n", err, errno)) 85 goto disable_pmu; 86 87 do { 88 char build_id[64]; 89 90 err = bpf_map_lookup_elem(stackmap_fd, &key, id_offs); 91 if (CHECK(err, "lookup_elem from stackmap", 92 "err %d, errno %d\n", err, errno)) 93 goto disable_pmu; 94 for (i = 0; i < PERF_MAX_STACK_DEPTH; ++i) 95 if (id_offs[i].status == BPF_STACK_BUILD_ID_VALID && 96 id_offs[i].offset != 0) { 97 for (j = 0; j < 20; ++j) 98 sprintf(build_id + 2 * j, "%02x", 99 id_offs[i].build_id[j] & 0xff); 100 if (strstr(buf, build_id) != NULL) 101 build_id_matches = 1; 102 } 103 previous_key = key; 104 } while (bpf_map_get_next_key(stackmap_fd, &previous_key, &key) == 0); 105 106 /* stack_map_get_build_id_offset() is racy and sometimes can return 107 * BPF_STACK_BUILD_ID_IP instead of BPF_STACK_BUILD_ID_VALID; 108 * try it one more time. 109 */ 110 if (build_id_matches < 1 && retry--) { 111 bpf_link__destroy(link); 112 bpf_object__close(obj); 113 printf("%s:WARN:Didn't find expected build ID from the map, retrying\n", 114 __func__); 115 goto retry; 116 } 117 118 if (CHECK(build_id_matches < 1, "build id match", 119 "Didn't find expected build ID from the map\n")) 120 goto disable_pmu; 121 122 stack_trace_len = PERF_MAX_STACK_DEPTH 123 * sizeof(struct bpf_stack_build_id); 124 err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len); 125 CHECK(err, "compare_stack_ips stackmap vs. stack_amap", 126 "err %d errno %d\n", err, errno); 127 128 disable_pmu: 129 bpf_link__destroy(link); 130 131 close_prog: 132 bpf_object__close(obj); 133 } 134