blob: bfe5a4082d8eaf7af2e3be0194f78ae1db14e09e [file] [log] [blame]
Sasha Levindbd1abb2014-06-04 11:39:23 -04001#!/bin/bash
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01002# SPDX-License-Identifier: GPL-2.0
Sasha Levindbd1abb2014-06-04 11:39:23 -04003# (c) 2014, Sasha Levin <sasha.levin@oracle.com>
4#set -x
5
Stephen Boyd26681eb2021-07-07 18:09:31 -07006usage() {
Sasha Levindbd1abb2014-06-04 11:39:23 -04007 echo "Usage:"
Stephen Boydd5ce7572021-07-07 18:09:38 -07008 echo " $0 -r <release> | <vmlinux> [<base path>|auto] [<modules path>]"
Stephen Boyd26681eb2021-07-07 18:09:31 -07009}
Sasha Levindbd1abb2014-06-04 11:39:23 -040010
Miguel Ojeda473791d2021-12-05 19:00:43 +010011# Try to find a Rust demangler
12if type llvm-cxxfilt >/dev/null 2>&1 ; then
13 cppfilt=llvm-cxxfilt
14elif type c++filt >/dev/null 2>&1 ; then
15 cppfilt=c++filt
16 cppfilt_opts=-i
17fi
18
Carlos Llamas3b3e4d32023-09-29 03:48:17 +000019UTIL_SUFFIX=
20if [[ -z ${LLVM:-} ]]; then
21 UTIL_PREFIX=${CROSS_COMPILE:-}
22else
23 UTIL_PREFIX=llvm-
24 if [[ ${LLVM} == */ ]]; then
25 UTIL_PREFIX=${LLVM}${UTIL_PREFIX}
26 elif [[ ${LLVM} == -* ]]; then
27 UTIL_SUFFIX=${LLVM}
28 fi
29fi
30
31READELF=${UTIL_PREFIX}readelf${UTIL_SUFFIX}
32ADDR2LINE=${UTIL_PREFIX}addr2line${UTIL_SUFFIX}
33
Konstantin Khlebnikovf90dde42020-08-06 23:17:43 -070034if [[ $1 == "-r" ]] ; then
35 vmlinux=""
36 basepath="auto"
37 modpath=""
38 release=$2
39
40 for fn in {,/usr/lib/debug}/boot/vmlinux-$release{,.debug} /lib/modules/$release{,/build}/vmlinux ; do
41 if [ -e "$fn" ] ; then
42 vmlinux=$fn
43 break
44 fi
45 done
46
47 if [[ $vmlinux == "" ]] ; then
48 echo "ERROR! vmlinux image for release $release is not found" >&2
Stephen Boyd26681eb2021-07-07 18:09:31 -070049 usage
Konstantin Khlebnikovf90dde42020-08-06 23:17:43 -070050 exit 2
51 fi
52else
53 vmlinux=$1
54 basepath=${2-auto}
55 modpath=$3
56 release=""
Stephen Boyd26681eb2021-07-07 18:09:31 -070057 debuginfod=
58
59 # Can we use debuginfod-find?
60 if type debuginfod-find >/dev/null 2>&1 ; then
61 debuginfod=${1-only}
62 fi
63
64 if [[ $vmlinux == "" && -z $debuginfod ]] ; then
65 echo "ERROR! vmlinux image must be specified" >&2
66 usage
67 exit 1
68 fi
Konstantin Khlebnikovf90dde42020-08-06 23:17:43 -070069fi
Konstantin Khlebnikov431151b2020-08-06 23:17:41 -070070
Schspa Shi1ce4ac52022-04-29 14:37:57 -070071declare aarray_support=true
72declare -A cache 2>/dev/null
73if [[ $? != 0 ]]; then
74 aarray_support=false
75else
76 declare -A modcache
77fi
Sasha Levindbd1abb2014-06-04 11:39:23 -040078
Konstantin Khlebnikov431151b2020-08-06 23:17:41 -070079find_module() {
Stephen Boyd26681eb2021-07-07 18:09:31 -070080 if [[ -n $debuginfod ]] ; then
81 if [[ -n $modbuildid ]] ; then
82 debuginfod-find debuginfo $modbuildid && return
83 fi
84
85 # Only using debuginfod so don't try to find vmlinux module path
86 if [[ $debuginfod == "only" ]] ; then
87 return
88 fi
89 fi
90
Konstantin Khlebnikov431151b2020-08-06 23:17:41 -070091 if [[ "$modpath" != "" ]] ; then
92 for fn in $(find "$modpath" -name "${module//_/[-_]}.ko*") ; do
Carlos Llamas3b3e4d32023-09-29 03:48:17 +000093 if ${READELF} -WS "$fn" | grep -qwF .debug_line ; then
Konstantin Khlebnikov431151b2020-08-06 23:17:41 -070094 echo $fn
95 return
96 fi
97 done
98 return 1
99 fi
100
101 modpath=$(dirname "$vmlinux")
102 find_module && return
103
104 if [[ $release == "" ]] ; then
Stephen Boyd5bf0f3bc2021-07-07 18:09:35 -0700105 release=$(gdb -ex 'print init_uts_ns.name.release' -ex 'quit' -quiet -batch "$vmlinux" 2>/dev/null | sed -n 's/\$1 = "\(.*\)".*/\1/p')
Konstantin Khlebnikov431151b2020-08-06 23:17:41 -0700106 fi
107
108 for dn in {/usr/lib/debug,}/lib/modules/$release ; do
109 if [ -e "$dn" ] ; then
110 modpath="$dn"
111 find_module && return
112 fi
113 done
114
115 modpath=""
116 return 1
117}
118
Sasha Levindbd1abb2014-06-04 11:39:23 -0400119parse_symbol() {
120 # The structure of symbol at this point is:
Robert Jarzmike260fe02015-09-04 15:43:26 -0700121 # ([name]+[offset]/[total length])
Sasha Levindbd1abb2014-06-04 11:39:23 -0400122 #
123 # For example:
124 # do_basic_setup+0x9c/0xbf
125
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700126 if [[ $module == "" ]] ; then
127 local objfile=$vmlinux
Schspa Shi1ce4ac52022-04-29 14:37:57 -0700128 elif [[ $aarray_support == true && "${modcache[$module]+isset}" == "isset" ]]; then
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700129 local objfile=${modcache[$module]}
130 else
Konstantin Khlebnikov431151b2020-08-06 23:17:41 -0700131 local objfile=$(find_module)
132 if [[ $objfile == "" ]] ; then
Sasha Levina5dc8302020-06-15 18:24:27 -0400133 echo "WARNING! Modules path isn't set, but is needed to parse this symbol" >&2
134 return
135 fi
Schspa Shi1ce4ac52022-04-29 14:37:57 -0700136 if [[ $aarray_support == true ]]; then
137 modcache[$module]=$objfile
138 fi
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700139 fi
140
Robert Jarzmike260fe02015-09-04 15:43:26 -0700141 # Remove the englobing parenthesis
142 symbol=${symbol#\(}
143 symbol=${symbol%\)}
Sasha Levindbd1abb2014-06-04 11:39:23 -0400144
Konstantin Khlebnikov1d6693f2019-03-05 15:41:34 -0800145 # Strip segment
146 local segment
147 if [[ $symbol == *:* ]] ; then
148 segment=${symbol%%:*}:
149 symbol=${symbol#*:}
150 fi
151
Sasha Levindbd1abb2014-06-04 11:39:23 -0400152 # Strip the symbol name so that we could look it up
153 local name=${symbol%+*}
154
155 # Use 'nm vmlinux' to figure out the base address of said symbol.
156 # It's actually faster to call it every time than to load it
157 # all into bash.
Schspa Shi1ce4ac52022-04-29 14:37:57 -0700158 if [[ $aarray_support == true && "${cache[$module,$name]+isset}" == "isset" ]]; then
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700159 local base_addr=${cache[$module,$name]}
Sasha Levindbd1abb2014-06-04 11:39:23 -0400160 else
Stephen Boyd5bf0f3bc2021-07-07 18:09:35 -0700161 local base_addr=$(nm "$objfile" 2>/dev/null | awk '$3 == "'$name'" && ($2 == "t" || $2 == "T") {print $1; exit}')
Konstantin Khlebnikovf643b9e2020-08-06 23:17:35 -0700162 if [[ $base_addr == "" ]] ; then
163 # address not found
164 return
165 fi
Schspa Shi1ce4ac52022-04-29 14:37:57 -0700166 if [[ $aarray_support == true ]]; then
167 cache[$module,$name]="$base_addr"
168 fi
Sasha Levindbd1abb2014-06-04 11:39:23 -0400169 fi
170 # Let's start doing the math to get the exact address into the
171 # symbol. First, strip out the symbol total length.
172 local expr=${symbol%/*}
173
174 # Now, replace the symbol name with the base address we found
175 # before.
176 expr=${expr/$name/0x$base_addr}
177
178 # Evaluate it to find the actual address
179 expr=$((expr))
180 local address=$(printf "%x\n" "$expr")
181
182 # Pass it to addr2line to get filename and line number
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700183 # Could get more than one result
Schspa Shi1ce4ac52022-04-29 14:37:57 -0700184 if [[ $aarray_support == true && "${cache[$module,$address]+isset}" == "isset" ]]; then
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700185 local code=${cache[$module,$address]}
Sasha Levindbd1abb2014-06-04 11:39:23 -0400186 else
Carlos Llamas3b3e4d32023-09-29 03:48:17 +0000187 local code=$(${ADDR2LINE} -i -e "$objfile" "$address" 2>/dev/null)
Schspa Shi1ce4ac52022-04-29 14:37:57 -0700188 if [[ $aarray_support == true ]]; then
189 cache[$module,$address]=$code
190 fi
Sasha Levindbd1abb2014-06-04 11:39:23 -0400191 fi
192
193 # addr2line doesn't return a proper error code if it fails, so
194 # we detect it using the value it prints so that we could preserve
195 # the offset/size into the function and bail out
196 if [[ $code == "??:0" ]]; then
197 return
198 fi
199
Pi-Hsun Shihd1787702020-07-23 21:15:43 -0700200 # Strip out the base of the path on each line
201 code=$(while read -r line; do echo "${line#$basepath/}"; done <<< "$code")
Sasha Levindbd1abb2014-06-04 11:39:23 -0400202
203 # In the case of inlines, move everything to same line
204 code=${code//$'\n'/' '}
205
Miguel Ojeda473791d2021-12-05 19:00:43 +0100206 # Demangle if the name looks like a Rust symbol and if
207 # we got a Rust demangler
208 if [[ $name =~ ^_R && $cppfilt != "" ]] ; then
209 name=$("$cppfilt" "$cppfilt_opts" "$name")
210 fi
211
Sasha Levindbd1abb2014-06-04 11:39:23 -0400212 # Replace old address with pretty line numbers
Konstantin Khlebnikov1d6693f2019-03-05 15:41:34 -0800213 symbol="$segment$name ($code)"
Sasha Levindbd1abb2014-06-04 11:39:23 -0400214}
215
Stephen Boyd26681eb2021-07-07 18:09:31 -0700216debuginfod_get_vmlinux() {
217 local vmlinux_buildid=${1##* }
218
219 if [[ $vmlinux != "" ]]; then
220 return
221 fi
222
223 if [[ $vmlinux_buildid =~ ^[0-9a-f]+ ]]; then
224 vmlinux=$(debuginfod-find debuginfo $vmlinux_buildid)
225 if [[ $? -ne 0 ]] ; then
226 echo "ERROR! vmlinux image not found via debuginfod-find" >&2
227 usage
228 exit 2
229 fi
230 return
231 fi
232 echo "ERROR! Build ID for vmlinux not found. Try passing -r or specifying vmlinux" >&2
233 usage
234 exit 2
235}
236
Sasha Levindbd1abb2014-06-04 11:39:23 -0400237decode_code() {
238 local scripts=`dirname "${BASH_SOURCE[0]}"`
239
240 echo "$1" | $scripts/decodecode
241}
242
243handle_line() {
Stephen Boyd26681eb2021-07-07 18:09:31 -0700244 if [[ $basepath == "auto" && $vmlinux != "" ]] ; then
245 module=""
246 symbol="kernel_init+0x0/0x0"
247 parse_symbol
248 basepath=${symbol#kernel_init (}
249 basepath=${basepath%/init/main.c:*)}
250 fi
251
Sasha Levindbd1abb2014-06-04 11:39:23 -0400252 local words
253
254 # Tokenize
255 read -a words <<<"$1"
256
257 # Remove hex numbers. Do it ourselves until it happens in the
258 # kernel
259
260 # We need to know the index of the last element before we
261 # remove elements because arrays are sparse
262 local last=$(( ${#words[@]} - 1 ))
263
264 for i in "${!words[@]}"; do
265 # Remove the address
266 if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then
267 unset words[$i]
268 fi
269
270 # Format timestamps with tabs
271 if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then
272 unset words[$i]
273 words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}")
274 fi
275 done
276
Stephen Boyd26681eb2021-07-07 18:09:31 -0700277 if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then
278 words[$last-1]="${words[$last-1]} ${words[$last]}"
279 unset words[$last]
280 last=$(( $last - 1 ))
281 fi
282
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700283 if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then
284 module=${words[$last]}
285 module=${module#\[}
286 module=${module%\]}
Stephen Boyd26681eb2021-07-07 18:09:31 -0700287 modbuildid=${module#* }
288 module=${module% *}
289 if [[ $modbuildid == $module ]]; then
290 modbuildid=
291 fi
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700292 symbol=${words[$last-1]}
293 unset words[$last-1]
294 else
295 # The symbol is the last element, process it
296 symbol=${words[$last]}
297 module=
Stephen Boyd26681eb2021-07-07 18:09:31 -0700298 modbuildid=
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700299 fi
300
Sasha Levindbd1abb2014-06-04 11:39:23 -0400301 unset words[$last]
302 parse_symbol # modifies $symbol
303
304 # Add up the line number to the symbol
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700305 echo "${words[@]}" "$symbol $module"
Sasha Levindbd1abb2014-06-04 11:39:23 -0400306}
307
308while read line; do
309 # Let's see if we have an address in the line
Josh Poimboeuf53938ee2016-11-28 17:06:35 -0600310 if [[ $line =~ \[\<([^]]+)\>\] ]] ||
311 [[ $line =~ [^+\ ]+\+0x[0-9a-f]+/0x[0-9a-f]+ ]]; then
Sasha Levindbd1abb2014-06-04 11:39:23 -0400312 # Translate address to line numbers
313 handle_line "$line"
314 # Is it a code line?
315 elif [[ $line == *Code:* ]]; then
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700316 decode_code "$line"
Stephen Boyd26681eb2021-07-07 18:09:31 -0700317 # Is it a version line?
318 elif [[ -n $debuginfod && $line =~ PID:\ [0-9]+\ Comm: ]]; then
319 debuginfod_get_vmlinux "$line"
Konstantin Khlebnikov310c6dd2016-05-19 17:09:11 -0700320 else
Sasha Levindbd1abb2014-06-04 11:39:23 -0400321 # Nothing special in this line, show it as is
322 echo "$line"
323 fi
324done