|
@@ -0,0 +1,415 @@
|
|
1
|
+/*
|
|
2
|
+ * Libbacktrace
|
|
3
|
+ * Copyright 2015 Stephen Street <stephen@redrocketcomputing.com>
|
|
4
|
+ *
|
|
5
|
+ * This Source Code Form is subject to the terms of the Mozilla Public
|
|
6
|
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
7
|
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
8
|
+ *
|
|
9
|
+ * This library was modified and adapted to be used in Marlin 3D printer
|
|
10
|
+ * firmware as backtracer for exceptions for debugging purposes in 2018
|
|
11
|
+ * by Eduardo José Tagle.
|
|
12
|
+ */
|
|
13
|
+
|
|
14
|
+#ifdef ARDUINO_ARCH_SAM
|
|
15
|
+
|
|
16
|
+#include "backtrace.h"
|
|
17
|
+
|
|
18
|
+#include <stdint.h>
|
|
19
|
+#include <string.h>
|
|
20
|
+
|
|
21
|
+typedef struct unwind_control_block {
|
|
22
|
+ uint32_t vrs[16];
|
|
23
|
+ const uint32_t *current;
|
|
24
|
+ int remaining;
|
|
25
|
+ int byte;
|
|
26
|
+} unwind_control_block_t;
|
|
27
|
+
|
|
28
|
+typedef struct unwind_index {
|
|
29
|
+ uint32_t addr_offset;
|
|
30
|
+ uint32_t insn;
|
|
31
|
+} unwind_index_t;
|
|
32
|
+
|
|
33
|
+/* These symbols point to the unwind index and should be provide by the linker script */
|
|
34
|
+extern const unwind_index_t __exidx_start[];
|
|
35
|
+extern const unwind_index_t __exidx_end[];
|
|
36
|
+
|
|
37
|
+/* This prevents the linking of libgcc unwinder code */
|
|
38
|
+void __aeabi_unwind_cpp_pr0(void) {};
|
|
39
|
+void __aeabi_unwind_cpp_pr1(void) {};
|
|
40
|
+void __aeabi_unwind_cpp_pr2(void) {};
|
|
41
|
+
|
|
42
|
+/* These symbols point to the start and end of stack */
|
|
43
|
+extern const int _sstack;
|
|
44
|
+extern const int _estack;
|
|
45
|
+
|
|
46
|
+/* Validate stack pointer */
|
|
47
|
+static inline __attribute__((always_inline)) int validate_sp(const void* sp) {
|
|
48
|
+ if ((uint32_t)sp < (uint32_t)&_sstack || (uint32_t)sp > (uint32_t)&_estack)
|
|
49
|
+ return -1;
|
|
50
|
+ return 0;
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+static inline __attribute__((always_inline)) uint32_t prel31_to_addr(const uint32_t *prel31) {
|
|
54
|
+ int32_t offset = (((int32_t)(*prel31)) << 1) >> 1;
|
|
55
|
+ return ((uint32_t)prel31 + offset) & 0x7fffffff;
|
|
56
|
+}
|
|
57
|
+
|
|
58
|
+static const struct unwind_index *unwind_search_index(const unwind_index_t *start, const unwind_index_t *end, uint32_t ip) {
|
|
59
|
+ const struct unwind_index *middle;
|
|
60
|
+
|
|
61
|
+ /* Perform a binary search of the unwind index */
|
|
62
|
+ while (start < end - 1) {
|
|
63
|
+ middle = start + ((end - start + 1) >> 1);
|
|
64
|
+ if (ip < prel31_to_addr(&middle->addr_offset))
|
|
65
|
+ end = middle;
|
|
66
|
+ else
|
|
67
|
+ start = middle;
|
|
68
|
+ }
|
|
69
|
+ return start;
|
|
70
|
+}
|
|
71
|
+
|
|
72
|
+static const char *unwind_get_function_name(void *address) {
|
|
73
|
+ uint32_t flag_word = *(uint32_t *)(address - 4);
|
|
74
|
+ if ((flag_word & 0xff000000) == 0xff000000) {
|
|
75
|
+ return (const char *)(address - 4 - (flag_word & 0x00ffffff));
|
|
76
|
+ }
|
|
77
|
+ return "unknown";
|
|
78
|
+}
|
|
79
|
+
|
|
80
|
+static int unwind_get_next_byte(unwind_control_block_t *ucb) {
|
|
81
|
+ int instruction;
|
|
82
|
+
|
|
83
|
+ /* Are there more instructions */
|
|
84
|
+ if (ucb->remaining == 0)
|
|
85
|
+ return -1;
|
|
86
|
+
|
|
87
|
+ /* Extract the current instruction */
|
|
88
|
+ instruction = ((*ucb->current) >> (ucb->byte << 3)) & 0xff;
|
|
89
|
+
|
|
90
|
+ /* Move the next byte */
|
|
91
|
+ --ucb->byte;
|
|
92
|
+ if (ucb->byte < 0) {
|
|
93
|
+ ++ucb->current;
|
|
94
|
+ ucb->byte = 3;
|
|
95
|
+ }
|
|
96
|
+ --ucb->remaining;
|
|
97
|
+
|
|
98
|
+ return instruction;
|
|
99
|
+}
|
|
100
|
+
|
|
101
|
+static int unwind_control_block_init(unwind_control_block_t *ucb, const uint32_t *instructions, const backtrace_frame_t *frame) {
|
|
102
|
+ /* Initialize control block */
|
|
103
|
+ memset(ucb, 0, sizeof(unwind_control_block_t));
|
|
104
|
+ ucb->current = instructions;
|
|
105
|
+
|
|
106
|
+ /* Is the a short unwind description */
|
|
107
|
+ if ((*instructions & 0xff000000) == 0x80000000) {
|
|
108
|
+ ucb->remaining = 3;
|
|
109
|
+ ucb->byte = 2;
|
|
110
|
+ /* Is the a long unwind description */
|
|
111
|
+ } else if ((*instructions & 0xff000000) == 0x81000000) {
|
|
112
|
+ ucb->remaining = ((*instructions & 0x00ff0000) >> 14) + 2;
|
|
113
|
+ ucb->byte = 1;
|
|
114
|
+ } else
|
|
115
|
+ return -1;
|
|
116
|
+
|
|
117
|
+ /* Initialize the virtual register set */
|
|
118
|
+ if (frame) {
|
|
119
|
+ ucb->vrs[7] = frame->fp;
|
|
120
|
+ ucb->vrs[13] = frame->sp;
|
|
121
|
+ ucb->vrs[14] = frame->lr;
|
|
122
|
+ ucb->vrs[15] = 0;
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ /* All good */
|
|
126
|
+ return 0;
|
|
127
|
+}
|
|
128
|
+
|
|
129
|
+static int unwind_execute_instruction(unwind_control_block_t *ucb) {
|
|
130
|
+
|
|
131
|
+ int instruction;
|
|
132
|
+ uint32_t mask;
|
|
133
|
+ uint32_t reg;
|
|
134
|
+ uint32_t *vsp;
|
|
135
|
+
|
|
136
|
+ /* Consume all instruction byte */
|
|
137
|
+ while ((instruction = unwind_get_next_byte(ucb)) != -1) {
|
|
138
|
+
|
|
139
|
+ if ((instruction & 0xc0) == 0x00) {
|
|
140
|
+ /* vsp = vsp + (xxxxxx << 2) + 4 */
|
|
141
|
+ ucb->vrs[13] += ((instruction & 0x3f) << 2) + 4;
|
|
142
|
+
|
|
143
|
+ } else if ((instruction & 0xc0) == 0x40) {
|
|
144
|
+ /* vsp = vsp - (xxxxxx << 2) - 4 */
|
|
145
|
+ ucb->vrs[13] -= ((instruction & 0x3f) << 2) - 4;
|
|
146
|
+
|
|
147
|
+ } else if ((instruction & 0xf0) == 0x80) {
|
|
148
|
+ /* pop under mask {r15-r12},{r11-r4} or refuse to unwind */
|
|
149
|
+ instruction = instruction << 8 | unwind_get_next_byte(ucb);
|
|
150
|
+
|
|
151
|
+ /* Check for refuse to unwind */
|
|
152
|
+ if (instruction == 0x8000)
|
|
153
|
+ return 0;
|
|
154
|
+
|
|
155
|
+ /* Pop registers using mask */
|
|
156
|
+ vsp = (uint32_t *)ucb->vrs[13];
|
|
157
|
+ mask = instruction & 0xfff;
|
|
158
|
+
|
|
159
|
+ reg = 4;
|
|
160
|
+ while (mask != 0) {
|
|
161
|
+ if ((mask & 0x001) != 0) {
|
|
162
|
+ if (validate_sp(vsp))
|
|
163
|
+ return -1;
|
|
164
|
+ ucb->vrs[reg] = *vsp++;
|
|
165
|
+ }
|
|
166
|
+ mask = mask >> 1;
|
|
167
|
+ ++reg;
|
|
168
|
+ }
|
|
169
|
+
|
|
170
|
+ /* Patch up the vrs sp if it was in the mask */
|
|
171
|
+ if ((mask & (1 << (13 - 4))) != 0)
|
|
172
|
+ ucb->vrs[13] = (uint32_t)vsp;
|
|
173
|
+
|
|
174
|
+ } else if ((instruction & 0xf0) == 0x90 && instruction != 0x9d && instruction != 0x9f) {
|
|
175
|
+ /* vsp = r[nnnn] */
|
|
176
|
+ ucb->vrs[13] = ucb->vrs[instruction & 0x0f];
|
|
177
|
+
|
|
178
|
+ } else if ((instruction & 0xf0) == 0xa0) {
|
|
179
|
+ /* pop r4-r[4+nnn] or pop r4-r[4+nnn], r14*/
|
|
180
|
+ vsp = (uint32_t *)ucb->vrs[13];
|
|
181
|
+
|
|
182
|
+ for (reg = 4; reg <= (instruction & 0x07) + 4; ++reg) {
|
|
183
|
+ if (validate_sp(vsp))
|
|
184
|
+ return -1;
|
|
185
|
+ ucb->vrs[reg] = *vsp++;
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+ if (instruction & 0x80) {
|
|
189
|
+ if (validate_sp(vsp))
|
|
190
|
+ return -1;
|
|
191
|
+ ucb->vrs[14] = *vsp++;
|
|
192
|
+ }
|
|
193
|
+
|
|
194
|
+ ucb->vrs[13] = (uint32_t)vsp;
|
|
195
|
+
|
|
196
|
+ } else if (instruction == 0xb0) {
|
|
197
|
+ /* finished */
|
|
198
|
+ if (ucb->vrs[15] == 0)
|
|
199
|
+ ucb->vrs[15] = ucb->vrs[14];
|
|
200
|
+
|
|
201
|
+ /* All done unwinding */
|
|
202
|
+ return 0;
|
|
203
|
+
|
|
204
|
+ } else if (instruction == 0xb1) {
|
|
205
|
+ /* pop register under mask {r3,r2,r1,r0} */
|
|
206
|
+ vsp = (uint32_t *)ucb->vrs[13];
|
|
207
|
+ mask = unwind_get_next_byte(ucb);
|
|
208
|
+
|
|
209
|
+ reg = 0;
|
|
210
|
+ while (mask != 0) {
|
|
211
|
+ if ((mask & 0x01) != 0) {
|
|
212
|
+ if (validate_sp(vsp))
|
|
213
|
+ return -1;
|
|
214
|
+ ucb->vrs[reg] = *vsp++;
|
|
215
|
+ }
|
|
216
|
+ mask = mask >> 1;
|
|
217
|
+ ++reg;
|
|
218
|
+ }
|
|
219
|
+ ucb->vrs[13] = (uint32_t)vsp;
|
|
220
|
+
|
|
221
|
+ } else if (instruction == 0xb2) {
|
|
222
|
+ /* vps = vsp + 0x204 + (uleb128 << 2) */
|
|
223
|
+ ucb->vrs[13] += 0x204 + (unwind_get_next_byte(ucb) << 2);
|
|
224
|
+
|
|
225
|
+ } else if (instruction == 0xb3 || instruction == 0xc8 || instruction == 0xc9) {
|
|
226
|
+ /* pop VFP double-precision registers */
|
|
227
|
+ vsp = (uint32_t *)ucb->vrs[13];
|
|
228
|
+
|
|
229
|
+ /* D[ssss]-D[ssss+cccc] */
|
|
230
|
+ if (validate_sp(vsp))
|
|
231
|
+ return -1;
|
|
232
|
+ ucb->vrs[14] = *vsp++;
|
|
233
|
+
|
|
234
|
+ if (instruction == 0xc8) {
|
|
235
|
+ /* D[16+sssss]-D[16+ssss+cccc] */
|
|
236
|
+ ucb->vrs[14] |= 1 << 16;
|
|
237
|
+ }
|
|
238
|
+
|
|
239
|
+ if (instruction != 0xb3) {
|
|
240
|
+ /* D[sssss]-D[ssss+cccc] */
|
|
241
|
+ ucb->vrs[14] |= 1 << 17;
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ ucb->vrs[13] = (uint32_t)vsp;
|
|
245
|
+
|
|
246
|
+ } else if ((instruction & 0xf8) == 0xb8 || (instruction & 0xf8) == 0xd0) {
|
|
247
|
+ /* Pop VFP double precision registers D[8]-D[8+nnn] */
|
|
248
|
+ ucb->vrs[14] = 0x80 | (instruction & 0x07);
|
|
249
|
+
|
|
250
|
+ if ((instruction & 0xf8) == 0xd0) {
|
|
251
|
+ ucb->vrs[14] = 1 << 17;
|
|
252
|
+ }
|
|
253
|
+
|
|
254
|
+ } else
|
|
255
|
+ return -1;
|
|
256
|
+ }
|
|
257
|
+
|
|
258
|
+ return instruction != -1;
|
|
259
|
+}
|
|
260
|
+
|
|
261
|
+static inline __attribute__((always_inline)) uint32_t *read_psp(void) {
|
|
262
|
+ /* Read the current PSP and return its value as a pointer */
|
|
263
|
+ uint32_t psp;
|
|
264
|
+
|
|
265
|
+ __asm volatile (
|
|
266
|
+ " mrs %0, psp \n"
|
|
267
|
+ : "=r" (psp) : :
|
|
268
|
+ );
|
|
269
|
+
|
|
270
|
+ return (uint32_t*)psp;
|
|
271
|
+}
|
|
272
|
+
|
|
273
|
+static int unwind_frame(backtrace_frame_t *frame) {
|
|
274
|
+
|
|
275
|
+ unwind_control_block_t ucb;
|
|
276
|
+ const unwind_index_t *index;
|
|
277
|
+ const uint32_t *instructions;
|
|
278
|
+ int execution_result;
|
|
279
|
+
|
|
280
|
+ /* Search the unwind index for the matching unwind table */
|
|
281
|
+ index = unwind_search_index(__exidx_start, __exidx_end, frame->pc);
|
|
282
|
+ if (index == NULL)
|
|
283
|
+ return -1;
|
|
284
|
+
|
|
285
|
+ /* Make sure we can unwind this frame */
|
|
286
|
+ if (index->insn == 0x00000001)
|
|
287
|
+ return 0;
|
|
288
|
+
|
|
289
|
+ /* Get the pointer to the first unwind instruction */
|
|
290
|
+ if (index->insn & 0x80000000)
|
|
291
|
+ instructions = &index->insn;
|
|
292
|
+ else
|
|
293
|
+ instructions = (uint32_t *)prel31_to_addr(&index->insn);
|
|
294
|
+
|
|
295
|
+ /* Initialize the unwind control block */
|
|
296
|
+ if (unwind_control_block_init(&ucb, instructions, frame) < 0)
|
|
297
|
+ return -1;
|
|
298
|
+
|
|
299
|
+ /* Execute the unwind instructions TODO range check the stack pointer */
|
|
300
|
+ while ((execution_result = unwind_execute_instruction(&ucb)) > 0);
|
|
301
|
+ if (execution_result == -1)
|
|
302
|
+ return -1;
|
|
303
|
+
|
|
304
|
+ /* Set the virtual pc to the virtual lr if this is the first unwind */
|
|
305
|
+ if (ucb.vrs[15] == 0)
|
|
306
|
+ ucb.vrs[15] = ucb.vrs[14];
|
|
307
|
+
|
|
308
|
+ /* Check for exception return */
|
|
309
|
+ /* TODO Test with other ARM processors to verify this method. */
|
|
310
|
+ if ((ucb.vrs[15] & 0xf0000000) == 0xf0000000) {
|
|
311
|
+ /* According to the Cortex Programming Manual (p.44), the stack address is always 8-byte aligned (Cortex-M7).
|
|
312
|
+ Depending on where the exception came from (MSP or PSP), we need the right SP value to work with.
|
|
313
|
+
|
|
314
|
+ ucb.vrs[7] contains the right value, so take it and align it by 8 bytes, store it as the current
|
|
315
|
+ SP to work with (ucb.vrs[13]) which is then saved as the current (virtual) frame's SP.
|
|
316
|
+ */
|
|
317
|
+ uint32_t *stack;
|
|
318
|
+ ucb.vrs[13] = (ucb.vrs[7] & ~7);
|
|
319
|
+
|
|
320
|
+ /* If we need to start from the MSP, we need to go down X words to find the PC, where:
|
|
321
|
+ X=2 if it was a non-floating-point exception
|
|
322
|
+ X=20 if it was a floating-point (VFP) exception
|
|
323
|
+
|
|
324
|
+ If we need to start from the PSP, we need to go up exactly 6 words to find the PC.
|
|
325
|
+ See the ARMv7-M Architecture Reference Manual p.594 and Cortex-M7 Processor Programming Manual p.44/p.45 for details.
|
|
326
|
+ */
|
|
327
|
+ if ((ucb.vrs[15] & 0xc) == 0) {
|
|
328
|
+ /* Return to Handler Mode: MSP (0xffffff-1) */
|
|
329
|
+ stack = (uint32_t*)(ucb.vrs[13]);
|
|
330
|
+
|
|
331
|
+ /* The PC is always 2 words down from the MSP, if it was a non-floating-point exception */
|
|
332
|
+ stack -= 2;
|
|
333
|
+
|
|
334
|
+ /* If there was a VFP exception (0xffffffe1), the PC is located another 18 words down */
|
|
335
|
+ if ((ucb.vrs[15] & 0xf0) == 0xe0)
|
|
336
|
+ {
|
|
337
|
+ stack -= 18;
|
|
338
|
+ }
|
|
339
|
+ }
|
|
340
|
+ else {
|
|
341
|
+ /* Return to Thread Mode: PSP (0xffffff-d) */
|
|
342
|
+ stack = read_psp();
|
|
343
|
+
|
|
344
|
+ /* The PC is always 6 words up from the PSP */
|
|
345
|
+ stack += 6;
|
|
346
|
+ }
|
|
347
|
+
|
|
348
|
+ /* Store the PC */
|
|
349
|
+ ucb.vrs[15] = *stack--;
|
|
350
|
+
|
|
351
|
+ /* Store the LR */
|
|
352
|
+ ucb.vrs[14] = *stack--;
|
|
353
|
+ }
|
|
354
|
+
|
|
355
|
+ /* We are done if current frame pc is equal to the virtual pc, prevent infinite loop */
|
|
356
|
+ if (frame->pc == ucb.vrs[15])
|
|
357
|
+ return 0;
|
|
358
|
+
|
|
359
|
+ /* Update the frame */
|
|
360
|
+ frame->fp = ucb.vrs[7];
|
|
361
|
+ frame->sp = ucb.vrs[13];
|
|
362
|
+ frame->lr = ucb.vrs[14];
|
|
363
|
+ frame->pc = ucb.vrs[15];
|
|
364
|
+
|
|
365
|
+ /* All good */
|
|
366
|
+ return 1;
|
|
367
|
+}
|
|
368
|
+
|
|
369
|
+int backtrace_dump(backtrace_frame_t *frame, backtrace_dump_fn_t dump_entry, void* ctx )
|
|
370
|
+{
|
|
371
|
+ backtrace_t entry;
|
|
372
|
+ int count = 1;
|
|
373
|
+
|
|
374
|
+ /* Unwind all frames */
|
|
375
|
+ do {
|
|
376
|
+ if (frame->pc == 0) {
|
|
377
|
+ /* Reached __exidx_end. */
|
|
378
|
+ entry.name = "<reached end of unwind table>";
|
|
379
|
+ entry.address = 0;
|
|
380
|
+ entry.function = 0;
|
|
381
|
+ dump_entry(count, &entry, ctx);
|
|
382
|
+ break;
|
|
383
|
+ }
|
|
384
|
+
|
|
385
|
+ if (frame->pc == 0x00000001) {
|
|
386
|
+ /* Reached .cantunwind instruction. */
|
|
387
|
+ entry.name = "<reached .cantunwind>";
|
|
388
|
+ entry.address = 0;
|
|
389
|
+ entry.function = 0;
|
|
390
|
+ dump_entry(count, &entry, ctx);
|
|
391
|
+ break;
|
|
392
|
+ }
|
|
393
|
+
|
|
394
|
+ /* Find the unwind index of the current frame pc */
|
|
395
|
+ const unwind_index_t *index = unwind_search_index(__exidx_start, __exidx_end, frame->pc);
|
|
396
|
+
|
|
397
|
+ /* Clear last bit (Thumb indicator) */
|
|
398
|
+ frame->pc &= 0xfffffffeU;
|
|
399
|
+
|
|
400
|
+ /* Generate the backtrace information */
|
|
401
|
+ entry.address = (void *)frame->pc;
|
|
402
|
+ entry.function = (void *)prel31_to_addr(&index->addr_offset);
|
|
403
|
+ entry.name = unwind_get_function_name(entry.function);
|
|
404
|
+ dump_entry(count, &entry, ctx);
|
|
405
|
+
|
|
406
|
+ /* Next backtrace frame */
|
|
407
|
+ ++count;
|
|
408
|
+
|
|
409
|
+ } while (unwind_frame(frame) == 1);
|
|
410
|
+
|
|
411
|
+ /* All done */
|
|
412
|
+ return count;
|
|
413
|
+}
|
|
414
|
+
|
|
415
|
+#endif
|