1 #include <linux/compiler.h>
2 #include <elfutils/libdw.h>
3 #include <elfutils/libdwfl.h>
8 #include "unwind-libdw.h"
11 #include <linux/types.h>
13 #include "perf_regs.h"
14 #include "callchain.h"
17 static char *debuginfo_path;
19 static const Dwfl_Callbacks offline_callbacks = {
20 .find_debuginfo = dwfl_standard_find_debuginfo,
21 .debuginfo_path = &debuginfo_path,
22 .section_address = dwfl_offline_section_address,
25 static int __report_module(struct addr_location *al, u64 ip,
26 struct unwind_info *ui)
29 struct dso *dso = NULL;
31 thread__find_addr_location(ui->thread,
32 PERF_RECORD_MISC_USER,
33 MAP__FUNCTION, ip, al);
41 mod = dwfl_addrmodule(ui->dwfl, ip);
43 mod = dwfl_report_elf(ui->dwfl, dso->short_name,
44 dso->long_name, -1, al->map->start,
47 return mod && dwfl_addrmodule(ui->dwfl, ip) == mod ? 0 : -1;
50 static int report_module(u64 ip, struct unwind_info *ui)
52 struct addr_location al;
54 return __report_module(&al, ip, ui);
58 * Store all entries within entries array,
59 * we will process it after we finish unwind.
61 static int entry(u64 ip, struct unwind_info *ui)
64 struct unwind_entry *e = &ui->entries[ui->idx++];
65 struct addr_location al;
67 if (__report_module(&al, ip, ui))
74 pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
75 al.sym ? al.sym->name : "''",
77 al.map ? al.map->map_ip(al.map, ip) : (u64) 0);
81 static pid_t next_thread(Dwfl *dwfl, void *arg, void **thread_argp)
83 /* We want only single thread to be processed. */
84 if (*thread_argp != NULL)
88 return dwfl_pid(dwfl);
91 static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,
94 struct addr_location al;
97 thread__find_addr_map(ui->thread, PERF_RECORD_MISC_USER,
98 MAP__FUNCTION, addr, &al);
101 * We've seen cases (softice) where DWARF unwinder went
102 * through non executable mmaps, which we need to lookup
103 * in MAP__VARIABLE tree.
105 thread__find_addr_map(ui->thread, PERF_RECORD_MISC_USER,
106 MAP__VARIABLE, addr, &al);
110 pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
117 size = dso__data_read_addr(al.map->dso, al.map, ui->machine,
118 addr, (u8 *) data, sizeof(*data));
120 return !(size == sizeof(*data));
123 static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
126 struct unwind_info *ui = arg;
127 struct stack_dump *stack = &ui->sample->user_stack;
132 ret = perf_reg_value(&start, &ui->sample->user_regs, PERF_REG_SP);
136 end = start + stack->size;
138 /* Check overflow. */
139 if (addr + sizeof(Dwarf_Word) < addr)
142 if (addr < start || addr + sizeof(Dwarf_Word) > end) {
143 ret = access_dso_mem(ui, addr, result);
145 pr_debug("unwind: access_mem 0x%" PRIx64 " not inside range"
146 " 0x%" PRIx64 "-0x%" PRIx64 "\n",
153 offset = addr - start;
154 *result = *(Dwarf_Word *)&stack->data[offset];
155 pr_debug("unwind: access_mem addr 0x%" PRIx64 ", val %lx, offset %d\n",
156 addr, (unsigned long)*result, offset);
160 static const Dwfl_Thread_Callbacks callbacks = {
161 .next_thread = next_thread,
162 .memory_read = memory_read,
163 .set_initial_registers = libdw__arch_set_initial_registers,
167 frame_callback(Dwfl_Frame *state, void *arg)
169 struct unwind_info *ui = arg;
172 if (!dwfl_frame_pc(state, &pc, NULL)) {
173 pr_err("%s", dwfl_errmsg(-1));
174 return DWARF_CB_ABORT;
177 return entry(pc, ui) || !(--ui->max_stack) ?
178 DWARF_CB_ABORT : DWARF_CB_OK;
181 int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
182 struct thread *thread,
183 struct perf_sample *data,
186 struct unwind_info *ui, ui_buf = {
189 .machine = thread->mg->machine,
192 .max_stack = max_stack,
195 int err = -EINVAL, i;
197 if (!data->user_regs.regs)
200 ui = zalloc(sizeof(ui_buf) + sizeof(ui_buf.entries[0]) * max_stack);
206 ui->dwfl = dwfl_begin(&offline_callbacks);
210 err = perf_reg_value(&ip, &data->user_regs, PERF_REG_IP);
214 err = report_module(ip, ui);
218 if (!dwfl_attach_state(ui->dwfl, EM_NONE, thread->tid, &callbacks, ui))
221 err = dwfl_getthread_frames(ui->dwfl, thread->tid, frame_callback, ui);
223 if (err && !ui->max_stack)
227 * Display what we got based on the order setup.
229 for (i = 0; i < ui->idx && !err; i++) {
232 if (callchain_param.order == ORDER_CALLER)
235 err = ui->entries[j].ip ? ui->cb(&ui->entries[j], ui->arg) : 0;
240 pr_debug("unwind: failed with '%s'\n", dwfl_errmsg(-1));