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