decodecode 2.5 KB

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