decodecode 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. ${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $1.o | \
  49. grep -v "/tmp\|Disassembly\|\.text\|^$" > $1.dis 2>&1
  50. }
  51. marker=`expr index "$code" "\<"`
  52. if [ $marker -eq 0 ]; then
  53. marker=`expr index "$code" "\("`
  54. fi
  55. touch $T.oo
  56. if [ $marker -ne 0 ]; then
  57. echo All code >> $T.oo
  58. echo ======== >> $T.oo
  59. beforemark=`echo "$code"`
  60. echo -n " .$type 0x" > $T.s
  61. echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
  62. disas $T
  63. cat $T.dis >> $T.oo
  64. rm -f $T.o $T.s $T.dis
  65. # and fix code at-and-after marker
  66. code=`echo "$code" | cut -c$((${marker} + 1))-`
  67. fi
  68. echo Code starting with the faulting instruction > $T.aa
  69. echo =========================================== >> $T.aa
  70. code=`echo $code | sed -e 's/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
  71. echo -n " .$type 0x" > $T.s
  72. echo $code >> $T.s
  73. disas $T
  74. cat $T.dis >> $T.aa
  75. # (lines of whole $T.oo) - (lines of $T.aa, i.e. "Code starting") + 3,
  76. # i.e. the title + the "===..=" line (sed is counting from 1, 0 address is
  77. # special)
  78. faultlinenum=$(( $(wc -l $T.oo | cut -d" " -f1) - \
  79. $(wc -l $T.aa | cut -d" " -f1) + 3))
  80. faultline=`cat $T.dis | head -1 | cut -d":" -f2-`
  81. faultline=`echo "$faultline" | sed -e 's/\[/\\\[/g; s/\]/\\\]/g'`
  82. cat $T.oo | sed -e "${faultlinenum}s/^\([^:]*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"
  83. echo
  84. cat $T.aa
  85. cleanup