xref: /linux/scripts/faddr2line (revision 24f171c7e145f43b9f187578e89b0982ce87e54c)
16b4679fcSPankaj Raghav#!/usr/bin/env 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
6186bf86e1SWill DeaconUTIL_SUFFIX=""
6286bf86e1SWill Deaconif [[ "${LLVM:-}" == "" ]]; then
6386bf86e1SWill Deacon	UTIL_PREFIX=${CROSS_COMPILE:-}
6486bf86e1SWill Deaconelse
6586bf86e1SWill Deacon	UTIL_PREFIX=llvm-
6686bf86e1SWill Deacon
6786bf86e1SWill Deacon	if [[ "${LLVM}" == *"/" ]]; then
6886bf86e1SWill Deacon		UTIL_PREFIX=${LLVM}${UTIL_PREFIX}
6986bf86e1SWill Deacon	elif [[ "${LLVM}" == "-"* ]]; then
7086bf86e1SWill Deacon		UTIL_SUFFIX=${LLVM}
7186bf86e1SWill Deacon	fi
7286bf86e1SWill Deaconfi
7386bf86e1SWill Deacon
7486bf86e1SWill DeaconREADELF="${UTIL_PREFIX}readelf${UTIL_SUFFIX}"
7586bf86e1SWill DeaconADDR2LINE="${UTIL_PREFIX}addr2line${UTIL_SUFFIX}"
761d1a0e7cSJosh PoimboeufAWK="awk"
77a41a2e2eSJosh PoimboeufGREP="grep"
781d1a0e7cSJosh Poimboeuf
79567f9c42SJohn Wang# Enforce ASCII-only output from tools like readelf
80567f9c42SJohn Wang# ensuring sed processes strings correctly.
81567f9c42SJohn Wangexport LANG=C
82567f9c42SJohn Wang
831d1a0e7cSJosh Poimboeufcommand -v ${AWK} >/dev/null 2>&1 || die "${AWK} isn't installed"
841d1a0e7cSJosh Poimboeufcommand -v ${READELF} >/dev/null 2>&1 || die "${READELF} isn't installed"
851d1a0e7cSJosh Poimboeufcommand -v ${ADDR2LINE} >/dev/null 2>&1 || die "${ADDR2LINE} isn't installed"
861d1a0e7cSJosh Poimboeuf
8767326666SJosh Poimboeuf# Try to figure out the source directory prefix so we can remove it from the
8867326666SJosh Poimboeuf# addr2line output.  HACK ALERT: This assumes that start_kernel() is in
89f5f67cc0SRandy Dunlap# init/main.c!  This only works for vmlinux.  Otherwise it falls back to
9067326666SJosh Poimboeuf# printing the absolute path.
9167326666SJosh Poimboeuffind_dir_prefix() {
9239cf650dSBrian Johannesmeyer	local start_kernel_addr=$(echo "${ELF_SYMS}" | sed 's/\[.*\]//' |
932d77de15SSrikar Dronamraju		${AWK} '$8 == "start_kernel" {printf "0x%s", $2}')
9467326666SJosh Poimboeuf	[[ -z $start_kernel_addr ]] && return
9567326666SJosh Poimboeuf
96406b5c12SBrian Johannesmeyer	run_addr2line ${start_kernel_addr} ""
97406b5c12SBrian Johannesmeyer	[[ -z $ADDR2LINE_OUT ]] && return
9867326666SJosh Poimboeuf
99406b5c12SBrian Johannesmeyer	local file_line=${ADDR2LINE_OUT#* at }
100406b5c12SBrian Johannesmeyer	if [[ -z $file_line ]] || [[ $file_line = $ADDR2LINE_OUT ]]; then
101406b5c12SBrian Johannesmeyer		return
102406b5c12SBrian Johannesmeyer	fi
10367326666SJosh Poimboeuf	local prefix=${file_line%init/main.c:*}
10467326666SJosh Poimboeuf	if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then
10567326666SJosh Poimboeuf		return
10667326666SJosh Poimboeuf	fi
10767326666SJosh Poimboeuf
10867326666SJosh Poimboeuf	DIR_PREFIX=$prefix
10967326666SJosh Poimboeuf	return 0
11067326666SJosh Poimboeuf}
11167326666SJosh Poimboeuf
11239cf650dSBrian Johannesmeyerrun_readelf() {
11339cf650dSBrian Johannesmeyer	local objfile=$1
114*ff5c0466SPankaj Raghav	local tmpfile
115*ff5c0466SPankaj Raghav	tmpfile=$(mktemp)
116*ff5c0466SPankaj Raghav
117*ff5c0466SPankaj Raghav	${READELF} --file-header --section-headers --symbols --wide "$objfile" > "$tmpfile"
11839cf650dSBrian Johannesmeyer
119b8d9d949SBrian Johannesmeyer	# This assumes that readelf first prints the file header, then the section headers, then the symbols.
120b8d9d949SBrian Johannesmeyer	# Note: It seems that GNU readelf does not prefix section headers with the "There are X section headers"
121b8d9d949SBrian Johannesmeyer	# line when multiple options are given, so let's also match with the "Section Headers:" line.
122*ff5c0466SPankaj Raghav	ELF_FILEHEADER=$(sed -n '/There are [0-9]* section headers, starting at offset\|Section Headers:/q;p' "$tmpfile")
123*ff5c0466SPankaj Raghav	ELF_SECHEADERS=$(sed -n '/There are [0-9]* section headers, starting at offset\|Section Headers:/,$p' "$tmpfile" | sed -n '/Symbol table .* contains [0-9]* entries:/q;p')
124*ff5c0466SPankaj Raghav	ELF_SYMS=$(sed -n '/Symbol table .* contains [0-9]* entries:/,$p' "$tmpfile")
125*ff5c0466SPankaj Raghav
126*ff5c0466SPankaj Raghav	rm -f -- "$tmpfile"
12739cf650dSBrian Johannesmeyer}
12839cf650dSBrian Johannesmeyer
1292c809186SBrian Johannesmeyercheck_vmlinux() {
1302c809186SBrian Johannesmeyer	# vmlinux uses absolute addresses in the section table rather than
1312c809186SBrian Johannesmeyer	# section offsets.
1322c809186SBrian Johannesmeyer	IS_VMLINUX=0
1332c809186SBrian Johannesmeyer	local file_type=$(echo "${ELF_FILEHEADER}" |
1342c809186SBrian Johannesmeyer		${AWK} '$1 == "Type:" { print $2; exit }')
1352c809186SBrian Johannesmeyer	if [[ $file_type = "EXEC" ]] || [[ $file_type == "DYN" ]]; then
1362c809186SBrian Johannesmeyer		IS_VMLINUX=1
1372c809186SBrian Johannesmeyer	fi
1382c809186SBrian Johannesmeyer}
1392c809186SBrian Johannesmeyer
140e36b69e9SBrian Johannesmeyerinit_addr2line() {
141e36b69e9SBrian Johannesmeyer	local objfile=$1
142e36b69e9SBrian Johannesmeyer
143e36b69e9SBrian Johannesmeyer	check_vmlinux
144e36b69e9SBrian Johannesmeyer
145e36b69e9SBrian Johannesmeyer	ADDR2LINE_ARGS="--functions --pretty-print --inlines --addresses --exe=$objfile"
146e36b69e9SBrian Johannesmeyer	if [[ $IS_VMLINUX = 1 ]]; then
147e36b69e9SBrian Johannesmeyer		# If the executable file is vmlinux, we don't pass section names to
148e36b69e9SBrian Johannesmeyer		# addr2line, so we can launch it now as a single long-running process.
149e36b69e9SBrian Johannesmeyer		coproc ADDR2LINE_PROC (${ADDR2LINE} ${ADDR2LINE_ARGS})
150e36b69e9SBrian Johannesmeyer	fi
151e36b69e9SBrian Johannesmeyer}
152e36b69e9SBrian Johannesmeyer
153e36b69e9SBrian Johannesmeyerrun_addr2line() {
154e36b69e9SBrian Johannesmeyer	local addr=$1
155e36b69e9SBrian Johannesmeyer	local sec_name=$2
156e36b69e9SBrian Johannesmeyer
157e36b69e9SBrian Johannesmeyer	if [[ $IS_VMLINUX = 1 ]]; then
158e36b69e9SBrian Johannesmeyer		# We send to the addr2line process: (1) the address, then (2) a sentinel
159e36b69e9SBrian Johannesmeyer		# value, i.e., something that can't be interpreted as a valid address
160e36b69e9SBrian Johannesmeyer		# (i.e., ","). This causes addr2line to write out: (1) the answer for
161e36b69e9SBrian Johannesmeyer		# our address, then (2) either "?? ??:0" or "0x0...0: ..." (if
162e36b69e9SBrian Johannesmeyer		# using binutils' addr2line), or "," (if using LLVM's addr2line).
163e36b69e9SBrian Johannesmeyer		echo ${addr} >& "${ADDR2LINE_PROC[1]}"
164e36b69e9SBrian Johannesmeyer		echo "," >& "${ADDR2LINE_PROC[1]}"
165e36b69e9SBrian Johannesmeyer		local first_line
166e36b69e9SBrian Johannesmeyer		read -r first_line <& "${ADDR2LINE_PROC[0]}"
167e36b69e9SBrian Johannesmeyer		ADDR2LINE_OUT=$(echo "${first_line}" | sed 's/^0x[0-9a-fA-F]*: //')
168e36b69e9SBrian Johannesmeyer		while read -r line <& "${ADDR2LINE_PROC[0]}"; do
169e36b69e9SBrian Johannesmeyer			if [[ "$line" == "?? ??:0" ]] || [[ "$line" == "," ]] || [[ $(echo "$line" | ${GREP} "^0x00*: ") ]]; then
170e36b69e9SBrian Johannesmeyer				break
171e36b69e9SBrian Johannesmeyer			fi
172e36b69e9SBrian Johannesmeyer			ADDR2LINE_OUT+=$'\n'$(echo "$line" | sed 's/^0x[0-9a-fA-F]*: //')
173e36b69e9SBrian Johannesmeyer		done
174e36b69e9SBrian Johannesmeyer	else
175e36b69e9SBrian Johannesmeyer		# Run addr2line as a single invocation.
176e36b69e9SBrian Johannesmeyer		local sec_arg
177e36b69e9SBrian Johannesmeyer		[[ -z $sec_name ]] && sec_arg="" || sec_arg="--section=${sec_name}"
178e36b69e9SBrian Johannesmeyer		ADDR2LINE_OUT=$(${ADDR2LINE} ${ADDR2LINE_ARGS} ${sec_arg} ${addr} | sed 's/^0x[0-9a-fA-F]*: //')
179e36b69e9SBrian Johannesmeyer	fi
180e36b69e9SBrian Johannesmeyer}
181e36b69e9SBrian Johannesmeyer
18267326666SJosh Poimboeuf__faddr2line() {
18367326666SJosh Poimboeuf	local objfile=$1
18467326666SJosh Poimboeuf	local func_addr=$2
18567326666SJosh Poimboeuf	local dir_prefix=$3
18667326666SJosh Poimboeuf	local print_warnings=$4
18767326666SJosh Poimboeuf
1881d1a0e7cSJosh Poimboeuf	local sym_name=${func_addr%+*}
189dcea997bSJosh Poimboeuf	local func_offset=${func_addr#*+}
190dcea997bSJosh Poimboeuf	func_offset=${func_offset%/*}
1911d1a0e7cSJosh Poimboeuf	local user_size=
1921d1a0e7cSJosh Poimboeuf	[[ $func_addr =~ "/" ]] && user_size=${func_addr#*/}
19367326666SJosh Poimboeuf
194dcea997bSJosh Poimboeuf	if [[ -z $sym_name ]] || [[ -z $func_offset ]] || [[ $sym_name = $func_addr ]]; then
19567326666SJosh Poimboeuf		warn "bad func+offset $func_addr"
19667326666SJosh Poimboeuf		DONE=1
19767326666SJosh Poimboeuf		return
19867326666SJosh Poimboeuf	fi
19967326666SJosh Poimboeuf
20067326666SJosh Poimboeuf	# Go through each of the object's symbols which match the func name.
2011d1a0e7cSJosh Poimboeuf	# In rare cases there might be duplicates, in which case we print all
2021d1a0e7cSJosh Poimboeuf	# matches.
2031d1a0e7cSJosh Poimboeuf	while read line; do
2041d1a0e7cSJosh Poimboeuf		local fields=($line)
2051d1a0e7cSJosh Poimboeuf		local sym_addr=0x${fields[1]}
2061d1a0e7cSJosh Poimboeuf		local sym_elf_size=${fields[2]}
2071d1a0e7cSJosh Poimboeuf		local sym_sec=${fields[6]}
208dcea997bSJosh Poimboeuf		local sec_size
209dcea997bSJosh Poimboeuf		local sec_name
210efdb4167SJosh Poimboeuf
2111d1a0e7cSJosh Poimboeuf		# Get the section size:
21239cf650dSBrian Johannesmeyer		sec_size=$(echo "${ELF_SECHEADERS}" | sed 's/\[ /\[/' |
2131d1a0e7cSJosh Poimboeuf			${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print "0x" $6; exit }')
2141d1a0e7cSJosh Poimboeuf
2151d1a0e7cSJosh Poimboeuf		if [[ -z $sec_size ]]; then
2161d1a0e7cSJosh Poimboeuf			warn "bad section size: section: $sym_sec"
217efdb4167SJosh Poimboeuf			DONE=1
218efdb4167SJosh Poimboeuf			return
219efdb4167SJosh Poimboeuf		fi
2201d1a0e7cSJosh Poimboeuf
221dcea997bSJosh Poimboeuf		# Get the section name:
22239cf650dSBrian Johannesmeyer		sec_name=$(echo "${ELF_SECHEADERS}" | sed 's/\[ /\[/' |
223dcea997bSJosh Poimboeuf			${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print $2; exit }')
224dcea997bSJosh Poimboeuf
225dcea997bSJosh Poimboeuf		if [[ -z $sec_name ]]; then
226dcea997bSJosh Poimboeuf			warn "bad section name: section: $sym_sec"
227dcea997bSJosh Poimboeuf			DONE=1
228dcea997bSJosh Poimboeuf			return
229dcea997bSJosh Poimboeuf		fi
230dcea997bSJosh Poimboeuf
2311d1a0e7cSJosh Poimboeuf		# Calculate the symbol size.
2321d1a0e7cSJosh Poimboeuf		#
2331d1a0e7cSJosh Poimboeuf		# Unfortunately we can't use the ELF size, because kallsyms
2341d1a0e7cSJosh Poimboeuf		# also includes the padding bytes in its size calculation.  For
2351d1a0e7cSJosh Poimboeuf		# kallsyms, the size calculation is the distance between the
2361d1a0e7cSJosh Poimboeuf		# symbol and the next symbol in a sorted list.
2371d1a0e7cSJosh Poimboeuf		local sym_size
2381d1a0e7cSJosh Poimboeuf		local cur_sym_addr
2391d1a0e7cSJosh Poimboeuf		local found=0
2401d1a0e7cSJosh Poimboeuf		while read line; do
2411d1a0e7cSJosh Poimboeuf			local fields=($line)
2421d1a0e7cSJosh Poimboeuf			cur_sym_addr=0x${fields[1]}
2431d1a0e7cSJosh Poimboeuf			local cur_sym_elf_size=${fields[2]}
2441d1a0e7cSJosh Poimboeuf			local cur_sym_name=${fields[7]:-}
2451d1a0e7cSJosh Poimboeuf
24660fd39afSWill Deacon			# is_mapping_symbol(cur_sym_name)
24760fd39afSWill Deacon			if [[ ${cur_sym_name} =~ ^(\.L|L0|\$) ]]; then
24860fd39afSWill Deacon				continue
24960fd39afSWill Deacon			fi
25060fd39afSWill Deacon
2511d1a0e7cSJosh Poimboeuf			if [[ $cur_sym_addr = $sym_addr ]] &&
2521d1a0e7cSJosh Poimboeuf			   [[ $cur_sym_elf_size = $sym_elf_size ]] &&
2531d1a0e7cSJosh Poimboeuf			   [[ $cur_sym_name = $sym_name ]]; then
2541d1a0e7cSJosh Poimboeuf				found=1
2551d1a0e7cSJosh Poimboeuf				continue
2561d1a0e7cSJosh Poimboeuf			fi
2571d1a0e7cSJosh Poimboeuf
2581d1a0e7cSJosh Poimboeuf			if [[ $found = 1 ]]; then
2591d1a0e7cSJosh Poimboeuf				sym_size=$(($cur_sym_addr - $sym_addr))
2601d1a0e7cSJosh Poimboeuf				[[ $sym_size -lt $sym_elf_size ]] && continue;
2611d1a0e7cSJosh Poimboeuf				found=2
2621d1a0e7cSJosh Poimboeuf				break
2631d1a0e7cSJosh Poimboeuf			fi
26456ac7bd2SCarlos Llamas		done < <(echo "${ELF_SYMS}" | sed 's/\[.*\]//' | ${AWK} -v sec=$sym_sec '$7 == sec' | sort --key=2)
2651d1a0e7cSJosh Poimboeuf
2661d1a0e7cSJosh Poimboeuf		if [[ $found = 0 ]]; then
2671d1a0e7cSJosh Poimboeuf			warn "can't find symbol: sym_name: $sym_name sym_sec: $sym_sec sym_addr: $sym_addr sym_elf_size: $sym_elf_size"
2681d1a0e7cSJosh Poimboeuf			DONE=1
2691d1a0e7cSJosh Poimboeuf			return
2701d1a0e7cSJosh Poimboeuf		fi
2711d1a0e7cSJosh Poimboeuf
2721d1a0e7cSJosh Poimboeuf		# If nothing was found after the symbol, assume it's the last
2731d1a0e7cSJosh Poimboeuf		# symbol in the section.
2741d1a0e7cSJosh Poimboeuf		[[ $found = 1 ]] && sym_size=$(($sec_size - $sym_addr))
2751d1a0e7cSJosh Poimboeuf
2761d1a0e7cSJosh Poimboeuf		if [[ -z $sym_size ]] || [[ $sym_size -le 0 ]]; then
2771d1a0e7cSJosh Poimboeuf			warn "bad symbol size: sym_addr: $sym_addr cur_sym_addr: $cur_sym_addr"
2781d1a0e7cSJosh Poimboeuf			DONE=1
2791d1a0e7cSJosh Poimboeuf			return
2801d1a0e7cSJosh Poimboeuf		fi
2811d1a0e7cSJosh Poimboeuf
282efdb4167SJosh Poimboeuf		sym_size=0x$(printf %x $sym_size)
28367326666SJosh Poimboeuf
284dcea997bSJosh Poimboeuf		# Calculate the address from user-supplied offset:
285dcea997bSJosh Poimboeuf		local addr=$(($sym_addr + $func_offset))
28667326666SJosh Poimboeuf		if [[ -z $addr ]] || [[ $addr = 0 ]]; then
287dcea997bSJosh Poimboeuf			warn "bad address: $sym_addr + $func_offset"
28867326666SJosh Poimboeuf			DONE=1
28967326666SJosh Poimboeuf			return
29067326666SJosh Poimboeuf		fi
291efdb4167SJosh Poimboeuf		addr=0x$(printf %x $addr)
29267326666SJosh Poimboeuf
2931d1a0e7cSJosh Poimboeuf		# If the user provided a size, make sure it matches the symbol's size:
2941d1a0e7cSJosh Poimboeuf		if [[ -n $user_size ]] && [[ $user_size -ne $sym_size ]]; then
29567326666SJosh Poimboeuf			[[ $print_warnings = 1 ]] &&
2961d1a0e7cSJosh Poimboeuf				echo "skipping $sym_name address at $addr due to size mismatch ($user_size != $sym_size)"
29767326666SJosh Poimboeuf			continue;
29867326666SJosh Poimboeuf		fi
29967326666SJosh Poimboeuf
3001d1a0e7cSJosh Poimboeuf		# Make sure the provided offset is within the symbol's range:
301dcea997bSJosh Poimboeuf		if [[ $func_offset -gt $sym_size ]]; then
30267326666SJosh Poimboeuf			[[ $print_warnings = 1 ]] &&
303dcea997bSJosh Poimboeuf				echo "skipping $sym_name address at $addr due to size mismatch ($func_offset > $sym_size)"
30467326666SJosh Poimboeuf			continue
30567326666SJosh Poimboeuf		fi
30667326666SJosh Poimboeuf
3071d1a0e7cSJosh Poimboeuf		# In case of duplicates or multiple addresses specified on the
3081d1a0e7cSJosh Poimboeuf		# cmdline, separate multiple entries with a blank line:
30967326666SJosh Poimboeuf		[[ $FIRST = 0 ]] && echo
31067326666SJosh Poimboeuf		FIRST=0
31167326666SJosh Poimboeuf
312dcea997bSJosh Poimboeuf		echo "$sym_name+$func_offset/$sym_size:"
3136870c016SChangbin Du
3141d1a0e7cSJosh Poimboeuf		# Pass section address to addr2line and strip absolute paths
3151d1a0e7cSJosh Poimboeuf		# from the output:
316e36b69e9SBrian Johannesmeyer		run_addr2line $addr $sec_name
317e36b69e9SBrian Johannesmeyer		local output=$(echo "${ADDR2LINE_OUT}" | sed "s; $dir_prefix\(\./\)*; ;")
3181d1a0e7cSJosh Poimboeuf		[[ -z $output ]] && continue
3191d1a0e7cSJosh Poimboeuf
3201d1a0e7cSJosh Poimboeuf		# Default output (non --list):
321689135f0SPeter Zijlstra (Intel)		if [[ $LIST = 0 ]]; then
3221d1a0e7cSJosh Poimboeuf			echo "$output" | while read -r line
323689135f0SPeter Zijlstra (Intel)			do
324689135f0SPeter Zijlstra (Intel)				echo $line
325689135f0SPeter Zijlstra (Intel)			done
326689135f0SPeter Zijlstra (Intel)			DONE=1;
3271d1a0e7cSJosh Poimboeuf			continue
328689135f0SPeter Zijlstra (Intel)		fi
329689135f0SPeter Zijlstra (Intel)
3301d1a0e7cSJosh Poimboeuf		# For --list, show each line with its corresponding source code:
3311d1a0e7cSJosh Poimboeuf		echo "$output" | while read -r line
3326870c016SChangbin Du		do
333689135f0SPeter Zijlstra (Intel)			echo
3346870c016SChangbin Du			echo $line
33578eb0c63SChangbin Du			n=$(echo $line | sed 's/.*:\([0-9]\+\).*/\1/g')
33678eb0c63SChangbin Du			n1=$[$n-5]
33778eb0c63SChangbin Du			n2=$[$n+5]
33878eb0c63SChangbin Du			f=$(echo $line | sed 's/.*at \(.\+\):.*/\1/g')
3391d1a0e7cSJosh Poimboeuf			${AWK} 'NR>=strtonum("'$n1'") && NR<=strtonum("'$n2'") { if (NR=='$n') printf(">%d<", NR); else printf(" %d ", NR); printf("\t%s\n", $0)}' $f
3406870c016SChangbin Du		done
3416870c016SChangbin Du
34267326666SJosh Poimboeuf		DONE=1
34367326666SJosh Poimboeuf
34439cf650dSBrian Johannesmeyer	done < <(echo "${ELF_SYMS}" | sed 's/\[.*\]//' | ${AWK} -v fn=$sym_name '$8 == fn')
34567326666SJosh Poimboeuf}
34667326666SJosh Poimboeuf
34767326666SJosh Poimboeuf[[ $# -lt 2 ]] && usage
34867326666SJosh Poimboeuf
34967326666SJosh Poimboeufobjfile=$1
350689135f0SPeter Zijlstra (Intel)
351689135f0SPeter Zijlstra (Intel)LIST=0
352689135f0SPeter Zijlstra (Intel)[[ "$objfile" == "--list" ]] && LIST=1 && shift && objfile=$1
353689135f0SPeter Zijlstra (Intel)
35467326666SJosh Poimboeuf[[ ! -f $objfile ]] && die "can't find objfile $objfile"
35567326666SJosh Poimboeufshift
35667326666SJosh Poimboeuf
35739cf650dSBrian Johannesmeyerrun_readelf $objfile
35839cf650dSBrian Johannesmeyer
35939cf650dSBrian Johannesmeyerecho "${ELF_SECHEADERS}" | ${GREP} -q '\.debug_info' || die "CONFIG_DEBUG_INFO not enabled"
360a41a2e2eSJosh Poimboeuf
361e36b69e9SBrian Johannesmeyerinit_addr2line $objfile
3622c809186SBrian Johannesmeyer
36367326666SJosh PoimboeufDIR_PREFIX=supercalifragilisticexpialidocious
364406b5c12SBrian Johannesmeyerfind_dir_prefix
36567326666SJosh Poimboeuf
36667326666SJosh PoimboeufFIRST=1
36767326666SJosh Poimboeufwhile [[ $# -gt 0 ]]; do
36867326666SJosh Poimboeuf	func_addr=$1
36967326666SJosh Poimboeuf	shift
37067326666SJosh Poimboeuf
37167326666SJosh Poimboeuf	# print any matches found
37267326666SJosh Poimboeuf	DONE=0
37367326666SJosh Poimboeuf	__faddr2line $objfile $func_addr $DIR_PREFIX 0
37467326666SJosh Poimboeuf
37567326666SJosh Poimboeuf	# if no match was found, print warnings
37667326666SJosh Poimboeuf	if [[ $DONE = 0 ]]; then
37767326666SJosh Poimboeuf		__faddr2line $objfile $func_addr $DIR_PREFIX 1
37867326666SJosh Poimboeuf		warn "no match for $func_addr"
37967326666SJosh Poimboeuf	fi
38067326666SJosh Poimboeufdone
381