Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/sh
0002 # SPDX-License-Identifier: GPL-2.0
0003 # Disassemble the Code: line in Linux oopses
0004 # usage: decodecode < oops.file
0005 #
0006 # options: set env. variable AFLAGS=options to pass options to "as";
0007 # e.g., to decode an i386 oops on an x86_64 system, use:
0008 # AFLAGS=--32 decodecode < 386.oops
0009 # PC=hex - the PC (program counter) the oops points to
0010 
0011 cleanup() {
0012         rm -f $T $T.s $T.o $T.oo $T.aa $T.dis
0013         exit 1
0014 }
0015 
0016 die() {
0017         echo "$@"
0018         exit 1
0019 }
0020 
0021 trap cleanup EXIT
0022 
0023 T=`mktemp` || die "cannot create temp file"
0024 code=
0025 cont=
0026 
0027 while read i ; do
0028 
0029 case "$i" in
0030 *Code:*)
0031         code=$i
0032         cont=yes
0033         ;;
0034 *)
0035         [ -n "$cont" ] && {
0036                 xdump="$(echo $i | grep '^[[:xdigit:]<>[:space:]]\+$')"
0037                 if [ -n "$xdump" ]; then
0038                         code="$code $xdump"
0039                 else
0040                         cont=
0041                 fi
0042         }
0043         ;;
0044 esac
0045 
0046 done
0047 
0048 if [ -z "$code" ]; then
0049         rm $T
0050         exit
0051 fi
0052 
0053 echo $code
0054 code=`echo $code | sed -e 's/.*Code: //'`
0055 
0056 width=`expr index "$code" ' '`
0057 width=$((($width-1)/2))
0058 case $width in
0059 1) type=byte ;;
0060 2) type=2byte ;;
0061 4) type=4byte ;;
0062 esac
0063 
0064 if [ -z "$ARCH" ]; then
0065     case `uname -m` in
0066         aarch64*) ARCH=arm64 ;;
0067         arm*) ARCH=arm ;;
0068     esac
0069 fi
0070 
0071 # Params: (tmp_file, pc_sub)
0072 disas() {
0073         t=$1
0074         pc_sub=$2
0075 
0076         ${CROSS_COMPILE}as $AFLAGS -o $t.o $t.s > /dev/null 2>&1
0077 
0078         if [ "$ARCH" = "arm" ]; then
0079                 if [ $width -eq 2 ]; then
0080                         OBJDUMPFLAGS="-M force-thumb"
0081                 fi
0082 
0083                 ${CROSS_COMPILE}strip $t.o
0084         fi
0085 
0086         if [ "$ARCH" = "arm64" ]; then
0087                 if [ $width -eq 4 ]; then
0088                         type=inst
0089                 fi
0090 
0091                 ${CROSS_COMPILE}strip $t.o
0092         fi
0093 
0094         if [ $pc_sub -ne 0 ]; then
0095                 if [ $PC ]; then
0096                         adj_vma=$(( $PC - $pc_sub ))
0097                         OBJDUMPFLAGS="$OBJDUMPFLAGS --adjust-vma=$adj_vma"
0098                 fi
0099         fi
0100 
0101         ${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $t.o | \
0102                 grep -v "/tmp\|Disassembly\|\.text\|^$" > $t.dis 2>&1
0103 }
0104 
0105 marker=`expr index "$code" "\<"`
0106 if [ $marker -eq 0 ]; then
0107         marker=`expr index "$code" "\("`
0108 fi
0109 
0110 
0111 touch $T.oo
0112 if [ $marker -ne 0 ]; then
0113         # 2 opcode bytes and a single space
0114         pc_sub=$(( $marker / 3 ))
0115         echo All code >> $T.oo
0116         echo ======== >> $T.oo
0117         beforemark=`echo "$code"`
0118         echo -n "       .$type 0x" > $T.s
0119         echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
0120         disas $T $pc_sub
0121         cat $T.dis >> $T.oo
0122         rm -f $T.o $T.s $T.dis
0123 
0124 # and fix code at-and-after marker
0125         code=`echo "$code" | cut -c$((${marker} + 1))-`
0126 fi
0127 echo Code starting with the faulting instruction  > $T.aa
0128 echo =========================================== >> $T.aa
0129 code=`echo $code | sed -e 's/\r//;s/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
0130 echo -n "       .$type 0x" > $T.s
0131 echo $code >> $T.s
0132 disas $T 0
0133 cat $T.dis >> $T.aa
0134 
0135 # (lines of whole $T.oo) - (lines of $T.aa, i.e. "Code starting") + 3,
0136 # i.e. the title + the "===..=" line (sed is counting from 1, 0 address is
0137 # special)
0138 faultlinenum=$(( $(wc -l $T.oo  | cut -d" " -f1) - \
0139                  $(wc -l $T.aa  | cut -d" " -f1) + 3))
0140 
0141 faultline=`cat $T.dis | head -1 | cut -d":" -f2-`
0142 faultline=`echo "$faultline" | sed -e 's/\[/\\\[/g; s/\]/\\\]/g'`
0143 
0144 cat $T.oo | sed -e "${faultlinenum}s/^\([^:]*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"
0145 echo
0146 cat $T.aa
0147 cleanup