-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.sh
More file actions
executable file
·176 lines (156 loc) · 4.08 KB
/
Copy pathdebug.sh
File metadata and controls
executable file
·176 lines (156 loc) · 4.08 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env bash
set -euo pipefail
SPEED=""
PROBE=""
PROFILE="debug"
CONNECT_UNDER_RESET=0
NO_BUILD=0
NO_FLASH=0
MODE=""
extract_serial() {
local probe="$1"
if [[ "$probe" == *:*:* ]]; then
printf '%s\n' "${probe##*:}"
else
printf '%s\n' "$probe"
fi
}
usage() {
cat <<'EOF'
Usage:
./debug.sh server
./debug.sh gdb
./debug.sh --release server
./debug.sh --release gdb
./debug.sh --no-build --no-flash server
./debug.sh --no-build --no-flash gdb
./debug.sh --probe <SERIAL|VID:PID:SERIAL> --speed <kHz> server
Modes:
server Build/flash if requested, then start openocd with GDB port 3333
gdb Start gdb-multiarch preconfigured for the secure ELF and NS symbols
Notes:
- Typical workflow is to run './debug.sh server' in one terminal and
'./debug.sh gdb' in another.
- The GDB mode does not flash the target. Use the server mode first, or flash
separately with ./flash.sh.
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
server|gdb)
if [[ -n "$MODE" ]]; then
echo "mode already specified: $MODE" >&2
exit 1
fi
MODE="$1"
shift
;;
--release)
PROFILE="release"
shift
;;
--probe)
PROBE="${2:?missing value for --probe}"
shift 2
;;
--speed)
SPEED="${2:?missing value for --speed}"
shift 2
;;
--connect-under-reset)
CONNECT_UNDER_RESET=1
shift
;;
--no-build)
NO_BUILD=1
shift
;;
--no-flash)
NO_FLASH=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
if [[ -z "$MODE" ]]; then
usage >&2
exit 1
fi
SECURE_ELF="target/thumbv8m.main-none-eabihf/${PROFILE}/secure"
NONSECURE_ELF="target/thumbv8m.main-none-eabihf/${PROFILE}/nonsecure"
if [[ "$MODE" == "server" ]]; then
if [[ "$NO_BUILD" -eq 0 ]]; then
if [[ "$PROFILE" == "release" ]]; then
./build.sh --release
else
./build.sh
fi
fi
if [[ "$NO_FLASH" -eq 0 ]]; then
flash_args=()
if [[ "$PROFILE" == "release" ]]; then
flash_args+=(--release)
fi
if [[ "$NO_BUILD" -eq 1 ]]; then
flash_args+=(--no-build)
fi
if [[ -n "$PROBE" ]]; then
flash_args+=(--probe "$PROBE")
fi
if [[ -n "$SPEED" ]]; then
flash_args+=(--speed "$SPEED")
fi
if [[ "$CONNECT_UNDER_RESET" -eq 1 ]]; then
flash_args+=(--connect-under-reset)
fi
./flash.sh "${flash_args[@]}"
fi
openocd_args=(
-f interface/stlink.cfg
-f target/stm32l5x.cfg
-c "transport select hla_swd"
-c "tcl_port disabled"
-c "telnet_port disabled"
)
if [[ -n "$PROBE" ]]; then
openocd_args+=(-c "hla_serial $(extract_serial "$PROBE")")
fi
if [[ -n "$SPEED" ]]; then
openocd_args+=(-c "adapter speed $SPEED")
fi
if [[ "$CONNECT_UNDER_RESET" -eq 1 ]]; then
openocd_args+=(-c "reset_config connect_assert_srst")
fi
echo "Starting openocd GDB server on :3333"
exec openocd \
"${openocd_args[@]}" \
-c "init" \
-c "reset halt"
fi
if ! command -v gdb-multiarch >/dev/null 2>&1; then
echo "gdb-multiarch not found on PATH" >&2
exit 1
fi
if [[ ! -f "$SECURE_ELF" ]]; then
echo "missing secure image: $SECURE_ELF" >&2
echo "run './build.sh' or './debug.sh server' first" >&2
exit 1
fi
if [[ ! -f "$NONSECURE_ELF" ]]; then
echo "missing non-secure image: $NONSECURE_ELF" >&2
echo "run './build.sh' or './debug.sh server' first" >&2
exit 1
fi
exec gdb-multiarch \
"$SECURE_ELF" \
-ex "target extended-remote :3333" \
-ex "add-symbol-file $NONSECURE_ELF 0x08040000" \
-ex "monitor reset halt"