xref: /linux/scripts/decodecode (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1dcecc6c7SRandy Dunlap#!/bin/sh
2*b2441318SGreg 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
9dcecc6c7SRandy Dunlap
10fa220d89SRandy Dunlapcleanup() {
115358db0bSRabin Vincent	rm -f $T $T.s $T.o $T.oo $T.aa $T.dis
12fa220d89SRandy Dunlap	exit 1
13fa220d89SRandy Dunlap}
14fa220d89SRandy Dunlap
15fa220d89SRandy Dunlapdie() {
16fa220d89SRandy Dunlap	echo "$@"
17fa220d89SRandy Dunlap	exit 1
18fa220d89SRandy Dunlap}
19fa220d89SRandy Dunlap
20fa220d89SRandy Dunlaptrap cleanup EXIT
21fa220d89SRandy Dunlap
22fa220d89SRandy DunlapT=`mktemp` || die "cannot create temp file"
23dcecc6c7SRandy Dunlapcode=
24dcecc6c7SRandy Dunlap
25dcecc6c7SRandy Dunlapwhile read i ; do
26dcecc6c7SRandy Dunlap
27dcecc6c7SRandy Dunlapcase "$i" in
28dcecc6c7SRandy Dunlap*Code:*)
29dcecc6c7SRandy Dunlap	code=$i
30dcecc6c7SRandy Dunlap	;;
31dcecc6c7SRandy Dunlapesac
32dcecc6c7SRandy Dunlap
33dcecc6c7SRandy Dunlapdone
34dcecc6c7SRandy Dunlap
35dcecc6c7SRandy Dunlapif [ -z "$code" ]; then
36fa220d89SRandy Dunlap	rm $T
37dcecc6c7SRandy Dunlap	exit
38dcecc6c7SRandy Dunlapfi
39dcecc6c7SRandy Dunlap
40dcecc6c7SRandy Dunlapecho $code
41dcecc6c7SRandy Dunlapcode=`echo $code | sed -e 's/.*Code: //'`
42dcecc6c7SRandy Dunlap
435358db0bSRabin Vincentwidth=`expr index "$code" ' '`
44b396aa03SRabin Vincentwidth=$((($width-1)/2))
455358db0bSRabin Vincentcase $width in
465358db0bSRabin Vincent1) type=byte ;;
475358db0bSRabin Vincent2) type=2byte ;;
485358db0bSRabin Vincent4) type=4byte ;;
495358db0bSRabin Vincentesac
505358db0bSRabin Vincent
515358db0bSRabin Vincentdisas() {
52b396aa03SRabin Vincent	${CROSS_COMPILE}as $AFLAGS -o $1.o $1.s > /dev/null 2>&1
535358db0bSRabin Vincent
54b396aa03SRabin Vincent	if [ "$ARCH" = "arm" ]; then
55b396aa03SRabin Vincent		if [ $width -eq 2 ]; then
565358db0bSRabin Vincent			OBJDUMPFLAGS="-M force-thumb"
575358db0bSRabin Vincent		fi
585358db0bSRabin Vincent
595358db0bSRabin Vincent		${CROSS_COMPILE}strip $1.o
605358db0bSRabin Vincent	fi
615358db0bSRabin Vincent
625358db0bSRabin Vincent	${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $1.o | \
63b396aa03SRabin Vincent		grep -v "/tmp\|Disassembly\|\.text\|^$" > $1.dis 2>&1
645358db0bSRabin Vincent}
655358db0bSRabin Vincent
66dcecc6c7SRandy Dunlapmarker=`expr index "$code" "\<"`
67dcecc6c7SRandy Dunlapif [ $marker -eq 0 ]; then
68dcecc6c7SRandy Dunlap	marker=`expr index "$code" "\("`
69dcecc6c7SRandy Dunlapfi
70dcecc6c7SRandy Dunlap
71846442c8SArjan van de Ventouch $T.oo
72dcecc6c7SRandy Dunlapif [ $marker -ne 0 ]; then
73846442c8SArjan van de Ven	echo All code >> $T.oo
74846442c8SArjan van de Ven	echo ======== >> $T.oo
75846442c8SArjan van de Ven	beforemark=`echo "$code"`
765358db0bSRabin Vincent	echo -n "	.$type 0x" > $T.s
775358db0bSRabin Vincent	echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
785358db0bSRabin Vincent	disas $T
795358db0bSRabin Vincent	cat $T.dis >> $T.oo
805358db0bSRabin Vincent	rm -f $T.o $T.s $T.dis
81dcecc6c7SRandy Dunlap
82dcecc6c7SRandy Dunlap# and fix code at-and-after marker
83dcecc6c7SRandy Dunlap	code=`echo "$code" | cut -c$((${marker} + 1))-`
84dcecc6c7SRandy Dunlapfi
85846442c8SArjan van de Venecho Code starting with the faulting instruction  > $T.aa
86846442c8SArjan van de Venecho =========================================== >> $T.aa
875358db0bSRabin Vincentcode=`echo $code | sed -e 's/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
885358db0bSRabin Vincentecho -n "	.$type 0x" > $T.s
89dcecc6c7SRandy Dunlapecho $code >> $T.s
905358db0bSRabin Vincentdisas $T
915358db0bSRabin Vincentcat $T.dis >> $T.aa
92846442c8SArjan van de Ven
9318ff44b1SBorislav Petkov# (lines of whole $T.oo) - (lines of $T.aa, i.e. "Code starting") + 3,
9418ff44b1SBorislav Petkov# i.e. the title + the "===..=" line (sed is counting from 1, 0 address is
9518ff44b1SBorislav Petkov# special)
9618ff44b1SBorislav Petkovfaultlinenum=$(( $(wc -l $T.oo  | cut -d" " -f1) - \
9718ff44b1SBorislav Petkov		 $(wc -l $T.aa  | cut -d" " -f1) + 3))
9818ff44b1SBorislav Petkov
992a95e37cSBorislav Petkovfaultline=`cat $T.dis | head -1 | cut -d":" -f2-`
1005358db0bSRabin Vincentfaultline=`echo "$faultline" | sed -e 's/\[/\\\[/g; s/\]/\\\]/g'`
101846442c8SArjan van de Ven
10218ff44b1SBorislav Petkovcat $T.oo | sed -e "${faultlinenum}s/^\(.*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"
103846442c8SArjan van de Venecho
104846442c8SArjan van de Vencat $T.aa
105846442c8SArjan van de Vencleanup
106