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