Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / arch / powerpc / util / skip-callchain-idx.c
blob3018a054526af931641aa1226e953dbf16295cb5
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Use DWARF Debug information to skip unnecessary callchain entries.
5 * Copyright (C) 2014 Sukadev Bhattiprolu, IBM Corporation.
6 * Copyright (C) 2014 Ulrich Weigand, IBM Corporation.
7 */
8 #include <inttypes.h>
9 #include <dwarf.h>
10 #include <elfutils/libdwfl.h>
12 #include "util/thread.h"
13 #include "util/callchain.h"
14 #include "util/debug.h"
15 #include "util/dso.h"
16 #include "util/event.h" // struct ip_callchain
17 #include "util/map.h"
18 #include "util/symbol.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
26 * entries.
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)
48 Dwarf_Op ops_mem[2];
49 Dwarf_Op dummy;
50 Dwarf_Op *ops = &dummy;
51 size_t nops;
52 int result;
54 result = dwarf_frame_register(frame, ra_regno, ops_mem, &ops, &nops);
55 if (result < 0) {
56 pr_debug("dwarf_frame_register() %s\n", dwarf_errmsg(-1));
57 return -1;
61 * Check if return address is on the stack. If return address
62 * is in a register (typically R0), it is yet to be saved on
63 * the stack.
65 if ((nops != 0 || ops != NULL) &&
66 !(nops == 1 && ops[0].atom == DW_OP_regx &&
67 ops[0].number2 == 0 && ops[0].offset == 0))
68 return 0;
71 * Return address is in LR. Check if a frame was allocated
72 * but not-yet used.
74 result = dwarf_frame_cfa(frame, &ops, &nops);
75 if (result < 0) {
76 pr_debug("dwarf_frame_cfa() returns %d, %s\n", result,
77 dwarf_errmsg(-1));
78 return -1;
82 * If call frame address is in r1, no new frame was allocated.
84 if (nops == 1 && ops[0].atom == DW_OP_bregx && ops[0].number == 1 &&
85 ops[0].number2 == 0)
86 return 1;
89 * A new frame was allocated but has not yet been used.
91 return 2;
95 * Get the DWARF frame from the .eh_frame section.
97 static Dwarf_Frame *get_eh_frame(Dwfl_Module *mod, Dwarf_Addr pc)
99 int result;
100 Dwarf_Addr bias;
101 Dwarf_CFI *cfi;
102 Dwarf_Frame *frame;
104 cfi = dwfl_module_eh_cfi(mod, &bias);
105 if (!cfi) {
106 pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
107 return NULL;
110 result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
111 if (result) {
112 pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
113 return NULL;
116 return frame;
120 * Get the DWARF frame from the .debug_frame section.
122 static Dwarf_Frame *get_dwarf_frame(Dwfl_Module *mod, Dwarf_Addr pc)
124 Dwarf_CFI *cfi;
125 Dwarf_Addr bias;
126 Dwarf_Frame *frame;
127 int result;
129 cfi = dwfl_module_dwarf_cfi(mod, &bias);
130 if (!cfi) {
131 pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
132 return NULL;
135 result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
136 if (result) {
137 pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
138 return NULL;
141 return frame;
145 * Return:
146 * 0 if return address for the program counter @pc is on stack
147 * 1 if return address is in LR and no new stack frame was allocated
148 * 2 if return address is in LR and a new frame was allocated (but not
149 * yet used)
150 * -1 in case of errors
152 static int check_return_addr(struct dso *dso, u64 map_start, Dwarf_Addr pc)
154 int rc = -1;
155 Dwfl *dwfl;
156 Dwfl_Module *mod;
157 Dwarf_Frame *frame;
158 int ra_regno;
159 Dwarf_Addr start = pc;
160 Dwarf_Addr end = pc;
161 bool signalp;
162 const char *exec_file = dso->long_name;
164 dwfl = dso->dwfl;
166 if (!dwfl) {
167 dwfl = dwfl_begin(&offline_callbacks);
168 if (!dwfl) {
169 pr_debug("dwfl_begin() failed: %s\n", dwarf_errmsg(-1));
170 return -1;
173 mod = dwfl_report_elf(dwfl, exec_file, exec_file, -1,
174 map_start, false);
175 if (!mod) {
176 pr_debug("dwfl_report_elf() failed %s\n",
177 dwarf_errmsg(-1));
179 * We normally cache the DWARF debug info and never
180 * call dwfl_end(). But to prevent fd leak, free in
181 * case of error.
183 dwfl_end(dwfl);
184 goto out;
186 dso->dwfl = dwfl;
189 mod = dwfl_addrmodule(dwfl, pc);
190 if (!mod) {
191 pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1));
192 goto out;
196 * To work with split debug info files (eg: glibc), check both
197 * .eh_frame and .debug_frame sections of the ELF header.
199 frame = get_eh_frame(mod, pc);
200 if (!frame) {
201 frame = get_dwarf_frame(mod, pc);
202 if (!frame)
203 goto out;
206 ra_regno = dwarf_frame_info(frame, &start, &end, &signalp);
207 if (ra_regno < 0) {
208 pr_debug("Return address register unavailable: %s\n",
209 dwarf_errmsg(-1));
210 goto out;
213 rc = check_return_reg(ra_regno, frame);
215 out:
216 return rc;
220 * The callchain saved by the kernel always includes the link register (LR).
222 * 0: PERF_CONTEXT_USER
223 * 1: Program counter (Next instruction pointer)
224 * 2: LR value
225 * 3: Caller's caller
226 * 4: ...
228 * The value in LR is only needed when it holds a return address. If the
229 * return address is on the stack, we should ignore the LR value.
231 * Further, when the return address is in the LR, if a new frame was just
232 * allocated but the LR was not saved into it, then the LR contains the
233 * caller, slot 4: contains the caller's caller and the contents of slot 3:
234 * (chain->ips[3]) is undefined and must be ignored.
236 * Use DWARF debug information to determine if any entries need to be skipped.
238 * Return:
239 * index: of callchain entry that needs to be ignored (if any)
240 * -1 if no entry needs to be ignored or in case of errors
242 int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
244 struct addr_location al;
245 struct dso *dso = NULL;
246 int rc;
247 u64 ip;
248 u64 skip_slot = -1;
250 if (!chain || chain->nr < 3)
251 return skip_slot;
253 ip = chain->ips[1];
255 thread__find_symbol(thread, PERF_RECORD_MISC_USER, ip, &al);
257 if (al.map)
258 dso = al.map->dso;
260 if (!dso) {
261 pr_debug("%" PRIx64 " dso is NULL\n", ip);
262 return skip_slot;
265 rc = check_return_addr(dso, al.map->start, ip);
267 pr_debug("[DSO %s, sym %s, ip 0x%" PRIx64 "] rc %d\n",
268 dso->long_name, al.sym->name, ip, rc);
270 if (rc == 0) {
272 * Return address on stack. Ignore LR value in callchain
274 skip_slot = 2;
275 } else if (rc == 2) {
277 * New frame allocated but return address still in LR.
278 * Ignore the caller's caller entry in callchain.
280 skip_slot = 3;
282 return skip_slot;