]>
Commit | Line | Data |
---|---|---|
dbd1abb2 | 1 | #!/bin/bash |
b2441318 | 2 | # SPDX-License-Identifier: GPL-2.0 |
dbd1abb2 SL |
3 | # (c) 2014, Sasha Levin <[email protected]> |
4 | #set -x | |
5 | ||
310c6dd0 | 6 | if [[ $# < 2 ]]; then |
dbd1abb2 | 7 | echo "Usage:" |
310c6dd0 | 8 | echo " $0 [vmlinux] [base path] [modules path]" |
dbd1abb2 SL |
9 | exit 1 |
10 | fi | |
11 | ||
12 | vmlinux=$1 | |
13 | basepath=$2 | |
310c6dd0 | 14 | modpath=$3 |
dbd1abb2 | 15 | declare -A cache |
310c6dd0 | 16 | declare -A modcache |
dbd1abb2 SL |
17 | |
18 | parse_symbol() { | |
19 | # The structure of symbol at this point is: | |
e260fe01 | 20 | # ([name]+[offset]/[total length]) |
dbd1abb2 SL |
21 | # |
22 | # For example: | |
23 | # do_basic_setup+0x9c/0xbf | |
24 | ||
310c6dd0 KK |
25 | if [[ $module == "" ]] ; then |
26 | local objfile=$vmlinux | |
27 | elif [[ "${modcache[$module]+isset}" == "isset" ]]; then | |
28 | local objfile=${modcache[$module]} | |
29 | else | |
a5dc8300 SL |
30 | if [[ $modpath == "" ]]; then |
31 | echo "WARNING! Modules path isn't set, but is needed to parse this symbol" >&2 | |
32 | return | |
33 | fi | |
ca90bbd4 | 34 | local objfile=$(find "$modpath" -name "${module//_/[-_]}.ko*" -print -quit) |
310c6dd0 KK |
35 | [[ $objfile == "" ]] && return |
36 | modcache[$module]=$objfile | |
37 | fi | |
38 | ||
e260fe01 RJ |
39 | # Remove the englobing parenthesis |
40 | symbol=${symbol#\(} | |
41 | symbol=${symbol%\)} | |
dbd1abb2 | 42 | |
1d6693fb KK |
43 | # Strip segment |
44 | local segment | |
45 | if [[ $symbol == *:* ]] ; then | |
46 | segment=${symbol%%:*}: | |
47 | symbol=${symbol#*:} | |
48 | fi | |
49 | ||
dbd1abb2 SL |
50 | # Strip the symbol name so that we could look it up |
51 | local name=${symbol%+*} | |
52 | ||
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 | |
55 | # all into bash. | |
310c6dd0 KK |
56 | if [[ "${cache[$module,$name]+isset}" == "isset" ]]; then |
57 | local base_addr=${cache[$module,$name]} | |
dbd1abb2 | 58 | else |
310c6dd0 KK |
59 | local base_addr=$(nm "$objfile" | grep -i ' t ' | awk "/ $name\$/ {print \$1}" | head -n1) |
60 | cache[$module,$name]="$base_addr" | |
dbd1abb2 SL |
61 | fi |
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%/*} | |
65 | ||
66 | # Now, replace the symbol name with the base address we found | |
67 | # before. | |
68 | expr=${expr/$name/0x$base_addr} | |
69 | ||
70 | # Evaluate it to find the actual address | |
71 | expr=$((expr)) | |
72 | local address=$(printf "%x\n" "$expr") | |
73 | ||
74 | # Pass it to addr2line to get filename and line number | |
310c6dd0 KK |
75 | # Could get more than one result |
76 | if [[ "${cache[$module,$address]+isset}" == "isset" ]]; then | |
77 | local code=${cache[$module,$address]} | |
dbd1abb2 | 78 | else |
c04e32e9 | 79 | local code=$(${CROSS_COMPILE}addr2line -i -e "$objfile" "$address") |
310c6dd0 | 80 | cache[$module,$address]=$code |
dbd1abb2 SL |
81 | fi |
82 | ||
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 | |
87 | return | |
88 | fi | |
89 | ||
d178770d PHS |
90 | # Strip out the base of the path on each line |
91 | code=$(while read -r line; do echo "${line#$basepath/}"; done <<< "$code") | |
dbd1abb2 SL |
92 | |
93 | # In the case of inlines, move everything to same line | |
94 | code=${code//$'\n'/' '} | |
95 | ||
96 | # Replace old address with pretty line numbers | |
1d6693fb | 97 | symbol="$segment$name ($code)" |
dbd1abb2 SL |
98 | } |
99 | ||
100 | decode_code() { | |
101 | local scripts=`dirname "${BASH_SOURCE[0]}"` | |
102 | ||
103 | echo "$1" | $scripts/decodecode | |
104 | } | |
105 | ||
106 | handle_line() { | |
107 | local words | |
108 | ||
109 | # Tokenize | |
110 | read -a words <<<"$1" | |
111 | ||
112 | # Remove hex numbers. Do it ourselves until it happens in the | |
113 | # kernel | |
114 | ||
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 )) | |
118 | ||
119 | for i in "${!words[@]}"; do | |
120 | # Remove the address | |
121 | if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then | |
122 | unset words[$i] | |
123 | fi | |
124 | ||
125 | # Format timestamps with tabs | |
126 | if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then | |
127 | unset words[$i] | |
128 | words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}") | |
129 | fi | |
130 | done | |
131 | ||
310c6dd0 KK |
132 | if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then |
133 | module=${words[$last]} | |
134 | module=${module#\[} | |
135 | module=${module%\]} | |
136 | symbol=${words[$last-1]} | |
137 | unset words[$last-1] | |
138 | else | |
139 | # The symbol is the last element, process it | |
140 | symbol=${words[$last]} | |
141 | module= | |
142 | fi | |
143 | ||
dbd1abb2 SL |
144 | unset words[$last] |
145 | parse_symbol # modifies $symbol | |
146 | ||
147 | # Add up the line number to the symbol | |
310c6dd0 | 148 | echo "${words[@]}" "$symbol $module" |
dbd1abb2 SL |
149 | } |
150 | ||
151 | while read line; do | |
152 | # Let's see if we have an address in the line | |
53938ee4 JP |
153 | if [[ $line =~ \[\<([^]]+)\>\] ]] || |
154 | [[ $line =~ [^+\ ]+\+0x[0-9a-f]+/0x[0-9a-f]+ ]]; then | |
dbd1abb2 SL |
155 | # Translate address to line numbers |
156 | handle_line "$line" | |
157 | # Is it a code line? | |
158 | elif [[ $line == *Code:* ]]; then | |
310c6dd0 KK |
159 | decode_code "$line" |
160 | else | |
dbd1abb2 SL |
161 | # Nothing special in this line, show it as is |
162 | echo "$line" | |
163 | fi | |
164 | done |