Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# (c) 2014, Sasha Levin <sasha.levin@oracle.com>
4#set -x
5
6usage() {
7 echo "Usage:"
8 echo " $0 -r <release> | <vmlinux> [<base path>|auto] [<modules path>]"
9}
10
11# 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
19UTIL_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}
33NM=${UTIL_PREFIX}nm${UTIL_SUFFIX}
34
35if [[ $1 == "-r" ]] ; then
36 vmlinux=""
37 basepath="auto"
38 modpath=""
39 release=$2
40
41 for fn in {,/usr/lib/debug}/boot/vmlinux-$release{,.debug} /lib/modules/$release{,/build}/vmlinux ; do
42 if [ -e "$fn" ] ; then
43 vmlinux=$fn
44 break
45 fi
46 done
47
48 if [[ $vmlinux == "" ]] ; then
49 echo "ERROR! vmlinux image for release $release is not found" >&2
50 usage
51 exit 2
52 fi
53else
54 vmlinux=$1
55 basepath=${2-auto}
56 modpath=$3
57 release=""
58 debuginfod=
59
60 # Can we use debuginfod-find?
61 if type debuginfod-find >/dev/null 2>&1 ; then
62 debuginfod=${1-only}
63 fi
64
65 if [[ $vmlinux == "" && -z $debuginfod ]] ; then
66 echo "ERROR! vmlinux image must be specified" >&2
67 usage
68 exit 1
69 fi
70fi
71
72declare aarray_support=true
73declare -A cache 2>/dev/null
74if [[ $? != 0 ]]; then
75 aarray_support=false
76else
77 declare -A modcache
78fi
79
80find_module() {
81 if [[ -n $debuginfod ]] ; then
82 if [[ -n $modbuildid ]] ; then
83 debuginfod-find debuginfo $modbuildid && return
84 fi
85
86 # Only using debuginfod so don't try to find vmlinux module path
87 if [[ $debuginfod == "only" ]] ; then
88 return
89 fi
90 fi
91
92 if [[ "$modpath" != "" ]] ; then
93 for fn in $(find "$modpath" -name "${module//_/[-_]}.ko*") ; do
94 if ${READELF} -WS "$fn" | grep -qwF .debug_line ; then
95 echo $fn
96 return
97 fi
98 done
99 return 1
100 fi
101
102 modpath=$(dirname "$vmlinux")
103 find_module && return
104
105 if [[ $release == "" ]] ; then
106 release=$(gdb -ex 'print init_uts_ns.name.release' -ex 'quit' -quiet -batch "$vmlinux" 2>/dev/null | sed -n 's/\$1 = "\(.*\)".*/\1/p')
107 fi
108
109 for dn in {/usr/lib/debug,}/lib/modules/$release ; do
110 if [ -e "$dn" ] ; then
111 modpath="$dn"
112 find_module && return
113 fi
114 done
115
116 modpath=""
117 return 1
118}
119
120parse_symbol() {
121 # The structure of symbol at this point is:
122 # ([name]+[offset]/[total length])
123 #
124 # For example:
125 # do_basic_setup+0x9c/0xbf
126
127 if [[ $module == "" ]] ; then
128 local objfile=$vmlinux
129 elif [[ $aarray_support == true && "${modcache[$module]+isset}" == "isset" ]]; then
130 local objfile=${modcache[$module]}
131 else
132 local objfile=$(find_module)
133 if [[ $objfile == "" ]] ; then
134 echo "WARNING! Modules path isn't set, but is needed to parse this symbol" >&2
135 return
136 fi
137 if [[ $aarray_support == true ]]; then
138 modcache[$module]=$objfile
139 fi
140 fi
141
142 # Remove the englobing parenthesis
143 symbol=${symbol#\(}
144 symbol=${symbol%\)}
145
146 # Strip segment
147 local segment
148 if [[ $symbol == *:* ]] ; then
149 segment=${symbol%%:*}:
150 symbol=${symbol#*:}
151 fi
152
153 # Strip the symbol name so that we could look it up
154 local name=${symbol%+*}
155
156 # Use 'nm vmlinux' to figure out the base address of said symbol.
157 # It's actually faster to call it every time than to load it
158 # all into bash.
159 if [[ $aarray_support == true && "${cache[$module,$name]+isset}" == "isset" ]]; then
160 local base_addr=${cache[$module,$name]}
161 else
162 local base_addr=$(${NM} "$objfile" 2>/dev/null | awk '$3 == "'$name'" && ($2 == "t" || $2 == "T") {print $1; exit}')
163 if [[ $base_addr == "" ]] ; then
164 # address not found
165 return
166 fi
167 if [[ $aarray_support == true ]]; then
168 cache[$module,$name]="$base_addr"
169 fi
170 fi
171 # Let's start doing the math to get the exact address into the
172 # symbol. First, strip out the symbol total length.
173 local expr=${symbol%/*}
174
175 # Now, replace the symbol name with the base address we found
176 # before.
177 expr=${expr/$name/0x$base_addr}
178
179 # Evaluate it to find the actual address
180 expr=$((expr))
181 local address=$(printf "%x\n" "$expr")
182
183 # Pass it to addr2line to get filename and line number
184 # Could get more than one result
185 if [[ $aarray_support == true && "${cache[$module,$address]+isset}" == "isset" ]]; then
186 local code=${cache[$module,$address]}
187 else
188 local code=$(${ADDR2LINE} -i -e "$objfile" "$address" 2>/dev/null)
189 if [[ $aarray_support == true ]]; then
190 cache[$module,$address]=$code
191 fi
192 fi
193
194 # addr2line doesn't return a proper error code if it fails, so
195 # we detect it using the value it prints so that we could preserve
196 # the offset/size into the function and bail out
197 if [[ $code == "??:0" ]]; then
198 return
199 fi
200
201 # Strip out the base of the path on each line
202 code=$(while read -r line; do echo "${line#$basepath/}"; done <<< "$code")
203
204 # In the case of inlines, move everything to same line
205 code=${code//$'\n'/' '}
206
207 # Demangle if the name looks like a Rust symbol and if
208 # we got a Rust demangler
209 if [[ $name =~ ^_R && $cppfilt != "" ]] ; then
210 name=$("$cppfilt" "$cppfilt_opts" "$name")
211 fi
212
213 # Replace old address with pretty line numbers
214 symbol="$segment$name ($code)"
215}
216
217debuginfod_get_vmlinux() {
218 local vmlinux_buildid=${1##* }
219
220 if [[ $vmlinux != "" ]]; then
221 return
222 fi
223
224 if [[ $vmlinux_buildid =~ ^[0-9a-f]+ ]]; then
225 vmlinux=$(debuginfod-find debuginfo $vmlinux_buildid)
226 if [[ $? -ne 0 ]] ; then
227 echo "ERROR! vmlinux image not found via debuginfod-find" >&2
228 usage
229 exit 2
230 fi
231 return
232 fi
233 echo "ERROR! Build ID for vmlinux not found. Try passing -r or specifying vmlinux" >&2
234 usage
235 exit 2
236}
237
238decode_code() {
239 local scripts=`dirname "${BASH_SOURCE[0]}"`
240
241 echo "$1" | $scripts/decodecode
242}
243
244handle_line() {
245 if [[ $basepath == "auto" && $vmlinux != "" ]] ; then
246 module=""
247 symbol="kernel_init+0x0/0x0"
248 parse_symbol
249 basepath=${symbol#kernel_init (}
250 basepath=${basepath%/init/main.c:*)}
251 fi
252
253 local words
254
255 # Tokenize
256 read -a words <<<"$1"
257
258 # Remove hex numbers. Do it ourselves until it happens in the
259 # kernel
260
261 # We need to know the index of the last element before we
262 # remove elements because arrays are sparse
263 local last=$(( ${#words[@]} - 1 ))
264
265 for i in "${!words[@]}"; do
266 # Remove the address
267 if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then
268 unset words[$i]
269 fi
270
271 # Format timestamps with tabs
272 if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then
273 unset words[$i]
274 words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}")
275 fi
276 done
277
278 if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then
279 words[$last-1]="${words[$last-1]} ${words[$last]}"
280 unset words[$last]
281 last=$(( $last - 1 ))
282 fi
283
284 if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then
285 module=${words[$last]}
286 # some traces format is "(%pS)", which like "(foo+0x0/0x1 [bar])"
287 # so $module may like "[bar])". Strip the right parenthesis firstly
288 module=${module%\)}
289 module=${module#\[}
290 module=${module%\]}
291 modbuildid=${module#* }
292 module=${module% *}
293 if [[ $modbuildid == $module ]]; then
294 modbuildid=
295 fi
296 symbol=${words[$last-1]}
297 unset words[$last-1]
298 else
299 # The symbol is the last element, process it
300 symbol=${words[$last]}
301 module=
302 modbuildid=
303 fi
304
305 unset words[$last]
306 parse_symbol # modifies $symbol
307
308 # Add up the line number to the symbol
309 echo "${words[@]}" "$symbol $module"
310}
311
312while read line; do
313 # Strip unexpected carriage return at end of line
314 line=${line%$'\r'}
315
316 # Let's see if we have an address in the line
317 if [[ $line =~ \[\<([^]]+)\>\] ]] ||
318 [[ $line =~ [^+\ ]+\+0x[0-9a-f]+/0x[0-9a-f]+ ]]; then
319 # Translate address to line numbers
320 handle_line "$line"
321 # Is it a code line?
322 elif [[ $line == *Code:* ]]; then
323 decode_code "$line"
324 # Is it a version line?
325 elif [[ -n $debuginfod && $line =~ PID:\ [0-9]+\ Comm: ]]; then
326 debuginfod_get_vmlinux "$line"
327 else
328 # Nothing special in this line, show it as is
329 echo "$line"
330 fi
331done