xref: /linux/scripts/decodecode (revision 75e2f715dffcf0cadedf49f2f3692bdd33fdd889)
1dcecc6c7SRandy Dunlap#!/bin/sh
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
3dcecc6c7SRandy Dunlap# Disassemble the Code: line in Linux oopses
4dcecc6c7SRandy Dunlap# usage: decodecode < oops.file
5dcecc6c7SRandy Dunlap#
6dcecc6c7SRandy Dunlap# options: set env. variable AFLAGS=options to pass options to "as";
7dcecc6c7SRandy Dunlap# e.g., to decode an i386 oops on an x86_64 system, use:
8dcecc6c7SRandy Dunlap# AFLAGS=--32 decodecode < 386.oops
9d72e720aSBorislav Petkov# PC=hex - the PC (program counter) the oops points to
10dcecc6c7SRandy Dunlap
11fa220d89SRandy Dunlapcleanup() {
125358db0bSRabin Vincent	rm -f $T $T.s $T.o $T.oo $T.aa $T.dis
13fa220d89SRandy Dunlap	exit 1
14fa220d89SRandy Dunlap}
15fa220d89SRandy Dunlap
16fa220d89SRandy Dunlapdie() {
17fa220d89SRandy Dunlap	echo "$@"
18fa220d89SRandy Dunlap	exit 1
19fa220d89SRandy Dunlap}
20fa220d89SRandy Dunlap
21fa220d89SRandy Dunlaptrap cleanup EXIT
22fa220d89SRandy Dunlap
23fa220d89SRandy DunlapT=`mktemp` || die "cannot create temp file"
24dcecc6c7SRandy Dunlapcode=
257e68b361SAndy Shevchenkocont=
26dcecc6c7SRandy Dunlap
27dcecc6c7SRandy Dunlapwhile read i ; do
28dcecc6c7SRandy Dunlap
29dcecc6c7SRandy Dunlapcase "$i" in
30dcecc6c7SRandy Dunlap*Code:*)
31dcecc6c7SRandy Dunlap	code=$i
327e68b361SAndy Shevchenko	cont=yes
337e68b361SAndy Shevchenko	;;
347e68b361SAndy Shevchenko*)
357e68b361SAndy Shevchenko	[ -n "$cont" ] && {
367e68b361SAndy Shevchenko		xdump="$(echo $i | grep '^[[:xdigit:]<>[:space:]]\+$')"
377e68b361SAndy Shevchenko		if [ -n "$xdump" ]; then
387e68b361SAndy Shevchenko			code="$code $xdump"
397e68b361SAndy Shevchenko		else
407e68b361SAndy Shevchenko			cont=
417e68b361SAndy Shevchenko		fi
427e68b361SAndy Shevchenko	}
43dcecc6c7SRandy Dunlap	;;
44dcecc6c7SRandy Dunlapesac
45dcecc6c7SRandy Dunlap
46dcecc6c7SRandy Dunlapdone
47dcecc6c7SRandy Dunlap
48dcecc6c7SRandy Dunlapif [ -z "$code" ]; then
49fa220d89SRandy Dunlap	rm $T
50dcecc6c7SRandy Dunlap	exit
51dcecc6c7SRandy Dunlapfi
52dcecc6c7SRandy Dunlap
53dcecc6c7SRandy Dunlapecho $code
54dcecc6c7SRandy Dunlapcode=`echo $code | sed -e 's/.*Code: //'`
55dcecc6c7SRandy Dunlap
565358db0bSRabin Vincentwidth=`expr index "$code" ' '`
57b396aa03SRabin Vincentwidth=$((($width-1)/2))
585358db0bSRabin Vincentcase $width in
595358db0bSRabin Vincent1) type=byte ;;
605358db0bSRabin Vincent2) type=2byte ;;
615358db0bSRabin Vincent4) type=4byte ;;
625358db0bSRabin Vincentesac
635358db0bSRabin Vincent
64c5cfb62fSMarc Zyngierif [ -z "$ARCH" ]; then
65c5cfb62fSMarc Zyngier    case `uname -m` in
66c5cfb62fSMarc Zyngier	aarch64*) ARCH=arm64 ;;
67c5cfb62fSMarc Zyngier	arm*) ARCH=arm ;;
68c5cfb62fSMarc Zyngier    esac
69c5cfb62fSMarc Zyngierfi
70c5cfb62fSMarc Zyngier
71d72e720aSBorislav Petkov# Params: (tmp_file, pc_sub)
725358db0bSRabin Vincentdisas() {
73d72e720aSBorislav Petkov	t=$1
74d72e720aSBorislav Petkov	pc_sub=$2
75d72e720aSBorislav Petkov
76d72e720aSBorislav Petkov	${CROSS_COMPILE}as $AFLAGS -o $t.o $t.s > /dev/null 2>&1
775358db0bSRabin Vincent
78b396aa03SRabin Vincent	if [ "$ARCH" = "arm" ]; then
79b396aa03SRabin Vincent		if [ $width -eq 2 ]; then
805358db0bSRabin Vincent			OBJDUMPFLAGS="-M force-thumb"
815358db0bSRabin Vincent		fi
825358db0bSRabin Vincent
83d72e720aSBorislav Petkov		${CROSS_COMPILE}strip $t.o
845358db0bSRabin Vincent	fi
855358db0bSRabin Vincent
86be9fa663SWill Deacon	if [ "$ARCH" = "arm64" ]; then
87be9fa663SWill Deacon		if [ $width -eq 4 ]; then
88be9fa663SWill Deacon			type=inst
89be9fa663SWill Deacon		fi
90be9fa663SWill Deacon
91d72e720aSBorislav Petkov		${CROSS_COMPILE}strip $t.o
92be9fa663SWill Deacon	fi
93be9fa663SWill Deacon
94d72e720aSBorislav Petkov	if [ $pc_sub -ne 0 ]; then
95d72e720aSBorislav Petkov		if [ $PC ]; then
96d72e720aSBorislav Petkov			adj_vma=$(( $PC - $pc_sub ))
97d72e720aSBorislav Petkov			OBJDUMPFLAGS="$OBJDUMPFLAGS --adjust-vma=$adj_vma"
98d72e720aSBorislav Petkov		fi
99d72e720aSBorislav Petkov	fi
100d72e720aSBorislav Petkov
101d72e720aSBorislav Petkov	${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $t.o | \
102d72e720aSBorislav Petkov		grep -v "/tmp\|Disassembly\|\.text\|^$" > $t.dis 2>&1
1035358db0bSRabin Vincent}
1045358db0bSRabin Vincent
105dcecc6c7SRandy Dunlapmarker=`expr index "$code" "\<"`
106dcecc6c7SRandy Dunlapif [ $marker -eq 0 ]; then
107dcecc6c7SRandy Dunlap	marker=`expr index "$code" "\("`
108dcecc6c7SRandy Dunlapfi
109dcecc6c7SRandy Dunlap
110d72e720aSBorislav Petkov
111846442c8SArjan van de Ventouch $T.oo
112dcecc6c7SRandy Dunlapif [ $marker -ne 0 ]; then
113d72e720aSBorislav Petkov	# 2 opcode bytes and a single space
114d72e720aSBorislav Petkov	pc_sub=$(( $marker / 3 ))
115846442c8SArjan van de Ven	echo All code >> $T.oo
116846442c8SArjan van de Ven	echo ======== >> $T.oo
117846442c8SArjan van de Ven	beforemark=`echo "$code"`
1185358db0bSRabin Vincent	echo -n "	.$type 0x" > $T.s
1195358db0bSRabin Vincent	echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
120d72e720aSBorislav Petkov	disas $T $pc_sub
1215358db0bSRabin Vincent	cat $T.dis >> $T.oo
1225358db0bSRabin Vincent	rm -f $T.o $T.s $T.dis
123dcecc6c7SRandy Dunlap
124dcecc6c7SRandy Dunlap# and fix code at-and-after marker
125dcecc6c7SRandy Dunlap	code=`echo "$code" | cut -c$((${marker} + 1))-`
126dcecc6c7SRandy Dunlapfi
127846442c8SArjan van de Venecho Code starting with the faulting instruction  > $T.aa
128846442c8SArjan van de Venecho =========================================== >> $T.aa
129*75e2f715Sweidonghuicode=`echo $code | sed -e 's/\r//;s/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
1305358db0bSRabin Vincentecho -n "	.$type 0x" > $T.s
131dcecc6c7SRandy Dunlapecho $code >> $T.s
132d72e720aSBorislav Petkovdisas $T 0
1335358db0bSRabin Vincentcat $T.dis >> $T.aa
134846442c8SArjan van de Ven
13518ff44b1SBorislav Petkov# (lines of whole $T.oo) - (lines of $T.aa, i.e. "Code starting") + 3,
13618ff44b1SBorislav Petkov# i.e. the title + the "===..=" line (sed is counting from 1, 0 address is
13718ff44b1SBorislav Petkov# special)
13818ff44b1SBorislav Petkovfaultlinenum=$(( $(wc -l $T.oo  | cut -d" " -f1) - \
13918ff44b1SBorislav Petkov		 $(wc -l $T.aa  | cut -d" " -f1) + 3))
14018ff44b1SBorislav Petkov
1412a95e37cSBorislav Petkovfaultline=`cat $T.dis | head -1 | cut -d":" -f2-`
1425358db0bSRabin Vincentfaultline=`echo "$faultline" | sed -e 's/\[/\\\[/g; s/\]/\\\]/g'`
143846442c8SArjan van de Ven
144e08df079SIvan Delalandecat $T.oo | sed -e "${faultlinenum}s/^\([^:]*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"
145846442c8SArjan van de Venecho
146846442c8SArjan van de Vencat $T.aa
147846442c8SArjan van de Vencleanup
148