decode_stacktrace.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # (c) 2014, Sasha Levin <[email protected]>
  4. #set -x
  5. usage() {
  6. echo "Usage:"
  7. echo " $0 -r <release> | <vmlinux> [<base path>|auto] [<modules path>]"
  8. }
  9. # Try to find a Rust demangler
  10. if type llvm-cxxfilt >/dev/null 2>&1 ; then
  11. cppfilt=llvm-cxxfilt
  12. elif type c++filt >/dev/null 2>&1 ; then
  13. cppfilt=c++filt
  14. cppfilt_opts=-i
  15. fi
  16. if [[ $1 == "-r" ]] ; then
  17. vmlinux=""
  18. basepath="auto"
  19. modpath=""
  20. release=$2
  21. for fn in {,/usr/lib/debug}/boot/vmlinux-$release{,.debug} /lib/modules/$release{,/build}/vmlinux ; do
  22. if [ -e "$fn" ] ; then
  23. vmlinux=$fn
  24. break
  25. fi
  26. done
  27. if [[ $vmlinux == "" ]] ; then
  28. echo "ERROR! vmlinux image for release $release is not found" >&2
  29. usage
  30. exit 2
  31. fi
  32. else
  33. vmlinux=$1
  34. basepath=${2-auto}
  35. modpath=$3
  36. release=""
  37. debuginfod=
  38. # Can we use debuginfod-find?
  39. if type debuginfod-find >/dev/null 2>&1 ; then
  40. debuginfod=${1-only}
  41. fi
  42. if [[ $vmlinux == "" && -z $debuginfod ]] ; then
  43. echo "ERROR! vmlinux image must be specified" >&2
  44. usage
  45. exit 1
  46. fi
  47. fi
  48. declare aarray_support=true
  49. declare -A cache 2>/dev/null
  50. if [[ $? != 0 ]]; then
  51. aarray_support=false
  52. else
  53. declare -A modcache
  54. fi
  55. find_module() {
  56. if [[ -n $debuginfod ]] ; then
  57. if [[ -n $modbuildid ]] ; then
  58. debuginfod-find debuginfo $modbuildid && return
  59. fi
  60. # Only using debuginfod so don't try to find vmlinux module path
  61. if [[ $debuginfod == "only" ]] ; then
  62. return
  63. fi
  64. fi
  65. if [[ "$modpath" != "" ]] ; then
  66. for fn in $(find "$modpath" -name "${module//_/[-_]}.ko*") ; do
  67. if readelf -WS "$fn" | grep -qwF .debug_line ; then
  68. echo $fn
  69. return
  70. fi
  71. done
  72. return 1
  73. fi
  74. modpath=$(dirname "$vmlinux")
  75. find_module && return
  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')
  78. fi
  79. for dn in {/usr/lib/debug,}/lib/modules/$release ; do
  80. if [ -e "$dn" ] ; then
  81. modpath="$dn"
  82. find_module && return
  83. fi
  84. done
  85. modpath=""
  86. return 1
  87. }
  88. parse_symbol() {
  89. # The structure of symbol at this point is:
  90. # ([name]+[offset]/[total length])
  91. #
  92. # For example:
  93. # do_basic_setup+0x9c/0xbf
  94. if [[ $module == "" ]] ; then
  95. local objfile=$vmlinux
  96. elif [[ $aarray_support == true && "${modcache[$module]+isset}" == "isset" ]]; then
  97. local objfile=${modcache[$module]}
  98. else
  99. local objfile=$(find_module)
  100. if [[ $objfile == "" ]] ; then
  101. echo "WARNING! Modules path isn't set, but is needed to parse this symbol" >&2
  102. return
  103. fi
  104. if [[ $aarray_support == true ]]; then
  105. modcache[$module]=$objfile
  106. fi
  107. fi
  108. # Remove the englobing parenthesis
  109. symbol=${symbol#\(}
  110. symbol=${symbol%\)}
  111. # Strip segment
  112. local segment
  113. if [[ $symbol == *:* ]] ; then
  114. segment=${symbol%%:*}:
  115. symbol=${symbol#*:}
  116. fi
  117. # Strip the symbol name so that we could look it up
  118. local name=${symbol%+*}
  119. # Use 'nm vmlinux' to figure out the base address of said symbol.
  120. # It's actually faster to call it every time than to load it
  121. # all into bash.
  122. if [[ $aarray_support == true && "${cache[$module,$name]+isset}" == "isset" ]]; then
  123. local base_addr=${cache[$module,$name]}
  124. else
  125. local base_addr=$(nm "$objfile" 2>/dev/null | awk '$3 == "'$name'" && ($2 == "t" || $2 == "T") {print $1; exit}')
  126. if [[ $base_addr == "" ]] ; then
  127. # address not found
  128. return
  129. fi
  130. if [[ $aarray_support == true ]]; then
  131. cache[$module,$name]="$base_addr"
  132. fi
  133. fi
  134. # Let's start doing the math to get the exact address into the
  135. # symbol. First, strip out the symbol total length.
  136. local expr=${symbol%/*}
  137. # Now, replace the symbol name with the base address we found
  138. # before.
  139. expr=${expr/$name/0x$base_addr}
  140. # Evaluate it to find the actual address
  141. expr=$((expr))
  142. local address=$(printf "%x\n" "$expr")
  143. # Pass it to addr2line to get filename and line number
  144. # Could get more than one result
  145. if [[ $aarray_support == true && "${cache[$module,$address]+isset}" == "isset" ]]; then
  146. local code=${cache[$module,$address]}
  147. else
  148. local code=$(${CROSS_COMPILE}addr2line -i -e "$objfile" "$address" 2>/dev/null)
  149. if [[ $aarray_support == true ]]; then
  150. cache[$module,$address]=$code
  151. fi
  152. fi
  153. # addr2line doesn't return a proper error code if it fails, so
  154. # we detect it using the value it prints so that we could preserve
  155. # the offset/size into the function and bail out
  156. if [[ $code == "??:0" ]]; then
  157. return
  158. fi
  159. # Strip out the base of the path on each line
  160. code=$(while read -r line; do echo "${line#$basepath/}"; done <<< "$code")
  161. # In the case of inlines, move everything to same line
  162. code=${code//$'\n'/' '}
  163. # Demangle if the name looks like a Rust symbol and if
  164. # we got a Rust demangler
  165. if [[ $name =~ ^_R && $cppfilt != "" ]] ; then
  166. name=$("$cppfilt" "$cppfilt_opts" "$name")
  167. fi
  168. # Replace old address with pretty line numbers
  169. symbol="$segment$name ($code)"
  170. }
  171. debuginfod_get_vmlinux() {
  172. local vmlinux_buildid=${1##* }
  173. if [[ $vmlinux != "" ]]; then
  174. return
  175. fi
  176. if [[ $vmlinux_buildid =~ ^[0-9a-f]+ ]]; then
  177. vmlinux=$(debuginfod-find debuginfo $vmlinux_buildid)
  178. if [[ $? -ne 0 ]] ; then
  179. echo "ERROR! vmlinux image not found via debuginfod-find" >&2
  180. usage
  181. exit 2
  182. fi
  183. return
  184. fi
  185. echo "ERROR! Build ID for vmlinux not found. Try passing -r or specifying vmlinux" >&2
  186. usage
  187. exit 2
  188. }
  189. decode_code() {
  190. local scripts=`dirname "${BASH_SOURCE[0]}"`
  191. echo "$1" | $scripts/decodecode
  192. }
  193. handle_line() {
  194. if [[ $basepath == "auto" && $vmlinux != "" ]] ; then
  195. module=""
  196. symbol="kernel_init+0x0/0x0"
  197. parse_symbol
  198. basepath=${symbol#kernel_init (}
  199. basepath=${basepath%/init/main.c:*)}
  200. fi
  201. local words
  202. # Tokenize
  203. read -a words <<<"$1"
  204. # Remove hex numbers. Do it ourselves until it happens in the
  205. # kernel
  206. # We need to know the index of the last element before we
  207. # remove elements because arrays are sparse
  208. local last=$(( ${#words[@]} - 1 ))
  209. for i in "${!words[@]}"; do
  210. # Remove the address
  211. if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then
  212. unset words[$i]
  213. fi
  214. # Format timestamps with tabs
  215. if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then
  216. unset words[$i]
  217. words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}")
  218. fi
  219. done
  220. if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then
  221. words[$last-1]="${words[$last-1]} ${words[$last]}"
  222. unset words[$last]
  223. last=$(( $last - 1 ))
  224. fi
  225. if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then
  226. module=${words[$last]}
  227. module=${module#\[}
  228. module=${module%\]}
  229. modbuildid=${module#* }
  230. module=${module% *}
  231. if [[ $modbuildid == $module ]]; then
  232. modbuildid=
  233. fi
  234. symbol=${words[$last-1]}
  235. unset words[$last-1]
  236. else
  237. # The symbol is the last element, process it
  238. symbol=${words[$last]}
  239. module=
  240. modbuildid=
  241. fi
  242. unset words[$last]
  243. parse_symbol # modifies $symbol
  244. # Add up the line number to the symbol
  245. echo "${words[@]}" "$symbol $module"
  246. }
  247. while read line; do
  248. # Let's see if we have an address in the line
  249. if [[ $line =~ \[\<([^]]+)\>\] ]] ||
  250. [[ $line =~ [^+\ ]+\+0x[0-9a-f]+/0x[0-9a-f]+ ]]; then
  251. # Translate address to line numbers
  252. handle_line "$line"
  253. # Is it a code line?
  254. elif [[ $line == *Code:* ]]; then
  255. decode_code "$line"
  256. # Is it a version line?
  257. elif [[ -n $debuginfod && $line =~ PID:\ [0-9]+\ Comm: ]]; then
  258. debuginfod_get_vmlinux "$line"
  259. else
  260. # Nothing special in this line, show it as is
  261. echo "$line"
  262. fi
  263. done