2 * Use DWARF Debug information to skip unnecessary callchain entries.
4 * Copyright (C) 2014 Sukadev Bhattiprolu, IBM Corporation.
5 * Copyright (C) 2014 Ulrich Weigand, IBM Corporation.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
14 #include <elfutils/libdwfl.h>
16 #include "util/thread.h"
17 #include "util/callchain.h"
18 #include "util/debug.h"
21 * When saving the callchain on Power, the kernel conservatively saves
22 * excess entries in the callchain. A few of these entries are needed
23 * in some cases but not others. If the unnecessary entries are not
24 * ignored, we end up with duplicate arcs in the call-graphs. Use
25 * DWARF debug information to skip over any unnecessary callchain
28 * See function header for arch_adjust_callchain() below for more details.
30 * The libdwfl code in this file is based on code from elfutils
31 * (libdwfl/argp-std.c, libdwfl/tests/addrcfi.c, etc).
33 static char *debuginfo_path;
35 static const Dwfl_Callbacks offline_callbacks = {
36 .debuginfo_path = &debuginfo_path,
37 .find_debuginfo = dwfl_standard_find_debuginfo,
38 .section_address = dwfl_offline_section_address,
43 * Use the DWARF expression for the Call-frame-address and determine
44 * if return address is in LR and if a new frame was allocated.
46 static int check_return_reg(int ra_regno, Dwarf_Frame *frame)
50 Dwarf_Op *ops = &dummy;
54 result = dwarf_frame_register(frame, ra_regno, ops_mem, &ops, &nops);
56 pr_debug("dwarf_frame_register() %s\n", dwarf_errmsg(-1));
61 * Check if return address is on the stack.
63 if (nops != 0 || ops != NULL)
67 * Return address is in LR. Check if a frame was allocated
70 result = dwarf_frame_cfa(frame, &ops, &nops);
72 pr_debug("dwarf_frame_cfa() returns %d, %s\n", result,
78 * If call frame address is in r1, no new frame was allocated.
80 if (nops == 1 && ops[0].atom == DW_OP_bregx && ops[0].number == 1 &&
85 * A new frame was allocated but has not yet been used.
91 * Get the DWARF frame from the .eh_frame section.
93 static Dwarf_Frame *get_eh_frame(Dwfl_Module *mod, Dwarf_Addr pc)
100 cfi = dwfl_module_eh_cfi(mod, &bias);
102 pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
106 result = dwarf_cfi_addrframe(cfi, pc, &frame);
108 pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
116 * Get the DWARF frame from the .debug_frame section.
118 static Dwarf_Frame *get_dwarf_frame(Dwfl_Module *mod, Dwarf_Addr pc)
125 cfi = dwfl_module_dwarf_cfi(mod, &bias);
127 pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
131 result = dwarf_cfi_addrframe(cfi, pc, &frame);
133 pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
142 * 0 if return address for the program counter @pc is on stack
143 * 1 if return address is in LR and no new stack frame was allocated
144 * 2 if return address is in LR and a new frame was allocated (but not
146 * -1 in case of errors
148 static int check_return_addr(const char *exec_file, Dwarf_Addr pc)
155 Dwarf_Addr start = pc;
159 dwfl = dwfl_begin(&offline_callbacks);
161 pr_debug("dwfl_begin() failed: %s\n", dwarf_errmsg(-1));
165 if (dwfl_report_offline(dwfl, "", exec_file, -1) == NULL) {
166 pr_debug("dwfl_report_offline() failed %s\n", dwarf_errmsg(-1));
170 mod = dwfl_addrmodule(dwfl, pc);
172 pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1));
177 * To work with split debug info files (eg: glibc), check both
178 * .eh_frame and .debug_frame sections of the ELF header.
180 frame = get_eh_frame(mod, pc);
182 frame = get_dwarf_frame(mod, pc);
187 ra_regno = dwarf_frame_info(frame, &start, &end, &signalp);
189 pr_debug("Return address register unavailable: %s\n",
194 rc = check_return_reg(ra_regno, frame);
202 * The callchain saved by the kernel always includes the link register (LR).
204 * 0: PERF_CONTEXT_USER
205 * 1: Program counter (Next instruction pointer)
210 * The value in LR is only needed when it holds a return address. If the
211 * return address is on the stack, we should ignore the LR value.
213 * Further, when the return address is in the LR, if a new frame was just
214 * allocated but the LR was not saved into it, then the LR contains the
215 * caller, slot 4: contains the caller's caller and the contents of slot 3:
216 * (chain->ips[3]) is undefined and must be ignored.
218 * Use DWARF debug information to determine if any entries need to be skipped.
221 * index: of callchain entry that needs to be ignored (if any)
222 * -1 if no entry needs to be ignored or in case of errors
224 int arch_skip_callchain_idx(struct machine *machine, struct thread *thread,
225 struct ip_callchain *chain)
227 struct addr_location al;
228 struct dso *dso = NULL;
238 thread__find_addr_location(thread, machine, PERF_RECORD_MISC_USER,
239 MAP__FUNCTION, ip, &al);
245 pr_debug("%" PRIx64 " dso is NULL\n", ip);
249 rc = check_return_addr(dso->long_name, ip);
251 pr_debug("DSO %s, nr %" PRIx64 ", ip 0x%" PRIx64 "rc %d\n",
252 dso->long_name, chain->nr, ip, rc);
256 * Return address on stack. Ignore LR value in callchain
259 } else if (rc == 2) {
261 * New frame allocated but return address still in LR.
262 * Ignore the caller's caller entry in callchain.