xref: /linux/scripts/decodecode (revision 5358db0b0e16470337c6ec08177deb3f68ed7673)
1dcecc6c7SRandy Dunlap#!/bin/sh
2dcecc6c7SRandy Dunlap# Disassemble the Code: line in Linux oopses
3dcecc6c7SRandy Dunlap# usage: decodecode < oops.file
4dcecc6c7SRandy Dunlap#
5dcecc6c7SRandy Dunlap# options: set env. variable AFLAGS=options to pass options to "as";
6dcecc6c7SRandy Dunlap# e.g., to decode an i386 oops on an x86_64 system, use:
7dcecc6c7SRandy Dunlap# AFLAGS=--32 decodecode < 386.oops
8dcecc6c7SRandy Dunlap
9fa220d89SRandy Dunlapcleanup() {
10*5358db0bSRabin Vincent	rm -f $T $T.s $T.o $T.oo $T.aa $T.dis
11fa220d89SRandy Dunlap	exit 1
12fa220d89SRandy Dunlap}
13fa220d89SRandy Dunlap
14fa220d89SRandy Dunlapdie() {
15fa220d89SRandy Dunlap	echo "$@"
16fa220d89SRandy Dunlap	exit 1
17fa220d89SRandy Dunlap}
18fa220d89SRandy Dunlap
19fa220d89SRandy Dunlaptrap cleanup EXIT
20fa220d89SRandy Dunlap
21fa220d89SRandy DunlapT=`mktemp` || die "cannot create temp file"
22dcecc6c7SRandy Dunlapcode=
23dcecc6c7SRandy Dunlap
24dcecc6c7SRandy Dunlapwhile read i ; do
25dcecc6c7SRandy Dunlap
26dcecc6c7SRandy Dunlapcase "$i" in
27dcecc6c7SRandy Dunlap*Code:*)
28dcecc6c7SRandy Dunlap	code=$i
29dcecc6c7SRandy Dunlap	;;
30dcecc6c7SRandy Dunlapesac
31dcecc6c7SRandy Dunlap
32dcecc6c7SRandy Dunlapdone
33dcecc6c7SRandy Dunlap
34dcecc6c7SRandy Dunlapif [ -z "$code" ]; then
35fa220d89SRandy Dunlap	rm $T
36dcecc6c7SRandy Dunlap	exit
37dcecc6c7SRandy Dunlapfi
38dcecc6c7SRandy Dunlap
39dcecc6c7SRandy Dunlapecho $code
40dcecc6c7SRandy Dunlapcode=`echo $code | sed -e 's/.*Code: //'`
41dcecc6c7SRandy Dunlap
42*5358db0bSRabin Vincentwidth=`expr index "$code" ' '`
43*5358db0bSRabin Vincentwidth=$[($width-1)/2]
44*5358db0bSRabin Vincentcase $width in
45*5358db0bSRabin Vincent1) type=byte ;;
46*5358db0bSRabin Vincent2) type=2byte ;;
47*5358db0bSRabin Vincent4) type=4byte ;;
48*5358db0bSRabin Vincentesac
49*5358db0bSRabin Vincent
50*5358db0bSRabin Vincentdisas() {
51*5358db0bSRabin Vincent	${CROSS_COMPILE}as $AFLAGS -o $1.o $1.s &> /dev/null
52*5358db0bSRabin Vincent
53*5358db0bSRabin Vincent	if [ "$ARCH" == "arm" ]; then
54*5358db0bSRabin Vincent		if [ $width == 2 ]; then
55*5358db0bSRabin Vincent			OBJDUMPFLAGS="-M force-thumb"
56*5358db0bSRabin Vincent		fi
57*5358db0bSRabin Vincent
58*5358db0bSRabin Vincent		${CROSS_COMPILE}strip $1.o
59*5358db0bSRabin Vincent	fi
60*5358db0bSRabin Vincent
61*5358db0bSRabin Vincent	${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $1.o | \
62*5358db0bSRabin Vincent		grep -v "/tmp\|Disassembly\|\.text\|^$" &> $1.dis
63*5358db0bSRabin Vincent}
64*5358db0bSRabin Vincent
65dcecc6c7SRandy Dunlapmarker=`expr index "$code" "\<"`
66dcecc6c7SRandy Dunlapif [ $marker -eq 0 ]; then
67dcecc6c7SRandy Dunlap	marker=`expr index "$code" "\("`
68dcecc6c7SRandy Dunlapfi
69dcecc6c7SRandy Dunlap
70846442c8SArjan van de Ventouch $T.oo
71dcecc6c7SRandy Dunlapif [ $marker -ne 0 ]; then
72846442c8SArjan van de Ven	echo All code >> $T.oo
73846442c8SArjan van de Ven	echo ======== >> $T.oo
74846442c8SArjan van de Ven	beforemark=`echo "$code"`
75*5358db0bSRabin Vincent	echo -n "	.$type 0x" > $T.s
76*5358db0bSRabin Vincent	echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
77*5358db0bSRabin Vincent	disas $T
78*5358db0bSRabin Vincent	cat $T.dis >> $T.oo
79*5358db0bSRabin Vincent	rm -f $T.o $T.s $T.dis
80dcecc6c7SRandy Dunlap
81dcecc6c7SRandy Dunlap# and fix code at-and-after marker
82dcecc6c7SRandy Dunlap	code=`echo "$code" | cut -c$((${marker} + 1))-`
83dcecc6c7SRandy Dunlapfi
84846442c8SArjan van de Venecho Code starting with the faulting instruction  > $T.aa
85846442c8SArjan van de Venecho =========================================== >> $T.aa
86*5358db0bSRabin Vincentcode=`echo $code | sed -e 's/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
87*5358db0bSRabin Vincentecho -n "	.$type 0x" > $T.s
88dcecc6c7SRandy Dunlapecho $code >> $T.s
89*5358db0bSRabin Vincentdisas $T
90*5358db0bSRabin Vincentcat $T.dis >> $T.aa
91846442c8SArjan van de Ven
92*5358db0bSRabin Vincentfaultline=`cat $T.dis | head -1 | cut -d":" -f2`
93*5358db0bSRabin Vincentfaultline=`echo "$faultline" | sed -e 's/\[/\\\[/g; s/\]/\\\]/g'`
94846442c8SArjan van de Ven
95846442c8SArjan van de Vencat $T.oo | sed -e "s/\($faultline\)/\*\1     <-- trapping instruction/g"
96846442c8SArjan van de Venecho
97846442c8SArjan van de Vencat $T.aa
98846442c8SArjan van de Vencleanup
99