decodecode 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Disassemble the Code: line in Linux oopses
  4. # usage: decodecode < oops.file
  5. #
  6. # options: set env. variable AFLAGS=options to pass options to "as";
  7. # e.g., to decode an i386 oops on an x86_64 system, use:
  8. # AFLAGS=--32 decodecode < 386.oops
  9. # PC=hex - the PC (program counter) the oops points to
  10. faultlinenum=1
  11. cleanup() {
  12. rm -f $T $T.s $T.o $T.oo $T.aa $T.dis
  13. exit 1
  14. }
  15. die() {
  16. echo "$@"
  17. exit 1
  18. }
  19. trap cleanup EXIT
  20. T=`mktemp` || die "cannot create temp file"
  21. code=
  22. cont=
  23. while read i ; do
  24. case "$i" in
  25. *Code:*)
  26. code=$i
  27. cont=yes
  28. ;;
  29. *)
  30. [ -n "$cont" ] && {
  31. xdump="$(echo $i | grep '^[[:xdigit:]<>[:space:]]\+$')"
  32. if [ -n "$xdump" ]; then
  33. code="$code $xdump"
  34. else
  35. cont=
  36. fi
  37. }
  38. ;;
  39. esac
  40. done
  41. if [ -z "$code" ]; then
  42. rm $T
  43. exit
  44. fi
  45. echo $code
  46. code=`echo $code | sed -e 's/.*Code: //'`
  47. width=`expr index "$code" ' '`
  48. width=$((($width-1)/2))
  49. case $width in
  50. 1) type=byte ;;
  51. 2) type=2byte ;;
  52. 4) type=4byte ;;
  53. esac
  54. if [ -z "$ARCH" ]; then
  55. case `uname -m` in
  56. aarch64*) ARCH=arm64 ;;
  57. arm*) ARCH=arm ;;
  58. loongarch*) ARCH=loongarch ;;
  59. esac
  60. fi
  61. # Params: (tmp_file, pc_sub)
  62. disas() {
  63. t=$1
  64. pc_sub=$2
  65. ${CROSS_COMPILE}as $AFLAGS -o $t.o $t.s > /dev/null 2>&1
  66. if [ "$ARCH" = "arm" ]; then
  67. if [ $width -eq 2 ]; then
  68. OBJDUMPFLAGS="-M force-thumb"
  69. fi
  70. ${CROSS_COMPILE}strip $t.o
  71. fi
  72. if [ "$ARCH" = "arm64" ]; then
  73. if [ $width -eq 4 ]; then
  74. type=inst
  75. fi
  76. ${CROSS_COMPILE}strip $t.o
  77. fi
  78. if [ "$ARCH" = "riscv" ]; then
  79. OBJDUMPFLAGS="-M no-aliases --section=.text -D"
  80. ${CROSS_COMPILE}strip $t.o
  81. fi
  82. if [ "$ARCH" = "loongarch" ]; then
  83. ${CROSS_COMPILE}strip $t.o
  84. fi
  85. if [ $pc_sub -ne 0 ]; then
  86. if [ $PC ]; then
  87. adj_vma=$(( $PC - $pc_sub ))
  88. OBJDUMPFLAGS="$OBJDUMPFLAGS --adjust-vma=$adj_vma"
  89. fi
  90. fi
  91. ${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $t.o | \
  92. grep -v "/tmp\|Disassembly\|\.text\|^$" > $t.dis 2>&1
  93. }
  94. # Match the maximum number of opcode bytes from @op_bytes contained within
  95. # @opline
  96. #
  97. # Params:
  98. # @op_bytes: The string of bytes from the Code: line
  99. # @opline: The disassembled line coming from objdump
  100. #
  101. # Returns:
  102. # The max number of opcode bytes from the beginning of @op_bytes which match
  103. # the opcode bytes in the objdump line.
  104. get_substr_opcode_bytes_num()
  105. {
  106. local op_bytes=$1
  107. local opline=$2
  108. local retval=0
  109. substr=""
  110. for opc in $op_bytes;
  111. do
  112. substr+="$opc"
  113. opcode="$substr"
  114. if [ "$ARCH" = "riscv" ]; then
  115. opcode=$(echo $opcode | tr ' ' '\n' | tac | tr -d '\n')
  116. fi
  117. # return if opcode bytes do not match @opline anymore
  118. if ! echo $opline | grep -q "$opcode";
  119. then
  120. break
  121. fi
  122. # add trailing space
  123. substr+=" "
  124. retval=$((retval+1))
  125. done
  126. return $retval
  127. }
  128. # Return the line number in objdump output to where the IP marker in the Code:
  129. # line points to
  130. #
  131. # Params:
  132. # @all_code: code in bytes without the marker
  133. # @dis_file: disassembled file
  134. # @ip_byte: The byte to which the IP points to
  135. get_faultlinenum()
  136. {
  137. local all_code="$1"
  138. local dis_file="$2"
  139. # num bytes including IP byte
  140. local num_bytes_ip=$(( $3 + 1 * $width ))
  141. # Add the two header lines (we're counting from 1).
  142. local retval=3
  143. # remove marker
  144. all_code=$(echo $all_code | sed -e 's/[<>()]//g')
  145. while read line
  146. do
  147. get_substr_opcode_bytes_num "$all_code" "$line"
  148. ate_opcodes=$?
  149. if ! (( $ate_opcodes )); then
  150. continue
  151. fi
  152. num_bytes_ip=$((num_bytes_ip - ($ate_opcodes * $width) ))
  153. if (( $num_bytes_ip <= 0 )); then
  154. break
  155. fi
  156. # Delete matched opcode bytes from all_code. For that, compute
  157. # how many chars those opcodes are represented by and include
  158. # trailing space.
  159. #
  160. # a byte is 2 chars, ate_opcodes is also the number of trailing
  161. # spaces
  162. del_chars=$(( ($ate_opcodes * $width * 2) + $ate_opcodes ))
  163. all_code=$(echo $all_code | sed -e "s!^.\{$del_chars\}!!")
  164. let "retval+=1"
  165. done < $dis_file
  166. return $retval
  167. }
  168. marker=`expr index "$code" "\<"`
  169. if [ $marker -eq 0 ]; then
  170. marker=`expr index "$code" "\("`
  171. fi
  172. touch $T.oo
  173. if [ $marker -ne 0 ]; then
  174. # How many bytes to subtract from the program counter
  175. # in order to get to the beginning virtual address of the
  176. # Code:
  177. pc_sub=$(( (($marker - 1) / (2 * $width + 1)) * $width ))
  178. echo All code >> $T.oo
  179. echo ======== >> $T.oo
  180. beforemark=`echo "$code"`
  181. echo -n " .$type 0x" > $T.s
  182. echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
  183. disas $T $pc_sub
  184. cat $T.dis >> $T.oo
  185. get_faultlinenum "$code" "$T.dis" $pc_sub
  186. faultlinenum=$?
  187. # and fix code at-and-after marker
  188. code=`echo "$code" | cut -c$((${marker} + 1))-`
  189. rm -f $T.o $T.s $T.dis
  190. fi
  191. echo Code starting with the faulting instruction > $T.aa
  192. echo =========================================== >> $T.aa
  193. code=`echo $code | sed -e 's/\r//;s/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
  194. echo -n " .$type 0x" > $T.s
  195. echo $code >> $T.s
  196. disas $T 0
  197. cat $T.dis >> $T.aa
  198. cat $T.oo | sed -e "${faultlinenum}s/^\([^:]*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"
  199. echo
  200. cat $T.aa
  201. cleanup