2 # SPDX-License-Identifier: GPL-2.0
3 # (c) 2014, Sasha Levin <sasha.levin@oracle.com>
8 echo " $0 -r <release> | <vmlinux> [base path] [modules path]"
12 # Try to find a Rust demangler
13 if type llvm-cxxfilt >/dev/null 2>&1 ; then
15 elif type c++filt >/dev/null 2>&1 ; then
21 if [[ -z ${LLVM:-} ]]; then
22 UTIL_PREFIX=${CROSS_COMPILE:-}
25 if [[ ${LLVM} == */ ]]; then
26 UTIL_PREFIX=${LLVM}${UTIL_PREFIX}
27 elif [[ ${LLVM} == -* ]]; then
32 READELF=${UTIL_PREFIX}readelf${UTIL_SUFFIX}
33 ADDR2LINE=${UTIL_PREFIX}addr2line${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
59 declare aarray_support=true
60 declare -A cache 2>/dev/null
61 if [[ $? != 0 ]]; 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=$(${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 # Demangle if the name looks like a Rust symbol and if
184 # we got a Rust demangler
185 if [[ $name =~ ^_R && $cppfilt != "" ]] ; then
186 name=$("$cppfilt" "$cppfilt_opts" "$name")
189 # Replace old address with pretty line numbers
190 symbol="$segment$name ($code)"
194 local scripts=`dirname "${BASH_SOURCE[0]}"`
196 echo "$1" | $scripts/decodecode
203 read -a words <<<"$1"
205 # Remove hex numbers. Do it ourselves until it happens in the
208 # We need to know the index of the last element before we
209 # remove elements because arrays are sparse
210 local last=$(( ${#words[@]} - 1 ))
212 for i in "${!words[@]}"; do
214 if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then
218 # Format timestamps with tabs
219 if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then
221 words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}")
225 if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then
226 module=${words[$last]}
229 symbol=${words[$last-1]}
232 # The symbol is the last element, process it
233 symbol=${words[$last]}
238 parse_symbol # modifies $symbol
240 # Add up the line number to the symbol
241 echo "${words[@]}" "$symbol $module"
244 if [[ $basepath == "auto" ]] ; then
246 symbol="kernel_init+0x0/0x0"
248 basepath=${symbol#kernel_init (}
249 basepath=${basepath%/init/main.c:*)}
253 # Let's see if we have an address in the line
254 if [[ $line =~ \[\<([^]]+)\>\] ]] ||
255 [[ $line =~ [^+\ ]+\+0x[0-9a-f]+/0x[0-9a-f]+ ]]; then
256 # Translate address to line numbers
259 elif [[ $line == *Code:* ]]; then
262 # Nothing special in this line, show it as is