xref: /openbmc/linux/scripts/faddr2line (revision dcea997beed694cbd8705100ca1a6eb0d886de69)
167326666SJosh Poimboeuf#!/bin/bash
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
367326666SJosh Poimboeuf#
467326666SJosh Poimboeuf# Translate stack dump function offsets.
567326666SJosh Poimboeuf#
667326666SJosh Poimboeuf# addr2line doesn't work with KASLR addresses.  This works similarly to
767326666SJosh Poimboeuf# addr2line, but instead takes the 'func+0x123' format as input:
867326666SJosh Poimboeuf#
967326666SJosh Poimboeuf#   $ ./scripts/faddr2line ~/k/vmlinux meminfo_proc_show+0x5/0x568
1067326666SJosh Poimboeuf#   meminfo_proc_show+0x5/0x568:
1167326666SJosh Poimboeuf#   meminfo_proc_show at fs/proc/meminfo.c:27
1267326666SJosh Poimboeuf#
1367326666SJosh Poimboeuf# If the address is part of an inlined function, the full inline call chain is
1467326666SJosh Poimboeuf# printed:
1567326666SJosh Poimboeuf#
1667326666SJosh Poimboeuf#   $ ./scripts/faddr2line ~/k/vmlinux native_write_msr+0x6/0x27
1767326666SJosh Poimboeuf#   native_write_msr+0x6/0x27:
1867326666SJosh Poimboeuf#   arch_static_branch at arch/x86/include/asm/msr.h:121
1967326666SJosh Poimboeuf#    (inlined by) static_key_false at include/linux/jump_label.h:125
2067326666SJosh Poimboeuf#    (inlined by) native_write_msr at arch/x86/include/asm/msr.h:125
2167326666SJosh Poimboeuf#
2267326666SJosh Poimboeuf# The function size after the '/' in the input is optional, but recommended.
2367326666SJosh Poimboeuf# It's used to help disambiguate any duplicate symbol names, which can occur
2467326666SJosh Poimboeuf# rarely.  If the size is omitted for a duplicate symbol then it's possible for
2567326666SJosh Poimboeuf# multiple code sites to be printed:
2667326666SJosh Poimboeuf#
2767326666SJosh Poimboeuf#   $ ./scripts/faddr2line ~/k/vmlinux raw_ioctl+0x5
2867326666SJosh Poimboeuf#   raw_ioctl+0x5/0x20:
2967326666SJosh Poimboeuf#   raw_ioctl at drivers/char/raw.c:122
3067326666SJosh Poimboeuf#
3167326666SJosh Poimboeuf#   raw_ioctl+0x5/0xb1:
3267326666SJosh Poimboeuf#   raw_ioctl at net/ipv4/raw.c:876
3367326666SJosh Poimboeuf#
3467326666SJosh Poimboeuf# Multiple addresses can be specified on a single command line:
3567326666SJosh Poimboeuf#
3667326666SJosh Poimboeuf#   $ ./scripts/faddr2line ~/k/vmlinux type_show+0x10/45 free_reserved_area+0x90
3767326666SJosh Poimboeuf#   type_show+0x10/0x2d:
3867326666SJosh Poimboeuf#   type_show at drivers/video/backlight/backlight.c:213
3967326666SJosh Poimboeuf#
4067326666SJosh Poimboeuf#   free_reserved_area+0x90/0x123:
4167326666SJosh Poimboeuf#   free_reserved_area at mm/page_alloc.c:6429 (discriminator 2)
4267326666SJosh Poimboeuf
4367326666SJosh Poimboeuf
4467326666SJosh Poimboeufset -o errexit
4567326666SJosh Poimboeufset -o nounset
4667326666SJosh Poimboeuf
4767326666SJosh Poimboeufusage() {
48689135f0SPeter Zijlstra (Intel)	echo "usage: faddr2line [--list] <object file> <func+offset> <func+offset>..." >&2
4967326666SJosh Poimboeuf	exit 1
5067326666SJosh Poimboeuf}
5167326666SJosh Poimboeuf
5267326666SJosh Poimboeufwarn() {
5367326666SJosh Poimboeuf	echo "$1" >&2
5467326666SJosh Poimboeuf}
5567326666SJosh Poimboeuf
5667326666SJosh Poimboeufdie() {
5767326666SJosh Poimboeuf	echo "ERROR: $1" >&2
5867326666SJosh Poimboeuf	exit 1
5967326666SJosh Poimboeuf}
6067326666SJosh Poimboeuf
611d1a0e7cSJosh PoimboeufREADELF="${CROSS_COMPILE:-}readelf"
621d1a0e7cSJosh PoimboeufADDR2LINE="${CROSS_COMPILE:-}addr2line"
631d1a0e7cSJosh PoimboeufAWK="awk"
641d1a0e7cSJosh Poimboeuf
651d1a0e7cSJosh Poimboeufcommand -v ${AWK} >/dev/null 2>&1 || die "${AWK} isn't installed"
661d1a0e7cSJosh Poimboeufcommand -v ${READELF} >/dev/null 2>&1 || die "${READELF} isn't installed"
671d1a0e7cSJosh Poimboeufcommand -v ${ADDR2LINE} >/dev/null 2>&1 || die "${ADDR2LINE} isn't installed"
681d1a0e7cSJosh Poimboeuf
6967326666SJosh Poimboeuf# Try to figure out the source directory prefix so we can remove it from the
7067326666SJosh Poimboeuf# addr2line output.  HACK ALERT: This assumes that start_kernel() is in
71f5f67cc0SRandy Dunlap# init/main.c!  This only works for vmlinux.  Otherwise it falls back to
7267326666SJosh Poimboeuf# printing the absolute path.
7367326666SJosh Poimboeuffind_dir_prefix() {
7467326666SJosh Poimboeuf	local objfile=$1
7567326666SJosh Poimboeuf
761d1a0e7cSJosh Poimboeuf	local start_kernel_addr=$(${READELF} --symbols --wide $objfile | ${AWK} '$8 == "start_kernel" {printf "0x%s", $2}')
7767326666SJosh Poimboeuf	[[ -z $start_kernel_addr ]] && return
7867326666SJosh Poimboeuf
7995a87982SLiu, Changcheng	local file_line=$(${ADDR2LINE} -e $objfile $start_kernel_addr)
8067326666SJosh Poimboeuf	[[ -z $file_line ]] && return
8167326666SJosh Poimboeuf
8267326666SJosh Poimboeuf	local prefix=${file_line%init/main.c:*}
8367326666SJosh Poimboeuf	if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then
8467326666SJosh Poimboeuf		return
8567326666SJosh Poimboeuf	fi
8667326666SJosh Poimboeuf
8767326666SJosh Poimboeuf	DIR_PREFIX=$prefix
8867326666SJosh Poimboeuf	return 0
8967326666SJosh Poimboeuf}
9067326666SJosh Poimboeuf
9167326666SJosh Poimboeuf__faddr2line() {
9267326666SJosh Poimboeuf	local objfile=$1
9367326666SJosh Poimboeuf	local func_addr=$2
9467326666SJosh Poimboeuf	local dir_prefix=$3
9567326666SJosh Poimboeuf	local print_warnings=$4
9667326666SJosh Poimboeuf
971d1a0e7cSJosh Poimboeuf	local sym_name=${func_addr%+*}
98*dcea997bSJosh Poimboeuf	local func_offset=${func_addr#*+}
99*dcea997bSJosh Poimboeuf	func_offset=${func_offset%/*}
1001d1a0e7cSJosh Poimboeuf	local user_size=
101*dcea997bSJosh Poimboeuf	local file_type
102*dcea997bSJosh Poimboeuf	local is_vmlinux=0
1031d1a0e7cSJosh Poimboeuf	[[ $func_addr =~ "/" ]] && user_size=${func_addr#*/}
10467326666SJosh Poimboeuf
105*dcea997bSJosh Poimboeuf	if [[ -z $sym_name ]] || [[ -z $func_offset ]] || [[ $sym_name = $func_addr ]]; then
10667326666SJosh Poimboeuf		warn "bad func+offset $func_addr"
10767326666SJosh Poimboeuf		DONE=1
10867326666SJosh Poimboeuf		return
10967326666SJosh Poimboeuf	fi
11067326666SJosh Poimboeuf
111*dcea997bSJosh Poimboeuf	# vmlinux uses absolute addresses in the section table rather than
112*dcea997bSJosh Poimboeuf	# section offsets.
113*dcea997bSJosh Poimboeuf	local file_type=$(${READELF} --file-header $objfile |
114*dcea997bSJosh Poimboeuf		${AWK} '$1 == "Type:" { print $2; exit }')
115*dcea997bSJosh Poimboeuf	[[ $file_type = "EXEC" ]] && is_vmlinux=1
116*dcea997bSJosh Poimboeuf
11767326666SJosh Poimboeuf	# Go through each of the object's symbols which match the func name.
1181d1a0e7cSJosh Poimboeuf	# In rare cases there might be duplicates, in which case we print all
1191d1a0e7cSJosh Poimboeuf	# matches.
1201d1a0e7cSJosh Poimboeuf	while read line; do
1211d1a0e7cSJosh Poimboeuf		local fields=($line)
1221d1a0e7cSJosh Poimboeuf		local sym_addr=0x${fields[1]}
1231d1a0e7cSJosh Poimboeuf		local sym_elf_size=${fields[2]}
1241d1a0e7cSJosh Poimboeuf		local sym_sec=${fields[6]}
125*dcea997bSJosh Poimboeuf		local sec_size
126*dcea997bSJosh Poimboeuf		local sec_name
127efdb4167SJosh Poimboeuf
1281d1a0e7cSJosh Poimboeuf		# Get the section size:
129*dcea997bSJosh Poimboeuf		sec_size=$(${READELF} --section-headers --wide $objfile |
1301d1a0e7cSJosh Poimboeuf			sed 's/\[ /\[/' |
1311d1a0e7cSJosh Poimboeuf			${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print "0x" $6; exit }')
1321d1a0e7cSJosh Poimboeuf
1331d1a0e7cSJosh Poimboeuf		if [[ -z $sec_size ]]; then
1341d1a0e7cSJosh Poimboeuf			warn "bad section size: section: $sym_sec"
135efdb4167SJosh Poimboeuf			DONE=1
136efdb4167SJosh Poimboeuf			return
137efdb4167SJosh Poimboeuf		fi
1381d1a0e7cSJosh Poimboeuf
139*dcea997bSJosh Poimboeuf		# Get the section name:
140*dcea997bSJosh Poimboeuf		sec_name=$(${READELF} --section-headers --wide $objfile |
141*dcea997bSJosh Poimboeuf			sed 's/\[ /\[/' |
142*dcea997bSJosh Poimboeuf			${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print $2; exit }')
143*dcea997bSJosh Poimboeuf
144*dcea997bSJosh Poimboeuf		if [[ -z $sec_name ]]; then
145*dcea997bSJosh Poimboeuf			warn "bad section name: section: $sym_sec"
146*dcea997bSJosh Poimboeuf			DONE=1
147*dcea997bSJosh Poimboeuf			return
148*dcea997bSJosh Poimboeuf		fi
149*dcea997bSJosh Poimboeuf
1501d1a0e7cSJosh Poimboeuf		# Calculate the symbol size.
1511d1a0e7cSJosh Poimboeuf		#
1521d1a0e7cSJosh Poimboeuf		# Unfortunately we can't use the ELF size, because kallsyms
1531d1a0e7cSJosh Poimboeuf		# also includes the padding bytes in its size calculation.  For
1541d1a0e7cSJosh Poimboeuf		# kallsyms, the size calculation is the distance between the
1551d1a0e7cSJosh Poimboeuf		# symbol and the next symbol in a sorted list.
1561d1a0e7cSJosh Poimboeuf		local sym_size
1571d1a0e7cSJosh Poimboeuf		local cur_sym_addr
1581d1a0e7cSJosh Poimboeuf		local found=0
1591d1a0e7cSJosh Poimboeuf		while read line; do
1601d1a0e7cSJosh Poimboeuf			local fields=($line)
1611d1a0e7cSJosh Poimboeuf			cur_sym_addr=0x${fields[1]}
1621d1a0e7cSJosh Poimboeuf			local cur_sym_elf_size=${fields[2]}
1631d1a0e7cSJosh Poimboeuf			local cur_sym_name=${fields[7]:-}
1641d1a0e7cSJosh Poimboeuf
1651d1a0e7cSJosh Poimboeuf			if [[ $cur_sym_addr = $sym_addr ]] &&
1661d1a0e7cSJosh Poimboeuf			   [[ $cur_sym_elf_size = $sym_elf_size ]] &&
1671d1a0e7cSJosh Poimboeuf			   [[ $cur_sym_name = $sym_name ]]; then
1681d1a0e7cSJosh Poimboeuf				found=1
1691d1a0e7cSJosh Poimboeuf				continue
1701d1a0e7cSJosh Poimboeuf			fi
1711d1a0e7cSJosh Poimboeuf
1721d1a0e7cSJosh Poimboeuf			if [[ $found = 1 ]]; then
1731d1a0e7cSJosh Poimboeuf				sym_size=$(($cur_sym_addr - $sym_addr))
1741d1a0e7cSJosh Poimboeuf				[[ $sym_size -lt $sym_elf_size ]] && continue;
1751d1a0e7cSJosh Poimboeuf				found=2
1761d1a0e7cSJosh Poimboeuf				break
1771d1a0e7cSJosh Poimboeuf			fi
1781d1a0e7cSJosh Poimboeuf		done < <(${READELF} --symbols --wide $objfile | ${AWK} -v sec=$sym_sec '$7 == sec' | sort --key=2)
1791d1a0e7cSJosh Poimboeuf
1801d1a0e7cSJosh Poimboeuf		if [[ $found = 0 ]]; then
1811d1a0e7cSJosh Poimboeuf			warn "can't find symbol: sym_name: $sym_name sym_sec: $sym_sec sym_addr: $sym_addr sym_elf_size: $sym_elf_size"
1821d1a0e7cSJosh Poimboeuf			DONE=1
1831d1a0e7cSJosh Poimboeuf			return
1841d1a0e7cSJosh Poimboeuf		fi
1851d1a0e7cSJosh Poimboeuf
1861d1a0e7cSJosh Poimboeuf		# If nothing was found after the symbol, assume it's the last
1871d1a0e7cSJosh Poimboeuf		# symbol in the section.
1881d1a0e7cSJosh Poimboeuf		[[ $found = 1 ]] && sym_size=$(($sec_size - $sym_addr))
1891d1a0e7cSJosh Poimboeuf
1901d1a0e7cSJosh Poimboeuf		if [[ -z $sym_size ]] || [[ $sym_size -le 0 ]]; then
1911d1a0e7cSJosh Poimboeuf			warn "bad symbol size: sym_addr: $sym_addr cur_sym_addr: $cur_sym_addr"
1921d1a0e7cSJosh Poimboeuf			DONE=1
1931d1a0e7cSJosh Poimboeuf			return
1941d1a0e7cSJosh Poimboeuf		fi
1951d1a0e7cSJosh Poimboeuf
196efdb4167SJosh Poimboeuf		sym_size=0x$(printf %x $sym_size)
19767326666SJosh Poimboeuf
198*dcea997bSJosh Poimboeuf		# Calculate the address from user-supplied offset:
199*dcea997bSJosh Poimboeuf		local addr=$(($sym_addr + $func_offset))
20067326666SJosh Poimboeuf		if [[ -z $addr ]] || [[ $addr = 0 ]]; then
201*dcea997bSJosh Poimboeuf			warn "bad address: $sym_addr + $func_offset"
20267326666SJosh Poimboeuf			DONE=1
20367326666SJosh Poimboeuf			return
20467326666SJosh Poimboeuf		fi
205efdb4167SJosh Poimboeuf		addr=0x$(printf %x $addr)
20667326666SJosh Poimboeuf
2071d1a0e7cSJosh Poimboeuf		# If the user provided a size, make sure it matches the symbol's size:
2081d1a0e7cSJosh Poimboeuf		if [[ -n $user_size ]] && [[ $user_size -ne $sym_size ]]; then
20967326666SJosh Poimboeuf			[[ $print_warnings = 1 ]] &&
2101d1a0e7cSJosh Poimboeuf				echo "skipping $sym_name address at $addr due to size mismatch ($user_size != $sym_size)"
21167326666SJosh Poimboeuf			continue;
21267326666SJosh Poimboeuf		fi
21367326666SJosh Poimboeuf
2141d1a0e7cSJosh Poimboeuf		# Make sure the provided offset is within the symbol's range:
215*dcea997bSJosh Poimboeuf		if [[ $func_offset -gt $sym_size ]]; then
21667326666SJosh Poimboeuf			[[ $print_warnings = 1 ]] &&
217*dcea997bSJosh Poimboeuf				echo "skipping $sym_name address at $addr due to size mismatch ($func_offset > $sym_size)"
21867326666SJosh Poimboeuf			continue
21967326666SJosh Poimboeuf		fi
22067326666SJosh Poimboeuf
2211d1a0e7cSJosh Poimboeuf		# In case of duplicates or multiple addresses specified on the
2221d1a0e7cSJosh Poimboeuf		# cmdline, separate multiple entries with a blank line:
22367326666SJosh Poimboeuf		[[ $FIRST = 0 ]] && echo
22467326666SJosh Poimboeuf		FIRST=0
22567326666SJosh Poimboeuf
226*dcea997bSJosh Poimboeuf		echo "$sym_name+$func_offset/$sym_size:"
2276870c016SChangbin Du
2281d1a0e7cSJosh Poimboeuf		# Pass section address to addr2line and strip absolute paths
2291d1a0e7cSJosh Poimboeuf		# from the output:
230*dcea997bSJosh Poimboeuf		local args="--functions --pretty-print --inlines --exe=$objfile"
231*dcea997bSJosh Poimboeuf		[[ $is_vmlinux = 0 ]] && args="$args --section=$sec_name"
232*dcea997bSJosh Poimboeuf		local output=$(${ADDR2LINE} $args $addr | sed "s; $dir_prefix\(\./\)*; ;")
2331d1a0e7cSJosh Poimboeuf		[[ -z $output ]] && continue
2341d1a0e7cSJosh Poimboeuf
2351d1a0e7cSJosh Poimboeuf		# Default output (non --list):
236689135f0SPeter Zijlstra (Intel)		if [[ $LIST = 0 ]]; then
2371d1a0e7cSJosh Poimboeuf			echo "$output" | while read -r line
238689135f0SPeter Zijlstra (Intel)			do
239689135f0SPeter Zijlstra (Intel)				echo $line
240689135f0SPeter Zijlstra (Intel)			done
241689135f0SPeter Zijlstra (Intel)			DONE=1;
2421d1a0e7cSJosh Poimboeuf			continue
243689135f0SPeter Zijlstra (Intel)		fi
244689135f0SPeter Zijlstra (Intel)
2451d1a0e7cSJosh Poimboeuf		# For --list, show each line with its corresponding source code:
2461d1a0e7cSJosh Poimboeuf		echo "$output" | while read -r line
2476870c016SChangbin Du		do
248689135f0SPeter Zijlstra (Intel)			echo
2496870c016SChangbin Du			echo $line
25078eb0c63SChangbin Du			n=$(echo $line | sed 's/.*:\([0-9]\+\).*/\1/g')
25178eb0c63SChangbin Du			n1=$[$n-5]
25278eb0c63SChangbin Du			n2=$[$n+5]
25378eb0c63SChangbin Du			f=$(echo $line | sed 's/.*at \(.\+\):.*/\1/g')
2541d1a0e7cSJosh Poimboeuf			${AWK} 'NR>=strtonum("'$n1'") && NR<=strtonum("'$n2'") { if (NR=='$n') printf(">%d<", NR); else printf(" %d ", NR); printf("\t%s\n", $0)}' $f
2556870c016SChangbin Du		done
2566870c016SChangbin Du
25767326666SJosh Poimboeuf		DONE=1
25867326666SJosh Poimboeuf
2591d1a0e7cSJosh Poimboeuf	done < <(${READELF} --symbols --wide $objfile | ${AWK} -v fn=$sym_name '$4 == "FUNC" && $8 == fn')
26067326666SJosh Poimboeuf}
26167326666SJosh Poimboeuf
26267326666SJosh Poimboeuf[[ $# -lt 2 ]] && usage
26367326666SJosh Poimboeuf
26467326666SJosh Poimboeufobjfile=$1
265689135f0SPeter Zijlstra (Intel)
266689135f0SPeter Zijlstra (Intel)LIST=0
267689135f0SPeter Zijlstra (Intel)[[ "$objfile" == "--list" ]] && LIST=1 && shift && objfile=$1
268689135f0SPeter Zijlstra (Intel)
26967326666SJosh Poimboeuf[[ ! -f $objfile ]] && die "can't find objfile $objfile"
27067326666SJosh Poimboeufshift
27167326666SJosh Poimboeuf
27267326666SJosh PoimboeufDIR_PREFIX=supercalifragilisticexpialidocious
27367326666SJosh Poimboeuffind_dir_prefix $objfile
27467326666SJosh Poimboeuf
27567326666SJosh PoimboeufFIRST=1
27667326666SJosh Poimboeufwhile [[ $# -gt 0 ]]; do
27767326666SJosh Poimboeuf	func_addr=$1
27867326666SJosh Poimboeuf	shift
27967326666SJosh Poimboeuf
28067326666SJosh Poimboeuf	# print any matches found
28167326666SJosh Poimboeuf	DONE=0
28267326666SJosh Poimboeuf	__faddr2line $objfile $func_addr $DIR_PREFIX 0
28367326666SJosh Poimboeuf
28467326666SJosh Poimboeuf	# if no match was found, print warnings
28567326666SJosh Poimboeuf	if [[ $DONE = 0 ]]; then
28667326666SJosh Poimboeuf		__faddr2line $objfile $func_addr $DIR_PREFIX 1
28767326666SJosh Poimboeuf		warn "no match for $func_addr"
28867326666SJosh Poimboeuf	fi
28967326666SJosh Poimboeufdone
290