2 # SPDX-License-Identifier: GPL-2.0
3 # (c) 2014, Sasha Levin <sasha.levin@oracle.com>
8 echo " $0 -r <release> | <vmlinux> [<base path>|auto] [<modules path>]"
11 if [[ $1 == "-r" ]] ; then
17 for fn in {,/usr/lib/debug}/boot/vmlinux-$release{,.debug} /lib/modules/$release{,/build}/vmlinux ; do
18 if [ -e "$fn" ] ; then
24 if [[ $vmlinux == "" ]] ; then
25 echo "ERROR! vmlinux image for release $release is not found" >&2
36 # Can we use debuginfod-find?
37 if type debuginfod-find >/dev/null 2>&1 ; then
41 if [[ $vmlinux == "" && -z $debuginfod ]] ; then
42 echo "ERROR! vmlinux image must be specified" >&2
52 if [[ -n $debuginfod ]] ; then
53 if [[ -n $modbuildid ]] ; then
54 debuginfod-find debuginfo $modbuildid && return
57 # Only using debuginfod so don't try to find vmlinux module path
58 if [[ $debuginfod == "only" ]] ; then
63 if [[ "$modpath" != "" ]] ; then
64 for fn in $(find "$modpath" -name "${module//_/[-_]}.ko*") ; do
65 if readelf -WS "$fn" | grep -qwF .debug_line ; then
73 modpath=$(dirname "$vmlinux")
76 if [[ $release == "" ]] ; then
77 release=$(gdb -ex 'print init_uts_ns.name.release' -ex 'quit' -quiet -batch "$vmlinux" 2>/dev/null | sed -n 's/\$1 = "\(.*\)".*/\1/p')
80 for dn in {/usr/lib/debug,}/lib/modules/$release ; do
81 if [ -e "$dn" ] ; then
92 # The structure of symbol at this point is:
93 # ([name]+[offset]/[total length])
96 # do_basic_setup+0x9c/0xbf
98 if [[ $module == "" ]] ; then
99 local objfile=$vmlinux
100 elif [[ "${modcache[$module]+isset}" == "isset" ]]; then
101 local objfile=${modcache[$module]}
103 local objfile=$(find_module)
104 if [[ $objfile == "" ]] ; then
105 echo "WARNING! Modules path isn't set, but is needed to parse this symbol" >&2
108 modcache[$module]=$objfile
111 # Remove the englobing parenthesis
117 if [[ $symbol == *:* ]] ; then
118 segment=${symbol%%:*}:
122 # Strip the symbol name so that we could look it up
123 local name=${symbol%+*}
125 # Use 'nm vmlinux' to figure out the base address of said symbol.
126 # It's actually faster to call it every time than to load it
128 if [[ "${cache[$module,$name]+isset}" == "isset" ]]; then
129 local base_addr=${cache[$module,$name]}
131 local base_addr=$(nm "$objfile" 2>/dev/null | awk '$3 == "'$name'" && ($2 == "t" || $2 == "T") {print $1; exit}')
132 if [[ $base_addr == "" ]] ; then
136 cache[$module,$name]="$base_addr"
138 # Let's start doing the math to get the exact address into the
139 # symbol. First, strip out the symbol total length.
140 local expr=${symbol%/*}
142 # Now, replace the symbol name with the base address we found
144 expr=${expr/$name/0x$base_addr}
146 # Evaluate it to find the actual address
148 local address=$(printf "%x\n" "$expr")
150 # Pass it to addr2line to get filename and line number
151 # Could get more than one result
152 if [[ "${cache[$module,$address]+isset}" == "isset" ]]; then
153 local code=${cache[$module,$address]}
155 local code=$(${CROSS_COMPILE}addr2line -i -e "$objfile" "$address" 2>/dev/null)
156 cache[$module,$address]=$code
159 # addr2line doesn't return a proper error code if it fails, so
160 # we detect it using the value it prints so that we could preserve
161 # the offset/size into the function and bail out
162 if [[ $code == "??:0" ]]; then
166 # Strip out the base of the path on each line
167 code=$(while read -r line; do echo "${line#$basepath/}"; done <<< "$code")
169 # In the case of inlines, move everything to same line
170 code=${code//$'\n'/' '}
172 # Replace old address with pretty line numbers
173 symbol="$segment$name ($code)"
176 debuginfod_get_vmlinux() {
177 local vmlinux_buildid=${1##* }
179 if [[ $vmlinux != "" ]]; then
183 if [[ $vmlinux_buildid =~ ^[0-9a-f]+ ]]; then
184 vmlinux=$(debuginfod-find debuginfo $vmlinux_buildid)
185 if [[ $? -ne 0 ]] ; then
186 echo "ERROR! vmlinux image not found via debuginfod-find" >&2
192 echo "ERROR! Build ID for vmlinux not found. Try passing -r or specifying vmlinux" >&2
198 local scripts=`dirname "${BASH_SOURCE[0]}"`
200 echo "$1" | $scripts/decodecode
204 if [[ $basepath == "auto" && $vmlinux != "" ]] ; then
206 symbol="kernel_init+0x0/0x0"
208 basepath=${symbol#kernel_init (}
209 basepath=${basepath%/init/main.c:*)}
215 read -a words <<<"$1"
217 # Remove hex numbers. Do it ourselves until it happens in the
220 # We need to know the index of the last element before we
221 # remove elements because arrays are sparse
222 local last=$(( ${#words[@]} - 1 ))
224 for i in "${!words[@]}"; do
226 if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then
230 # Format timestamps with tabs
231 if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then
233 words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}")
237 if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then
238 words[$last-1]="${words[$last-1]} ${words[$last]}"
240 last=$(( $last - 1 ))
243 if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then
244 module=${words[$last]}
247 modbuildid=${module#* }
249 if [[ $modbuildid == $module ]]; then
252 symbol=${words[$last-1]}
255 # The symbol is the last element, process it
256 symbol=${words[$last]}
262 parse_symbol # modifies $symbol
264 # Add up the line number to the symbol
265 echo "${words[@]}" "$symbol $module"
269 # Let's see if we have an address in the line
270 if [[ $line =~ \[\<([^]]+)\>\] ]] ||
271 [[ $line =~ [^+\ ]+\+0x[0-9a-f]+/0x[0-9a-f]+ ]]; then
272 # Translate address to line numbers
275 elif [[ $line == *Code:* ]]; then
277 # Is it a version line?
278 elif [[ -n $debuginfod && $line =~ PID:\ [0-9]+\ Comm: ]]; then
279 debuginfod_get_vmlinux "$line"
281 # Nothing special in this line, show it as is