@@ -134,6 +134,26 @@ WEAK MBED_NORETURN void error(const char *format, ...)
134
134
mbed_halt_system ();
135
135
}
136
136
137
+ static inline bool mbed_error_is_hw_fault (mbed_error_status_t error_status )
138
+ {
139
+ return (error_status == MBED_ERROR_MEMMANAGE_EXCEPTION ||
140
+ error_status == MBED_ERROR_BUSFAULT_EXCEPTION ||
141
+ error_status == MBED_ERROR_USAGEFAULT_EXCEPTION ||
142
+ error_status == MBED_ERROR_HARDFAULT_EXCEPTION );
143
+ }
144
+
145
+ static bool mbed_error_is_handler (const mbed_error_ctx * ctx )
146
+ {
147
+ bool is_handler = false;
148
+ if (ctx && mbed_error_is_hw_fault (ctx -> error_status )) {
149
+ mbed_fault_context_t * mfc = (mbed_fault_context_t * )ctx -> error_value ;
150
+ if (mfc && mfc -> EXC_RETURN & 0x8 ) {
151
+ is_handler = true;
152
+ }
153
+ }
154
+ return is_handler ;
155
+ }
156
+
137
157
//Set an error status with the error handling system
138
158
static mbed_error_status_t handle_error (mbed_error_status_t error_status , unsigned int error_value , const char * filename , int line_number , void * caller )
139
159
{
@@ -152,12 +172,8 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
152
172
//Capture error information
153
173
current_error_ctx .error_status = error_status ;
154
174
current_error_ctx .error_value = error_value ;
155
- bool bHWException = (error_status == MBED_ERROR_MEMMANAGE_EXCEPTION ||
156
- error_status == MBED_ERROR_BUSFAULT_EXCEPTION ||
157
- error_status == MBED_ERROR_USAGEFAULT_EXCEPTION ||
158
- error_status == MBED_ERROR_HARDFAULT_EXCEPTION );
159
175
mbed_fault_context_t * mfc = NULL ;
160
- if (bHWException ) {
176
+ if (mbed_error_is_hw_fault ( error_status ) ) {
161
177
mfc = (mbed_fault_context_t * )error_value ;
162
178
current_error_ctx .error_address = (uint32_t )mfc -> PC_reg ;
163
179
// Note that this SP_reg is the correct SP value of the fault. PSP and MSP are slightly different due to HardFault_Handler.
@@ -168,22 +184,14 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
168
184
current_error_ctx .thread_current_sp = (uint32_t )& current_error_ctx ; // Address local variable to get a stack pointer
169
185
}
170
186
171
- if (mfc && !(mfc -> EXC_RETURN & 0x4 )) {
172
- // handler mode
173
- current_error_ctx .thread_id = 0 ;
174
- current_error_ctx .thread_entry_address = 0 ;
175
- current_error_ctx .thread_stack_size = MAX (0 , (int )INITIAL_SP - (int )current_error_ctx .thread_current_sp - (int )sizeof (int ));
176
- current_error_ctx .thread_stack_mem = current_error_ctx .thread_current_sp ;
177
- } else {
178
187
#ifdef MBED_CONF_RTOS_PRESENT
179
- // Capture thread info in thread mode
180
- osRtxThread_t * current_thread = osRtxInfo .thread .run .curr ;
181
- current_error_ctx .thread_id = (uint32_t )current_thread ;
182
- current_error_ctx .thread_entry_address = (uint32_t )current_thread -> thread_addr ;
183
- current_error_ctx .thread_stack_size = current_thread -> stack_size ;
184
- current_error_ctx .thread_stack_mem = (uint32_t )current_thread -> stack_mem ;
188
+ // Capture thread info in thread mode
189
+ osRtxThread_t * current_thread = osRtxInfo .thread .run .curr ;
190
+ current_error_ctx .thread_id = (uint32_t )current_thread ;
191
+ current_error_ctx .thread_entry_address = (uint32_t )current_thread -> thread_addr ;
192
+ current_error_ctx .thread_stack_size = current_thread -> stack_size ;
193
+ current_error_ctx .thread_stack_mem = (uint32_t )current_thread -> stack_mem ;
185
194
#endif //MBED_CONF_RTOS_PRESENT
186
- }
187
195
188
196
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
189
197
//Capture filename/linenumber if provided
@@ -467,25 +475,27 @@ mbed_error_status_t mbed_clear_all_errors(void)
467
475
468
476
static inline const char * name_or_unnamed (const osRtxThread_t * thread )
469
477
{
478
+ const char * unnamed = "<unnamed>" ;
479
+
470
480
if (!thread ) {
471
- return "<handler>" ;
481
+ return unnamed ;
472
482
}
473
483
474
484
const char * name = thread -> name ;
475
- return name ? name : "< unnamed>" ;
485
+ return name ? name : unnamed ;
476
486
}
477
487
478
488
/** Prints stack dump from given stack information.
479
489
* The arguments should be given in address raw value to check alignment.
480
490
* @param stack_start The address of stack start.
481
491
* @param stack_size The size of stack
482
492
* @param stack_sp The stack pointer currently at. */
483
- static void print_stack_dump (uint32_t stack_start , uint32_t stack_size , uint32_t stack_sp )
493
+ static void print_stack_dump_core (uint32_t stack_start , uint32_t stack_size , uint32_t stack_sp , const char * postfix )
484
494
{
485
495
#if MBED_STACK_DUMP_ENABLED
486
496
#define STACK_DUMP_WIDTH 8
487
497
#define INT_ALIGN_MASK (~(sizeof(int) - 1))
488
- mbed_error_printf ("\n\nStack Dump:" );
498
+ mbed_error_printf ("\n\nStack Dump: %s" , postfix );
489
499
uint32_t st_end = (stack_start + stack_size ) & INT_ALIGN_MASK ;
490
500
uint32_t st = (stack_sp ) & INT_ALIGN_MASK ;
491
501
for (; st <= st_end ; st += sizeof (int ) * STACK_DUMP_WIDTH ) {
@@ -503,14 +513,35 @@ static void print_stack_dump(uint32_t stack_start, uint32_t stack_size, uint32_t
503
513
#endif // MBED_STACK_DUMP_ENABLED
504
514
}
505
515
516
+ static void print_stack_dump (uint32_t stack_start , uint32_t stack_size , uint32_t stack_sp , const mbed_error_ctx * ctx )
517
+ {
518
+ #if MBED_STACK_DUMP_ENABLED
519
+ if (ctx && mbed_error_is_handler (ctx )) {
520
+ // Stack dump extra for handler stack which may have accessed MSP.
521
+ mbed_fault_context_t * mfc = (mbed_fault_context_t * )ctx -> error_value ;
522
+ uint32_t msp_sp = mfc -> MSP ;
523
+ if (mfc && !(mfc -> EXC_RETURN & 0x4 )) {
524
+ // MSP mode. Then SP_reg is more correct.
525
+ msp_sp = mfc -> SP_reg ;
526
+ }
527
+ // Do not access beyond INITIAL_SP.
528
+ uint32_t msp_size = MAX (0 , (int )INITIAL_SP - (int )msp_sp - (int )sizeof (int ));
529
+ print_stack_dump_core (msp_sp , msp_size , msp_sp , "MSP" );
530
+ }
531
+
532
+ // Handler thread may have accessed PSP.
533
+ print_stack_dump_core (stack_start , stack_size , stack_sp , "PSP" );
534
+ #endif // MBED_STACK_DUMP_ENABLED
535
+ }
536
+
506
537
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT )
507
538
/* Prints info of a thread(using osRtxThread_t struct)*/
508
539
static void print_thread (const osRtxThread_t * thread )
509
540
{
510
541
uint32_t stack_mem = (uint32_t )thread -> stack_mem ;
511
542
mbed_error_printf ("\n%s State: 0x%" PRIX8 " Entry: 0x%08" PRIX32 " Stack Size: 0x%08" PRIX32 " Mem: 0x%08" PRIX32 " SP: 0x%08" PRIX32 , name_or_unnamed (thread ), thread -> state , thread -> thread_addr , thread -> stack_size , stack_mem , thread -> sp );
512
543
513
- print_stack_dump (stack_mem , thread -> stack_size , thread -> sp );
544
+ print_stack_dump (stack_mem , thread -> stack_size , thread -> sp , NULL );
514
545
}
515
546
516
547
/* Prints thread info from a list */
@@ -592,13 +623,14 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
592
623
}
593
624
594
625
mbed_error_printf ("\nError Value: 0x%" PRIX32 , ctx -> error_value );
626
+ bool is_handler = mbed_error_is_handler (ctx );
595
627
#ifdef MBED_CONF_RTOS_PRESENT
596
- mbed_error_printf ("\nCurrent Thread: %s Id: 0x%" PRIX32 " Entry: 0x%" PRIX32 " StackSize: 0x%" PRIX32 " StackMem: 0x%" PRIX32 " SP: 0x%" PRIX32 " " ,
597
- name_or_unnamed ((osRtxThread_t * )ctx -> thread_id ),
628
+ mbed_error_printf ("\nCurrent Thread: %s%s Id: 0x%" PRIX32 " Entry: 0x%" PRIX32 " StackSize: 0x%" PRIX32 " StackMem: 0x%" PRIX32 " SP: 0x%" PRIX32 " " ,
629
+ name_or_unnamed ((osRtxThread_t * )ctx -> thread_id ), is_handler ? " <handler>" : "" ,
598
630
ctx -> thread_id , ctx -> thread_entry_address , ctx -> thread_stack_size , ctx -> thread_stack_mem , ctx -> thread_current_sp );
599
631
#endif
600
632
601
- print_stack_dump (ctx -> thread_stack_mem , ctx -> thread_stack_size , ctx -> thread_current_sp );
633
+ print_stack_dump (ctx -> thread_stack_mem , ctx -> thread_stack_size , ctx -> thread_current_sp , ctx );
602
634
603
635
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT )
604
636
mbed_error_printf ("\nNext:" );
0 commit comments