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
48 declare aarray_support=true
49 declare -A cache 2>/dev/null
50 if [[ $? != 0 ]]; then
57 if [[ -n $debuginfod ]] ; then
58 if [[ -n $modbuildid ]] ; then
59 debuginfod-find debuginfo $modbuildid && return
62 # Only using debuginfod so don't try to find vmlinux module path
63 if [[ $debuginfod == "only" ]] ; then
68 if [[ "$modpath" != "" ]] ; then
69 for fn in $(find "$modpath" -name "${module//_/[-_]}.ko*") ; do
70 if readelf -WS "$fn" | grep -qwF .debug_line ; then
78 modpath=$(dirname "$vmlinux")
81 if [[ $release == "" ]] ; then
82 release=$(gdb -ex 'print init_uts_ns.name.release' -ex 'quit' -quiet -batch "$vmlinux" 2>/dev/null | sed -n 's/\$1 = "\(.*\)".*/\1/p')
85 for dn in {/usr/lib/debug,}/lib/modules/$release ; do
86 if [ -e "$dn" ] ; then
97 # The structure of symbol at this point is:
98 # ([name]+[offset]/[total length])
101 # do_basic_setup+0x9c/0xbf
103 if [[ $module == "" ]] ; then
104 local objfile=$vmlinux
105 elif [[ $aarray_support == true && "${modcache[$module]+isset}" == "isset" ]]; then
106 local objfile=${modcache[$module]}
108 local objfile=$(find_module)
109 if [[ $objfile == "" ]] ; then
110 echo "WARNING! Modules path isn't set, but is needed to parse this symbol" >&2
113 if [[ $aarray_support == true ]]; then
114 modcache[$module]=$objfile
118 # Remove the englobing parenthesis
124 if [[ $symbol == *:* ]] ; then
125 segment=${symbol%%:*}:
129 # Strip the symbol name so that we could look it up
130 local name=${symbol%+*}
132 # Use 'nm vmlinux' to figure out the base address of said symbol.
133 # It's actually faster to call it every time than to load it
135 if [[ $aarray_support == true && "${cache[$module,$name]+isset}" == "isset" ]]; then
136 local base_addr=${cache[$module,$name]}
138 local base_addr=$(nm "$objfile" 2>/dev/null | awk '$3 == "'$name'" && ($2 == "t" || $2 == "T") {print $1; exit}')
139 if [[ $base_addr == "" ]] ; then
143 if [[ $aarray_support == true ]]; then
144 cache[$module,$name]="$base_addr"
147 # Let's start doing the math to get the exact address into the
148 # symbol. First, strip out the symbol total length.
149 local expr=${symbol%/*}
151 # Now, replace the symbol name with the base address we found
153 expr=${expr/$name/0x$base_addr}
155 # Evaluate it to find the actual address
157 local address=$(printf "%x\n" "$expr")
159 # Pass it to addr2line to get filename and line number
160 # Could get more than one result
161 if [[ $aarray_support == true && "${cache[$module,$address]+isset}" == "isset" ]]; then
162 local code=${cache[$module,$address]}
164 local code=$(${CROSS_COMPILE}addr2line -i -e "$objfile" "$address" 2>/dev/null)
165 if [[ $aarray_support == true ]]; then
166 cache[$module,$address]=$code
170 # addr2line doesn't return a proper error code if it fails, so
171 # we detect it using the value it prints so that we could preserve
172 # the offset/size into the function and bail out
173 if [[ $code == "??:0" ]]; then
177 # Strip out the base of the path on each line
178 code=$(while read -r line; do echo "${line#$basepath/}"; done <<< "$code")
180 # In the case of inlines, move everything to same line
181 code=${code//$'\n'/' '}
183 # Replace old address with pretty line numbers
184 symbol="$segment$name ($code)"
187 debuginfod_get_vmlinux() {
188 local vmlinux_buildid=${1##* }
190 if [[ $vmlinux != "" ]]; then
194 if [[ $vmlinux_buildid =~ ^[0-9a-f]+ ]]; then
195 vmlinux=$(debuginfod-find debuginfo $vmlinux_buildid)
196 if [[ $? -ne 0 ]] ; then
197 echo "ERROR! vmlinux image not found via debuginfod-find" >&2
203 echo "ERROR! Build ID for vmlinux not found. Try passing -r or specifying vmlinux" >&2
209 local scripts=`dirname "${BASH_SOURCE[0]}"`
211 echo "$1" | $scripts/decodecode
215 if [[ $basepath == "auto" && $vmlinux != "" ]] ; then
217 symbol="kernel_init+0x0/0x0"
219 basepath=${symbol#kernel_init (}
220 basepath=${basepath%/init/main.c:*)}
226 read -a words <<<"$1"
228 # Remove hex numbers. Do it ourselves until it happens in the
231 # We need to know the index of the last element before we
232 # remove elements because arrays are sparse
233 local last=$(( ${#words[@]} - 1 ))
235 for i in "${!words[@]}"; do
237 if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then
241 # Format timestamps with tabs
242 if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then
244 words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}")
248 if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then
249 words[$last-1]="${words[$last-1]} ${words[$last]}"
251 last=$(( $last - 1 ))
254 if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then
255 module=${words[$last]}
258 modbuildid=${module#* }
260 if [[ $modbuildid == $module ]]; then
263 symbol=${words[$last-1]}
266 # The symbol is the last element, process it
267 symbol=${words[$last]}
273 parse_symbol # modifies $symbol
275 # Add up the line number to the symbol
276 echo "${words[@]}" "$symbol $module"
280 # Let's see if we have an address in the line
281 if [[ $line =~ \[\<([^]]+)\>\] ]] ||
282 [[ $line =~ [^+\ ]+\+0x[0-9a-f]+/0x[0-9a-f]+ ]]; then
283 # Translate address to line numbers
286 elif [[ $line == *Code:* ]]; then
288 # Is it a version line?
289 elif [[ -n $debuginfod && $line =~ PID:\ [0-9]+\ Comm: ]]; then
290 debuginfod_get_vmlinux "$line"
292 # Nothing special in this line, show it as is