decodecode 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. cleanup() {
  10. rm -f $T $T.s $T.o $T.oo $T.aa $T.dis
  11. exit 1
  12. }
  13. die() {
  14. echo "$@"
  15. exit 1
  16. }
  17. trap cleanup EXIT
  18. T=`mktemp` || die "cannot create temp file"
  19. code=
  20. while read i ; do
  21. case "$i" in
  22. *Code:*)
  23. code=$i
  24. ;;
  25. esac
  26. done
  27. if [ -z "$code" ]; then
  28. rm $T
  29. exit
  30. fi
  31. echo $code
  32. code=`echo $code | sed -e 's/.*Code: //'`
  33. width=`expr index "$code" ' '`
  34. width=$((($width-1)/2))
  35. case $width in
  36. 1) type=byte ;;
  37. 2) type=2byte ;;
  38. 4) type=4byte ;;
  39. esac
  40. disas() {
  41. ${CROSS_COMPILE}as $AFLAGS -o $1.o $1.s > /dev/null 2>&1
  42. if [ "$ARCH" = "arm" ]; then
  43. if [ $width -eq 2 ]; then
  44. OBJDUMPFLAGS="-M force-thumb"
  45. fi
  46. ${CROSS_COMPILE}strip $1.o
  47. fi
  48. if [ "$ARCH" = "arm64" ]; then
  49. if [ $width -eq 4 ]; then
  50. type=inst
  51. fi
  52. ${CROSS_COMPILE}strip $1.o
  53. fi
  54. ${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $1.o | \
  55. grep -v "/tmp\|Disassembly\|\.text\|^$" > $1.dis 2>&1
  56. }
  57. marker=`expr index "$code" "\<"`
  58. if [ $marker -eq 0 ]; then
  59. marker=`expr index "$code" "\("`
  60. fi
  61. touch $T.oo
  62. if [ $marker -ne 0 ]; then
  63. echo All code >> $T.oo
  64. echo ======== >> $T.oo
  65. beforemark=`echo "$code"`
  66. echo -n " .$type 0x" > $T.s
  67. echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
  68. disas $T
  69. cat $T.dis >> $T.oo
  70. rm -f $T.o $T.s $T.dis
  71. # and fix code at-and-after marker
  72. code=`echo "$code" | cut -c$((${marker} + 1))-`
  73. fi
  74. echo Code starting with the faulting instruction > $T.aa
  75. echo =========================================== >> $T.aa
  76. code=`echo $code | sed -e 's/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
  77. echo -n " .$type 0x" > $T.s
  78. echo $code >> $T.s
  79. disas $T
  80. cat $T.dis >> $T.aa
  81. # (lines of whole $T.oo) - (lines of $T.aa, i.e. "Code starting") + 3,
  82. # i.e. the title + the "===..=" line (sed is counting from 1, 0 address is
  83. # special)
  84. faultlinenum=$(( $(wc -l $T.oo | cut -d" " -f1) - \
  85. $(wc -l $T.aa | cut -d" " -f1) + 3))
  86. faultline=`cat $T.dis | head -1 | cut -d":" -f2-`
  87. faultline=`echo "$faultline" | sed -e 's/\[/\\\[/g; s/\]/\\\]/g'`
  88. cat $T.oo | sed -e "${faultlinenum}s/^\(.*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"
  89. echo
  90. cat $T.aa
  91. cleanup