decode_stacktrace.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # (c) 2014, Sasha Levin <sasha.levin@oracle.com>
  4. #set -x
  5. usage() {
  6. echo "Usage:"
  7. echo " $0 -r <release>"
  8. echo " $0 [<vmlinux> [<base_path>|auto [<modules_path>]]]"
  9. echo " $0 -h"
  10. }
  11. # Try to find a Rust demangler
  12. if type llvm-cxxfilt >/dev/null 2>&1 ; then
  13. cppfilt=llvm-cxxfilt
  14. elif type c++filt >/dev/null 2>&1 ; then
  15. cppfilt=c++filt
  16. cppfilt_opts=-i
  17. fi
  18. UTIL_SUFFIX=
  19. if [[ -z ${LLVM:-} ]]; then
  20. UTIL_PREFIX=${CROSS_COMPILE:-}
  21. else
  22. UTIL_PREFIX=llvm-
  23. if [[ ${LLVM} == */ ]]; then
  24. UTIL_PREFIX=${LLVM}${UTIL_PREFIX}
  25. elif [[ ${LLVM} == -* ]]; then
  26. UTIL_SUFFIX=${LLVM}
  27. fi
  28. fi
  29. READELF=${UTIL_PREFIX}readelf${UTIL_SUFFIX}
  30. ADDR2LINE=${UTIL_PREFIX}addr2line${UTIL_SUFFIX}
  31. NM=${UTIL_PREFIX}nm${UTIL_SUFFIX}
  32. if [[ $1 == "-h" ]] ; then
  33. usage
  34. exit 0
  35. elif [[ $1 == "-r" ]] ; then
  36. vmlinux=""
  37. basepath="auto"
  38. modpath=""
  39. release=$2
  40. for fn in {,/usr/lib/debug}/boot/vmlinux-$release{,.debug} /lib/modules/$release{,/build}/vmlinux ; do
  41. if [ -e "$fn" ] ; then
  42. vmlinux=$fn
  43. break
  44. fi
  45. done
  46. if [[ $vmlinux == "" ]] ; then
  47. echo "ERROR! vmlinux image for release $release is not found" >&2
  48. usage
  49. exit 2
  50. fi
  51. else
  52. vmlinux=$1
  53. basepath=${2-auto}
  54. modpath=$3
  55. release=""
  56. debuginfod=
  57. # Can we use debuginfod-find?
  58. if type debuginfod-find >/dev/null 2>&1 ; then
  59. debuginfod=${1-only}
  60. fi
  61. if [[ $vmlinux == "" && -z $debuginfod ]] ; then
  62. echo "ERROR! vmlinux image must be specified" >&2
  63. usage
  64. exit 1
  65. fi
  66. fi
  67. declare aarray_support=true
  68. declare -A cache 2>/dev/null
  69. if [[ $? != 0 ]]; then
  70. aarray_support=false
  71. else
  72. declare -A modcache
  73. fi
  74. find_module() {
  75. if [[ -n $debuginfod ]] ; then
  76. if [[ -n $modbuildid ]] ; then
  77. debuginfod-find debuginfo $modbuildid && return
  78. fi
  79. # Only using debuginfod so don't try to find vmlinux module path
  80. if [[ $debuginfod == "only" ]] ; then
  81. return
  82. fi
  83. fi
  84. if [ -z $release ] ; then
  85. release=$(gdb -ex 'print init_uts_ns.name.release' -ex 'quit' -quiet -batch "$vmlinux" 2>/dev/null | sed -n 's/\$1 = "\(.*\)".*/\1/p')
  86. fi
  87. if [ -n "${release}" ] ; then
  88. release_dirs="/usr/lib/debug/lib/modules/$release /lib/modules/$release"
  89. fi
  90. found_without_debug_info=false
  91. for dir in "$modpath" "$(dirname "$vmlinux")" ${release_dirs}; do
  92. if [ -n "${dir}" ] && [ -e "${dir}" ]; then
  93. for fn in $(find "$dir" -name "${module//_/[-_]}.ko*") ; do
  94. if ${READELF} -WS "$fn" | grep -qwF .debug_line ; then
  95. echo $fn
  96. return
  97. fi
  98. found_without_debug_info=true
  99. done
  100. fi
  101. done
  102. if [[ ${found_without_debug_info} == true ]]; then
  103. echo "WARNING! No debugging info in module ${module}, rebuild with DEBUG_KERNEL and DEBUG_INFO" >&2
  104. else
  105. echo "WARNING! Cannot find .ko for module ${module}, please pass a valid module path" >&2
  106. fi
  107. return 1
  108. }
  109. parse_symbol() {
  110. # The structure of symbol at this point is:
  111. # ([name]+[offset]/[total length])
  112. #
  113. # For example:
  114. # do_basic_setup+0x9c/0xbf
  115. if [[ $module == "" ]] ; then
  116. local objfile=$vmlinux
  117. elif [[ $aarray_support == true && "${modcache[$module]+isset}" == "isset" ]]; then
  118. local objfile=${modcache[$module]}
  119. else
  120. local objfile=$(find_module)
  121. if [[ $objfile == "" ]] ; then
  122. return
  123. fi
  124. if [[ $aarray_support == true ]]; then
  125. modcache[$module]=$objfile
  126. fi
  127. fi
  128. # Remove the englobing parenthesis
  129. symbol=${symbol#\(}
  130. symbol=${symbol%\)}
  131. # Strip segment
  132. local segment
  133. if [[ $symbol == *:* ]] ; then
  134. segment=${symbol%%:*}:
  135. symbol=${symbol#*:}
  136. fi
  137. # Strip the symbol name so that we could look it up
  138. local name=${symbol%+*}
  139. # Use 'nm vmlinux' to figure out the base address of said symbol.
  140. # It's actually faster to call it every time than to load it
  141. # all into bash.
  142. if [[ $aarray_support == true && "${cache[$module,$name]+isset}" == "isset" ]]; then
  143. local base_addr=${cache[$module,$name]}
  144. else
  145. local base_addr=$(${NM} "$objfile" 2>/dev/null | awk '$3 == "'$name'" && ($2 == "t" || $2 == "T") {print $1; exit}')
  146. if [[ $base_addr == "" ]] ; then
  147. # address not found
  148. return
  149. fi
  150. if [[ $aarray_support == true ]]; then
  151. cache[$module,$name]="$base_addr"
  152. fi
  153. fi
  154. # Let's start doing the math to get the exact address into the
  155. # symbol. First, strip out the symbol total length.
  156. local expr=${symbol%/*}
  157. # Now, replace the symbol name with the base address we found
  158. # before.
  159. expr=${expr/$name/0x$base_addr}
  160. # Evaluate it to find the actual address
  161. expr=$((expr))
  162. local address=$(printf "%x\n" "$expr")
  163. # Pass it to addr2line to get filename and line number
  164. # Could get more than one result
  165. if [[ $aarray_support == true && "${cache[$module,$address]+isset}" == "isset" ]]; then
  166. local code=${cache[$module,$address]}
  167. else
  168. local code=$(${ADDR2LINE} -i -e "$objfile" "$address" 2>/dev/null)
  169. if [[ $aarray_support == true ]]; then
  170. cache[$module,$address]=$code
  171. fi
  172. fi
  173. # addr2line doesn't return a proper error code if it fails, so
  174. # we detect it using the value it prints so that we could preserve
  175. # the offset/size into the function and bail out
  176. if [[ $code == "??:0" ]]; then
  177. return
  178. fi
  179. # Strip out the base of the path on each line
  180. code=$(while read -r line; do echo "${line#$basepath/}"; done <<< "$code")
  181. # In the case of inlines, move everything to same line
  182. 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")
  187. fi
  188. # Replace old address with pretty line numbers
  189. symbol="$segment$name ($code)"
  190. }
  191. debuginfod_get_vmlinux() {
  192. local vmlinux_buildid=${1##* }
  193. if [[ $vmlinux != "" ]]; then
  194. return
  195. fi
  196. if [[ $vmlinux_buildid =~ ^[0-9a-f]+ ]]; then
  197. vmlinux=$(debuginfod-find debuginfo $vmlinux_buildid)
  198. if [[ $? -ne 0 ]] ; then
  199. echo "ERROR! vmlinux image not found via debuginfod-find" >&2
  200. usage
  201. exit 2
  202. fi
  203. return
  204. fi
  205. echo "ERROR! Build ID for vmlinux not found. Try passing -r or specifying vmlinux" >&2
  206. usage
  207. exit 2
  208. }
  209. decode_code() {
  210. local scripts=`dirname "${BASH_SOURCE[0]}"`
  211. local lim="Code: "
  212. echo -n "${1%%${lim}*}"
  213. echo "${lim}${1##*${lim}}" | $scripts/decodecode
  214. }
  215. handle_line() {
  216. if [[ $basepath == "auto" && $vmlinux != "" ]] ; then
  217. module=""
  218. symbol="kernel_init+0x0/0x0"
  219. parse_symbol
  220. basepath=${symbol#kernel_init (}
  221. basepath=${basepath%/init/main.c:*)}
  222. fi
  223. local words spaces
  224. # Tokenize: words and spaces to preserve the alignment
  225. read -ra words <<<"$1"
  226. IFS='#' read -ra spaces <<<"$(shopt -s extglob; echo "${1//+([^[:space:]])/#}")"
  227. # Remove hex numbers. Do it ourselves until it happens in the
  228. # kernel
  229. # We need to know the index of the last element before we
  230. # remove elements because arrays are sparse
  231. local last=$(( ${#words[@]} - 1 ))
  232. for i in "${!words[@]}"; do
  233. # Remove the address
  234. if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then
  235. unset words[$i] spaces[$i]
  236. fi
  237. done
  238. # Extract info after the symbol if present. E.g.:
  239. # func_name+0x54/0x80 (P)
  240. # ^^^
  241. # The regex assumes only uppercase letters will be used. To be
  242. # extended if needed.
  243. local info_str=""
  244. if [[ ${words[$last]} =~ \([A-Z]*\) ]]; then
  245. info_str=${words[$last]}
  246. unset words[$last] spaces[$last]
  247. last=$(( $last - 1 ))
  248. fi
  249. # Join module name with its build id if present, as these were
  250. # split during tokenization (e.g. "[module" and "modbuildid]").
  251. if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then
  252. words[$last-1]="${words[$last-1]} ${words[$last]}"
  253. unset words[$last] spaces[$last]
  254. last=$(( $last - 1 ))
  255. fi
  256. if [[ ${words[$last]} =~ \[([^]]+)\] ]]; then
  257. module=${words[$last]}
  258. # some traces format is "(%pS)", which like "(foo+0x0/0x1 [bar])"
  259. # so $module may like "[bar])". Strip the right parenthesis firstly
  260. module=${module%\)}
  261. module=${module#\[}
  262. module=${module%\]}
  263. modbuildid=${module#* }
  264. module=${module% *}
  265. if [[ $modbuildid == $module ]]; then
  266. modbuildid=
  267. fi
  268. symbol=${words[$last-1]}
  269. unset words[$last-1] spaces[$last-1]
  270. else
  271. # The symbol is the last element, process it
  272. symbol=${words[$last]}
  273. module=
  274. modbuildid=
  275. fi
  276. unset words[$last]
  277. parse_symbol # modifies $symbol
  278. # Add up the line number to the symbol
  279. for i in "${!words[@]}"; do
  280. echo -n "${spaces[i]}${words[i]}"
  281. done
  282. echo "${spaces[$last]}${symbol}${module:+ ${module}}${info_str:+ ${info_str}}"
  283. }
  284. while read line; do
  285. # Strip unexpected carriage return at end of line
  286. line=${line%$'\r'}
  287. # Let's see if we have an address in the line
  288. if [[ $line =~ \[\<([^]]+)\>\] ]] ||
  289. [[ $line =~ [^+\ ]+\+0x[0-9a-f]+/0x[0-9a-f]+ ]]; then
  290. # Translate address to line numbers
  291. handle_line "$line"
  292. # Is it a code line?
  293. elif [[ $line == *Code:* ]]; then
  294. decode_code "$line"
  295. # Is it a version line?
  296. elif [[ -n $debuginfod && $line =~ PID:\ [0-9]+\ Comm: ]]; then
  297. debuginfod_get_vmlinux "$line"
  298. else
  299. # Nothing special in this line, show it as is
  300. echo "$line"
  301. fi
  302. done