26
26
#include "platform/mbed_interface.h"
27
27
#include "platform/mbed_power_mgmt.h"
28
28
#include "platform/mbed_stats.h"
29
+ #include "platform/source/TARGET_CORTEX_M/mbed_fault_handler.h"
30
+ #include "mbed_rtx.h"
29
31
#ifdef MBED_CONF_RTOS_PRESENT
30
32
#include "rtx_os.h"
31
33
#endif
38
40
#endif
39
41
#include <inttypes.h>
40
42
43
+ #ifndef MAX
44
+ #define MAX (a , b ) ((a) > (b) ? (a) : (b))
45
+ #endif
46
+
41
47
#ifndef NDEBUG
42
48
#define ERROR_REPORT (ctx , error_msg , error_filename , error_line ) print_error_report(ctx, error_msg, error_filename, error_line)
43
49
static void print_error_report (const mbed_error_ctx * ctx , const char * , const char * error_filename , int error_line );
@@ -132,6 +138,26 @@ WEAK MBED_NORETURN void error(const char *format, ...)
132
138
mbed_halt_system ();
133
139
}
134
140
141
+ static inline bool mbed_error_is_hw_fault (mbed_error_status_t error_status )
142
+ {
143
+ return (error_status == MBED_ERROR_MEMMANAGE_EXCEPTION ||
144
+ error_status == MBED_ERROR_BUSFAULT_EXCEPTION ||
145
+ error_status == MBED_ERROR_USAGEFAULT_EXCEPTION ||
146
+ error_status == MBED_ERROR_HARDFAULT_EXCEPTION );
147
+ }
148
+
149
+ static bool mbed_error_is_handler (const mbed_error_ctx * ctx )
150
+ {
151
+ bool is_handler = false;
152
+ if (ctx && mbed_error_is_hw_fault (ctx -> error_status )) {
153
+ mbed_fault_context_t * mfc = (mbed_fault_context_t * )ctx -> error_value ;
154
+ if (mfc && !(mfc -> EXC_RETURN & 0x8 )) {
155
+ is_handler = true;
156
+ }
157
+ }
158
+ return is_handler ;
159
+ }
160
+
135
161
//Set an error status with the error handling system
136
162
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 )
137
163
{
@@ -146,18 +172,29 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
146
172
147
173
//Clear the context capturing buffer
148
174
memset (& current_error_ctx , 0 , sizeof (mbed_error_ctx ));
175
+
149
176
//Capture error information
150
177
current_error_ctx .error_status = error_status ;
151
- current_error_ctx .error_address = (uint32_t )caller ;
152
178
current_error_ctx .error_value = error_value ;
179
+ mbed_fault_context_t * mfc = NULL ;
180
+ if (mbed_error_is_hw_fault (error_status )) {
181
+ mfc = (mbed_fault_context_t * )error_value ;
182
+ current_error_ctx .error_address = (uint32_t )mfc -> PC_reg ;
183
+ // Note that this SP_reg is the correct SP value of the fault. PSP and MSP are slightly different due to HardFault_Handler.
184
+ current_error_ctx .thread_current_sp = (uint32_t )mfc -> SP_reg ;
185
+ // Note that the RTX thread itself is the same even under this fault exception handler.
186
+ } else {
187
+ current_error_ctx .error_address = (uint32_t )caller ;
188
+ current_error_ctx .thread_current_sp = (uint32_t )& current_error_ctx ; // Address local variable to get a stack pointer
189
+ }
190
+
153
191
#ifdef MBED_CONF_RTOS_PRESENT
154
- //Capture thread info
192
+ // Capture thread info in thread mode
155
193
osRtxThread_t * current_thread = osRtxInfo .thread .run .curr ;
156
194
current_error_ctx .thread_id = (uint32_t )current_thread ;
157
195
current_error_ctx .thread_entry_address = (uint32_t )current_thread -> thread_addr ;
158
196
current_error_ctx .thread_stack_size = current_thread -> stack_size ;
159
197
current_error_ctx .thread_stack_mem = (uint32_t )current_thread -> stack_mem ;
160
- current_error_ctx .thread_current_sp = (uint32_t )& current_error_ctx ; // Address local variable to get a stack pointer
161
198
#endif //MBED_CONF_RTOS_PRESENT
162
199
163
200
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
@@ -440,16 +477,83 @@ mbed_error_status_t mbed_clear_all_errors(void)
440
477
return status ;
441
478
}
442
479
443
- static inline const char * name_or_unnamed (const char * name )
480
+ #ifdef MBED_CONF_RTOS_PRESENT
481
+ static inline const char * name_or_unnamed (const osRtxThread_t * thread )
482
+ {
483
+ const char * unnamed = "<unnamed>" ;
484
+
485
+ if (!thread ) {
486
+ return unnamed ;
487
+ }
488
+
489
+ const char * name = thread -> name ;
490
+ return name ? name : unnamed ;
491
+ }
492
+ #endif // MBED_CONF_RTOS_PRESENT
493
+
494
+ #if MBED_STACK_DUMP_ENABLED
495
+ /** Prints stack dump from given stack information.
496
+ * The arguments should be given in address raw value to check alignment.
497
+ * @param stack_start The address of stack start.
498
+ * @param stack_size The size of stack
499
+ * @param stack_sp The stack pointer currently at. */
500
+ static void print_stack_dump_core (uint32_t stack_start , uint32_t stack_size , uint32_t stack_sp , const char * postfix )
501
+ {
502
+ #if MBED_STACK_DUMP_ENABLED
503
+ #define STACK_DUMP_WIDTH 8
504
+ #define INT_ALIGN_MASK (~(sizeof(int) - 1))
505
+ mbed_error_printf ("\nStack Dump: %s" , postfix );
506
+ uint32_t st_end = (stack_start + stack_size ) & INT_ALIGN_MASK ;
507
+ uint32_t st = (stack_sp ) & INT_ALIGN_MASK ;
508
+ for (; st < st_end ; st += sizeof (int ) * STACK_DUMP_WIDTH ) {
509
+ mbed_error_printf ("\n0x%08" PRIX32 ":" , st );
510
+ for (int i = 0 ; i < STACK_DUMP_WIDTH ; i ++ ) {
511
+ uint32_t st_cur = st + i * sizeof (int );
512
+ if (st_cur >= st_end ) {
513
+ break ;
514
+ }
515
+ uint32_t st_val = * ((uint32_t * )st_cur );
516
+ mbed_error_printf ("0x%08" PRIX32 " " , st_val );
517
+ }
518
+ }
519
+ mbed_error_printf ("\n" );
520
+ #endif // MBED_STACK_DUMP_ENABLED
521
+ }
522
+
523
+ static void print_stack_dump (uint32_t stack_start , uint32_t stack_size , uint32_t stack_sp , const mbed_error_ctx * ctx )
444
524
{
445
- return name ? name : "<unnamed>" ;
525
+ if (ctx && mbed_error_is_handler (ctx )) {
526
+ // Stack dump extra for handler stack which may have accessed MSP.
527
+ mbed_fault_context_t * mfc = (mbed_fault_context_t * )ctx -> error_value ;
528
+ uint32_t msp_sp = mfc -> MSP ;
529
+ uint32_t psp_sp = mfc -> PSP ;
530
+ if (mfc && !(mfc -> EXC_RETURN & 0x4 )) {
531
+ // MSP mode. Then SP_reg is more correct.
532
+ msp_sp = mfc -> SP_reg ;
533
+ } else {
534
+ // PSP mode. Then SP_reg is more correct.
535
+ psp_sp = mfc -> SP_reg ;
536
+ }
537
+ uint32_t msp_size = MAX (0 , (int )INITIAL_SP - (int )msp_sp );
538
+ print_stack_dump_core (msp_sp , msp_size , msp_sp , "MSP" );
539
+
540
+ stack_sp = psp_sp ;
541
+ }
542
+
543
+ print_stack_dump_core (stack_start , stack_size , stack_sp , "PSP" );
446
544
}
545
+ #endif // MBED_STACK_DUMP_ENABLED
447
546
448
547
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT )
449
548
/* Prints info of a thread(using osRtxThread_t struct)*/
450
549
static void print_thread (const osRtxThread_t * thread )
451
550
{
452
- 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 -> name ), thread -> state , thread -> thread_addr , thread -> stack_size , (uint32_t )thread -> stack_mem , thread -> sp );
551
+ uint32_t stack_mem = (uint32_t )thread -> stack_mem ;
552
+ 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 );
553
+
554
+ #if MBED_STACK_DUMP_ENABLED
555
+ print_stack_dump (stack_mem , thread -> stack_size , thread -> sp , NULL );
556
+ #endif
453
557
}
454
558
455
559
/* Prints thread info from a list */
@@ -532,11 +636,16 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
532
636
533
637
mbed_error_printf ("\nError Value: 0x%" PRIX32 , ctx -> error_value );
534
638
#ifdef MBED_CONF_RTOS_PRESENT
535
- mbed_error_printf ("\nCurrent Thread: %s Id: 0x%" PRIX32 " Entry: 0x%" PRIX32 " StackSize: 0x%" PRIX32 " StackMem: 0x%" PRIX32 " SP: 0x%" PRIX32 " " ,
536
- name_or_unnamed (((osRtxThread_t * )ctx -> thread_id )-> name ),
639
+ bool is_handler = mbed_error_is_handler (ctx );
640
+ mbed_error_printf ("\nCurrent Thread: %s%s Id: 0x%" PRIX32 " Entry: 0x%" PRIX32 " StackSize: 0x%" PRIX32 " StackMem: 0x%" PRIX32 " SP: 0x%" PRIX32 " " ,
641
+ name_or_unnamed ((osRtxThread_t * )ctx -> thread_id ), is_handler ? " <handler>" : "" ,
537
642
ctx -> thread_id , ctx -> thread_entry_address , ctx -> thread_stack_size , ctx -> thread_stack_mem , ctx -> thread_current_sp );
538
643
#endif
539
644
645
+ #if MBED_STACK_DUMP_ENABLED
646
+ print_stack_dump (ctx -> thread_stack_mem , ctx -> thread_stack_size , ctx -> thread_current_sp , ctx );
647
+ #endif
648
+
540
649
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT )
541
650
mbed_error_printf ("\nNext:" );
542
651
print_thread (osRtxInfo .thread .run .next );
@@ -557,6 +666,7 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
557
666
mbed_stats_sys_get (& sys_stats );
558
667
mbed_error_printf ("\nFor more info, visit: https://mbed.com/s/error?error=0x%08X&osver=%" PRId32 "&core=0x%08" PRIX32 "&comp=%d&ver=%" PRIu32 "&tgt=" GET_TARGET_NAME (TARGET_NAME ), ctx -> error_status , sys_stats .os_version , sys_stats .cpu_id , sys_stats .compiler_id , sys_stats .compiler_version );
559
668
#endif
669
+
560
670
mbed_error_printf ("\n-- MbedOS Error Info --\n" );
561
671
}
562
672
#endif //ifndef NDEBUG
0 commit comments