Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 1 | #include <linux/compiler.h> |
Borislav Petkov | d944c4e | 2014-04-25 21:31:02 +0200 | [diff] [blame] | 2 | #include <linux/types.h> |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 3 | #include <unistd.h> |
| 4 | #include "tests.h" |
| 5 | #include "debug.h" |
| 6 | #include "machine.h" |
| 7 | #include "event.h" |
| 8 | #include "unwind.h" |
| 9 | #include "perf_regs.h" |
| 10 | #include "map.h" |
| 11 | #include "thread.h" |
Namhyung Kim | 0cdccac | 2014-10-06 09:45:59 +0900 | [diff] [blame] | 12 | #include "callchain.h" |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 13 | |
Matt Fleming | d8b167f | 2015-10-05 15:40:20 +0100 | [diff] [blame] | 14 | #if defined (__x86_64__) || defined (__i386__) |
| 15 | #include "arch-tests.h" |
| 16 | #endif |
| 17 | |
Wang Nan | b93b096 | 2015-01-14 10:36:47 +0800 | [diff] [blame] | 18 | /* For bsearch. We try to unwind functions in shared object. */ |
| 19 | #include <stdlib.h> |
| 20 | |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 21 | static int mmap_handler(struct perf_tool *tool __maybe_unused, |
| 22 | union perf_event *event, |
| 23 | struct perf_sample *sample __maybe_unused, |
| 24 | struct machine *machine) |
| 25 | { |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 26 | return machine__process_mmap2_event(machine, event, NULL); |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | static int init_live_machine(struct machine *machine) |
| 30 | { |
| 31 | union perf_event event; |
| 32 | pid_t pid = getpid(); |
| 33 | |
| 34 | return perf_event__synthesize_mmap_events(NULL, &event, pid, pid, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 35 | mmap_handler, machine, true, 500); |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Wang Nan | b93b096 | 2015-01-14 10:36:47 +0800 | [diff] [blame] | 38 | #define MAX_STACK 8 |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 39 | |
| 40 | static int unwind_entry(struct unwind_entry *entry, void *arg) |
| 41 | { |
| 42 | unsigned long *cnt = (unsigned long *) arg; |
| 43 | char *symbol = entry->sym ? entry->sym->name : NULL; |
| 44 | static const char *funcs[MAX_STACK] = { |
| 45 | "test__arch_unwind_sample", |
| 46 | "unwind_thread", |
Wang Nan | b93b096 | 2015-01-14 10:36:47 +0800 | [diff] [blame] | 47 | "compare", |
| 48 | "bsearch", |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 49 | "krava_3", |
| 50 | "krava_2", |
| 51 | "krava_1", |
| 52 | "test__dwarf_unwind" |
| 53 | }; |
Jiri Olsa | 8dc0564 | 2015-11-17 16:05:39 +0100 | [diff] [blame] | 54 | /* |
| 55 | * The funcs[MAX_STACK] array index, based on the |
| 56 | * callchain order setup. |
| 57 | */ |
| 58 | int idx = callchain_param.order == ORDER_CALLER ? |
| 59 | MAX_STACK - *cnt - 1 : *cnt; |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 60 | |
| 61 | if (*cnt >= MAX_STACK) { |
| 62 | pr_debug("failed: crossed the max stack value %d\n", MAX_STACK); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | if (!symbol) { |
| 67 | pr_debug("failed: got unresolved address 0x%" PRIx64 "\n", |
| 68 | entry->ip); |
| 69 | return -1; |
| 70 | } |
| 71 | |
Jiri Olsa | 8dc0564 | 2015-11-17 16:05:39 +0100 | [diff] [blame] | 72 | (*cnt)++; |
| 73 | pr_debug("got: %s 0x%" PRIx64 ", expecting %s\n", |
| 74 | symbol, entry->ip, funcs[idx]); |
| 75 | return strcmp((const char *) symbol, funcs[idx]); |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | __attribute__ ((noinline)) |
Arnaldo Carvalho de Melo | dd8c17a | 2014-10-23 16:42:19 -0300 | [diff] [blame] | 79 | static int unwind_thread(struct thread *thread) |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 80 | { |
| 81 | struct perf_sample sample; |
| 82 | unsigned long cnt = 0; |
| 83 | int err = -1; |
| 84 | |
| 85 | memset(&sample, 0, sizeof(sample)); |
| 86 | |
| 87 | if (test__arch_unwind_sample(&sample, thread)) { |
| 88 | pr_debug("failed to get unwind sample\n"); |
| 89 | goto out; |
| 90 | } |
| 91 | |
Arnaldo Carvalho de Melo | dd8c17a | 2014-10-23 16:42:19 -0300 | [diff] [blame] | 92 | err = unwind__get_entries(unwind_entry, &cnt, thread, |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 93 | &sample, MAX_STACK); |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 94 | if (err) |
| 95 | pr_debug("unwind failed\n"); |
| 96 | else if (cnt != MAX_STACK) { |
| 97 | pr_debug("got wrong number of stack entries %lu != %d\n", |
| 98 | cnt, MAX_STACK); |
| 99 | err = -1; |
| 100 | } |
| 101 | |
| 102 | out: |
| 103 | free(sample.user_stack.data); |
| 104 | free(sample.user_regs.regs); |
| 105 | return err; |
| 106 | } |
| 107 | |
Wang Nan | b93b096 | 2015-01-14 10:36:47 +0800 | [diff] [blame] | 108 | static int global_unwind_retval = -INT_MAX; |
| 109 | |
| 110 | __attribute__ ((noinline)) |
| 111 | static int compare(void *p1, void *p2) |
| 112 | { |
| 113 | /* Any possible value should be 'thread' */ |
| 114 | struct thread *thread = *(struct thread **)p1; |
| 115 | |
Jiri Olsa | 8dc0564 | 2015-11-17 16:05:39 +0100 | [diff] [blame] | 116 | if (global_unwind_retval == -INT_MAX) { |
| 117 | /* Call unwinder twice for both callchain orders. */ |
| 118 | callchain_param.order = ORDER_CALLER; |
| 119 | |
Wang Nan | b93b096 | 2015-01-14 10:36:47 +0800 | [diff] [blame] | 120 | global_unwind_retval = unwind_thread(thread); |
Jiri Olsa | 8dc0564 | 2015-11-17 16:05:39 +0100 | [diff] [blame] | 121 | if (!global_unwind_retval) { |
| 122 | callchain_param.order = ORDER_CALLEE; |
| 123 | global_unwind_retval = unwind_thread(thread); |
| 124 | } |
| 125 | } |
Wang Nan | b93b096 | 2015-01-14 10:36:47 +0800 | [diff] [blame] | 126 | |
| 127 | return p1 - p2; |
| 128 | } |
| 129 | |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 130 | __attribute__ ((noinline)) |
Arnaldo Carvalho de Melo | dd8c17a | 2014-10-23 16:42:19 -0300 | [diff] [blame] | 131 | static int krava_3(struct thread *thread) |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 132 | { |
Wang Nan | b93b096 | 2015-01-14 10:36:47 +0800 | [diff] [blame] | 133 | struct thread *array[2] = {thread, thread}; |
| 134 | void *fp = &bsearch; |
| 135 | /* |
| 136 | * make _bsearch a volatile function pointer to |
| 137 | * prevent potential optimization, which may expand |
| 138 | * bsearch and call compare directly from this function, |
| 139 | * instead of libc shared object. |
| 140 | */ |
| 141 | void *(*volatile _bsearch)(void *, void *, size_t, |
| 142 | size_t, int (*)(void *, void *)); |
| 143 | |
| 144 | _bsearch = fp; |
| 145 | _bsearch(array, &thread, 2, sizeof(struct thread **), compare); |
| 146 | return global_unwind_retval; |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | __attribute__ ((noinline)) |
Arnaldo Carvalho de Melo | dd8c17a | 2014-10-23 16:42:19 -0300 | [diff] [blame] | 150 | static int krava_2(struct thread *thread) |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 151 | { |
Arnaldo Carvalho de Melo | dd8c17a | 2014-10-23 16:42:19 -0300 | [diff] [blame] | 152 | return krava_3(thread); |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | __attribute__ ((noinline)) |
Arnaldo Carvalho de Melo | dd8c17a | 2014-10-23 16:42:19 -0300 | [diff] [blame] | 156 | static int krava_1(struct thread *thread) |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 157 | { |
Arnaldo Carvalho de Melo | dd8c17a | 2014-10-23 16:42:19 -0300 | [diff] [blame] | 158 | return krava_2(thread); |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Arnaldo Carvalho de Melo | 721a1f5 | 2015-11-19 12:01:48 -0300 | [diff] [blame] | 161 | int test__dwarf_unwind(int subtest __maybe_unused) |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 162 | { |
| 163 | struct machines machines; |
| 164 | struct machine *machine; |
| 165 | struct thread *thread; |
| 166 | int err = -1; |
| 167 | |
| 168 | machines__init(&machines); |
| 169 | |
| 170 | machine = machines__find(&machines, HOST_KERNEL_ID); |
| 171 | if (!machine) { |
| 172 | pr_err("Could not get machine\n"); |
| 173 | return -1; |
| 174 | } |
| 175 | |
Jiri Olsa | 9bdcede | 2015-11-27 09:21:21 +0100 | [diff] [blame^] | 176 | if (machine__create_kernel_maps(machine)) { |
| 177 | pr_err("Failed to create kernel maps\n"); |
| 178 | return -1; |
| 179 | } |
| 180 | |
Namhyung Kim | 0cdccac | 2014-10-06 09:45:59 +0900 | [diff] [blame] | 181 | callchain_param.record_mode = CALLCHAIN_DWARF; |
| 182 | |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 183 | if (init_live_machine(machine)) { |
| 184 | pr_err("Could not init machine\n"); |
| 185 | goto out; |
| 186 | } |
| 187 | |
| 188 | if (verbose > 1) |
| 189 | machine__fprintf(machine, stderr); |
| 190 | |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 191 | thread = machine__find_thread(machine, getpid(), getpid()); |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 192 | if (!thread) { |
| 193 | pr_err("Could not get thread\n"); |
| 194 | goto out; |
| 195 | } |
| 196 | |
Arnaldo Carvalho de Melo | dd8c17a | 2014-10-23 16:42:19 -0300 | [diff] [blame] | 197 | err = krava_1(thread); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 198 | thread__put(thread); |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 199 | |
| 200 | out: |
| 201 | machine__delete_threads(machine); |
| 202 | machine__exit(machine); |
| 203 | machines__exit(&machines); |
| 204 | return err; |
| 205 | } |