GNU Linux-libre 4.4.288-gnu1
[releases.git] / tools / perf / util / unwind-libdw.c
1 #include <linux/compiler.h>
2 #include <elfutils/libdw.h>
3 #include <elfutils/libdwfl.h>
4 #include <inttypes.h>
5 #include <errno.h>
6 #include "debug.h"
7 #include "unwind.h"
8 #include "unwind-libdw.h"
9 #include "machine.h"
10 #include "thread.h"
11 #include <linux/types.h>
12 #include "event.h"
13 #include "perf_regs.h"
14
15 static char *debuginfo_path;
16
17 static const Dwfl_Callbacks offline_callbacks = {
18         .find_debuginfo         = dwfl_standard_find_debuginfo,
19         .debuginfo_path         = &debuginfo_path,
20         .section_address        = dwfl_offline_section_address,
21 };
22
23 static int __report_module(struct addr_location *al, u64 ip,
24                             struct unwind_info *ui)
25 {
26         Dwfl_Module *mod;
27         struct dso *dso = NULL;
28
29         thread__find_addr_location(ui->thread,
30                                    PERF_RECORD_MISC_USER,
31                                    MAP__FUNCTION, ip, al);
32
33         if (al->map)
34                 dso = al->map->dso;
35
36         if (!dso)
37                 return 0;
38
39         mod = dwfl_addrmodule(ui->dwfl, ip);
40         if (mod) {
41                 Dwarf_Addr s;
42
43                 dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL);
44                 if (s != al->map->start - al->map->pgoff)
45                         mod = 0;
46         }
47
48         if (!mod)
49                 mod = dwfl_report_elf(ui->dwfl, dso->short_name,
50                                       (dso->symsrc_filename ? dso->symsrc_filename : dso->long_name), -1, al->map->start - al->map->pgoff,
51                                       false);
52
53         return mod && dwfl_addrmodule(ui->dwfl, ip) == mod ? 0 : -1;
54 }
55
56 static int report_module(u64 ip, struct unwind_info *ui)
57 {
58         struct addr_location al;
59
60         return __report_module(&al, ip, ui);
61 }
62
63 static int entry(u64 ip, struct unwind_info *ui)
64
65 {
66         struct unwind_entry e;
67         struct addr_location al;
68
69         if (__report_module(&al, ip, ui))
70                 return -1;
71
72         e.ip  = ip;
73         e.map = al.map;
74         e.sym = al.sym;
75
76         pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
77                  al.sym ? al.sym->name : "''",
78                  ip,
79                  al.map ? al.map->map_ip(al.map, ip) : (u64) 0);
80
81         return ui->cb(&e, ui->arg);
82 }
83
84 static pid_t next_thread(Dwfl *dwfl, void *arg, void **thread_argp)
85 {
86         /* We want only single thread to be processed. */
87         if (*thread_argp != NULL)
88                 return 0;
89
90         *thread_argp = arg;
91         return dwfl_pid(dwfl);
92 }
93
94 static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,
95                           Dwarf_Word *data)
96 {
97         struct addr_location al;
98         ssize_t size;
99
100         thread__find_addr_map(ui->thread, PERF_RECORD_MISC_USER,
101                               MAP__FUNCTION, addr, &al);
102         if (!al.map) {
103                 pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
104                 return -1;
105         }
106
107         if (!al.map->dso)
108                 return -1;
109
110         size = dso__data_read_addr(al.map->dso, al.map, ui->machine,
111                                    addr, (u8 *) data, sizeof(*data));
112
113         return !(size == sizeof(*data));
114 }
115
116 static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
117                         void *arg)
118 {
119         struct unwind_info *ui = arg;
120         struct stack_dump *stack = &ui->sample->user_stack;
121         u64 start, end;
122         int offset;
123         int ret;
124
125         ret = perf_reg_value(&start, &ui->sample->user_regs, PERF_REG_SP);
126         if (ret)
127                 return false;
128
129         end = start + stack->size;
130
131         /* Check overflow. */
132         if (addr + sizeof(Dwarf_Word) < addr)
133                 return false;
134
135         if (addr < start || addr + sizeof(Dwarf_Word) > end) {
136                 ret = access_dso_mem(ui, addr, result);
137                 if (ret) {
138                         pr_debug("unwind: access_mem 0x%" PRIx64 " not inside range"
139                                  " 0x%" PRIx64 "-0x%" PRIx64 "\n",
140                                 addr, start, end);
141                         return false;
142                 }
143                 return true;
144         }
145
146         offset  = addr - start;
147         *result = *(Dwarf_Word *)&stack->data[offset];
148         pr_debug("unwind: access_mem addr 0x%" PRIx64 ", val %lx, offset %d\n",
149                  addr, (unsigned long)*result, offset);
150         return true;
151 }
152
153 static const Dwfl_Thread_Callbacks callbacks = {
154         .next_thread            = next_thread,
155         .memory_read            = memory_read,
156         .set_initial_registers  = libdw__arch_set_initial_registers,
157 };
158
159 static int
160 frame_callback(Dwfl_Frame *state, void *arg)
161 {
162         struct unwind_info *ui = arg;
163         Dwarf_Addr pc;
164
165         if (!dwfl_frame_pc(state, &pc, NULL)) {
166                 pr_err("%s", dwfl_errmsg(-1));
167                 return DWARF_CB_ABORT;
168         }
169
170         return entry(pc, ui) || !(--ui->max_stack) ?
171                DWARF_CB_ABORT : DWARF_CB_OK;
172 }
173
174 int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
175                         struct thread *thread,
176                         struct perf_sample *data,
177                         int max_stack)
178 {
179         struct unwind_info ui = {
180                 .sample         = data,
181                 .thread         = thread,
182                 .machine        = thread->mg->machine,
183                 .cb             = cb,
184                 .arg            = arg,
185                 .max_stack      = max_stack,
186         };
187         Dwarf_Word ip;
188         int err = -EINVAL;
189
190         if (!data->user_regs.regs)
191                 return -EINVAL;
192
193         ui.dwfl = dwfl_begin(&offline_callbacks);
194         if (!ui.dwfl)
195                 goto out;
196
197         err = perf_reg_value(&ip, &data->user_regs, PERF_REG_IP);
198         if (err)
199                 goto out;
200
201         err = report_module(ip, &ui);
202         if (err)
203                 goto out;
204
205         if (!dwfl_attach_state(ui.dwfl, EM_NONE, thread->tid, &callbacks, &ui))
206                 goto out;
207
208         err = dwfl_getthread_frames(ui.dwfl, thread->tid, frame_callback, &ui);
209
210         if (err && !ui.max_stack)
211                 err = 0;
212
213  out:
214         if (err)
215                 pr_debug("unwind: failed with '%s'\n", dwfl_errmsg(-1));
216
217         dwfl_end(ui.dwfl);
218         return 0;
219 }