xref: /openbmc/linux/scripts/decodecode (revision 31eeb6b0)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3# 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
9# PC=hex - the PC (program counter) the oops points to
10
11cleanup() {
12	rm -f $T $T.s $T.o $T.oo $T.aa $T.dis
13	exit 1
14}
15
16die() {
17	echo "$@"
18	exit 1
19}
20
21trap cleanup EXIT
22
23T=`mktemp` || die "cannot create temp file"
24code=
25cont=
26
27while read i ; do
28
29case "$i" in
30*Code:*)
31	code=$i
32	cont=yes
33	;;
34*)
35	[ -n "$cont" ] && {
36		xdump="$(echo $i | grep '^[[:xdigit:]<>[:space:]]\+$')"
37		if [ -n "$xdump" ]; then
38			code="$code $xdump"
39		else
40			cont=
41		fi
42	}
43	;;
44esac
45
46done
47
48if [ -z "$code" ]; then
49	rm $T
50	exit
51fi
52
53echo $code
54code=`echo $code | sed -e 's/.*Code: //'`
55
56width=`expr index "$code" ' '`
57width=$((($width-1)/2))
58case $width in
591) type=byte ;;
602) type=2byte ;;
614) type=4byte ;;
62esac
63
64if [ -z "$ARCH" ]; then
65    case `uname -m` in
66	aarch64*) ARCH=arm64 ;;
67	arm*) ARCH=arm ;;
68    esac
69fi
70
71# Params: (tmp_file, pc_sub)
72disas() {
73	t=$1
74	pc_sub=$2
75
76	${CROSS_COMPILE}as $AFLAGS -o $t.o $t.s > /dev/null 2>&1
77
78	if [ "$ARCH" = "arm" ]; then
79		if [ $width -eq 2 ]; then
80			OBJDUMPFLAGS="-M force-thumb"
81		fi
82
83		${CROSS_COMPILE}strip $t.o
84	fi
85
86	if [ "$ARCH" = "arm64" ]; then
87		if [ $width -eq 4 ]; then
88			type=inst
89		fi
90
91		${CROSS_COMPILE}strip $t.o
92	fi
93
94	if [ $pc_sub -ne 0 ]; then
95		if [ $PC ]; then
96			adj_vma=$(( $PC - $pc_sub ))
97			OBJDUMPFLAGS="$OBJDUMPFLAGS --adjust-vma=$adj_vma"
98		fi
99	fi
100
101	${CROSS_COMPILE}objdump $OBJDUMPFLAGS -S $t.o | \
102		grep -v "/tmp\|Disassembly\|\.text\|^$" > $t.dis 2>&1
103}
104
105marker=`expr index "$code" "\<"`
106if [ $marker -eq 0 ]; then
107	marker=`expr index "$code" "\("`
108fi
109
110
111touch $T.oo
112if [ $marker -ne 0 ]; then
113	# 2 opcode bytes and a single space
114	pc_sub=$(( $marker / 3 ))
115	echo All code >> $T.oo
116	echo ======== >> $T.oo
117	beforemark=`echo "$code"`
118	echo -n "	.$type 0x" > $T.s
119	echo $beforemark | sed -e 's/ /,0x/g; s/[<>()]//g' >> $T.s
120	disas $T $pc_sub
121	cat $T.dis >> $T.oo
122	rm -f $T.o $T.s $T.dis
123
124# and fix code at-and-after marker
125	code=`echo "$code" | cut -c$((${marker} + 1))-`
126fi
127echo Code starting with the faulting instruction  > $T.aa
128echo =========================================== >> $T.aa
129code=`echo $code | sed -e 's/\r//;s/ [<(]/ /;s/[>)] / /;s/ /,0x/g; s/[>)]$//'`
130echo -n "	.$type 0x" > $T.s
131echo $code >> $T.s
132disas $T 0
133cat $T.dis >> $T.aa
134
135# (lines of whole $T.oo) - (lines of $T.aa, i.e. "Code starting") + 3,
136# i.e. the title + the "===..=" line (sed is counting from 1, 0 address is
137# special)
138faultlinenum=$(( $(wc -l $T.oo  | cut -d" " -f1) - \
139		 $(wc -l $T.aa  | cut -d" " -f1) + 3))
140
141faultline=`cat $T.dis | head -1 | cut -d":" -f2-`
142faultline=`echo "$faultline" | sed -e 's/\[/\\\[/g; s/\]/\\\]/g'`
143
144cat $T.oo | sed -e "${faultlinenum}s/^\([^:]*:\)\(.*\)/\1\*\2\t\t<-- trapping instruction/"
145echo
146cat $T.aa
147cleanup
148