decodecode 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. esac
  59. fi
  60. # Params: (tmp_file, pc_sub)
  61. disas() {
  62. t=$1
  63. pc_sub=$2
  64. ${CROSS_COMPILE}as $AFLAGS -o $t.o $t.s > /dev/null 2>&1
  65. if [ "$ARCH" = "arm" ]; then
  66. if [ $width -eq 2 ]; then
  67. OBJDUMPFLAGS="-M force-thumb"
  68. fi
  69. ${CROSS_COMPILE}strip $t.o
  70. fi
  71. if [ "$ARCH" = "arm64" ]; then
  72. if [ $width -eq 4 ]; then
  73. type=inst
  74. fi
  75. ${CROSS_COMPILE}strip $t.o
  76. fi
  77. if [ $pc_sub -ne 0 ]; then
  78. if [ $PC ]; then
  79. adj_vma=$(( $PC - $pc_sub ))
  80. OBJDUMPFLAGS="$OBJDUMPFLAGS --adjust-vma=$adj_vma"
  81. fi
  82. fi
  83. ${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $t.o | \
  84. grep -v "/tmp\|Disassembly\|\.text\|^$" > $t.dis 2>&1
  85. }
  86. # Match the maximum number of opcode bytes from @op_bytes contained within
  87. # @opline
  88. #
  89. # Params:
  90. # @op_bytes: The string of bytes from the Code: line
  91. # @opline: The disassembled line coming from objdump
  92. #
  93. # Returns:
  94. # The max number of opcode bytes from the beginning of @op_bytes which match
  95. # the opcode bytes in the objdump line.
  96. get_substr_opcode_bytes_num()
  97. {
  98. local op_bytes=$1
  99. local opline=$2
  100. local retval=0
  101. substr=""
  102. for opc in $op_bytes;
  103. do
  104. substr+="$opc"
  105. # return if opcode bytes do not match @opline anymore
  106. if ! echo $opline | grep -q "$substr";
  107. then
  108. break
  109. fi
  110. # add trailing space
  111. substr+=" "
  112. retval=$((retval+1))
  113. done
  114. return $retval
  115. }
  116. # Return the line number in objdump output to where the IP marker in the Code:
  117. # line points to
  118. #
  119. # Params:
  120. # @all_code: code in bytes without the marker
  121. # @dis_file: disassembled file
  122. # @ip_byte: The byte to which the IP points to
  123. get_faultlinenum()
  124. {
  125. local all_code="$1"
  126. local dis_file="$2"
  127. # num bytes including IP byte
  128. local num_bytes_ip=$(( $3 + 1 * $width ))
  129. # Add the two header lines (we're counting from 1).
  130. local retval=3
  131. # remove marker
  132. all_code=$(echo $all_code | sed -e 's/[<>()]//g')
  133. while read line
  134. do
  135. get_substr_opcode_bytes_num "$all_code" "$line"
  136. ate_opcodes=$?
  137. if ! (( $ate_opcodes )); then
  138. continue
  139. fi
  140. num_bytes_ip=$((num_bytes_ip - ($ate_opcodes * $width) ))
  141. if (( $num_bytes_ip <= 0 )); then
  142. break
  143. fi
  144. # Delete matched opcode bytes from all_code. For that, compute
  145. # how many chars those opcodes are represented by and include
  146. # trailing space.
  147. #
  148. # a byte is 2 chars, ate_opcodes is also the number of trailing
  149. # spaces
  150. del_chars=$(( ($ate_opcodes * $width * 2) + $ate_opcodes ))
  151. all_code=$(echo $all_code | sed -e "s!^.\{$del_chars\}!!")
  152. let "retval+=1"
  153. done < $dis_file
  154. return $retval
  155. }
  156. marker=`expr index "$code" "\<"`
  157. if [ $marker -eq 0 ]; then
  158. marker=`expr index "$code" "\("`
  159. fi
  160. touch $T.oo
  161. if [ $marker -ne 0 ]; then
  162. # How many bytes to subtract from the program counter
  163. # in order to get to the beginning virtual address of the
  164. # Code:
  165. pc_sub=$(( (($marker - 1) / (2 * $width + 1)) * $width ))
  166. echo All code >> $T.oo
  167. echo ======== >> $T.oo
  168. beforemark=`echo "$code"`
  169. echo -n " .$type 0x" > $T.s
  170. echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
  171. disas $T $pc_sub
  172. cat $T.dis >> $T.oo
  173. get_faultlinenum "$code" "$T.dis" $pc_sub
  174. faultlinenum=$?
  175. # and fix code at-and-after marker
  176. code=`echo "$code" | cut -c$((${marker} + 1))-`
  177. rm -f $T.o $T.s $T.dis
  178. fi
  179. echo Code starting with the faulting instruction > $T.aa
  180. echo =========================================== >> $T.aa
  181. code=`echo $code | sed -e 's/\r//;s/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
  182. echo -n " .$type 0x" > $T.s
  183. echo $code >> $T.s
  184. disas $T 0
  185. cat $T.dis >> $T.aa
  186. cat $T.oo | sed -e "${faultlinenum}s/^\([^:]*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"
  187. echo
  188. cat $T.aa
  189. cleanup