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