-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbacktrace
executable file
·91 lines (85 loc) · 2.69 KB
/
backtrace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! /bin/bash -e
# Hacked together by Thorsten von Eicken in 2019
XTENSA_GDB=~/.platformio/packages/toolchain-xtensa32/bin/xtensa-esp32-elf-gdb
# Validate commandline arguments
if [ -z "$1" ]; then
echo "usage: $0 <elf file> [<backtrace-text>]" 1>&2
echo "reads from stdin if no backtrace-text is specified" 2>&2
exit 1
fi
elf="$1"
if ! [ -f $elf ]; then
echo "ELF file not found ($elf)" 1>&2
exit 1
fi
if [ -z "$2" ]; then
echo "reading backtrace from stdin"
bt=/dev/stdin
elif ! [ -f "$2" ]; then
echo "Backtrace file not found ($2)" 1>&2
exit 1
else
bt=$2
fi
# Parse exception info and command backtrace
rePC='PC\s*:? (0x40[0-2][0-9A-Fa-f]*)'
reEA='EXCVADDR\s*: (0x40[0-2][0-9A-Fa-f]*)'
reBT='Backtrace:\s?(.*)'
reIN='^[0-9A-Fa-f:x ]+$'
reOT='[^0-9a-zA-Z](0x40[0-2][0-9A-Fa-f]{5})[^0-9a-zA-Z]'
inBT=0
declare -a REGS
BT=
while read p; do
if [[ "$p" =~ $rePC ]]; then
# PC : 0x400d38fe PS : 0x00060630 A0 : 0x800d35a6 A1 : 0x3ffb1c50
REGS+=(PC:${BASH_REMATCH[1]})
elif [[ "$p" =~ $reEA ]]; then
# EXCVADDR: 0x16000001 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xffffffff
REGS+=(EXCVADDR:${BASH_REMATCH[1]})
elif [[ "$p" =~ $reBT ]]; then
# Backtrace: 0x400d38fe:0x3ffb1c50 0x400d35a3:0x3ffb1c90 0x400e40bf:0x3ffb1fb0
BT="${BASH_REMATCH[1]}"
# echo $BT
inBT=1
elif [[ $inBT ]]; then
if [[ "$p" =~ $reIN ]]; then
# backtrace continuation lines cut by terminal emulator
# 0x400d38fe:0x3ffb1c50 0x400d35a3:0x3ffb1c90
BT="$BT${BASH_REMATCH[0]}"
# echo $BT
else
inBT=0
fi
elif [[ "$p" =~ $reOT ]]; then
# other lines with addresses
# A2 : 0x00000001 A3 : 0x00000000 A4 : 0x16000001 A5 : 0x31a07a28
REGS+=(OTHER:${BASH_REMATCH[1]})
fi
done <$bt
#echo "PC is ${PC}"
#echo "EA is ${EA}"
#echo "BT is ${BT}"
# Parse addresses in backtrace and add them to REGS
n=0
for stk in $BT; do
# ex: 0x400d38fe:0x3ffb1c50
# echo $stk ...
if [[ $stk =~ (0x40[0-2][0-9A-Fa-f]+): ]]; then
addr=${BASH_REMATCH[1]}
REGS+=("BT-${n}:${addr}")
# echo addr: $addr
fi
let "n=$n + 1"
#[[ $n -gt 10 ]] && break
done
# Iterate through all addresses and ask GDB to print source info for each one
for reg in "${REGS[@]}"; do
name=${reg%:*}
addr=${reg#*:}
# echo "Checking $name: $addr"
info=$($XTENSA_GDB --batch $elf -ex "set listsize 1" -ex "l *$addr" -ex q 2>&1 |\
sed -e 's;/[^ ]*/ESP32/;;' |\
egrep -v "(No such file or directory)|(^$)") || true
if [[ -n "$info" ]]; then echo "$name: $info"; fi
done