18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Use DWARF Debug information to skip unnecessary callchain entries.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2014 Sukadev Bhattiprolu, IBM Corporation.
68c2ecf20Sopenharmony_ci * Copyright (C) 2014 Ulrich Weigand, IBM Corporation.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <inttypes.h>
98c2ecf20Sopenharmony_ci#include <dwarf.h>
108c2ecf20Sopenharmony_ci#include <elfutils/libdwfl.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "util/thread.h"
138c2ecf20Sopenharmony_ci#include "util/callchain.h"
148c2ecf20Sopenharmony_ci#include "util/debug.h"
158c2ecf20Sopenharmony_ci#include "util/dso.h"
168c2ecf20Sopenharmony_ci#include "util/event.h" // struct ip_callchain
178c2ecf20Sopenharmony_ci#include "util/map.h"
188c2ecf20Sopenharmony_ci#include "util/symbol.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * When saving the callchain on Power, the kernel conservatively saves
228c2ecf20Sopenharmony_ci * excess entries in the callchain. A few of these entries are needed
238c2ecf20Sopenharmony_ci * in some cases but not others. If the unnecessary entries are not
248c2ecf20Sopenharmony_ci * ignored, we end up with duplicate arcs in the call-graphs. Use
258c2ecf20Sopenharmony_ci * DWARF debug information to skip over any unnecessary callchain
268c2ecf20Sopenharmony_ci * entries.
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * See function header for arch_adjust_callchain() below for more details.
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * The libdwfl code in this file is based on code from elfutils
318c2ecf20Sopenharmony_ci * (libdwfl/argp-std.c, libdwfl/tests/addrcfi.c, etc).
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_cistatic char *debuginfo_path;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic const Dwfl_Callbacks offline_callbacks = {
368c2ecf20Sopenharmony_ci	.debuginfo_path = &debuginfo_path,
378c2ecf20Sopenharmony_ci	.find_debuginfo = dwfl_standard_find_debuginfo,
388c2ecf20Sopenharmony_ci	.section_address = dwfl_offline_section_address,
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/*
438c2ecf20Sopenharmony_ci * Use the DWARF expression for the Call-frame-address and determine
448c2ecf20Sopenharmony_ci * if return address is in LR and if a new frame was allocated.
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_cistatic int check_return_reg(int ra_regno, Dwarf_Frame *frame)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	Dwarf_Op ops_mem[2];
498c2ecf20Sopenharmony_ci	Dwarf_Op dummy;
508c2ecf20Sopenharmony_ci	Dwarf_Op *ops = &dummy;
518c2ecf20Sopenharmony_ci	size_t nops;
528c2ecf20Sopenharmony_ci	int result;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	result = dwarf_frame_register(frame, ra_regno, ops_mem, &ops, &nops);
558c2ecf20Sopenharmony_ci	if (result < 0) {
568c2ecf20Sopenharmony_ci		pr_debug("dwarf_frame_register() %s\n", dwarf_errmsg(-1));
578c2ecf20Sopenharmony_ci		return -1;
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/*
618c2ecf20Sopenharmony_ci	 * Check if return address is on the stack. If return address
628c2ecf20Sopenharmony_ci	 * is in a register (typically R0), it is yet to be saved on
638c2ecf20Sopenharmony_ci	 * the stack.
648c2ecf20Sopenharmony_ci	 */
658c2ecf20Sopenharmony_ci	if ((nops != 0 || ops != NULL) &&
668c2ecf20Sopenharmony_ci		!(nops == 1 && ops[0].atom == DW_OP_regx &&
678c2ecf20Sopenharmony_ci			ops[0].number2 == 0 && ops[0].offset == 0))
688c2ecf20Sopenharmony_ci		return 0;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/*
718c2ecf20Sopenharmony_ci	 * Return address is in LR. Check if a frame was allocated
728c2ecf20Sopenharmony_ci	 * but not-yet used.
738c2ecf20Sopenharmony_ci	 */
748c2ecf20Sopenharmony_ci	result = dwarf_frame_cfa(frame, &ops, &nops);
758c2ecf20Sopenharmony_ci	if (result < 0) {
768c2ecf20Sopenharmony_ci		pr_debug("dwarf_frame_cfa() returns %d, %s\n", result,
778c2ecf20Sopenharmony_ci					dwarf_errmsg(-1));
788c2ecf20Sopenharmony_ci		return -1;
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/*
828c2ecf20Sopenharmony_ci	 * If call frame address is in r1, no new frame was allocated.
838c2ecf20Sopenharmony_ci	 */
848c2ecf20Sopenharmony_ci	if (nops == 1 && ops[0].atom == DW_OP_bregx && ops[0].number == 1 &&
858c2ecf20Sopenharmony_ci				ops[0].number2 == 0)
868c2ecf20Sopenharmony_ci		return 1;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	/*
898c2ecf20Sopenharmony_ci	 * A new frame was allocated but has not yet been used.
908c2ecf20Sopenharmony_ci	 */
918c2ecf20Sopenharmony_ci	return 2;
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/*
958c2ecf20Sopenharmony_ci * Get the DWARF frame from the .eh_frame section.
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_cistatic Dwarf_Frame *get_eh_frame(Dwfl_Module *mod, Dwarf_Addr pc)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	int		result;
1008c2ecf20Sopenharmony_ci	Dwarf_Addr	bias;
1018c2ecf20Sopenharmony_ci	Dwarf_CFI	*cfi;
1028c2ecf20Sopenharmony_ci	Dwarf_Frame	*frame;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	cfi = dwfl_module_eh_cfi(mod, &bias);
1058c2ecf20Sopenharmony_ci	if (!cfi) {
1068c2ecf20Sopenharmony_ci		pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
1078c2ecf20Sopenharmony_ci		return NULL;
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
1118c2ecf20Sopenharmony_ci	if (result) {
1128c2ecf20Sopenharmony_ci		pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
1138c2ecf20Sopenharmony_ci		return NULL;
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	return frame;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/*
1208c2ecf20Sopenharmony_ci * Get the DWARF frame from the .debug_frame section.
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_cistatic Dwarf_Frame *get_dwarf_frame(Dwfl_Module *mod, Dwarf_Addr pc)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	Dwarf_CFI       *cfi;
1258c2ecf20Sopenharmony_ci	Dwarf_Addr      bias;
1268c2ecf20Sopenharmony_ci	Dwarf_Frame     *frame;
1278c2ecf20Sopenharmony_ci	int             result;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	cfi = dwfl_module_dwarf_cfi(mod, &bias);
1308c2ecf20Sopenharmony_ci	if (!cfi) {
1318c2ecf20Sopenharmony_ci		pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));
1328c2ecf20Sopenharmony_ci		return NULL;
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);
1368c2ecf20Sopenharmony_ci	if (result) {
1378c2ecf20Sopenharmony_ci		pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));
1388c2ecf20Sopenharmony_ci		return NULL;
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	return frame;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/*
1458c2ecf20Sopenharmony_ci * Return:
1468c2ecf20Sopenharmony_ci *	0 if return address for the program counter @pc is on stack
1478c2ecf20Sopenharmony_ci *	1 if return address is in LR and no new stack frame was allocated
1488c2ecf20Sopenharmony_ci *	2 if return address is in LR and a new frame was allocated (but not
1498c2ecf20Sopenharmony_ci *		yet used)
1508c2ecf20Sopenharmony_ci *	-1 in case of errors
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_cistatic int check_return_addr(struct dso *dso, u64 map_start, Dwarf_Addr pc)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	int		rc = -1;
1558c2ecf20Sopenharmony_ci	Dwfl		*dwfl;
1568c2ecf20Sopenharmony_ci	Dwfl_Module	*mod;
1578c2ecf20Sopenharmony_ci	Dwarf_Frame	*frame;
1588c2ecf20Sopenharmony_ci	int		ra_regno;
1598c2ecf20Sopenharmony_ci	Dwarf_Addr	start = pc;
1608c2ecf20Sopenharmony_ci	Dwarf_Addr	end = pc;
1618c2ecf20Sopenharmony_ci	bool		signalp;
1628c2ecf20Sopenharmony_ci	const char	*exec_file = dso->long_name;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	dwfl = dso->dwfl;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	if (!dwfl) {
1678c2ecf20Sopenharmony_ci		dwfl = dwfl_begin(&offline_callbacks);
1688c2ecf20Sopenharmony_ci		if (!dwfl) {
1698c2ecf20Sopenharmony_ci			pr_debug("dwfl_begin() failed: %s\n", dwarf_errmsg(-1));
1708c2ecf20Sopenharmony_ci			return -1;
1718c2ecf20Sopenharmony_ci		}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci		mod = dwfl_report_elf(dwfl, exec_file, exec_file, -1,
1748c2ecf20Sopenharmony_ci						map_start, false);
1758c2ecf20Sopenharmony_ci		if (!mod) {
1768c2ecf20Sopenharmony_ci			pr_debug("dwfl_report_elf() failed %s\n",
1778c2ecf20Sopenharmony_ci						dwarf_errmsg(-1));
1788c2ecf20Sopenharmony_ci			/*
1798c2ecf20Sopenharmony_ci			 * We normally cache the DWARF debug info and never
1808c2ecf20Sopenharmony_ci			 * call dwfl_end(). But to prevent fd leak, free in
1818c2ecf20Sopenharmony_ci			 * case of error.
1828c2ecf20Sopenharmony_ci			 */
1838c2ecf20Sopenharmony_ci			dwfl_end(dwfl);
1848c2ecf20Sopenharmony_ci			goto out;
1858c2ecf20Sopenharmony_ci		}
1868c2ecf20Sopenharmony_ci		dso->dwfl = dwfl;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	mod = dwfl_addrmodule(dwfl, pc);
1908c2ecf20Sopenharmony_ci	if (!mod) {
1918c2ecf20Sopenharmony_ci		pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1));
1928c2ecf20Sopenharmony_ci		goto out;
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	/*
1968c2ecf20Sopenharmony_ci	 * To work with split debug info files (eg: glibc), check both
1978c2ecf20Sopenharmony_ci	 * .eh_frame and .debug_frame sections of the ELF header.
1988c2ecf20Sopenharmony_ci	 */
1998c2ecf20Sopenharmony_ci	frame = get_eh_frame(mod, pc);
2008c2ecf20Sopenharmony_ci	if (!frame) {
2018c2ecf20Sopenharmony_ci		frame = get_dwarf_frame(mod, pc);
2028c2ecf20Sopenharmony_ci		if (!frame)
2038c2ecf20Sopenharmony_ci			goto out;
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	ra_regno = dwarf_frame_info(frame, &start, &end, &signalp);
2078c2ecf20Sopenharmony_ci	if (ra_regno < 0) {
2088c2ecf20Sopenharmony_ci		pr_debug("Return address register unavailable: %s\n",
2098c2ecf20Sopenharmony_ci				dwarf_errmsg(-1));
2108c2ecf20Sopenharmony_ci		goto out;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	rc = check_return_reg(ra_regno, frame);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ciout:
2168c2ecf20Sopenharmony_ci	return rc;
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci/*
2208c2ecf20Sopenharmony_ci * The callchain saved by the kernel always includes the link register (LR).
2218c2ecf20Sopenharmony_ci *
2228c2ecf20Sopenharmony_ci *	0:	PERF_CONTEXT_USER
2238c2ecf20Sopenharmony_ci *	1:	Program counter (Next instruction pointer)
2248c2ecf20Sopenharmony_ci *	2:	LR value
2258c2ecf20Sopenharmony_ci *	3:	Caller's caller
2268c2ecf20Sopenharmony_ci *	4:	...
2278c2ecf20Sopenharmony_ci *
2288c2ecf20Sopenharmony_ci * The value in LR is only needed when it holds a return address. If the
2298c2ecf20Sopenharmony_ci * return address is on the stack, we should ignore the LR value.
2308c2ecf20Sopenharmony_ci *
2318c2ecf20Sopenharmony_ci * Further, when the return address is in the LR, if a new frame was just
2328c2ecf20Sopenharmony_ci * allocated but the LR was not saved into it, then the LR contains the
2338c2ecf20Sopenharmony_ci * caller, slot 4: contains the caller's caller and the contents of slot 3:
2348c2ecf20Sopenharmony_ci * (chain->ips[3]) is undefined and must be ignored.
2358c2ecf20Sopenharmony_ci *
2368c2ecf20Sopenharmony_ci * Use DWARF debug information to determine if any entries need to be skipped.
2378c2ecf20Sopenharmony_ci *
2388c2ecf20Sopenharmony_ci * Return:
2398c2ecf20Sopenharmony_ci *	index:	of callchain entry that needs to be ignored (if any)
2408c2ecf20Sopenharmony_ci *	-1	if no entry needs to be ignored or in case of errors
2418c2ecf20Sopenharmony_ci */
2428c2ecf20Sopenharmony_ciint arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	struct addr_location al;
2458c2ecf20Sopenharmony_ci	struct dso *dso = NULL;
2468c2ecf20Sopenharmony_ci	int rc;
2478c2ecf20Sopenharmony_ci	u64 ip;
2488c2ecf20Sopenharmony_ci	u64 skip_slot = -1;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (!chain || chain->nr < 3)
2518c2ecf20Sopenharmony_ci		return skip_slot;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	ip = chain->ips[1];
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	thread__find_symbol(thread, PERF_RECORD_MISC_USER, ip, &al);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	if (al.map)
2588c2ecf20Sopenharmony_ci		dso = al.map->dso;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	if (!dso) {
2618c2ecf20Sopenharmony_ci		pr_debug("%" PRIx64 " dso is NULL\n", ip);
2628c2ecf20Sopenharmony_ci		return skip_slot;
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	rc = check_return_addr(dso, al.map->start, ip);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	pr_debug("[DSO %s, sym %s, ip 0x%" PRIx64 "] rc %d\n",
2688c2ecf20Sopenharmony_ci				dso->long_name, al.sym->name, ip, rc);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	if (rc == 0) {
2718c2ecf20Sopenharmony_ci		/*
2728c2ecf20Sopenharmony_ci		 * Return address on stack. Ignore LR value in callchain
2738c2ecf20Sopenharmony_ci		 */
2748c2ecf20Sopenharmony_ci		skip_slot = 2;
2758c2ecf20Sopenharmony_ci	} else if (rc == 2) {
2768c2ecf20Sopenharmony_ci		/*
2778c2ecf20Sopenharmony_ci		 * New frame allocated but return address still in LR.
2788c2ecf20Sopenharmony_ci		 * Ignore the caller's caller entry in callchain.
2798c2ecf20Sopenharmony_ci		 */
2808c2ecf20Sopenharmony_ci		skip_slot = 3;
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ci	return skip_slot;
2838c2ecf20Sopenharmony_ci}
284