xref: /openbmc/linux/scripts/faddr2line (revision 67326666e2d45ebea7db3ed8e3e735f15e60dd91)
1*67326666SJosh Poimboeuf#!/bin/bash
2*67326666SJosh Poimboeuf#
3*67326666SJosh Poimboeuf# Translate stack dump function offsets.
4*67326666SJosh Poimboeuf#
5*67326666SJosh Poimboeuf# addr2line doesn't work with KASLR addresses.  This works similarly to
6*67326666SJosh Poimboeuf# addr2line, but instead takes the 'func+0x123' format as input:
7*67326666SJosh Poimboeuf#
8*67326666SJosh Poimboeuf#   $ ./scripts/faddr2line ~/k/vmlinux meminfo_proc_show+0x5/0x568
9*67326666SJosh Poimboeuf#   meminfo_proc_show+0x5/0x568:
10*67326666SJosh Poimboeuf#   meminfo_proc_show at fs/proc/meminfo.c:27
11*67326666SJosh Poimboeuf#
12*67326666SJosh Poimboeuf# If the address is part of an inlined function, the full inline call chain is
13*67326666SJosh Poimboeuf# printed:
14*67326666SJosh Poimboeuf#
15*67326666SJosh Poimboeuf#   $ ./scripts/faddr2line ~/k/vmlinux native_write_msr+0x6/0x27
16*67326666SJosh Poimboeuf#   native_write_msr+0x6/0x27:
17*67326666SJosh Poimboeuf#   arch_static_branch at arch/x86/include/asm/msr.h:121
18*67326666SJosh Poimboeuf#    (inlined by) static_key_false at include/linux/jump_label.h:125
19*67326666SJosh Poimboeuf#    (inlined by) native_write_msr at arch/x86/include/asm/msr.h:125
20*67326666SJosh Poimboeuf#
21*67326666SJosh Poimboeuf# The function size after the '/' in the input is optional, but recommended.
22*67326666SJosh Poimboeuf# It's used to help disambiguate any duplicate symbol names, which can occur
23*67326666SJosh Poimboeuf# rarely.  If the size is omitted for a duplicate symbol then it's possible for
24*67326666SJosh Poimboeuf# multiple code sites to be printed:
25*67326666SJosh Poimboeuf#
26*67326666SJosh Poimboeuf#   $ ./scripts/faddr2line ~/k/vmlinux raw_ioctl+0x5
27*67326666SJosh Poimboeuf#   raw_ioctl+0x5/0x20:
28*67326666SJosh Poimboeuf#   raw_ioctl at drivers/char/raw.c:122
29*67326666SJosh Poimboeuf#
30*67326666SJosh Poimboeuf#   raw_ioctl+0x5/0xb1:
31*67326666SJosh Poimboeuf#   raw_ioctl at net/ipv4/raw.c:876
32*67326666SJosh Poimboeuf#
33*67326666SJosh Poimboeuf# Multiple addresses can be specified on a single command line:
34*67326666SJosh Poimboeuf#
35*67326666SJosh Poimboeuf#   $ ./scripts/faddr2line ~/k/vmlinux type_show+0x10/45 free_reserved_area+0x90
36*67326666SJosh Poimboeuf#   type_show+0x10/0x2d:
37*67326666SJosh Poimboeuf#   type_show at drivers/video/backlight/backlight.c:213
38*67326666SJosh Poimboeuf#
39*67326666SJosh Poimboeuf#   free_reserved_area+0x90/0x123:
40*67326666SJosh Poimboeuf#   free_reserved_area at mm/page_alloc.c:6429 (discriminator 2)
41*67326666SJosh Poimboeuf
42*67326666SJosh Poimboeuf
43*67326666SJosh Poimboeufset -o errexit
44*67326666SJosh Poimboeufset -o nounset
45*67326666SJosh Poimboeuf
46*67326666SJosh Poimboeufcommand -v awk >/dev/null 2>&1 || die "awk isn't installed"
47*67326666SJosh Poimboeufcommand -v readelf >/dev/null 2>&1 || die "readelf isn't installed"
48*67326666SJosh Poimboeufcommand -v addr2line >/dev/null 2>&1 || die "addr2line isn't installed"
49*67326666SJosh Poimboeuf
50*67326666SJosh Poimboeufusage() {
51*67326666SJosh Poimboeuf	echo "usage: faddr2line <object file> <func+offset> <func+offset>..." >&2
52*67326666SJosh Poimboeuf	exit 1
53*67326666SJosh Poimboeuf}
54*67326666SJosh Poimboeuf
55*67326666SJosh Poimboeufwarn() {
56*67326666SJosh Poimboeuf	echo "$1" >&2
57*67326666SJosh Poimboeuf}
58*67326666SJosh Poimboeuf
59*67326666SJosh Poimboeufdie() {
60*67326666SJosh Poimboeuf	echo "ERROR: $1" >&2
61*67326666SJosh Poimboeuf	exit 1
62*67326666SJosh Poimboeuf}
63*67326666SJosh Poimboeuf
64*67326666SJosh Poimboeuf# Try to figure out the source directory prefix so we can remove it from the
65*67326666SJosh Poimboeuf# addr2line output.  HACK ALERT: This assumes that start_kernel() is in
66*67326666SJosh Poimboeuf# kernel/init.c!  This only works for vmlinux.  Otherwise it falls back to
67*67326666SJosh Poimboeuf# printing the absolute path.
68*67326666SJosh Poimboeuffind_dir_prefix() {
69*67326666SJosh Poimboeuf	local objfile=$1
70*67326666SJosh Poimboeuf
71*67326666SJosh Poimboeuf	local start_kernel_addr=$(readelf -sW $objfile | awk '$8 == "start_kernel" {printf "0x%s", $2}')
72*67326666SJosh Poimboeuf	[[ -z $start_kernel_addr ]] && return
73*67326666SJosh Poimboeuf
74*67326666SJosh Poimboeuf	local file_line=$(addr2line -e $objfile $start_kernel_addr)
75*67326666SJosh Poimboeuf	[[ -z $file_line ]] && return
76*67326666SJosh Poimboeuf
77*67326666SJosh Poimboeuf	local prefix=${file_line%init/main.c:*}
78*67326666SJosh Poimboeuf	if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then
79*67326666SJosh Poimboeuf		return
80*67326666SJosh Poimboeuf	fi
81*67326666SJosh Poimboeuf
82*67326666SJosh Poimboeuf	DIR_PREFIX=$prefix
83*67326666SJosh Poimboeuf	return 0
84*67326666SJosh Poimboeuf}
85*67326666SJosh Poimboeuf
86*67326666SJosh Poimboeuf__faddr2line() {
87*67326666SJosh Poimboeuf	local objfile=$1
88*67326666SJosh Poimboeuf	local func_addr=$2
89*67326666SJosh Poimboeuf	local dir_prefix=$3
90*67326666SJosh Poimboeuf	local print_warnings=$4
91*67326666SJosh Poimboeuf
92*67326666SJosh Poimboeuf	local func=${func_addr%+*}
93*67326666SJosh Poimboeuf	local offset=${func_addr#*+}
94*67326666SJosh Poimboeuf	offset=${offset%/*}
95*67326666SJosh Poimboeuf	local size=
96*67326666SJosh Poimboeuf	[[ $func_addr =~ "/" ]] && size=${func_addr#*/}
97*67326666SJosh Poimboeuf
98*67326666SJosh Poimboeuf	if [[ -z $func ]] || [[ -z $offset ]] || [[ $func = $func_addr ]]; then
99*67326666SJosh Poimboeuf		warn "bad func+offset $func_addr"
100*67326666SJosh Poimboeuf		DONE=1
101*67326666SJosh Poimboeuf		return
102*67326666SJosh Poimboeuf	fi
103*67326666SJosh Poimboeuf
104*67326666SJosh Poimboeuf	# Go through each of the object's symbols which match the func name.
105*67326666SJosh Poimboeuf	# In rare cases there might be duplicates.
106*67326666SJosh Poimboeuf	while read symbol; do
107*67326666SJosh Poimboeuf		local fields=($symbol)
108*67326666SJosh Poimboeuf		local sym_base=0x${fields[1]}
109*67326666SJosh Poimboeuf		local sym_size=${fields[2]}
110*67326666SJosh Poimboeuf		local sym_type=${fields[3]}
111*67326666SJosh Poimboeuf
112*67326666SJosh Poimboeuf		# calculate the address
113*67326666SJosh Poimboeuf		local addr=$(($sym_base + $offset))
114*67326666SJosh Poimboeuf		if [[ -z $addr ]] || [[ $addr = 0 ]]; then
115*67326666SJosh Poimboeuf			warn "bad address: $sym_base + $offset"
116*67326666SJosh Poimboeuf			DONE=1
117*67326666SJosh Poimboeuf			return
118*67326666SJosh Poimboeuf		fi
119*67326666SJosh Poimboeuf		local hexaddr=0x$(printf %x $addr)
120*67326666SJosh Poimboeuf
121*67326666SJosh Poimboeuf		# weed out non-function symbols
122*67326666SJosh Poimboeuf		if [[ $sym_type != "FUNC" ]]; then
123*67326666SJosh Poimboeuf			[[ $print_warnings = 1 ]] &&
124*67326666SJosh Poimboeuf				echo "skipping $func address at $hexaddr due to non-function symbol"
125*67326666SJosh Poimboeuf			continue
126*67326666SJosh Poimboeuf		fi
127*67326666SJosh Poimboeuf
128*67326666SJosh Poimboeuf		# if the user provided a size, make sure it matches the symbol's size
129*67326666SJosh Poimboeuf		if [[ -n $size ]] && [[ $size -ne $sym_size ]]; then
130*67326666SJosh Poimboeuf			[[ $print_warnings = 1 ]] &&
131*67326666SJosh Poimboeuf				echo "skipping $func address at $hexaddr due to size mismatch ($size != $sym_size)"
132*67326666SJosh Poimboeuf			continue;
133*67326666SJosh Poimboeuf		fi
134*67326666SJosh Poimboeuf
135*67326666SJosh Poimboeuf		# make sure the provided offset is within the symbol's range
136*67326666SJosh Poimboeuf		if [[ $offset -gt $sym_size ]]; then
137*67326666SJosh Poimboeuf			[[ $print_warnings = 1 ]] &&
138*67326666SJosh Poimboeuf				echo "skipping $func address at $hexaddr due to size mismatch ($offset > $sym_size)"
139*67326666SJosh Poimboeuf			continue
140*67326666SJosh Poimboeuf		fi
141*67326666SJosh Poimboeuf
142*67326666SJosh Poimboeuf		# separate multiple entries with a blank line
143*67326666SJosh Poimboeuf		[[ $FIRST = 0 ]] && echo
144*67326666SJosh Poimboeuf		FIRST=0
145*67326666SJosh Poimboeuf
146*67326666SJosh Poimboeuf		local hexsize=0x$(printf %x $sym_size)
147*67326666SJosh Poimboeuf		echo "$func+$offset/$hexsize:"
148*67326666SJosh Poimboeuf		addr2line -fpie $objfile $hexaddr | sed "s;$dir_prefix;;"
149*67326666SJosh Poimboeuf		DONE=1
150*67326666SJosh Poimboeuf
151*67326666SJosh Poimboeuf	done < <(readelf -sW $objfile | awk -v f=$func '$8 == f {print}')
152*67326666SJosh Poimboeuf}
153*67326666SJosh Poimboeuf
154*67326666SJosh Poimboeuf[[ $# -lt 2 ]] && usage
155*67326666SJosh Poimboeuf
156*67326666SJosh Poimboeufobjfile=$1
157*67326666SJosh Poimboeuf[[ ! -f $objfile ]] && die "can't find objfile $objfile"
158*67326666SJosh Poimboeufshift
159*67326666SJosh Poimboeuf
160*67326666SJosh PoimboeufDIR_PREFIX=supercalifragilisticexpialidocious
161*67326666SJosh Poimboeuffind_dir_prefix $objfile
162*67326666SJosh Poimboeuf
163*67326666SJosh PoimboeufFIRST=1
164*67326666SJosh Poimboeufwhile [[ $# -gt 0 ]]; do
165*67326666SJosh Poimboeuf	func_addr=$1
166*67326666SJosh Poimboeuf	shift
167*67326666SJosh Poimboeuf
168*67326666SJosh Poimboeuf	# print any matches found
169*67326666SJosh Poimboeuf	DONE=0
170*67326666SJosh Poimboeuf	__faddr2line $objfile $func_addr $DIR_PREFIX 0
171*67326666SJosh Poimboeuf
172*67326666SJosh Poimboeuf	# if no match was found, print warnings
173*67326666SJosh Poimboeuf	if [[ $DONE = 0 ]]; then
174*67326666SJosh Poimboeuf		__faddr2line $objfile $func_addr $DIR_PREFIX 1
175*67326666SJosh Poimboeuf		warn "no match for $func_addr"
176*67326666SJosh Poimboeuf	fi
177*67326666SJosh Poimboeufdone
178