2 # SPDX-License-Identifier: GPL-2.0
8 echo " $0 -r <release> | <vmlinux> [<base path>|auto] [<modules path>]"
11 # Try to find a Rust demangler
12 if type llvm-cxxfilt >/dev/null 2>&1 ; then
14 elif type c++filt >/dev/null 2>&1 ; then
20 if [[ -z ${LLVM:-} ]]; then
21 UTIL_PREFIX=${CROSS_COMPILE:-}
24 if [[ ${LLVM} == */ ]]; then
25 UTIL_PREFIX=${LLVM}${UTIL_PREFIX}
26 elif [[ ${LLVM} == -* ]]; then
31 READELF=${UTIL_PREFIX}readelf${UTIL_SUFFIX}
32 ADDR2LINE=${UTIL_PREFIX}addr2line${UTIL_SUFFIX}
33 NM=${UTIL_PREFIX}nm${UTIL_SUFFIX}
35 if [[ $1 == "-r" ]] ; then
41 for fn in {,/usr/lib/debug}/boot/vmlinux-$release{,.debug} /lib/modules/$release{,/build}/vmlinux ; do
42 if [ -e "$fn" ] ; then
48 if [[ $vmlinux == "" ]] ; then
49 echo "ERROR! vmlinux image for release $release is not found" >&2
60 # Can we use debuginfod-find?
61 if type debuginfod-find >/dev/null 2>&1 ; then
65 if [[ $vmlinux == "" && -z $debuginfod ]] ; then
66 echo "ERROR! vmlinux image must be specified" >&2
72 declare aarray_support=true
73 declare -A cache 2>/dev/null
74 if [[ $? != 0 ]]; then
81 if [[ -n $debuginfod ]] ; then
82 if [[ -n $modbuildid ]] ; then
83 debuginfod-find debuginfo $modbuildid && return
86 # Only using debuginfod so don't try to find vmlinux module path
87 if [[ $debuginfod == "only" ]] ; then
92 if [[ "$modpath" != "" ]] ; then
93 for fn in $(find "$modpath" -name "${module//_/[-_]}.ko*") ; do
94 if ${READELF} -WS "$fn" | grep -qwF .debug_line ; then
102 modpath=$(dirname "$vmlinux")
103 find_module && return
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')
109 for dn in {/usr/lib/debug,}/lib/modules/$release ; do
110 if [ -e "$dn" ] ; then
112 find_module && return
121 # The structure of symbol at this point is:
122 # ([name]+[offset]/[total length])
125 # do_basic_setup+0x9c/0xbf
127 if [[ $module == "" ]] ; then
128 local objfile=$vmlinux
129 elif [[ $aarray_support == true && "${modcache[$module]+isset}" == "isset" ]]; then
130 local objfile=${modcache[$module]}
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
137 if [[ $aarray_support == true ]]; then
138 modcache[$module]=$objfile
142 # Remove the englobing parenthesis
148 if [[ $symbol == *:* ]] ; then
149 segment=${symbol%%:*}:
153 # Strip the symbol name so that we could look it up
154 local name=${symbol%+*}
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
159 if [[ $aarray_support == true && "${cache[$module,$name]+isset}" == "isset" ]]; then
160 local base_addr=${cache[$module,$name]}
162 local base_addr=$(${NM} "$objfile" 2>/dev/null | awk '$3 == "'$name'" && ($2 == "t" || $2 == "T") {print $1; exit}')
163 if [[ $base_addr == "" ]] ; then
167 if [[ $aarray_support == true ]]; then
168 cache[$module,$name]="$base_addr"
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%/*}
175 # Now, replace the symbol name with the base address we found
177 expr=${expr/$name/0x$base_addr}
179 # Evaluate it to find the actual address
181 local address=$(printf "%x\n" "$expr")
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]}
188 local code=$(${ADDR2LINE} -i -e "$objfile" "$address" 2>/dev/null)
189 if [[ $aarray_support == true ]]; then
190 cache[$module,$address]=$code
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
201 # Strip out the base of the path on each line
202 code=$(while read -r line; do echo "${line#$basepath/}"; done <<< "$code")
204 # In the case of inlines, move everything to same line
205 code=${code//$'\n'/' '}
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")
213 # Replace old address with pretty line numbers
214 symbol="$segment$name ($code)"
217 debuginfod_get_vmlinux() {
218 local vmlinux_buildid=${1##* }
220 if [[ $vmlinux != "" ]]; then
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
233 echo "ERROR! Build ID for vmlinux not found. Try passing -r or specifying vmlinux" >&2
239 local scripts=`dirname "${BASH_SOURCE[0]}"`
241 echo "$1" | $scripts/decodecode
245 if [[ $basepath == "auto" && $vmlinux != "" ]] ; then
247 symbol="kernel_init+0x0/0x0"
249 basepath=${symbol#kernel_init (}
250 basepath=${basepath%/init/main.c:*)}
256 read -a words <<<"$1"
258 # Remove hex numbers. Do it ourselves until it happens in the
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 ))
265 for i in "${!words[@]}"; do
267 if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then
271 # Format timestamps with tabs
272 if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then
274 words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}")
278 if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then
279 words[$last-1]="${words[$last-1]} ${words[$last]}"
281 last=$(( $last - 1 ))
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
291 modbuildid=${module#* }
293 if [[ $modbuildid == $module ]]; then
296 symbol=${words[$last-1]}
299 # The symbol is the last element, process it
300 symbol=${words[$last]}
306 parse_symbol # modifies $symbol
308 # Add up the line number to the symbol
309 echo "${words[@]}" "$symbol $module"
313 # Strip unexpected carriage return at end of line
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
322 elif [[ $line == *Code:* ]]; then
324 # Is it a version line?
325 elif [[ -n $debuginfod && $line =~ PID:\ [0-9]+\ Comm: ]]; then
326 debuginfod_get_vmlinux "$line"
328 # Nothing special in this line, show it as is