2 # SPDX-License-Identifier: GPL-2.0
3 # Disassemble the Code: line in Linux oopses
4 # usage: decodecode < oops.file
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 # PC=hex - the PC (program counter) the oops points to
14 rm -f $T $T.s $T.o $T.oo $T.aa $T.dis
25 T=`mktemp` || die "cannot create temp file"
38 xdump="$(echo $i | grep '^[[:xdigit:]<>[:space:]]\+$')"
39 if [ -n "$xdump" ]; then
50 if [ -z "$code" ]; then
56 code=`echo $code | sed -e 's/.*Code: //'`
58 width=`expr index "$code" ' '`
59 width=$((($width-1)/2))
66 if [ -z "$ARCH" ]; then
68 aarch64*) ARCH=arm64 ;;
73 # Params: (tmp_file, pc_sub)
78 ${CROSS_COMPILE}as $AFLAGS -o $t.o $t.s > /dev/null 2>&1
80 if [ "$ARCH" = "arm" ]; then
81 if [ $width -eq 2 ]; then
82 OBJDUMPFLAGS="-M force-thumb"
85 ${CROSS_COMPILE}strip $t.o
88 if [ "$ARCH" = "arm64" ]; then
89 if [ $width -eq 4 ]; then
93 ${CROSS_COMPILE}strip $t.o
96 if [ $pc_sub -ne 0 ]; then
98 adj_vma=$(( $PC - $pc_sub ))
99 OBJDUMPFLAGS="$OBJDUMPFLAGS --adjust-vma=$adj_vma"
103 ${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $t.o | \
104 grep -v "/tmp\|Disassembly\|\.text\|^$" > $t.dis 2>&1
107 # Match the maximum number of opcode bytes from @op_bytes contained within
111 # @op_bytes: The string of bytes from the Code: line
112 # @opline: The disassembled line coming from objdump
115 # The max number of opcode bytes from the beginning of @op_bytes which match
116 # the opcode bytes in the objdump line.
117 get_substr_opcode_bytes_num()
125 for opc in $op_bytes;
129 # return if opcode bytes do not match @opline anymore
130 if ! echo $opline | grep -q "$substr";
143 # Return the line number in objdump output to where the IP marker in the Code:
147 # @all_code: code in bytes without the marker
148 # @dis_file: disassembled file
149 # @ip_byte: The byte to which the IP points to
155 # num bytes including IP byte
156 local num_bytes_ip=$(( $3 + 1 * $width ))
158 # Add the two header lines (we're counting from 1).
162 all_code=$(echo $all_code | sed -e 's/[<>()]//g')
166 get_substr_opcode_bytes_num "$all_code" "$line"
169 if ! (( $ate_opcodes )); then
173 num_bytes_ip=$((num_bytes_ip - ($ate_opcodes * $width) ))
174 if (( $num_bytes_ip <= 0 )); then
178 # Delete matched opcode bytes from all_code. For that, compute
179 # how many chars those opcodes are represented by and include
182 # a byte is 2 chars, ate_opcodes is also the number of trailing
184 del_chars=$(( ($ate_opcodes * $width * 2) + $ate_opcodes ))
186 all_code=$(echo $all_code | sed -e "s!^.\{$del_chars\}!!")
195 marker=`expr index "$code" "\<"`
196 if [ $marker -eq 0 ]; then
197 marker=`expr index "$code" "\("`
201 if [ $marker -ne 0 ]; then
202 # How many bytes to subtract from the program counter
203 # in order to get to the beginning virtual address of the
205 pc_sub=$(( (($marker - 1) / (2 * $width + 1)) * $width ))
206 echo All code >> $T.oo
207 echo ======== >> $T.oo
208 beforemark=`echo "$code"`
209 echo -n " .$type 0x" > $T.s
211 echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
217 get_faultlinenum "$code" "$T.dis" $pc_sub
220 # and fix code at-and-after marker
221 code=`echo "$code" | cut -c$((${marker} + 1))-`
223 rm -f $T.o $T.s $T.dis
226 echo Code starting with the faulting instruction > $T.aa
227 echo =========================================== >> $T.aa
228 code=`echo $code | sed -e 's/\r//;s/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
229 echo -n " .$type 0x" > $T.s
234 cat $T.oo | sed -e "${faultlinenum}s/^\([^:]*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"