1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Use DWARF Debug information to skip unnecessary callchain entries.
4 *
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>
11
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"
19
20 /*
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.
27 *
28 * See function header for arch_adjust_callchain() below for more details.
29 *
30 * The libdwfl code in this file is based on code from elfutils
31 * (libdwfl/argp-std.c, libdwfl/tests/addrcfi.c, etc).
32 */
33
34 /*
35 * Use the DWARF expression for the Call-frame-address and determine
36 * if return address is in LR and if a new frame was allocated.
37 */
check_return_reg(int ra_regno,Dwarf_Frame * frame)38 static int check_return_reg(int ra_regno, Dwarf_Frame *frame)
39 {
40 Dwarf_Op ops_mem[3];
41 Dwarf_Op dummy;
42 Dwarf_Op *ops = &dummy;
43 size_t nops;
44 int result;
45
46 result = dwarf_frame_register(frame, ra_regno, ops_mem, &ops, &nops);
47 if (result < 0) {
48 pr_debug("dwarf_frame_register() %s\n", dwarf_errmsg(-1));
49 return -1;
50 }
51
52 /*
53 * Check if return address is on the stack. If return address
54 * is in a register (typically R0), it is yet to be saved on
55 * the stack.
56 */
57 if ((nops != 0 || ops != NULL) &&
58 !(nops == 1 && ops[0].atom == DW_OP_regx &&
59 ops[0].number2 == 0 && ops[0].offset == 0))
60 return 0;
61
62 /*
63 * Return address is in LR. Check if a frame was allocated
64 * but not-yet used.
65 */
66 result = dwarf_frame_cfa(frame, &ops, &nops);
67 if (result < 0) {
68 pr_debug("dwarf_frame_cfa() returns %d, %s\n", result,
69 dwarf_errmsg(-1));
70 return -1;
71 }
72
73 /*
74 * If call frame address is in r1, no new frame was allocated.
75 */
76 if (nops == 1 && ops[0].atom == DW_OP_bregx && ops[0].number == 1 &&
77 ops[0].number2 == 0)
78 return 1;
79
80 /*
81 * A new frame was allocated but has not yet been used.
82 */
83 return 2;
84 }
85
86 /*
87 * Get the DWARF frame from the .eh_frame section.
88 */
get_eh_frame(Dwfl_Module * mod,Dwarf_Addr pc)89 static Dwarf_Frame *get_eh_frame(Dwfl_Module *mod, Dwarf_Addr pc)
90 {
91 int result;
92 Dwarf_Addr bias;
93 Dwarf_CFI *cfi;
94 Dwarf_Frame *frame;
95
96 cfi = dwfl_module_eh_cfi(mod, &bias);
97 if (!cfi) {
98 pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
99 return NULL;
100 }
101
102 result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
103 if (result) {
104 pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
105 return NULL;
106 }
107
108 return frame;
109 }
110
111 /*
112 * Get the DWARF frame from the .debug_frame section.
113 */
get_dwarf_frame(Dwfl_Module * mod,Dwarf_Addr pc)114 static Dwarf_Frame *get_dwarf_frame(Dwfl_Module *mod, Dwarf_Addr pc)
115 {
116 Dwarf_CFI *cfi;
117 Dwarf_Addr bias;
118 Dwarf_Frame *frame;
119 int result;
120
121 cfi = dwfl_module_dwarf_cfi(mod, &bias);
122 if (!cfi) {
123 pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
124 return NULL;
125 }
126
127 result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
128 if (result) {
129 pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
130 return NULL;
131 }
132
133 return frame;
134 }
135
136 /*
137 * Return:
138 * 0 if return address for the program counter @pc is on stack
139 * 1 if return address is in LR and no new stack frame was allocated
140 * 2 if return address is in LR and a new frame was allocated (but not
141 * yet used)
142 * -1 in case of errors
143 */
check_return_addr(struct dso * dso,Dwarf_Addr mapped_pc)144 static int check_return_addr(struct dso *dso, Dwarf_Addr mapped_pc)
145 {
146 int rc = -1;
147 Dwfl *dwfl;
148 Dwfl_Module *mod;
149 Dwarf_Frame *frame;
150 int ra_regno;
151 Dwarf_Addr start = mapped_pc;
152 Dwarf_Addr end = mapped_pc;
153 bool signalp;
154
155 dwfl = dso__libdw_dwfl(dso);
156 if (!dwfl)
157 return -1;
158
159 mod = dwfl_addrmodule(dwfl, mapped_pc);
160 if (!mod) {
161 pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1));
162 goto out;
163 }
164
165 /*
166 * To work with split debug info files (eg: glibc), check both
167 * .eh_frame and .debug_frame sections of the ELF header.
168 */
169 frame = get_eh_frame(mod, mapped_pc);
170 if (!frame) {
171 frame = get_dwarf_frame(mod, mapped_pc);
172 if (!frame)
173 goto out;
174 }
175
176 ra_regno = dwarf_frame_info(frame, &start, &end, &signalp);
177 if (ra_regno < 0) {
178 pr_debug("Return address register unavailable: %s\n",
179 dwarf_errmsg(-1));
180 goto out;
181 }
182
183 rc = check_return_reg(ra_regno, frame);
184
185 out:
186 return rc;
187 }
188
189 /*
190 * The callchain saved by the kernel always includes the link register (LR).
191 *
192 * 0: PERF_CONTEXT_USER
193 * 1: Program counter (Next instruction pointer)
194 * 2: LR value
195 * 3: Caller's caller
196 * 4: ...
197 *
198 * The value in LR is only needed when it holds a return address. If the
199 * return address is on the stack, we should ignore the LR value.
200 *
201 * Further, when the return address is in the LR, if a new frame was just
202 * allocated but the LR was not saved into it, then the LR contains the
203 * caller, slot 4: contains the caller's caller and the contents of slot 3:
204 * (chain->ips[3]) is undefined and must be ignored.
205 *
206 * Use DWARF debug information to determine if any entries need to be skipped.
207 *
208 * Return:
209 * index: of callchain entry that needs to be ignored (if any)
210 * -1 if no entry needs to be ignored or in case of errors
211 */
arch_skip_callchain_idx(struct thread * thread,struct ip_callchain * chain)212 int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
213 {
214 struct addr_location al;
215 struct dso *dso = NULL;
216 int rc;
217 u64 ip;
218 u64 skip_slot = -1;
219
220 if (!chain || chain->nr < 3)
221 return skip_slot;
222
223 addr_location__init(&al);
224 ip = chain->ips[1];
225
226 thread__find_symbol(thread, PERF_RECORD_MISC_USER, ip, &al);
227
228 if (al.map)
229 dso = map__dso(al.map);
230
231 if (!dso) {
232 pr_debug("%" PRIx64 " dso is NULL\n", ip);
233 addr_location__exit(&al);
234 return skip_slot;
235 }
236
237 rc = check_return_addr(dso, map__map_ip(al.map, ip));
238
239 pr_debug("[DSO %s, sym %s, ip 0x%" PRIx64 "] rc %d\n",
240 dso__long_name(dso), al.sym->name, ip, rc);
241
242 if (rc == 0) {
243 /*
244 * Return address on stack. Ignore LR value in callchain
245 */
246 skip_slot = 2;
247 } else if (rc == 2) {
248 /*
249 * New frame allocated but return address still in LR.
250 * Ignore the caller's caller entry in callchain.
251 */
252 skip_slot = 3;
253 }
254
255 addr_location__exit(&al);
256 return skip_slot;
257 }
258