blob: 6364218b2178f538b86dab8f37ced5acfada7742 [file] [log] [blame]
Borislav Petkov765f2bf2022-08-08 10:59:28 +02001#!/bin/bash
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01002# SPDX-License-Identifier: GPL-2.0
Randy Dunlapdcecc6c2007-07-15 23:41:15 -07003# 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
Borislav Petkovd72e7202020-10-13 16:48:14 -07009# PC=hex - the PC (program counter) the oops points to
Randy Dunlapdcecc6c2007-07-15 23:41:15 -070010
Borislav Petkov765f2bf2022-08-08 10:59:28 +020011faultlinenum=1
12
Randy Dunlapfa220d82008-01-14 15:18:31 -080013cleanup() {
Rabin Vincent5358db02010-01-05 20:27:58 +053014 rm -f $T $T.s $T.o $T.oo $T.aa $T.dis
Randy Dunlapfa220d82008-01-14 15:18:31 -080015 exit 1
16}
17
18die() {
19 echo "$@"
20 exit 1
21}
22
23trap cleanup EXIT
24
25T=`mktemp` || die "cannot create temp file"
Randy Dunlapdcecc6c2007-07-15 23:41:15 -070026code=
Andy Shevchenko7e68b362018-01-31 16:14:10 -080027cont=
Randy Dunlapdcecc6c2007-07-15 23:41:15 -070028
29while read i ; do
30
31case "$i" in
32*Code:*)
33 code=$i
Andy Shevchenko7e68b362018-01-31 16:14:10 -080034 cont=yes
35 ;;
36*)
37 [ -n "$cont" ] && {
38 xdump="$(echo $i | grep '^[[:xdigit:]<>[:space:]]\+$')"
39 if [ -n "$xdump" ]; then
40 code="$code $xdump"
41 else
42 cont=
43 fi
44 }
Randy Dunlapdcecc6c2007-07-15 23:41:15 -070045 ;;
46esac
47
48done
49
50if [ -z "$code" ]; then
Randy Dunlapfa220d82008-01-14 15:18:31 -080051 rm $T
Randy Dunlapdcecc6c2007-07-15 23:41:15 -070052 exit
53fi
54
55echo $code
56code=`echo $code | sed -e 's/.*Code: //'`
57
Rabin Vincent5358db02010-01-05 20:27:58 +053058width=`expr index "$code" ' '`
Rabin Vincentb396aa02010-06-03 22:48:12 +053059width=$((($width-1)/2))
Rabin Vincent5358db02010-01-05 20:27:58 +053060case $width in
611) type=byte ;;
622) type=2byte ;;
634) type=4byte ;;
64esac
65
Marc Zyngierc5cfb62f2018-12-28 00:31:21 -080066if [ -z "$ARCH" ]; then
67 case `uname -m` in
68 aarch64*) ARCH=arm64 ;;
69 arm*) ARCH=arm ;;
Youling Tangd738bce2023-12-16 14:51:28 +080070 loongarch*) ARCH=loongarch ;;
Marc Zyngierc5cfb62f2018-12-28 00:31:21 -080071 esac
72fi
73
Borislav Petkovd72e7202020-10-13 16:48:14 -070074# Params: (tmp_file, pc_sub)
Rabin Vincent5358db02010-01-05 20:27:58 +053075disas() {
Borislav Petkovd72e7202020-10-13 16:48:14 -070076 t=$1
77 pc_sub=$2
78
79 ${CROSS_COMPILE}as $AFLAGS -o $t.o $t.s > /dev/null 2>&1
Rabin Vincent5358db02010-01-05 20:27:58 +053080
Rabin Vincentb396aa02010-06-03 22:48:12 +053081 if [ "$ARCH" = "arm" ]; then
82 if [ $width -eq 2 ]; then
Rabin Vincent5358db02010-01-05 20:27:58 +053083 OBJDUMPFLAGS="-M force-thumb"
84 fi
85
Borislav Petkovd72e7202020-10-13 16:48:14 -070086 ${CROSS_COMPILE}strip $t.o
Rabin Vincent5358db02010-01-05 20:27:58 +053087 fi
88
Will Deaconbe9fa662018-01-18 16:33:57 -080089 if [ "$ARCH" = "arm64" ]; then
90 if [ $width -eq 4 ]; then
91 type=inst
92 fi
93
Borislav Petkovd72e7202020-10-13 16:48:14 -070094 ${CROSS_COMPILE}strip $t.o
Will Deaconbe9fa662018-01-18 16:33:57 -080095 fi
96
Björn Töpel00b24252023-01-19 08:47:38 +010097 if [ "$ARCH" = "riscv" ]; then
98 OBJDUMPFLAGS="-M no-aliases --section=.text -D"
99 ${CROSS_COMPILE}strip $t.o
100 fi
101
Youling Tangd738bce2023-12-16 14:51:28 +0800102 if [ "$ARCH" = "loongarch" ]; then
103 ${CROSS_COMPILE}strip $t.o
104 fi
105
Borislav Petkovd72e7202020-10-13 16:48:14 -0700106 if [ $pc_sub -ne 0 ]; then
107 if [ $PC ]; then
108 adj_vma=$(( $PC - $pc_sub ))
109 OBJDUMPFLAGS="$OBJDUMPFLAGS --adjust-vma=$adj_vma"
110 fi
111 fi
112
113 ${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $t.o | \
114 grep -v "/tmp\|Disassembly\|\.text\|^$" > $t.dis 2>&1
Rabin Vincent5358db02010-01-05 20:27:58 +0530115}
116
Borislav Petkov765f2bf2022-08-08 10:59:28 +0200117# Match the maximum number of opcode bytes from @op_bytes contained within
118# @opline
119#
120# Params:
121# @op_bytes: The string of bytes from the Code: line
122# @opline: The disassembled line coming from objdump
123#
124# Returns:
125# The max number of opcode bytes from the beginning of @op_bytes which match
126# the opcode bytes in the objdump line.
127get_substr_opcode_bytes_num()
128{
129 local op_bytes=$1
130 local opline=$2
131
132 local retval=0
133 substr=""
134
135 for opc in $op_bytes;
136 do
137 substr+="$opc"
138
Björn Töpel00b24252023-01-19 08:47:38 +0100139 opcode="$substr"
140 if [ "$ARCH" = "riscv" ]; then
141 opcode=$(echo $opcode | tr ' ' '\n' | tac | tr -d '\n')
142 fi
143
Borislav Petkov765f2bf2022-08-08 10:59:28 +0200144 # return if opcode bytes do not match @opline anymore
Björn Töpel00b24252023-01-19 08:47:38 +0100145 if ! echo $opline | grep -q "$opcode";
Borislav Petkov765f2bf2022-08-08 10:59:28 +0200146 then
147 break
148 fi
149
150 # add trailing space
151 substr+=" "
152 retval=$((retval+1))
153 done
154
155 return $retval
156}
157
158# Return the line number in objdump output to where the IP marker in the Code:
159# line points to
160#
161# Params:
162# @all_code: code in bytes without the marker
163# @dis_file: disassembled file
164# @ip_byte: The byte to which the IP points to
165get_faultlinenum()
166{
167 local all_code="$1"
168 local dis_file="$2"
169
170 # num bytes including IP byte
171 local num_bytes_ip=$(( $3 + 1 * $width ))
172
173 # Add the two header lines (we're counting from 1).
174 local retval=3
175
176 # remove marker
177 all_code=$(echo $all_code | sed -e 's/[<>()]//g')
178
179 while read line
180 do
181 get_substr_opcode_bytes_num "$all_code" "$line"
182 ate_opcodes=$?
183
184 if ! (( $ate_opcodes )); then
185 continue
186 fi
187
188 num_bytes_ip=$((num_bytes_ip - ($ate_opcodes * $width) ))
189 if (( $num_bytes_ip <= 0 )); then
190 break
191 fi
192
193 # Delete matched opcode bytes from all_code. For that, compute
194 # how many chars those opcodes are represented by and include
195 # trailing space.
196 #
197 # a byte is 2 chars, ate_opcodes is also the number of trailing
198 # spaces
199 del_chars=$(( ($ate_opcodes * $width * 2) + $ate_opcodes ))
200
201 all_code=$(echo $all_code | sed -e "s!^.\{$del_chars\}!!")
202
203 let "retval+=1"
204
205 done < $dis_file
206
207 return $retval
208}
209
Randy Dunlapdcecc6c2007-07-15 23:41:15 -0700210marker=`expr index "$code" "\<"`
211if [ $marker -eq 0 ]; then
212 marker=`expr index "$code" "\("`
213fi
214
Arjan van de Ven846442c2008-12-01 14:21:06 -0800215touch $T.oo
Randy Dunlapdcecc6c2007-07-15 23:41:15 -0700216if [ $marker -ne 0 ]; then
Borislav Petkov765f2bf2022-08-08 10:59:28 +0200217 # How many bytes to subtract from the program counter
218 # in order to get to the beginning virtual address of the
219 # Code:
220 pc_sub=$(( (($marker - 1) / (2 * $width + 1)) * $width ))
Arjan van de Ven846442c2008-12-01 14:21:06 -0800221 echo All code >> $T.oo
222 echo ======== >> $T.oo
223 beforemark=`echo "$code"`
Rabin Vincent5358db02010-01-05 20:27:58 +0530224 echo -n " .$type 0x" > $T.s
Randy Dunlapdcecc6c2007-07-15 23:41:15 -0700225
Borislav Petkov765f2bf2022-08-08 10:59:28 +0200226 echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
227
228 disas $T $pc_sub
229
230 cat $T.dis >> $T.oo
231
232 get_faultlinenum "$code" "$T.dis" $pc_sub
233 faultlinenum=$?
234
235 # and fix code at-and-after marker
Randy Dunlapdcecc6c2007-07-15 23:41:15 -0700236 code=`echo "$code" | cut -c$((${marker} + 1))-`
Borislav Petkov765f2bf2022-08-08 10:59:28 +0200237
238 rm -f $T.o $T.s $T.dis
Randy Dunlapdcecc6c2007-07-15 23:41:15 -0700239fi
Borislav Petkov765f2bf2022-08-08 10:59:28 +0200240
Arjan van de Ven846442c2008-12-01 14:21:06 -0800241echo Code starting with the faulting instruction > $T.aa
242echo =========================================== >> $T.aa
weidonghui75e2f712021-11-05 13:34:42 -0700243code=`echo $code | sed -e 's/\r//;s/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
Rabin Vincent5358db02010-01-05 20:27:58 +0530244echo -n " .$type 0x" > $T.s
Randy Dunlapdcecc6c2007-07-15 23:41:15 -0700245echo $code >> $T.s
Borislav Petkovd72e7202020-10-13 16:48:14 -0700246disas $T 0
Rabin Vincent5358db02010-01-05 20:27:58 +0530247cat $T.dis >> $T.aa
Arjan van de Ven846442c2008-12-01 14:21:06 -0800248
Ivan Delalandee08df072020-05-07 18:35:53 -0700249cat $T.oo | sed -e "${faultlinenum}s/^\([^:]*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"
Arjan van de Ven846442c2008-12-01 14:21:06 -0800250echo
251cat $T.aa
252cleanup