Browse Source

Added detection of case when no unwind tables are available

etagle 6 years ago
parent
commit
c3b23974bd
3 changed files with 226 additions and 70 deletions
  1. 16
    1
      Marlin/src/HAL/HAL_DUE/DebugMonitor_Due.cpp
  2. 198
    69
      Marlin/src/HAL/HAL_DUE/backtrace/backtrace.c
  3. 12
    0
      platformio.ini

+ 16
- 1
Marlin/src/HAL/HAL_DUE/DebugMonitor_Due.cpp View File

@@ -114,7 +114,9 @@ static void TXDec(uint32_t v) {
114 114
 
115 115
 // Dump a backtrace entry
116 116
 static void backtrace_dump_fn(int idx, const backtrace_t* bte, void* ctx) {
117
-  TX('#'); TXDec(idx); TX(' '); TX(bte->name); TX(" @ ");TXHex((uint32_t)bte->address); TX('\n');
117
+  TX('#'); TXDec(idx); TX(' ');
118
+  TX(bte->name); TX('@');TXHex((uint32_t)bte->function); TX('+'); TXDec((uint32_t)bte->address - (uint32_t)bte->function);
119
+  TX(" PC:");TXHex((uint32_t)bte->address); TX('\n');
118 120
 }
119 121
 
120 122
 /**
@@ -176,6 +178,19 @@ void HardFault_HandlerC(unsigned long *hardfault_args, unsigned long cause) {
176 178
   btf.pc = ((unsigned long)hardfault_args[6]);
177 179
   backtrace_dump(&btf, backtrace_dump_fn, nullptr);
178 180
 
181
+  // Disable all NVIC interrupts
182
+  NVIC->ICER[0] = 0xFFFFFFFF;
183
+  NVIC->ICER[1] = 0xFFFFFFFF;
184
+
185
+  // Relocate VTOR table to default position
186
+  SCB->VTOR = 0;
187
+
188
+  // Disable USB
189
+  otg_disable();
190
+
191
+  // Restart watchdog
192
+  WDT_Restart(WDT);
193
+
179 194
   // Reset controller
180 195
   NVIC_SystemReset();
181 196
   while(1) { WDT_Restart(WDT); }

+ 198
- 69
Marlin/src/HAL/HAL_DUE/backtrace/backtrace.c View File

@@ -6,9 +6,9 @@
6 6
  * License, v. 2.0. If a copy of the MPL was not distributed with this
7 7
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 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.
9
+ * This library was modified, some bugs fixed, stack address validated
10
+ * and adapted to be used in Marlin 3D printer firmware as backtracer
11
+ * for exceptions for debugging purposes in 2018 by Eduardo José Tagle.
12 12
  */
13 13
 
14 14
 #ifdef ARDUINO_ARCH_SAM
@@ -43,10 +43,30 @@ void __aeabi_unwind_cpp_pr2(void) {};
43 43
 extern const int _sstack;
44 44
 extern const int _estack;
45 45
 
46
-/* Validate stack pointer */
46
+/* These symbols point to the start and end of the code section */
47
+extern const int _sfixed;
48
+extern const int _efixed;
49
+
50
+/* These symbols point to the start and end of initialized data (could be SRAM functions!) */
51
+extern const int _srelocate;
52
+extern const int _erelocate;
53
+
54
+/* Validate stack pointer (SP): It must be in the stack area */
47 55
 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;
56
+  // SP must point into the allocated stack area
57
+  if ((uint32_t)sp >= (uint32_t)&_sstack && (uint32_t)sp <= (uint32_t)&_estack)
58
+    return 0;
59
+  return -1;
60
+}
61
+
62
+/* Validate code pointer (PC): It must be either in TEXT or in SRAM */
63
+static inline __attribute__((always_inline)) int validate_pc(const void* pc) {
64
+  // PC must point into the text (CODE) area
65
+  if ((uint32_t)pc >= (uint32_t)&_sfixed && (uint32_t)pc <= (uint32_t)&_efixed)
66
+    return 0;
67
+  // Or into the SRAM function area
68
+  if ((uint32_t)pc >= (uint32_t)&_srelocate && (uint32_t)pc <= (uint32_t)&_erelocate)
69
+    return 0;
50 70
   return 0;
51 71
 }
52 72
 
@@ -103,11 +123,11 @@ static int unwind_control_block_init(unwind_control_block_t *ucb, const uint32_t
103 123
   memset(ucb, 0, sizeof(unwind_control_block_t));
104 124
   ucb->current = instructions;
105 125
 
106
-  /* Is the a short unwind description */
126
+  /* Is a short unwind description */
107 127
   if ((*instructions & 0xff000000) == 0x80000000) {
108 128
     ucb->remaining = 3;
109 129
     ucb->byte = 2;
110
-  /* Is the a long unwind description */
130
+  /* Is a long unwind description */
111 131
   } else if ((*instructions & 0xff000000) == 0x81000000) {
112 132
     ucb->remaining = ((*instructions & 0x00ff0000) >> 14) + 2;
113 133
     ucb->byte = 1;
@@ -115,12 +135,10 @@ static int unwind_control_block_init(unwind_control_block_t *ucb, const uint32_t
115 135
     return -1;
116 136
 
117 137
   /* 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
-  }
138
+  ucb->vrs[7] = frame->fp;
139
+  ucb->vrs[13] = frame->sp;
140
+  ucb->vrs[14] = frame->lr;
141
+  ucb->vrs[15] = 0;
124 142
 
125 143
   /* All good */
126 144
   return 0;
@@ -136,46 +154,49 @@ static int unwind_execute_instruction(unwind_control_block_t *ucb) {
136 154
   /* Consume all instruction byte */
137 155
   while ((instruction = unwind_get_next_byte(ucb)) != -1) {
138 156
 
139
-    if ((instruction & 0xc0) == 0x00) {
157
+    if ((instruction & 0xc0) == 0x00) { // ARM_EXIDX_CMD_DATA_POP
140 158
       /* vsp = vsp + (xxxxxx << 2) + 4 */
141 159
       ucb->vrs[13] += ((instruction & 0x3f) << 2) + 4;
142
-
143
-    } else if ((instruction & 0xc0) == 0x40) {
160
+    } else
161
+    if ((instruction & 0xc0) == 0x40) { // ARM_EXIDX_CMD_DATA_PUSH
144 162
       /* vsp = vsp - (xxxxxx << 2) - 4 */
145 163
       ucb->vrs[13] -= ((instruction & 0x3f) << 2) - 4;
146
-
147
-    } else if ((instruction & 0xf0) == 0x80) {
164
+    } else
165
+    if ((instruction & 0xf0) == 0x80) {
148 166
       /* pop under mask {r15-r12},{r11-r4} or refuse to unwind */
149 167
       instruction = instruction << 8 | unwind_get_next_byte(ucb);
150 168
 
151 169
       /* Check for refuse to unwind */
152
-      if (instruction == 0x8000)
170
+      if (instruction == 0x8000)        // ARM_EXIDX_CMD_REFUSED
153 171
         return 0;
154 172
 
155
-      /* Pop registers using mask */
173
+      /* Pop registers using mask */    // ARM_EXIDX_CMD_REG_POP
156 174
       vsp = (uint32_t *)ucb->vrs[13];
157 175
       mask = instruction & 0xfff;
158 176
 
159 177
       reg = 4;
160
-      while (mask != 0) {
161
-        if ((mask & 0x001) != 0) {
178
+      while (mask) {
179
+        if ((mask & 1) != 0) {
162 180
           if (validate_sp(vsp))
163 181
             return -1;
164 182
           ucb->vrs[reg] = *vsp++;
165 183
         }
166
-        mask = mask >> 1;
184
+        mask >>= 1;
167 185
         ++reg;
168 186
       }
169 187
 
170 188
       /* Patch up the vrs sp if it was in the mask */
171
-      if ((mask & (1 << (13 - 4))) != 0)
189
+      if ((instruction & (1 << (13 - 4))) != 0)
172 190
         ucb->vrs[13] = (uint32_t)vsp;
173 191
 
174
-    } else if ((instruction & 0xf0) == 0x90 && instruction != 0x9d && instruction != 0x9f) {
192
+    } else
193
+    if ((instruction & 0xf0) == 0x90 && // ARM_EXIDX_CMD_REG_TO_SP
194
+        instruction != 0x9d &&
195
+        instruction != 0x9f) {
175 196
       /* vsp = r[nnnn] */
176 197
       ucb->vrs[13] = ucb->vrs[instruction & 0x0f];
177
-
178
-    } else if ((instruction & 0xf0) == 0xa0) {
198
+    } else
199
+    if ((instruction & 0xf0) == 0xa0) { // ARM_EXIDX_CMD_REG_POP
179 200
       /* pop r4-r[4+nnn] or pop r4-r[4+nnn], r14*/
180 201
       vsp = (uint32_t *)ucb->vrs[13];
181 202
 
@@ -185,7 +206,7 @@ static int unwind_execute_instruction(unwind_control_block_t *ucb) {
185 206
         ucb->vrs[reg] = *vsp++;
186 207
       }
187 208
 
188
-      if (instruction & 0x80) {
209
+      if (instruction & 0x08) { // ARM_EXIDX_CMD_REG_POP
189 210
         if (validate_sp(vsp))
190 211
           return -1;
191 212
         ucb->vrs[14] = *vsp++;
@@ -193,7 +214,8 @@ static int unwind_execute_instruction(unwind_control_block_t *ucb) {
193 214
 
194 215
       ucb->vrs[13] = (uint32_t)vsp;
195 216
 
196
-    } else if (instruction == 0xb0) {
217
+    } else
218
+    if (instruction == 0xb0) { // ARM_EXIDX_CMD_FINISH
197 219
       /* finished */
198 220
       if (ucb->vrs[15] == 0)
199 221
         ucb->vrs[15] = ucb->vrs[14];
@@ -201,28 +223,34 @@ static int unwind_execute_instruction(unwind_control_block_t *ucb) {
201 223
       /* All done unwinding */
202 224
       return 0;
203 225
 
204
-    } else if (instruction == 0xb1) {
226
+    } else
227
+    if (instruction == 0xb1) { // ARM_EXIDX_CMD_REG_POP
205 228
       /* pop register under mask {r3,r2,r1,r0} */
206 229
       vsp = (uint32_t *)ucb->vrs[13];
207 230
       mask = unwind_get_next_byte(ucb);
208 231
 
209 232
       reg = 0;
210
-      while (mask != 0) {
211
-        if ((mask & 0x01) != 0) {
233
+      while (mask) {
234
+        if ((mask & 1) != 0) {
212 235
           if (validate_sp(vsp))
213 236
             return -1;
214 237
           ucb->vrs[reg] = *vsp++;
215 238
         }
216
-        mask = mask >> 1;
239
+        mask >>= 1;
217 240
         ++reg;
218 241
       }
219 242
       ucb->vrs[13] = (uint32_t)vsp;
220 243
 
221
-    } else if (instruction == 0xb2) {
244
+    } else
245
+    if (instruction == 0xb2) { // ARM_EXIDX_CMD_DATA_POP
222 246
       /* vps = vsp + 0x204 + (uleb128 << 2) */
223 247
       ucb->vrs[13] += 0x204 + (unwind_get_next_byte(ucb) << 2);
224 248
 
225
-    } else if (instruction == 0xb3 || instruction == 0xc8 || instruction == 0xc9) {
249
+    } else
250
+    if (instruction == 0xb3 || // ARM_EXIDX_CMD_VFP_POP
251
+      instruction == 0xc8 ||
252
+      instruction == 0xc9) {
253
+
226 254
       /* pop VFP double-precision registers */
227 255
       vsp = (uint32_t *)ucb->vrs[13];
228 256
 
@@ -243,7 +271,10 @@ static int unwind_execute_instruction(unwind_control_block_t *ucb) {
243 271
 
244 272
       ucb->vrs[13] = (uint32_t)vsp;
245 273
 
246
-    } else if ((instruction & 0xf8) == 0xb8 || (instruction & 0xf8) == 0xd0) {
274
+    } else
275
+    if ((instruction & 0xf8) == 0xb8 ||
276
+        (instruction & 0xf8) == 0xd0) {
277
+
247 278
       /* Pop VFP double precision registers D[8]-D[8+nnn] */
248 279
       ucb->vrs[14] = 0x80 | (instruction & 0x07);
249 280
 
@@ -296,7 +327,7 @@ static int unwind_frame(backtrace_frame_t *frame) {
296 327
   if (unwind_control_block_init(&ucb, instructions, frame) < 0)
297 328
     return -1;
298 329
 
299
-  /* Execute the unwind instructions TODO range check the stack pointer */
330
+  /* Execute the unwind instructions */
300 331
   while ((execution_result = unwind_execute_instruction(&ucb)) > 0);
301 332
   if (execution_result == -1)
302 333
     return -1;
@@ -332,8 +363,7 @@ static int unwind_frame(backtrace_frame_t *frame) {
332 363
       stack -= 2;
333 364
 
334 365
       /* If there was a VFP exception (0xffffffe1), the PC is located another 18 words down */
335
-      if ((ucb.vrs[15] & 0xf0) == 0xe0)
336
-      {
366
+      if ((ucb.vrs[15] & 0xf0) == 0xe0) {
337 367
         stack -= 18;
338 368
       }
339 369
     }
@@ -366,47 +396,146 @@ static int unwind_frame(backtrace_frame_t *frame) {
366 396
   return 1;
367 397
 }
368 398
 
399
+// Detect if function names are available
400
+static int __attribute__ ((noinline)) has_function_names(void) {
401
+  uint32_t flag_word = ((uint32_t*)&has_function_names)[-1];
402
+  return ((flag_word & 0xff000000) == 0xff000000) ? 1 : 0;
403
+}
404
+
405
+// Detect if unwind information is present or not
406
+static int has_unwind_info(void) {
407
+  return ((char*)(&__exidx_end) - (char*)(&__exidx_start)) > 16 ? 1 : 0; // 16 because there are default entries we can´t supress
408
+}
409
+
369 410
 int backtrace_dump(backtrace_frame_t *frame, backtrace_dump_fn_t dump_entry, void* ctx )
370 411
 {
371 412
   backtrace_t entry;
372 413
   int count = 1;
373 414
 
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
-    }
415
+  /* If there is no unwind information, perform a RAW try at it. Idea was taken from
416
+   * https://stackoverflow.com/questions/3398664/how-to-get-a-call-stack-backtrace-deeply-embedded-no-library-support
417
+   *
418
+   * And requires code to be compiled with the following flags:
419
+   * -mtpcs-frame -mtpcs-leaf-frame -fno-omit-frame-pointer
420
+   *  With these options, the Stack pointer is automatically
421
+   * pushed to the stack at the beginning of each function.
422
+   */
423
+  if (!has_unwind_info()) {
424
+
425
+    /*
426
+     *  We basically iterate through the current stack finding the
427
+     * following combination of values:
428
+     *  - <Frame Address>
429
+     *  - <Link Address>
430
+     * This combination will occur for each function in the call stack
431
+     */
432
+
433
+    uint32_t previous_frame_address = (uint32_t)frame->sp;
434
+    uint32_t* stack_pointer = (uint32_t*)frame->sp;
435
+
436
+    // loop following stack frames
437
+    while (1) {
438
+
439
+      // Validate stack address
440
+      if (validate_sp(stack_pointer))
441
+        break;
442
+
443
+      // Attempt to obtain next stack pointer
444
+      // The link address should come immediately after
445
+      const uint32_t possible_frame_address = *stack_pointer;
446
+      const uint32_t possible_link_address = *(stack_pointer+1);
447
+
448
+      // Next check that the frame addresss (i.e. stack pointer for the function)
449
+      // and Link address are within an acceptable range
450
+      if(possible_frame_address > previous_frame_address &&
451
+         validate_sp((const void *)possible_frame_address) == 0 &&
452
+        (possible_link_address & 1) != 0 && // in THUMB mode the address will be odd
453
+         validate_pc((const void *)possible_link_address) == 0) {
454
+
455
+        // We found two acceptable values.
456
+        entry.name = "unknown";
457
+        entry.address = (void*)possible_link_address;
458
+        entry.function = 0;
459
+
460
+        // If there are function names, try to solve name
461
+        if (has_function_names()) {
462
+          // Lets find the function name, if possible
463
+
464
+          // Align address to 4 bytes
465
+          uint32_t* pf = (uint32_t*) (((uint32_t)possible_link_address) & (-4));
466
+
467
+          // Scan backwards until we find the function name
468
+          while(validate_pc(pf-1) == 0) {
469
+
470
+            // Get name descriptor value
471
+            uint32_t v = pf[-1];
472
+
473
+            // Check if name descriptor is valid and name is terminated in 0.
474
+            if ((v & 0xffffff00) == 0xff000000 &&
475
+                (v & 0xff) > 1) {
476
+
477
+              // Assume the name was found!
478
+              entry.name = ((const char*)pf) - 4 - (v & 0xff);
479
+              entry.function = (void*)pf;
480
+              break;
481
+            }
482
+
483
+            // Go backwards to the previous word
484
+            --pf;
485
+          }
486
+        }
487
+        dump_entry(count, &entry, ctx);
488
+        ++count;
384 489
 
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;
490
+        // Update the book-keeping registers for the next search
491
+        previous_frame_address = possible_frame_address;
492
+        stack_pointer = (uint32_t*)(possible_frame_address + 4);
493
+
494
+      } else {
495
+        // Keep iterating through the stack until we find an acceptable combination
496
+        ++stack_pointer;
497
+      }
392 498
     }
393 499
 
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);
500
+  } else {
501
+
502
+    /* Otherwise, unwind information is present. Use it to unwind frames */
503
+    do {
504
+      if (frame->pc == 0) {
505
+        /* Reached __exidx_end. */
506
+        entry.name = "<reached end of unwind table>";
507
+        entry.address = 0;
508
+        entry.function = 0;
509
+        dump_entry(count, &entry, ctx);
510
+        break;
511
+      }
396 512
 
397
-    /* Clear last bit (Thumb indicator) */
398
-    frame->pc &= 0xfffffffeU;
513
+      if (frame->pc == 0x00000001) {
514
+        /* Reached .cantunwind instruction. */
515
+        entry.name = "<reached .cantunwind>";
516
+        entry.address = 0;
517
+        entry.function = 0;
518
+        dump_entry(count, &entry, ctx);
519
+        break;
520
+      }
399 521
 
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);
522
+      /* Find the unwind index of the current frame pc */
523
+      const unwind_index_t *index = unwind_search_index(__exidx_start, __exidx_end, frame->pc);
405 524
 
406
-    /* Next backtrace frame */
407
-    ++count;
525
+      /* Clear last bit (Thumb indicator) */
526
+      frame->pc &= 0xfffffffeU;
408 527
 
409
-  } while (unwind_frame(frame) == 1);
528
+      /* Generate the backtrace information */
529
+      entry.address = (void *)frame->pc;
530
+      entry.function = (void *)prel31_to_addr(&index->addr_offset);
531
+      entry.name = unwind_get_function_name(entry.function);
532
+      dump_entry(count, &entry, ctx);
533
+
534
+      /* Next backtrace frame */
535
+      ++count;
536
+
537
+    } while (unwind_frame(frame) == 1);
538
+  }
410 539
 
411 540
   /* All done */
412 541
   return count;

+ 12
- 0
platformio.ini View File

@@ -97,6 +97,18 @@ lib_deps     = ${common.lib_deps}
97 97
 lib_ignore   = c1921b4
98 98
 src_filter   = ${common.default_src_filter}
99 99
 monitor_baud = 250000
100
+[env:DUE_debug]
101
+# Used when WATCHDOG_RESET_MANUAL is enabled
102
+platform     = atmelsam
103
+framework    = arduino
104
+board        = due
105
+build_flags  = ${common.build_flags}
106
+  -funwind-tables 
107
+  -mpoke-function-name
108
+lib_deps     = ${common.lib_deps}
109
+lib_ignore   = c1921b4
110
+src_filter   = ${common.default_src_filter}
111
+monitor_baud = 250000
100 112
 
101 113
 #
102 114
 # NXP LPC1768 ARM Cortex-M3

Loading…
Cancel
Save