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