2 # SPDX-License-Identifier: GPL-2.0
3 # (c) 2014, Sasha Levin <sasha.levin@oracle.com>
8 echo " $0 [vmlinux] [base path] [modules path]"
19 # The structure of symbol at this point is:
20 # ([name]+[offset]/[total length])
23 # do_basic_setup+0x9c/0xbf
25 if [[ $module == "" ]] ; then
26 local objfile=$vmlinux
27 elif [[ "${modcache[$module]+isset}" == "isset" ]]; then
28 local objfile=${modcache[$module]}
30 if [[ $modpath == "" ]]; then
31 echo "WARNING! Modules path isn't set, but is needed to parse this symbol" >&2
34 local objfile=$(find "$modpath" -name "${module//_/[-_]}.ko*" -print -quit)
35 [[ $objfile == "" ]] && return
36 modcache[$module]=$objfile
39 # Remove the englobing parenthesis
45 if [[ $symbol == *:* ]] ; then
46 segment=${symbol%%:*}:
50 # Strip the symbol name so that we could look it up
51 local name=${symbol%+*}
53 # Use 'nm vmlinux' to figure out the base address of said symbol.
54 # It's actually faster to call it every time than to load it
56 if [[ "${cache[$module,$name]+isset}" == "isset" ]]; then
57 local base_addr=${cache[$module,$name]}
59 local base_addr=$(nm "$objfile" | grep -i ' t ' | awk "/ $name\$/ {print \$1}" | head -n1)
60 cache[$module,$name]="$base_addr"
62 # Let's start doing the math to get the exact address into the
63 # symbol. First, strip out the symbol total length.
64 local expr=${symbol%/*}
66 # Now, replace the symbol name with the base address we found
68 expr=${expr/$name/0x$base_addr}
70 # Evaluate it to find the actual address
72 local address=$(printf "%x\n" "$expr")
74 # Pass it to addr2line to get filename and line number
75 # Could get more than one result
76 if [[ "${cache[$module,$address]+isset}" == "isset" ]]; then
77 local code=${cache[$module,$address]}
79 local code=$(${CROSS_COMPILE}addr2line -i -e "$objfile" "$address")
80 cache[$module,$address]=$code
83 # addr2line doesn't return a proper error code if it fails, so
84 # we detect it using the value it prints so that we could preserve
85 # the offset/size into the function and bail out
86 if [[ $code == "??:0" ]]; then
90 # Strip out the base of the path on each line
91 code=$(while read -r line; do echo "${line#$basepath/}"; done <<< "$code")
93 # In the case of inlines, move everything to same line
94 code=${code//$'\n'/' '}
96 # Replace old address with pretty line numbers
97 symbol="$segment$name ($code)"
101 local scripts=`dirname "${BASH_SOURCE[0]}"`
103 echo "$1" | $scripts/decodecode
110 read -a words <<<"$1"
112 # Remove hex numbers. Do it ourselves until it happens in the
115 # We need to know the index of the last element before we
116 # remove elements because arrays are sparse
117 local last=$(( ${#words[@]} - 1 ))
119 for i in "${!words[@]}"; do
121 if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then
125 # Format timestamps with tabs
126 if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then
128 words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}")
132 if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then
133 module=${words[$last]}
136 symbol=${words[$last-1]}
139 # The symbol is the last element, process it
140 symbol=${words[$last]}
145 parse_symbol # modifies $symbol
147 # Add up the line number to the symbol
148 echo "${words[@]}" "$symbol $module"
152 # Let's see if we have an address in the line
153 if [[ $line =~ \[\<([^]]+)\>\] ]] ||
154 [[ $line =~ [^+\ ]+\+0x[0-9a-f]+/0x[0-9a-f]+ ]]; then
155 # Translate address to line numbers
158 elif [[ $line == *Code:* ]]; then
161 # Nothing special in this line, show it as is