1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/compiler.h> 3 #include <linux/types.h> 4 #include <linux/zalloc.h> 5 #include <inttypes.h> 6 #include <limits.h> 7 #include <unistd.h> 8 #include "tests.h" 9 #include "debug.h" 10 #include "machine.h" 11 #include "event.h" 12 #include "../util/unwind.h" 13 #include "perf_regs.h" 14 #include "map.h" 15 #include "symbol.h" 16 #include "thread.h" 17 #include "callchain.h" 18 19 #if defined (__x86_64__) || defined (__i386__) || defined (__powerpc__) 20 #include "arch-tests.h" 21 #endif 22 23 /* For bsearch. We try to unwind functions in shared object. */ 24 #include <stdlib.h> 25 26 static int mmap_handler(struct perf_tool *tool __maybe_unused, 27 union perf_event *event, 28 struct perf_sample *sample, 29 struct machine *machine) 30 { 31 return machine__process_mmap2_event(machine, event, sample); 32 } 33 34 static int init_live_machine(struct machine *machine) 35 { 36 union perf_event event; 37 pid_t pid = getpid(); 38 39 return perf_event__synthesize_mmap_events(NULL, &event, pid, pid, 40 mmap_handler, machine, true); 41 } 42 43 /* 44 * We need to keep these functions global, despite the 45 * fact that they are used only locally in this object, 46 * in order to keep them around even if the binary is 47 * stripped. If they are gone, the unwind check for 48 * symbol fails. 49 */ 50 int test_dwarf_unwind__thread(struct thread *thread); 51 int test_dwarf_unwind__compare(void *p1, void *p2); 52 int test_dwarf_unwind__krava_3(struct thread *thread); 53 int test_dwarf_unwind__krava_2(struct thread *thread); 54 int test_dwarf_unwind__krava_1(struct thread *thread); 55 56 #define MAX_STACK 8 57 58 static int unwind_entry(struct unwind_entry *entry, void *arg) 59 { 60 unsigned long *cnt = (unsigned long *) arg; 61 char *symbol = entry->sym ? entry->sym->name : NULL; 62 static const char *funcs[MAX_STACK] = { 63 "test__arch_unwind_sample", 64 "test_dwarf_unwind__thread", 65 "test_dwarf_unwind__compare", 66 "bsearch", 67 "test_dwarf_unwind__krava_3", 68 "test_dwarf_unwind__krava_2", 69 "test_dwarf_unwind__krava_1", 70 "test__dwarf_unwind" 71 }; 72 /* 73 * The funcs[MAX_STACK] array index, based on the 74 * callchain order setup. 75 */ 76 int idx = callchain_param.order == ORDER_CALLER ? 77 MAX_STACK - *cnt - 1 : *cnt; 78 79 if (*cnt >= MAX_STACK) { 80 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK); 81 return -1; 82 } 83 84 if (!symbol) { 85 pr_debug("failed: got unresolved address 0x%" PRIx64 "\n", 86 entry->ip); 87 return -1; 88 } 89 90 (*cnt)++; 91 pr_debug("got: %s 0x%" PRIx64 ", expecting %s\n", 92 symbol, entry->ip, funcs[idx]); 93 return strcmp((const char *) symbol, funcs[idx]); 94 } 95 96 noinline int test_dwarf_unwind__thread(struct thread *thread) 97 { 98 struct perf_sample sample; 99 unsigned long cnt = 0; 100 int err = -1; 101 102 memset(&sample, 0, sizeof(sample)); 103 104 if (test__arch_unwind_sample(&sample, thread)) { 105 pr_debug("failed to get unwind sample\n"); 106 goto out; 107 } 108 109 err = unwind__get_entries(unwind_entry, &cnt, thread, 110 &sample, MAX_STACK); 111 if (err) 112 pr_debug("unwind failed\n"); 113 else if (cnt != MAX_STACK) { 114 pr_debug("got wrong number of stack entries %lu != %d\n", 115 cnt, MAX_STACK); 116 err = -1; 117 } 118 119 out: 120 zfree(&sample.user_stack.data); 121 zfree(&sample.user_regs.regs); 122 return err; 123 } 124 125 static int global_unwind_retval = -INT_MAX; 126 127 noinline int test_dwarf_unwind__compare(void *p1, void *p2) 128 { 129 /* Any possible value should be 'thread' */ 130 struct thread *thread = *(struct thread **)p1; 131 132 if (global_unwind_retval == -INT_MAX) { 133 /* Call unwinder twice for both callchain orders. */ 134 callchain_param.order = ORDER_CALLER; 135 136 global_unwind_retval = test_dwarf_unwind__thread(thread); 137 if (!global_unwind_retval) { 138 callchain_param.order = ORDER_CALLEE; 139 global_unwind_retval = test_dwarf_unwind__thread(thread); 140 } 141 } 142 143 return p1 - p2; 144 } 145 146 noinline int test_dwarf_unwind__krava_3(struct thread *thread) 147 { 148 struct thread *array[2] = {thread, thread}; 149 void *fp = &bsearch; 150 /* 151 * make _bsearch a volatile function pointer to 152 * prevent potential optimization, which may expand 153 * bsearch and call compare directly from this function, 154 * instead of libc shared object. 155 */ 156 void *(*volatile _bsearch)(void *, void *, size_t, 157 size_t, int (*)(void *, void *)); 158 159 _bsearch = fp; 160 _bsearch(array, &thread, 2, sizeof(struct thread **), 161 test_dwarf_unwind__compare); 162 return global_unwind_retval; 163 } 164 165 noinline int test_dwarf_unwind__krava_2(struct thread *thread) 166 { 167 return test_dwarf_unwind__krava_3(thread); 168 } 169 170 noinline int test_dwarf_unwind__krava_1(struct thread *thread) 171 { 172 return test_dwarf_unwind__krava_2(thread); 173 } 174 175 int test__dwarf_unwind(struct test *test __maybe_unused, int subtest __maybe_unused) 176 { 177 struct machine *machine; 178 struct thread *thread; 179 int err = -1; 180 181 machine = machine__new_host(); 182 if (!machine) { 183 pr_err("Could not get machine\n"); 184 return -1; 185 } 186 187 if (machine__create_kernel_maps(machine)) { 188 pr_err("Failed to create kernel maps\n"); 189 return -1; 190 } 191 192 callchain_param.record_mode = CALLCHAIN_DWARF; 193 dwarf_callchain_users = true; 194 195 if (init_live_machine(machine)) { 196 pr_err("Could not init machine\n"); 197 goto out; 198 } 199 200 if (verbose > 1) 201 machine__fprintf(machine, stderr); 202 203 thread = machine__find_thread(machine, getpid(), getpid()); 204 if (!thread) { 205 pr_err("Could not get thread\n"); 206 goto out; 207 } 208 209 err = test_dwarf_unwind__krava_1(thread); 210 thread__put(thread); 211 212 out: 213 machine__delete_threads(machine); 214 machine__delete(machine); 215 return err; 216 } 217