1076979eeSKent Overstreet#!/usr/bin/env bash
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
3dbd1abb2SSasha Levin# (c) 2014, Sasha Levin <[email protected]>
4dbd1abb2SSasha Levin#set -x
5dbd1abb2SSasha Levin
626681eb3SStephen Boydusage() {
7dbd1abb2SSasha Levin	echo "Usage:"
8*a6d05e82SLuca Ceresoli	echo "	$0 -r <release>"
9*a6d05e82SLuca Ceresoli	echo "	$0 [<vmlinux> [<base_path>|auto [<modules_path>]]]"
1026681eb3SStephen Boyd}
11dbd1abb2SSasha Levin
1299115db4SMiguel Ojeda# Try to find a Rust demangler
1399115db4SMiguel Ojedaif type llvm-cxxfilt >/dev/null 2>&1 ; then
1499115db4SMiguel Ojeda	cppfilt=llvm-cxxfilt
1599115db4SMiguel Ojedaelif type c++filt >/dev/null 2>&1 ; then
1699115db4SMiguel Ojeda	cppfilt=c++filt
1799115db4SMiguel Ojeda	cppfilt_opts=-i
1899115db4SMiguel Ojedafi
1999115db4SMiguel Ojeda
20efbd6398SCarlos LlamasUTIL_SUFFIX=
21efbd6398SCarlos Llamasif [[ -z ${LLVM:-} ]]; then
22efbd6398SCarlos Llamas	UTIL_PREFIX=${CROSS_COMPILE:-}
23efbd6398SCarlos Llamaselse
24efbd6398SCarlos Llamas	UTIL_PREFIX=llvm-
25efbd6398SCarlos Llamas	if [[ ${LLVM} == */ ]]; then
26efbd6398SCarlos Llamas		UTIL_PREFIX=${LLVM}${UTIL_PREFIX}
27efbd6398SCarlos Llamas	elif [[ ${LLVM} == -* ]]; then
28efbd6398SCarlos Llamas		UTIL_SUFFIX=${LLVM}
29efbd6398SCarlos Llamas	fi
30efbd6398SCarlos Llamasfi
31efbd6398SCarlos Llamas
32efbd6398SCarlos LlamasREADELF=${UTIL_PREFIX}readelf${UTIL_SUFFIX}
33efbd6398SCarlos LlamasADDR2LINE=${UTIL_PREFIX}addr2line${UTIL_SUFFIX}
34b41838feSXiong NandiNM=${UTIL_PREFIX}nm${UTIL_SUFFIX}
35efbd6398SCarlos Llamas
36f90dde44SKonstantin Khlebnikovif [[ $1 == "-r" ]] ; then
37f90dde44SKonstantin Khlebnikov	vmlinux=""
38f90dde44SKonstantin Khlebnikov	basepath="auto"
39f90dde44SKonstantin Khlebnikov	modpath=""
40f90dde44SKonstantin Khlebnikov	release=$2
41f90dde44SKonstantin Khlebnikov
42f90dde44SKonstantin Khlebnikov	for fn in {,/usr/lib/debug}/boot/vmlinux-$release{,.debug} /lib/modules/$release{,/build}/vmlinux ; do
43f90dde44SKonstantin Khlebnikov		if [ -e "$fn" ] ; then
44f90dde44SKonstantin Khlebnikov			vmlinux=$fn
45f90dde44SKonstantin Khlebnikov			break
46f90dde44SKonstantin Khlebnikov		fi
47f90dde44SKonstantin Khlebnikov	done
48f90dde44SKonstantin Khlebnikov
49f90dde44SKonstantin Khlebnikov	if [[ $vmlinux == "" ]] ; then
50f90dde44SKonstantin Khlebnikov		echo "ERROR! vmlinux image for release $release is not found" >&2
5126681eb3SStephen Boyd		usage
52f90dde44SKonstantin Khlebnikov		exit 2
53f90dde44SKonstantin Khlebnikov	fi
54f90dde44SKonstantin Khlebnikovelse
55dbd1abb2SSasha Levin	vmlinux=$1
56ecda6e27SKonstantin Khlebnikov	basepath=${2-auto}
57310c6dd0SKonstantin Khlebnikov	modpath=$3
58431151b6SKonstantin Khlebnikov	release=""
5926681eb3SStephen Boyd	debuginfod=
6026681eb3SStephen Boyd
6126681eb3SStephen Boyd	# Can we use debuginfod-find?
6226681eb3SStephen Boyd	if type debuginfod-find >/dev/null 2>&1 ; then
6326681eb3SStephen Boyd		debuginfod=${1-only}
6426681eb3SStephen Boyd	fi
6526681eb3SStephen Boyd
6626681eb3SStephen Boyd	if [[ $vmlinux == "" && -z $debuginfod ]] ; then
6726681eb3SStephen Boyd		echo "ERROR! vmlinux image must be specified" >&2
6826681eb3SStephen Boyd		usage
6926681eb3SStephen Boyd		exit 1
7026681eb3SStephen Boyd	fi
71f90dde44SKonstantin Khlebnikovfi
72431151b6SKonstantin Khlebnikov
733af8acf6SSchspa Shideclare aarray_support=true
743af8acf6SSchspa Shideclare -A cache 2>/dev/null
753af8acf6SSchspa Shiif [[ $? != 0 ]]; then
763af8acf6SSchspa Shi	aarray_support=false
773af8acf6SSchspa Shielse
78310c6dd0SKonstantin Khlebnikov	declare -A modcache
793af8acf6SSchspa Shifi
80dbd1abb2SSasha Levin
81431151b6SKonstantin Khlebnikovfind_module() {
8226681eb3SStephen Boyd	if [[ -n $debuginfod ]] ; then
8326681eb3SStephen Boyd		if [[ -n $modbuildid ]] ; then
8426681eb3SStephen Boyd			debuginfod-find debuginfo $modbuildid && return
8526681eb3SStephen Boyd		fi
8626681eb3SStephen Boyd
8726681eb3SStephen Boyd		# Only using debuginfod so don't try to find vmlinux module path
8826681eb3SStephen Boyd		if [[ $debuginfod == "only" ]] ; then
8926681eb3SStephen Boyd			return
9026681eb3SStephen Boyd		fi
9126681eb3SStephen Boyd	fi
9226681eb3SStephen Boyd
930f69dc29SLuca Ceresoli	if [ -z $release ] ; then
940f69dc29SLuca Ceresoli		release=$(gdb -ex 'print init_uts_ns.name.release' -ex 'quit' -quiet -batch "$vmlinux" 2>/dev/null | sed -n 's/\$1 = "\(.*\)".*/\1/p')
950f69dc29SLuca Ceresoli	fi
960f69dc29SLuca Ceresoli	if [ -n "${release}" ] ; then
970f69dc29SLuca Ceresoli		release_dirs="/usr/lib/debug/lib/modules/$release /lib/modules/$release"
980f69dc29SLuca Ceresoli	fi
990f69dc29SLuca Ceresoli
1000f69dc29SLuca Ceresoli	found_without_debug_info=false
1010f69dc29SLuca Ceresoli	for dir in "$modpath" "$(dirname "$vmlinux")" ${release_dirs}; do
1020f69dc29SLuca Ceresoli		if [ -n "${dir}" ] && [ -e "${dir}" ]; then
1030f69dc29SLuca Ceresoli			for fn in $(find "$dir" -name "${module//_/[-_]}.ko*") ; do
104efbd6398SCarlos Llamas				if ${READELF} -WS "$fn" | grep -qwF .debug_line ; then
105431151b6SKonstantin Khlebnikov					echo $fn
106431151b6SKonstantin Khlebnikov					return
107431151b6SKonstantin Khlebnikov				fi
1080f69dc29SLuca Ceresoli				found_without_debug_info=true
109431151b6SKonstantin Khlebnikov			done
110431151b6SKonstantin Khlebnikov		fi
111431151b6SKonstantin Khlebnikov	done
112431151b6SKonstantin Khlebnikov
1130f69dc29SLuca Ceresoli	if [[ ${found_without_debug_info} == true ]]; then
1140f69dc29SLuca Ceresoli		echo "WARNING! No debugging info in module ${module}, rebuild with DEBUG_KERNEL and DEBUG_INFO" >&2
1150f69dc29SLuca Ceresoli	else
1160f69dc29SLuca Ceresoli		echo "WARNING! Cannot find .ko for module ${module}, please pass a valid module path" >&2
1170f69dc29SLuca Ceresoli	fi
1180f69dc29SLuca Ceresoli
119431151b6SKonstantin Khlebnikov	return 1
120431151b6SKonstantin Khlebnikov}
121431151b6SKonstantin Khlebnikov
122dbd1abb2SSasha Levinparse_symbol() {
123dbd1abb2SSasha Levin	# The structure of symbol at this point is:
124e260fe01SRobert Jarzmik	#   ([name]+[offset]/[total length])
125dbd1abb2SSasha Levin	#
126dbd1abb2SSasha Levin	# For example:
127dbd1abb2SSasha Levin	#   do_basic_setup+0x9c/0xbf
128dbd1abb2SSasha Levin
129310c6dd0SKonstantin Khlebnikov	if [[ $module == "" ]] ; then
130310c6dd0SKonstantin Khlebnikov		local objfile=$vmlinux
1313af8acf6SSchspa Shi	elif [[ $aarray_support == true && "${modcache[$module]+isset}" == "isset" ]]; then
132310c6dd0SKonstantin Khlebnikov		local objfile=${modcache[$module]}
133310c6dd0SKonstantin Khlebnikov	else
134431151b6SKonstantin Khlebnikov		local objfile=$(find_module)
135431151b6SKonstantin Khlebnikov		if [[ $objfile == "" ]] ; then
136a5dc8300SSasha Levin			return
137a5dc8300SSasha Levin		fi
1383af8acf6SSchspa Shi		if [[ $aarray_support == true ]]; then
139310c6dd0SKonstantin Khlebnikov			modcache[$module]=$objfile
140310c6dd0SKonstantin Khlebnikov		fi
1413af8acf6SSchspa Shi	fi
142310c6dd0SKonstantin Khlebnikov
143e260fe01SRobert Jarzmik	# Remove the englobing parenthesis
144e260fe01SRobert Jarzmik	symbol=${symbol#\(}
145e260fe01SRobert Jarzmik	symbol=${symbol%\)}
146dbd1abb2SSasha Levin
1471d6693fbSKonstantin Khlebnikov	# Strip segment
1481d6693fbSKonstantin Khlebnikov	local segment
1491d6693fbSKonstantin Khlebnikov	if [[ $symbol == *:* ]] ; then
1501d6693fbSKonstantin Khlebnikov		segment=${symbol%%:*}:
1511d6693fbSKonstantin Khlebnikov		symbol=${symbol#*:}
1521d6693fbSKonstantin Khlebnikov	fi
1531d6693fbSKonstantin Khlebnikov
154dbd1abb2SSasha Levin	# Strip the symbol name so that we could look it up
155dbd1abb2SSasha Levin	local name=${symbol%+*}
156dbd1abb2SSasha Levin
157dbd1abb2SSasha Levin	# Use 'nm vmlinux' to figure out the base address of said symbol.
158dbd1abb2SSasha Levin	# It's actually faster to call it every time than to load it
159dbd1abb2SSasha Levin	# all into bash.
1603af8acf6SSchspa Shi	if [[ $aarray_support == true && "${cache[$module,$name]+isset}" == "isset" ]]; then
161310c6dd0SKonstantin Khlebnikov		local base_addr=${cache[$module,$name]}
162dbd1abb2SSasha Levin	else
163b41838feSXiong Nandi		local base_addr=$(${NM} "$objfile" 2>/dev/null | awk '$3 == "'$name'" && ($2 == "t" || $2 == "T") {print $1; exit}')
164f643b9eeSKonstantin Khlebnikov		if [[ $base_addr == "" ]] ; then
165f643b9eeSKonstantin Khlebnikov			# address not found
166f643b9eeSKonstantin Khlebnikov			return
167f643b9eeSKonstantin Khlebnikov		fi
1683af8acf6SSchspa Shi		if [[ $aarray_support == true ]]; then
169310c6dd0SKonstantin Khlebnikov			cache[$module,$name]="$base_addr"
170dbd1abb2SSasha Levin		fi
1713af8acf6SSchspa Shi	fi
172dbd1abb2SSasha Levin	# Let's start doing the math to get the exact address into the
173dbd1abb2SSasha Levin	# symbol. First, strip out the symbol total length.
174dbd1abb2SSasha Levin	local expr=${symbol%/*}
175dbd1abb2SSasha Levin
176dbd1abb2SSasha Levin	# Now, replace the symbol name with the base address we found
177dbd1abb2SSasha Levin	# before.
178dbd1abb2SSasha Levin	expr=${expr/$name/0x$base_addr}
179dbd1abb2SSasha Levin
180dbd1abb2SSasha Levin	# Evaluate it to find the actual address
181dbd1abb2SSasha Levin	expr=$((expr))
182dbd1abb2SSasha Levin	local address=$(printf "%x\n" "$expr")
183dbd1abb2SSasha Levin
184dbd1abb2SSasha Levin	# Pass it to addr2line to get filename and line number
185dbd1abb2SSasha Levin	# Could get more than one result
1863af8acf6SSchspa Shi	if [[ $aarray_support == true && "${cache[$module,$address]+isset}" == "isset" ]]; then
187310c6dd0SKonstantin Khlebnikov		local code=${cache[$module,$address]}
188dbd1abb2SSasha Levin	else
189efbd6398SCarlos Llamas		local code=$(${ADDR2LINE} -i -e "$objfile" "$address" 2>/dev/null)
1903af8acf6SSchspa Shi		if [[ $aarray_support == true ]]; then
191310c6dd0SKonstantin Khlebnikov			cache[$module,$address]=$code
192dbd1abb2SSasha Levin		fi
1933af8acf6SSchspa Shi	fi
194dbd1abb2SSasha Levin
195dbd1abb2SSasha Levin	# addr2line doesn't return a proper error code if it fails, so
196dbd1abb2SSasha Levin	# we detect it using the value it prints so that we could preserve
197dbd1abb2SSasha Levin	# the offset/size into the function and bail out
198dbd1abb2SSasha Levin	if [[ $code == "??:0" ]]; then
199dbd1abb2SSasha Levin		return
200dbd1abb2SSasha Levin	fi
201dbd1abb2SSasha Levin
202d178770dSPi-Hsun Shih	# Strip out the base of the path on each line
203d178770dSPi-Hsun Shih	code=$(while read -r line; do echo "${line#$basepath/}"; done <<< "$code")
204dbd1abb2SSasha Levin
205dbd1abb2SSasha Levin	# In the case of inlines, move everything to same line
206dbd1abb2SSasha Levin	code=${code//$'\n'/' '}
207dbd1abb2SSasha Levin
20899115db4SMiguel Ojeda	# Demangle if the name looks like a Rust symbol and if
20999115db4SMiguel Ojeda	# we got a Rust demangler
21099115db4SMiguel Ojeda	if [[ $name =~ ^_R && $cppfilt != "" ]] ; then
21199115db4SMiguel Ojeda		name=$("$cppfilt" "$cppfilt_opts" "$name")
21299115db4SMiguel Ojeda	fi
21399115db4SMiguel Ojeda
214dbd1abb2SSasha Levin	# Replace old address with pretty line numbers
2151d6693fbSKonstantin Khlebnikov	symbol="$segment$name ($code)"
216dbd1abb2SSasha Levin}
217dbd1abb2SSasha Levin
21826681eb3SStephen Boyddebuginfod_get_vmlinux() {
21926681eb3SStephen Boyd	local vmlinux_buildid=${1##* }
22026681eb3SStephen Boyd
22126681eb3SStephen Boyd	if [[ $vmlinux != "" ]]; then
22226681eb3SStephen Boyd		return
22326681eb3SStephen Boyd	fi
22426681eb3SStephen Boyd
22526681eb3SStephen Boyd	if [[ $vmlinux_buildid =~ ^[0-9a-f]+ ]]; then
22626681eb3SStephen Boyd		vmlinux=$(debuginfod-find debuginfo $vmlinux_buildid)
22726681eb3SStephen Boyd		if [[ $? -ne 0 ]] ; then
22826681eb3SStephen Boyd			echo "ERROR! vmlinux image not found via debuginfod-find" >&2
22926681eb3SStephen Boyd			usage
23026681eb3SStephen Boyd			exit 2
23126681eb3SStephen Boyd		fi
23226681eb3SStephen Boyd		return
23326681eb3SStephen Boyd	fi
23426681eb3SStephen Boyd	echo "ERROR! Build ID for vmlinux not found. Try passing -r or specifying vmlinux" >&2
23526681eb3SStephen Boyd	usage
23626681eb3SStephen Boyd	exit 2
23726681eb3SStephen Boyd}
23826681eb3SStephen Boyd
239dbd1abb2SSasha Levindecode_code() {
240dbd1abb2SSasha Levin	local scripts=`dirname "${BASH_SOURCE[0]}"`
241dbd1abb2SSasha Levin
242dbd1abb2SSasha Levin	echo "$1" | $scripts/decodecode
243dbd1abb2SSasha Levin}
244dbd1abb2SSasha Levin
245dbd1abb2SSasha Levinhandle_line() {
24626681eb3SStephen Boyd	if [[ $basepath == "auto" && $vmlinux != "" ]] ; then
24726681eb3SStephen Boyd		module=""
24826681eb3SStephen Boyd		symbol="kernel_init+0x0/0x0"
24926681eb3SStephen Boyd		parse_symbol
25026681eb3SStephen Boyd		basepath=${symbol#kernel_init (}
25126681eb3SStephen Boyd		basepath=${basepath%/init/main.c:*)}
25226681eb3SStephen Boyd	fi
25326681eb3SStephen Boyd
254dbd1abb2SSasha Levin	local words
255dbd1abb2SSasha Levin
256dbd1abb2SSasha Levin	# Tokenize
257dbd1abb2SSasha Levin	read -a words <<<"$1"
258dbd1abb2SSasha Levin
259dbd1abb2SSasha Levin	# Remove hex numbers. Do it ourselves until it happens in the
260dbd1abb2SSasha Levin	# kernel
261dbd1abb2SSasha Levin
262dbd1abb2SSasha Levin	# We need to know the index of the last element before we
263dbd1abb2SSasha Levin	# remove elements because arrays are sparse
264dbd1abb2SSasha Levin	local last=$(( ${#words[@]} - 1 ))
265dbd1abb2SSasha Levin
266dbd1abb2SSasha Levin	for i in "${!words[@]}"; do
267dbd1abb2SSasha Levin		# Remove the address
268dbd1abb2SSasha Levin		if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then
269dbd1abb2SSasha Levin			unset words[$i]
270dbd1abb2SSasha Levin		fi
271dbd1abb2SSasha Levin
272dbd1abb2SSasha Levin		# Format timestamps with tabs
273dbd1abb2SSasha Levin		if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then
274dbd1abb2SSasha Levin			unset words[$i]
275dbd1abb2SSasha Levin			words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}")
276dbd1abb2SSasha Levin		fi
277dbd1abb2SSasha Levin	done
278dbd1abb2SSasha Levin
27926681eb3SStephen Boyd	if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then
28026681eb3SStephen Boyd		words[$last-1]="${words[$last-1]} ${words[$last]}"
28126681eb3SStephen Boyd		unset words[$last]
28226681eb3SStephen Boyd		last=$(( $last - 1 ))
28326681eb3SStephen Boyd	fi
28426681eb3SStephen Boyd
285310c6dd0SKonstantin Khlebnikov	if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then
286310c6dd0SKonstantin Khlebnikov		module=${words[$last]}
28778efbfb5SXiong Nandi		# some traces format is "(%pS)", which like "(foo+0x0/0x1 [bar])"
28878efbfb5SXiong Nandi		# so $module may like "[bar])". Strip the right parenthesis firstly
28978efbfb5SXiong Nandi		module=${module%\)}
290310c6dd0SKonstantin Khlebnikov		module=${module#\[}
291310c6dd0SKonstantin Khlebnikov		module=${module%\]}
29226681eb3SStephen Boyd		modbuildid=${module#* }
29326681eb3SStephen Boyd		module=${module% *}
29426681eb3SStephen Boyd		if [[ $modbuildid == $module ]]; then
29526681eb3SStephen Boyd			modbuildid=
29626681eb3SStephen Boyd		fi
297310c6dd0SKonstantin Khlebnikov		symbol=${words[$last-1]}
298310c6dd0SKonstantin Khlebnikov		unset words[$last-1]
299310c6dd0SKonstantin Khlebnikov	else
300dbd1abb2SSasha Levin		# The symbol is the last element, process it
301dbd1abb2SSasha Levin		symbol=${words[$last]}
302310c6dd0SKonstantin Khlebnikov		module=
30326681eb3SStephen Boyd		modbuildid=
304310c6dd0SKonstantin Khlebnikov	fi
305310c6dd0SKonstantin Khlebnikov
306dbd1abb2SSasha Levin	unset words[$last]
307dbd1abb2SSasha Levin	parse_symbol # modifies $symbol
308dbd1abb2SSasha Levin
309dbd1abb2SSasha Levin	# Add up the line number to the symbol
310310c6dd0SKonstantin Khlebnikov	echo "${words[@]}" "$symbol $module"
311dbd1abb2SSasha Levin}
312dbd1abb2SSasha Levin
313dbd1abb2SSasha Levinwhile read line; do
314436efd9eSBjorn Andersson	# Strip unexpected carriage return at end of line
315436efd9eSBjorn Andersson	line=${line%$'\r'}
316436efd9eSBjorn Andersson
317dbd1abb2SSasha Levin	# Let's see if we have an address in the line
31853938ee4SJosh Poimboeuf	if [[ $line =~ \[\<([^]]+)\>\] ]] ||
31953938ee4SJosh Poimboeuf	   [[ $line =~ [^+\ ]+\+0x[0-9a-f]+/0x[0-9a-f]+ ]]; then
320dbd1abb2SSasha Levin		# Translate address to line numbers
321dbd1abb2SSasha Levin		handle_line "$line"
322dbd1abb2SSasha Levin	# Is it a code line?
323dbd1abb2SSasha Levin	elif [[ $line == *Code:* ]]; then
324dbd1abb2SSasha Levin		decode_code "$line"
32526681eb3SStephen Boyd	# Is it a version line?
32626681eb3SStephen Boyd	elif [[ -n $debuginfod && $line =~ PID:\ [0-9]+\ Comm: ]]; then
32726681eb3SStephen Boyd		debuginfod_get_vmlinux "$line"
328dbd1abb2SSasha Levin	else
329dbd1abb2SSasha Levin		# Nothing special in this line, show it as is
330dbd1abb2SSasha Levin		echo "$line"
331dbd1abb2SSasha Levin	fi
332dbd1abb2SSasha Levindone
333