33
33
#define GET_CURRENT_SP (sp ) \
34
34
{ \
35
35
/*If in Handler mode we are always using MSP*/ \
36
- if ( __get_IPSR () != 0U ) { \
36
+ if ( __get_IPSR () != 0U ) { \
37
37
sp = __get_MSP (); \
38
38
} else { \
39
39
/*Look into CONTROL.SPSEL value*/ \
@@ -95,7 +95,7 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
95
95
mbed_error_ctx current_error_ctx ;
96
96
97
97
//Error status should always be < 0
98
- if (error_status >= 0 ) {
98
+ if (error_status >= 0 ) {
99
99
//This is a weird situation, someone called mbed_error with invalid error code.
100
100
//We will still handle the situation but change the error code to ERROR_INVALID_ARGUMENT, atleast the context will have info on who called it
101
101
error_status = MBED_ERROR_INVALID_ARGUMENT ;
@@ -132,29 +132,29 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
132
132
133
133
#endif //MBED_CONF_RTOS_PRESENT
134
134
135
- #ifdef MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED
135
+ #ifdef MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
136
136
//Capture filename/linenumber if provided
137
137
//Index for tracking error_filename
138
- memset (& current_error_ctx .error_filename , 0 , MBED_CONF_MAX_ERROR_FILENAME_LEN );
139
- strncpy (current_error_ctx .error_filename , filename , MBED_CONF_MAX_ERROR_FILENAME_LEN );
138
+ memset (& current_error_ctx .error_filename , 0 , MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN );
139
+ strncpy (current_error_ctx .error_filename , filename , MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN );
140
140
current_error_ctx .error_line_number = line_number ;
141
141
#endif
142
142
143
143
//Capture the fist system error and store it
144
- if (error_count == 1 ) { //first error
144
+ if (error_count == 1 ) { //first error
145
145
memcpy (& first_error_ctx , & current_error_ctx , sizeof (mbed_error_ctx ));
146
146
}
147
147
148
148
//copy this error to last error
149
149
memcpy (& last_error_ctx , & current_error_ctx , sizeof (mbed_error_ctx ));
150
150
151
- #ifndef MBED_CONF_ERROR_HIST_DISABLED
151
+ #if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED
152
152
//Log the error with error log
153
153
mbed_error_hist_put (& current_error_ctx );
154
154
#endif
155
155
156
156
//Call the error hook if available
157
- if (error_hook != NULL ) {
157
+ if (error_hook != NULL ) {
158
158
error_hook (& last_error_ctx );
159
159
}
160
160
@@ -190,11 +190,11 @@ mbed_error_status_t mbed_warning(mbed_error_status_t error_status, const char *e
190
190
return handle_error (error_status , error_value , filename , line_number );
191
191
}
192
192
193
- //Sets a fatal error
193
+ //Sets a fatal error, this function is marked WEAK to be able to override this for some tests
194
194
WEAK mbed_error_status_t mbed_error (mbed_error_status_t error_status , const char * error_msg , unsigned int error_value , const char * filename , int line_number )
195
195
{
196
196
//set the error reported and then halt the system
197
- if ( MBED_SUCCESS != handle_error (error_status , error_value , filename , line_number ) )
197
+ if ( MBED_SUCCESS != handle_error (error_status , error_value , filename , line_number ))
198
198
return MBED_ERROR_FAILED_OPERATION ;
199
199
200
200
//On fatal errors print the error context/report
@@ -208,7 +208,7 @@ WEAK mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char
208
208
mbed_error_status_t mbed_set_error_hook (mbed_error_hook_t error_hook_in )
209
209
{
210
210
//register the new hook/callback
211
- if ( error_hook_in != NULL ) {
211
+ if ( error_hook_in != NULL ) {
212
212
error_hook = error_hook_in ;
213
213
return MBED_SUCCESS ;
214
214
}
@@ -236,17 +236,17 @@ mbed_error_status_t mbed_make_error(mbed_error_type_t error_type, mbed_module_ty
236
236
switch (error_type )
237
237
{
238
238
case MBED_ERROR_TYPE_POSIX :
239
- if (error_code >= MBED_POSIX_ERROR_BASE && error_code <= MBED_SYSTEM_ERROR_BASE )
239
+ if (error_code >= MBED_POSIX_ERROR_BASE && error_code <= MBED_SYSTEM_ERROR_BASE )
240
240
return - error_code ;
241
241
break ;
242
242
243
243
case MBED_ERROR_TYPE_SYSTEM :
244
- if (error_code >= MBED_SYSTEM_ERROR_BASE && error_code <= MBED_CUSTOM_ERROR_BASE )
244
+ if (error_code >= MBED_SYSTEM_ERROR_BASE && error_code <= MBED_CUSTOM_ERROR_BASE )
245
245
return MAKE_MBED_ERROR (MBED_ERROR_TYPE_SYSTEM , entity , error_code );
246
246
break ;
247
247
248
248
case MBED_ERROR_TYPE_CUSTOM :
249
- if (error_code >= MBED_CUSTOM_ERROR_BASE )
249
+ if (error_code >= MBED_CUSTOM_ERROR_BASE )
250
250
return MAKE_MBED_ERROR (MBED_ERROR_TYPE_CUSTOM , entity , error_code );
251
251
break ;
252
252
@@ -273,15 +273,117 @@ mbed_error_status_t mbed_clear_all_errors(void)
273
273
memset (& last_error_ctx , sizeof (mbed_error_ctx ), 0 );
274
274
//reset error count to 0
275
275
error_count = 0 ;
276
- #ifndef MBED_CONF_ERROR_HIST_DISABLED
276
+ #if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED
277
277
status = mbed_error_hist_reset ();
278
278
#endif
279
279
core_util_critical_section_exit ();
280
280
281
281
return status ;
282
282
}
283
283
284
- #ifndef MBED_CONF_ERROR_HIST_DISABLED
284
+ #if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT )
285
+ /* Prints info of a thread(using osRtxThread_t struct)*/
286
+ static void print_thread (osRtxThread_t * thread )
287
+ {
288
+ mbed_error_printf ("\nState: 0x%08X Entry: 0x%08X Stack Size: 0x%08X Mem: 0x%08X SP: 0x%08X" , thread -> state , thread -> thread_addr , thread -> stack_size , (uint32_t )thread -> stack_mem , thread -> sp );
289
+ }
290
+
291
+ /* Prints thread info from a list */
292
+ static void print_threads_info (osRtxThread_t * threads )
293
+ {
294
+ while (threads != NULL ) {
295
+ print_thread ( threads );
296
+ threads = threads -> thread_next ;
297
+ }
298
+ }
299
+ #endif
300
+
301
+ static void print_error_report (mbed_error_ctx * ctx , const char * error_msg )
302
+ {
303
+ uint32_t error_code = MBED_GET_ERROR_CODE (ctx -> error_status );
304
+ uint32_t error_module = MBED_GET_ERROR_MODULE (ctx -> error_status );
305
+
306
+ mbed_error_printf ("\n\n++ MbedOS Error Info ++\nError Status: 0x%X Code: %d Module: %d\nError Message: " , ctx -> error_status , error_code , error_module );
307
+
308
+ switch (error_code ) {
309
+ //These are errors reported by kernel handled from mbed_rtx_handlers
310
+ case MBED_ERROR_CODE_RTOS_EVENT :
311
+ mbed_error_printf ("Kernel Error: 0x%X, " , ctx -> error_value );
312
+ break ;
313
+
314
+ case MBED_ERROR_CODE_RTOS_THREAD_EVENT :
315
+ mbed_error_printf ("Thread: 0x%X, " , ctx -> error_value );
316
+ break ;
317
+
318
+ case MBED_ERROR_CODE_RTOS_MUTEX_EVENT :
319
+ mbed_error_printf ("Mutex: 0x%X, " , ctx -> error_value );
320
+ break ;
321
+
322
+ case MBED_ERROR_CODE_RTOS_SEMAPHORE_EVENT :
323
+ mbed_error_printf ("Semaphore: 0x%X, " , ctx -> error_value );
324
+ break ;
325
+
326
+ case MBED_ERROR_CODE_RTOS_MEMORY_POOL_EVENT :
327
+ mbed_error_printf ("MemoryPool: 0x%X, " , ctx -> error_value );
328
+ break ;
329
+
330
+ case MBED_ERROR_CODE_RTOS_EVENT_FLAGS_EVENT :
331
+ mbed_error_printf ("EventFlags: 0x%X, " , ctx -> error_value );
332
+ break ;
333
+
334
+ case MBED_ERROR_CODE_RTOS_TIMER_EVENT :
335
+ mbed_error_printf ("Timer: 0x%X, " , ctx -> error_value );
336
+ break ;
337
+
338
+ case MBED_ERROR_CODE_RTOS_MESSAGE_QUEUE_EVENT :
339
+ mbed_error_printf ("MessageQueue: 0x%X, " , ctx -> error_value );
340
+ break ;
341
+
342
+ default :
343
+ //Nothing to do here, just print the error info down
344
+ break ;
345
+ }
346
+ mbed_error_printf (error_msg );
347
+ mbed_error_printf ("\nLocation: 0x%X" , ctx -> error_address );
348
+
349
+ #if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED && !defined(NDEBUG )
350
+ if ((NULL != ctx -> error_filename [0 ]) && (ctx -> error_line_number != 0 )) {
351
+ //for string, we must pass address of a ptr which has the address of the string
352
+ mbed_error_printf ("\nFile:%s+%d" , ctx -> error_filename , ctx -> error_line_number );
353
+ }
354
+ #endif
355
+
356
+ mbed_error_printf ("\nError Value: 0x%X" , ctx -> error_value );
357
+ #ifdef TARGET_CORTEX_M
358
+ mbed_error_printf ("\nCurrent Thread: Id: 0x%X Entry: 0x%X StackSize: 0x%X StackMem: 0x%X SP: 0x%X " ,
359
+ ctx -> thread_id , ctx -> thread_entry_address , ctx -> thread_stack_size , ctx -> thread_stack_mem , ctx -> thread_current_sp );
360
+ #else
361
+ //For Cortex-A targets we dont have support to capture the current SP
362
+ mbed_error_printf ("\nCurrent Thread: Id: 0x%X Entry: 0x%X StackSize: 0x%X StackMem: 0x%X " ,
363
+ ctx -> thread_id , ctx -> thread_entry_address , ctx -> thread_stack_size , ctx -> thread_stack_mem );
364
+ #endif //TARGET_CORTEX_M
365
+
366
+ #if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT )
367
+ mbed_error_printf ("\nNext:" );
368
+ print_thread (osRtxInfo .thread .run .next );
369
+
370
+ mbed_error_printf ("\nWait:" );
371
+ osRtxThread_t * threads = (osRtxThread_t * )& osRtxInfo .thread .wait_list ;
372
+ print_threads_info (threads );
373
+
374
+ mbed_error_printf ("\nDelay:" );
375
+ threads = (osRtxThread_t * )& osRtxInfo .thread .delay_list ;
376
+ print_threads_info (threads );
377
+
378
+ mbed_error_printf ("\nIdle:" );
379
+ threads = (osRtxThread_t * )& osRtxInfo .thread .idle ;
380
+ print_threads_info (threads );
381
+ #endif
382
+
383
+ mbed_error_printf ("\n-- MbedOS Error Info --\n" );
384
+ }
385
+
386
+ #if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED
285
387
//Retrieve the error context from error log at the specified index
286
388
mbed_error_status_t mbed_get_error_hist_info (int index , mbed_error_ctx * error_info )
287
389
{
@@ -302,19 +404,19 @@ mbed_error_status_t mbed_save_error_hist(const char *path)
302
404
FILE * error_log_file = NULL ;
303
405
304
406
//Ensure path is valid
305
- if (path == NULL ) {
407
+ if (path == NULL ) {
306
408
ret = MBED_MAKE_ERROR (MBED_MODULE_PLATFORM , MBED_ERROR_CODE_INVALID_ARGUMENT );
307
409
goto exit ;
308
410
}
309
411
310
412
//Open the file for saving the error log info
311
- if ((error_log_file = fopen ( path , "w" ) ) == NULL ){
413
+ if ((error_log_file = fopen ( path , "w" )) == NULL ){
312
414
ret = MBED_MAKE_ERROR (MBED_MODULE_PLATFORM , MBED_ERROR_CODE_OPEN_FAILED );
313
415
goto exit ;
314
416
}
315
417
316
418
//First store the first and last errors
317
- if (fprintf (error_log_file , "\nFirst Error: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n" ,
419
+ if (fprintf (error_log_file , "\nFirst Error: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n" ,
318
420
(unsigned int )first_error_ctx .error_status ,
319
421
(unsigned int )first_error_ctx .thread_id ,
320
422
(unsigned int )first_error_ctx .error_address ,
@@ -323,7 +425,7 @@ mbed_error_status_t mbed_save_error_hist(const char *path)
323
425
goto exit ;
324
426
}
325
427
326
- if (fprintf (error_log_file , "\nLast Error: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n" ,
428
+ if (fprintf (error_log_file , "\nLast Error: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n" ,
327
429
(unsigned int )last_error_ctx .error_status ,
328
430
(unsigned int )last_error_ctx .thread_id ,
329
431
(unsigned int )last_error_ctx .error_address ,
@@ -333,10 +435,10 @@ mbed_error_status_t mbed_save_error_hist(const char *path)
333
435
}
334
436
335
437
//Update with error log info
336
- while (-- log_count >= 0 ) {
438
+ while (-- log_count >= 0 ) {
337
439
mbed_error_hist_get (log_count , & ctx );
338
440
//first line of file will be error log count
339
- if (fprintf (error_log_file , "\n%d: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n" ,
441
+ if (fprintf (error_log_file , "\n%d: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n" ,
340
442
log_count ,
341
443
(unsigned int )ctx .error_status ,
342
444
(unsigned int )ctx .thread_id ,
@@ -352,83 +454,5 @@ mbed_error_status_t mbed_save_error_hist(const char *path)
352
454
353
455
return ret ;
354
456
}
355
-
356
- static void print_error_report (mbed_error_ctx * ctx , const char * error_msg )
357
- {
358
- uint32_t error_code = MBED_GET_ERROR_CODE (ctx -> error_status );
359
- uint32_t error_module = MBED_GET_ERROR_MODULE (ctx -> error_status );
360
-
361
- mbed_error_printf ("\n\n++ MbedOS Error Info ++\nError Status: 0x%x Code: %d Module: %d\nError Message: " , ctx -> error_status , error_code , error_module );
362
-
363
- //Report error info based on error code, some errors require different
364
- //error_vals[1] contains the error code
365
- if (error_code == MBED_ERROR_CODE_HARDFAULT_EXCEPTION ||
366
- error_code == MBED_ERROR_CODE_MEMMANAGE_EXCEPTION ||
367
- error_code == MBED_ERROR_CODE_BUSFAULT_EXCEPTION ||
368
- error_code == MBED_ERROR_CODE_USAGEFAULT_EXCEPTION ) {
369
- mbed_error_printf (error_msg );
370
- mbed_error_printf ("\nLocation: 0x%x\n" , ctx -> error_value );
371
- } else {
372
- switch (error_code ) {
373
- //These are errors reported by kernel handled from mbed_rtx_handlers
374
- case MBED_ERROR_CODE_RTOS_EVENT :
375
- mbed_error_printf ("Kernel Error: 0x%x, " , ctx -> error_value );
376
- break ;
377
-
378
- case MBED_ERROR_CODE_RTOS_THREAD_EVENT :
379
- mbed_error_printf ("Thread: 0x%x, " , ctx -> error_value );
380
- break ;
381
-
382
- case MBED_ERROR_CODE_RTOS_MUTEX_EVENT :
383
- mbed_error_printf ("Mutex: 0x%x, " , ctx -> error_value );
384
- break ;
385
-
386
- case MBED_ERROR_CODE_RTOS_SEMAPHORE_EVENT :
387
- mbed_error_printf ("Semaphore: 0x%x, " , ctx -> error_value );
388
- break ;
389
-
390
- case MBED_ERROR_CODE_RTOS_MEMORY_POOL_EVENT :
391
- mbed_error_printf ("MemoryPool: 0x%x, " , ctx -> error_value );
392
- break ;
393
-
394
- case MBED_ERROR_CODE_RTOS_EVENT_FLAGS_EVENT :
395
- mbed_error_printf ("EventFlags: 0x%x, " , ctx -> error_value );
396
- break ;
397
-
398
- case MBED_ERROR_CODE_RTOS_TIMER_EVENT :
399
- mbed_error_printf ("Timer: 0x%x, " , ctx -> error_value );
400
- break ;
401
-
402
- case MBED_ERROR_CODE_RTOS_MESSAGE_QUEUE_EVENT :
403
- mbed_error_printf ("MessageQueue: 0x%x, " , ctx -> error_value );
404
- break ;
405
-
406
- default :
407
- //Nothing to do here, just print the error info down
408
- break ;
409
- }
410
- mbed_error_printf (error_msg , NULL );
411
- mbed_error_printf ("\nLocation: 0x%x" , ctx -> error_address );
412
- #if defined(MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED ) && !defined(NDEBUG )
413
- if (NULL != ctx -> error_filename ) {
414
- //for string, we must pass address of a ptr which has the address of the string
415
- mbed_error_printf ("\nFile:%s+%d" , ctx -> error_filename , ctx -> error_line_number );
416
- }
417
- #endif
418
-
419
- #ifdef TARGET_CORTEX_M
420
- mbed_error_printf ("\nError Value: 0x%x\nCurrent Thread: Id: 0x%x Entry: 0x%x StackSize: 0x%x StackMem: 0x%x SP: 0x%x " ,
421
- ctx -> error_value , ctx -> thread_id , ctx -> thread_entry_address , ctx -> thread_stack_size , ctx -> thread_stack_mem , ctx -> thread_current_sp );
422
- #else
423
- //For Cortex-A targets we dont have support to capture the current SP
424
- mbed_error_printf ("\nError Value: 0x%x\nCurrent Thread: Id: 0x%x Entry: 0x%x StackSize: 0x%x StackMem: 0x%x " ,
425
- ctx -> error_value , ctx -> thread_id , ctx -> thread_entry_address , ctx -> thread_stack_size , ctx -> thread_stack_mem );
426
- #endif //TARGET_CORTEX_M
427
- }
428
-
429
- mbed_error_printf ("\n-- MbedOS Error Info --\n" );
430
- }
431
-
432
-
433
457
#endif
434
458
0 commit comments