decodecode 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. # (lines of whole $T.oo) - (lines of $T.aa, i.e. "Code starting") + 3,
  75. # i.e. the title + the "===..=" line (sed is counting from 1, 0 address is
  76. # special)
  77. faultlinenum=$(( $(wc -l $T.oo | cut -d" " -f1) - \
  78. $(wc -l $T.aa | cut -d" " -f1) + 3))
  79. faultline=`cat $T.dis | head -1 | cut -d":" -f2-`
  80. faultline=`echo "$faultline" | sed -e 's/\[/\\\[/g; s/\]/\\\]/g'`
  81. cat $T.oo | sed -e "${faultlinenum}s/^\(.*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"
  82. echo
  83. cat $T.aa
  84. cleanup