decodecode 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/bin/sh
  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. cleanup() {
  11. rm -f $T $T.s $T.o $T.oo $T.aa $T.dis
  12. exit 1
  13. }
  14. die() {
  15. echo "$@"
  16. exit 1
  17. }
  18. trap cleanup EXIT
  19. T=`mktemp` || die "cannot create temp file"
  20. code=
  21. cont=
  22. while read i ; do
  23. case "$i" in
  24. *Code:*)
  25. code=$i
  26. cont=yes
  27. ;;
  28. *)
  29. [ -n "$cont" ] && {
  30. xdump="$(echo $i | grep '^[[:xdigit:]<>[:space:]]\+$')"
  31. if [ -n "$xdump" ]; then
  32. code="$code $xdump"
  33. else
  34. cont=
  35. fi
  36. }
  37. ;;
  38. esac
  39. done
  40. if [ -z "$code" ]; then
  41. rm $T
  42. exit
  43. fi
  44. echo $code
  45. code=`echo $code | sed -e 's/.*Code: //'`
  46. width=`expr index "$code" ' '`
  47. width=$((($width-1)/2))
  48. case $width in
  49. 1) type=byte ;;
  50. 2) type=2byte ;;
  51. 4) type=4byte ;;
  52. esac
  53. if [ -z "$ARCH" ]; then
  54. case `uname -m` in
  55. aarch64*) ARCH=arm64 ;;
  56. arm*) ARCH=arm ;;
  57. esac
  58. fi
  59. # Params: (tmp_file, pc_sub)
  60. disas() {
  61. t=$1
  62. pc_sub=$2
  63. ${CROSS_COMPILE}as $AFLAGS -o $t.o $t.s > /dev/null 2>&1
  64. if [ "$ARCH" = "arm" ]; then
  65. if [ $width -eq 2 ]; then
  66. OBJDUMPFLAGS="-M force-thumb"
  67. fi
  68. ${CROSS_COMPILE}strip $t.o
  69. fi
  70. if [ "$ARCH" = "arm64" ]; then
  71. if [ $width -eq 4 ]; then
  72. type=inst
  73. fi
  74. ${CROSS_COMPILE}strip $t.o
  75. fi
  76. if [ $pc_sub -ne 0 ]; then
  77. if [ $PC ]; then
  78. adj_vma=$(( $PC - $pc_sub ))
  79. OBJDUMPFLAGS="$OBJDUMPFLAGS --adjust-vma=$adj_vma"
  80. fi
  81. fi
  82. ${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $t.o | \
  83. grep -v "/tmp\|Disassembly\|\.text\|^$" > $t.dis 2>&1
  84. }
  85. marker=`expr index "$code" "\<"`
  86. if [ $marker -eq 0 ]; then
  87. marker=`expr index "$code" "\("`
  88. fi
  89. touch $T.oo
  90. if [ $marker -ne 0 ]; then
  91. # 2 opcode bytes and a single space
  92. pc_sub=$(( $marker / 3 ))
  93. echo All code >> $T.oo
  94. echo ======== >> $T.oo
  95. beforemark=`echo "$code"`
  96. echo -n " .$type 0x" > $T.s
  97. echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
  98. disas $T $pc_sub
  99. cat $T.dis >> $T.oo
  100. rm -f $T.o $T.s $T.dis
  101. # and fix code at-and-after marker
  102. code=`echo "$code" | cut -c$((${marker} + 1))-`
  103. fi
  104. echo Code starting with the faulting instruction > $T.aa
  105. echo =========================================== >> $T.aa
  106. code=`echo $code | sed -e 's/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
  107. echo -n " .$type 0x" > $T.s
  108. echo $code >> $T.s
  109. disas $T 0
  110. cat $T.dis >> $T.aa
  111. # (lines of whole $T.oo) - (lines of $T.aa, i.e. "Code starting") + 3,
  112. # i.e. the title + the "===..=" line (sed is counting from 1, 0 address is
  113. # special)
  114. faultlinenum=$(( $(wc -l $T.oo | cut -d" " -f1) - \
  115. $(wc -l $T.aa | cut -d" " -f1) + 3))
  116. faultline=`cat $T.dis | head -1 | cut -d":" -f2-`
  117. faultline=`echo "$faultline" | sed -e 's/\[/\\\[/g; s/\]/\\\]/g'`
  118. cat $T.oo | sed -e "${faultlinenum}s/^\([^:]*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"
  119. echo
  120. cat $T.aa
  121. cleanup