decodecode 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/sh
  2. # Disassemble the Code: line in Linux oopses
  3. # usage: decodecode < oops.file
  4. #
  5. # options: set env. variable AFLAGS=options to pass options to "as";
  6. # e.g., to decode an i386 oops on an x86_64 system, use:
  7. # AFLAGS=--32 decodecode < 386.oops
  8. cleanup() {
  9. rm -f $T $T.s $T.o $T.oo $T.aa $T.dis
  10. exit 1
  11. }
  12. die() {
  13. echo "$@"
  14. exit 1
  15. }
  16. trap cleanup EXIT
  17. T=`mktemp` || die "cannot create temp file"
  18. code=
  19. while read i ; do
  20. case "$i" in
  21. *Code:*)
  22. code=$i
  23. ;;
  24. esac
  25. done
  26. if [ -z "$code" ]; then
  27. rm $T
  28. exit
  29. fi
  30. echo $code
  31. code=`echo $code | sed -e 's/.*Code: //'`
  32. width=`expr index "$code" ' '`
  33. width=$((($width-1)/2))
  34. case $width in
  35. 1) type=byte ;;
  36. 2) type=2byte ;;
  37. 4) type=4byte ;;
  38. esac
  39. disas() {
  40. ${CROSS_COMPILE}as $AFLAGS -o $1.o $1.s > /dev/null 2>&1
  41. if [ "$ARCH" = "arm" ]; then
  42. if [ $width -eq 2 ]; then
  43. OBJDUMPFLAGS="-M force-thumb"
  44. fi
  45. ${CROSS_COMPILE}strip $1.o
  46. fi
  47. ${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $1.o | \
  48. grep -v "/tmp\|Disassembly\|\.text\|^$" > $1.dis 2>&1
  49. }
  50. marker=`expr index "$code" "\<"`
  51. if [ $marker -eq 0 ]; then
  52. marker=`expr index "$code" "\("`
  53. fi
  54. touch $T.oo
  55. if [ $marker -ne 0 ]; then
  56. echo All code >> $T.oo
  57. echo ======== >> $T.oo
  58. beforemark=`echo "$code"`
  59. echo -n " .$type 0x" > $T.s
  60. echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
  61. disas $T
  62. cat $T.dis >> $T.oo
  63. rm -f $T.o $T.s $T.dis
  64. # and fix code at-and-after marker
  65. code=`echo "$code" | cut -c$((${marker} + 1))-`
  66. fi
  67. echo Code starting with the faulting instruction > $T.aa
  68. echo =========================================== >> $T.aa
  69. code=`echo $code | sed -e 's/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
  70. echo -n " .$type 0x" > $T.s
  71. echo $code >> $T.s
  72. disas $T
  73. cat $T.dis >> $T.aa
  74. faultline=`cat $T.dis | head -1 | cut -d":" -f2`
  75. faultline=`echo "$faultline" | sed -e 's/\[/\\\[/g; s/\]/\\\]/g'`
  76. cat $T.oo | sed -e "s/\($faultline\)/\*\1 <-- trapping instruction/g"
  77. echo
  78. cat $T.aa
  79. cleanup