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