Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
mbed_error.c
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2019 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #include <stdlib.h> 00018 #include <stdarg.h> 00019 #include <string.h> 00020 #include "device.h" 00021 #include "platform/source/mbed_crash_data_offsets.h" 00022 #include "platform/mbed_atomic.h" 00023 #include "platform/mbed_critical.h" 00024 #include "platform/mbed_error.h" 00025 #include "platform/source/mbed_error_hist.h" 00026 #include "platform/mbed_interface.h" 00027 #include "platform/mbed_power_mgmt.h" 00028 #include "platform/mbed_stats.h" 00029 #include "platform/source/TARGET_CORTEX_M/mbed_fault_handler.h" 00030 #include "drivers/MbedCRC.h" 00031 #include "mbed_rtx.h" 00032 #ifdef MBED_CONF_RTOS_PRESENT 00033 #include "rtx_os.h" 00034 #endif 00035 00036 #if DEVICE_STDIO_MESSAGES 00037 #include <stdio.h> 00038 #endif 00039 #ifndef __STDC_FORMAT_MACROS 00040 #define __STDC_FORMAT_MACROS 00041 #endif 00042 #include <inttypes.h> 00043 00044 #ifndef MAX 00045 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 00046 #endif 00047 00048 #ifndef NDEBUG 00049 #define ERROR_REPORT(ctx, error_msg, error_filename, error_line) print_error_report(ctx, error_msg, error_filename, error_line) 00050 static void print_error_report(const mbed_error_ctx *ctx, const char *, const char *error_filename, int error_line); 00051 #else 00052 #define ERROR_REPORT(ctx, error_msg, error_filename, error_line) ((void) 0) 00053 #endif 00054 00055 bool mbed_error_in_progress; 00056 static core_util_atomic_flag halt_in_progress = CORE_UTIL_ATOMIC_FLAG_INIT; 00057 static int error_count = 0; 00058 static mbed_error_ctx first_error_ctx = {0}; 00059 00060 static mbed_error_ctx last_error_ctx = {0}; 00061 static mbed_error_hook_t error_hook = NULL; 00062 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); 00063 00064 #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED 00065 //Global for populating the context in exception handler 00066 static mbed_error_ctx *const report_error_ctx = (mbed_error_ctx *)(ERROR_CONTEXT_LOCATION); 00067 static bool is_reboot_error_valid = false; 00068 #endif 00069 00070 //Helper function to halt the system 00071 static MBED_NORETURN void mbed_halt_system(void) 00072 { 00073 // Prevent recursion if halt is called again during halt attempt - try 00074 // something simple instead. 00075 if (core_util_atomic_flag_test_and_set(&halt_in_progress)) { 00076 core_util_critical_section_enter(); 00077 __DSB(); 00078 for (;;) { 00079 __WFE(); // Not WFI, as don't want to wake for pending interrupts 00080 } 00081 } 00082 00083 //If in ISR context, call mbed_die directly 00084 if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) { 00085 mbed_die(); 00086 } 00087 00088 // In normal context, try orderly exit(1), which eventually calls mbed_die 00089 exit(1); 00090 } 00091 00092 WEAK MBED_NORETURN void error(const char *format, ...) 00093 { 00094 // Prevent recursion if error is called again during store+print attempt 00095 if (!core_util_atomic_exchange_bool (&mbed_error_in_progress, true)) { 00096 handle_error(MBED_ERROR_UNKNOWN, 0, NULL, 0, MBED_CALLER_ADDR()); 00097 ERROR_REPORT(&last_error_ctx, "Fatal Run-time error", NULL, 0); 00098 00099 #ifndef NDEBUG 00100 va_list arg; 00101 va_start(arg, format); 00102 mbed_error_vprintf(format, arg); 00103 va_end(arg); 00104 // Add a newline to prevent any line buffering 00105 mbed_error_puts("\n"); 00106 #endif 00107 } 00108 00109 mbed_halt_system(); 00110 } 00111 00112 static inline bool mbed_error_is_hw_fault(mbed_error_status_t error_status) 00113 { 00114 return (error_status == MBED_ERROR_MEMMANAGE_EXCEPTION || 00115 error_status == MBED_ERROR_BUSFAULT_EXCEPTION || 00116 error_status == MBED_ERROR_USAGEFAULT_EXCEPTION || 00117 error_status == MBED_ERROR_HARDFAULT_EXCEPTION); 00118 } 00119 00120 static bool mbed_error_is_handler(const mbed_error_ctx *ctx) 00121 { 00122 bool is_handler = false; 00123 if (ctx && mbed_error_is_hw_fault(ctx->error_status)) { 00124 mbed_fault_context_t *mfc = (mbed_fault_context_t *)ctx->error_value; 00125 if (mfc && !(mfc->EXC_RETURN & 0x8)) { 00126 is_handler = true; 00127 } 00128 } 00129 return is_handler; 00130 } 00131 00132 //Set an error status with the error handling system 00133 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) 00134 { 00135 mbed_error_ctx current_error_ctx; 00136 00137 //Error status should always be < 0 00138 if (error_status >= 0) { 00139 //This is a weird situation, someone called mbed_error with an invalid error code. 00140 //We will still handle the situation but change the error code to ERROR_INVALID_ARGUMENT, at least the context will have info on who called it 00141 error_status = MBED_ERROR_INVALID_ARGUMENT; 00142 } 00143 00144 //Clear the context capturing buffer 00145 memset(¤t_error_ctx, 0, sizeof(mbed_error_ctx)); 00146 00147 //Capture error information 00148 current_error_ctx.error_status = error_status; 00149 current_error_ctx.error_value = error_value; 00150 mbed_fault_context_t *mfc = NULL; 00151 if (mbed_error_is_hw_fault(error_status)) { 00152 mfc = (mbed_fault_context_t *)error_value; 00153 current_error_ctx.error_address = (uint32_t)mfc->PC_reg; 00154 // Note that this SP_reg is the correct SP value of the fault. PSP and MSP are slightly different due to HardFault_Handler. 00155 current_error_ctx.thread_current_sp = (uint32_t)mfc->SP_reg; 00156 // Note that the RTX thread itself is the same even under this fault exception handler. 00157 } else { 00158 current_error_ctx.error_address = (uint32_t)caller; 00159 current_error_ctx.thread_current_sp = (uint32_t)¤t_error_ctx; // Address local variable to get a stack pointer 00160 } 00161 00162 #ifdef MBED_CONF_RTOS_PRESENT 00163 // Capture thread info in thread mode 00164 osRtxThread_t *current_thread = osRtxInfo.thread.run.curr; 00165 current_error_ctx.thread_id = (uint32_t)current_thread; 00166 current_error_ctx.thread_entry_address = (uint32_t)current_thread->thread_addr; 00167 current_error_ctx.thread_stack_size = current_thread->stack_size; 00168 current_error_ctx.thread_stack_mem = (uint32_t)current_thread->stack_mem; 00169 #endif //MBED_CONF_RTOS_PRESENT 00170 00171 #if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED 00172 //Capture filename/linenumber if provided 00173 //Index for tracking error_filename 00174 strncpy(current_error_ctx.error_filename, filename, MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN); 00175 current_error_ctx.error_line_number = line_number; 00176 #endif 00177 00178 //Prevent corruption by holding out other callers 00179 core_util_critical_section_enter(); 00180 00181 //Increment error count 00182 error_count++; 00183 00184 //Capture the first system error and store it 00185 if (error_count == 1) { //first error 00186 memcpy(&first_error_ctx, ¤t_error_ctx, sizeof(mbed_error_ctx)); 00187 } 00188 00189 //copy this error to last error 00190 memcpy(&last_error_ctx, ¤t_error_ctx, sizeof(mbed_error_ctx)); 00191 00192 #if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED 00193 //Log the error with error log 00194 mbed_error_hist_put(¤t_error_ctx); 00195 #endif 00196 00197 //Call the error hook if available 00198 if (error_hook != NULL) { 00199 error_hook(&last_error_ctx); 00200 } 00201 00202 core_util_critical_section_exit(); 00203 00204 return MBED_SUCCESS; 00205 } 00206 00207 WEAK void mbed_error_reboot_callback(mbed_error_ctx *error_context) 00208 { 00209 //Dont do anything here, let application override this if required. 00210 } 00211 00212 //Initialize Error handling system and report any errors detected on rebooted 00213 mbed_error_status_t mbed_error_initialize(void) 00214 { 00215 #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED 00216 uint32_t crc_val = 0; 00217 00218 //Just check if we have valid value for error_status, if error_status is positive(which is not valid), no need to check crc 00219 if (report_error_ctx->error_status < 0) { 00220 crc_val = mbed_tiny_compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx)); 00221 //Read report_error_ctx and check if CRC is correct, and with valid status code 00222 if ((report_error_ctx->crc_error_ctx == crc_val) && (report_error_ctx->is_error_processed == 0)) { 00223 is_reboot_error_valid = true; 00224 00225 //Call the mbed_error_reboot_callback, this enables applications to do some handling before we do the handling 00226 mbed_error_reboot_callback(report_error_ctx); 00227 00228 //We let the callback reset the error info, so check if its still valid and do the rest only if its still valid. 00229 if (report_error_ctx->error_reboot_count > 0) { 00230 00231 report_error_ctx->is_error_processed = 1;//Set the flag that we already processed this error 00232 crc_val = mbed_tiny_compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx)); 00233 report_error_ctx->crc_error_ctx = crc_val; 00234 00235 //Enforce max-reboot only if auto reboot is enabled 00236 #if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED 00237 if (report_error_ctx->error_reboot_count >= MBED_CONF_PLATFORM_ERROR_REBOOT_MAX) { 00238 mbed_halt_system(); 00239 } 00240 #endif 00241 } 00242 } 00243 } 00244 #endif 00245 return MBED_SUCCESS; 00246 } 00247 00248 //Return the first error 00249 mbed_error_status_t mbed_get_first_error(void) 00250 { 00251 //return the first error recorded 00252 return first_error_ctx.error_status; 00253 } 00254 00255 //Return the last error 00256 mbed_error_status_t mbed_get_last_error(void) 00257 { 00258 //return the last error recorded 00259 return last_error_ctx.error_status; 00260 } 00261 00262 //Gets the current error count 00263 int mbed_get_error_count(void) 00264 { 00265 //return the current error count 00266 return error_count; 00267 } 00268 00269 //Reads the fatal error occurred" flag 00270 bool mbed_get_error_in_progress(void) 00271 { 00272 return core_util_atomic_load_bool (&mbed_error_in_progress); 00273 } 00274 00275 //Sets a non-fatal error 00276 mbed_error_status_t mbed_warning(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number) 00277 { 00278 return handle_error(error_status, error_value, filename, line_number, MBED_CALLER_ADDR()); 00279 } 00280 00281 //Sets a fatal error, this function is marked WEAK to be able to override this for some tests 00282 WEAK MBED_NORETURN 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) 00283 { 00284 // Prevent recursion if error is called again during store+print attempt 00285 if (!core_util_atomic_exchange_bool (&mbed_error_in_progress, true)) { 00286 //set the error reported 00287 (void) handle_error(error_status, error_value, filename, line_number, MBED_CALLER_ADDR()); 00288 00289 //On fatal errors print the error context/report 00290 ERROR_REPORT(&last_error_ctx, error_msg, filename, line_number); 00291 } 00292 00293 #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED 00294 uint32_t crc_val = 0; 00295 crc_val = mbed_tiny_compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx)); 00296 //Read report_error_ctx and check if CRC is correct for report_error_ctx 00297 if (report_error_ctx->crc_error_ctx == crc_val) { 00298 uint32_t current_reboot_count = report_error_ctx->error_reboot_count; 00299 last_error_ctx.error_reboot_count = current_reboot_count + 1; 00300 } else { 00301 last_error_ctx.error_reboot_count = 1; 00302 } 00303 last_error_ctx.is_error_processed = 0;//Set the flag that this is a new error 00304 //Update the struct with crc 00305 last_error_ctx.crc_error_ctx = mbed_tiny_compute_crc32(&last_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx)); 00306 //Protect report_error_ctx while we update it 00307 core_util_critical_section_enter(); 00308 memcpy(report_error_ctx, &last_error_ctx, sizeof(mbed_error_ctx)); 00309 core_util_critical_section_exit(); 00310 //We need not call delete_mbed_crc(crc_obj) here as we are going to reset the system anyway, and calling delete while handling a fatal error may cause nested exception 00311 #if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED && (MBED_CONF_PLATFORM_ERROR_REBOOT_MAX > 0) 00312 #ifndef NDEBUG 00313 mbed_error_printf("\n= System will be rebooted due to a fatal error =\n"); 00314 if (report_error_ctx->error_reboot_count >= MBED_CONF_PLATFORM_ERROR_REBOOT_MAX) { 00315 //We have rebooted more than enough, hold the system here. 00316 mbed_error_printf("= Reboot count(=%" PRIi32") reached maximum, system will halt after rebooting =\n", report_error_ctx->error_reboot_count); 00317 } 00318 #endif 00319 system_reset();//do a system reset to get the system rebooted 00320 #endif 00321 #endif 00322 mbed_halt_system(); 00323 } 00324 00325 //Register an application defined callback with error handling 00326 mbed_error_status_t mbed_set_error_hook(mbed_error_hook_t error_hook_in) 00327 { 00328 //register the new hook/callback 00329 if (error_hook_in != NULL) { 00330 error_hook = error_hook_in; 00331 return MBED_SUCCESS; 00332 } 00333 00334 return MBED_ERROR_INVALID_ARGUMENT; 00335 } 00336 00337 //Reset the reboot error context 00338 mbed_error_status_t mbed_reset_reboot_error_info() 00339 { 00340 #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED 00341 //Protect for thread safety 00342 core_util_critical_section_enter(); 00343 memset(report_error_ctx, 0, sizeof(mbed_error_ctx)); 00344 core_util_critical_section_exit(); 00345 #endif 00346 return MBED_SUCCESS; 00347 } 00348 00349 //Reset the reboot error context 00350 mbed_error_status_t mbed_reset_reboot_count() 00351 { 00352 #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED 00353 if (is_reboot_error_valid) { 00354 uint32_t crc_val = 0; 00355 core_util_critical_section_enter(); 00356 report_error_ctx->error_reboot_count = 0;//Set reboot count to 0 00357 //Update CRC 00358 crc_val = mbed_tiny_compute_crc32(report_error_ctx, offsetof(mbed_error_ctx, crc_error_ctx)); 00359 report_error_ctx->crc_error_ctx = crc_val; 00360 core_util_critical_section_exit(); 00361 return MBED_SUCCESS; 00362 } 00363 #endif 00364 return MBED_ERROR_ITEM_NOT_FOUND; 00365 } 00366 00367 //Retrieve the reboot error context 00368 mbed_error_status_t mbed_get_reboot_error_info(mbed_error_ctx *error_info) 00369 { 00370 mbed_error_status_t status = MBED_ERROR_ITEM_NOT_FOUND; 00371 #if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED 00372 if (is_reboot_error_valid) { 00373 if (error_info != NULL) { 00374 memcpy(error_info, report_error_ctx, sizeof(mbed_error_ctx)); 00375 status = MBED_SUCCESS; 00376 } else { 00377 status = MBED_ERROR_INVALID_ARGUMENT; 00378 } 00379 } 00380 #endif 00381 return status; 00382 } 00383 00384 //Retrieve the first error context from error log 00385 mbed_error_status_t mbed_get_first_error_info(mbed_error_ctx *error_info) 00386 { 00387 memcpy(error_info, &first_error_ctx, sizeof(first_error_ctx)); 00388 return MBED_SUCCESS; 00389 } 00390 00391 //Retrieve the last error context from error log 00392 mbed_error_status_t mbed_get_last_error_info(mbed_error_ctx *error_info) 00393 { 00394 memcpy(error_info, &last_error_ctx, sizeof(mbed_error_ctx)); 00395 return MBED_SUCCESS; 00396 } 00397 00398 //Makes an mbed_error_status_t value 00399 mbed_error_status_t mbed_make_error(mbed_error_type_t error_type, mbed_module_type_t entity, mbed_error_code_t error_code) 00400 { 00401 switch (error_type) { 00402 case MBED_ERROR_TYPE_POSIX: 00403 if (error_code >= MBED_POSIX_ERROR_BASE && error_code <= MBED_SYSTEM_ERROR_BASE) { 00404 return -error_code; 00405 } 00406 break; 00407 00408 case MBED_ERROR_TYPE_SYSTEM: 00409 if (error_code >= MBED_SYSTEM_ERROR_BASE && error_code <= MBED_CUSTOM_ERROR_BASE) { 00410 return MAKE_MBED_ERROR(MBED_ERROR_TYPE_SYSTEM, entity, error_code); 00411 } 00412 break; 00413 00414 case MBED_ERROR_TYPE_CUSTOM: 00415 if (error_code >= MBED_CUSTOM_ERROR_BASE) { 00416 return MAKE_MBED_ERROR(MBED_ERROR_TYPE_CUSTOM, entity, error_code); 00417 } 00418 break; 00419 00420 default: 00421 break; 00422 } 00423 00424 //If we are passed incorrect values return a generic system error 00425 return MAKE_MBED_ERROR(MBED_ERROR_TYPE_SYSTEM, MBED_MODULE_UNKNOWN, MBED_ERROR_CODE_UNKNOWN); 00426 } 00427 00428 /** 00429 * Clears all the last error, error count and all entries in the error log. 00430 * @return 0 or MBED_SUCCESS on success. 00431 * 00432 */ 00433 mbed_error_status_t mbed_clear_all_errors(void) 00434 { 00435 mbed_error_status_t status = MBED_SUCCESS; 00436 00437 //Make sure we dont multiple clients resetting 00438 core_util_critical_section_enter(); 00439 //Clear the error and context capturing buffer 00440 memset(&last_error_ctx, 0, sizeof(mbed_error_ctx)); 00441 //reset error count to 0 00442 error_count = 0; 00443 #if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED 00444 status = mbed_error_hist_reset(); 00445 #endif 00446 core_util_critical_section_exit(); 00447 00448 return status; 00449 } 00450 00451 #ifdef MBED_CONF_RTOS_PRESENT 00452 static inline const char *name_or_unnamed(const osRtxThread_t *thread) 00453 { 00454 const char *unnamed = "<unnamed>"; 00455 00456 if (!thread) { 00457 return unnamed; 00458 } 00459 00460 const char *name = thread->name; 00461 return name ? name : unnamed; 00462 } 00463 #endif // MBED_CONF_RTOS_PRESENT 00464 00465 #if MBED_STACK_DUMP_ENABLED 00466 /** Prints stack dump from given stack information. 00467 * The arguments should be given in address raw value to check alignment. 00468 * @param stack_start The address of stack start. 00469 * @param stack_size The size of stack 00470 * @param stack_sp The stack pointer currently at. */ 00471 static void print_stack_dump_core(uint32_t stack_start, uint32_t stack_size, uint32_t stack_sp, const char *postfix) 00472 { 00473 #if MBED_STACK_DUMP_ENABLED 00474 #define STACK_DUMP_WIDTH 8 00475 #define INT_ALIGN_MASK (~(sizeof(int) - 1)) 00476 mbed_error_printf("\nStack Dump: %s", postfix); 00477 uint32_t st_end = (stack_start + stack_size) & INT_ALIGN_MASK; 00478 uint32_t st = (stack_sp) & INT_ALIGN_MASK; 00479 for (; st < st_end; st += sizeof(int) * STACK_DUMP_WIDTH) { 00480 mbed_error_printf("\n0x%08" PRIX32 ":", st); 00481 for (int i = 0; i < STACK_DUMP_WIDTH; i++) { 00482 uint32_t st_cur = st + i * sizeof(int); 00483 if (st_cur >= st_end) { 00484 break; 00485 } 00486 uint32_t st_val = *((uint32_t *)st_cur); 00487 mbed_error_printf("0x%08" PRIX32 " ", st_val); 00488 } 00489 } 00490 mbed_error_printf("\n"); 00491 #endif // MBED_STACK_DUMP_ENABLED 00492 } 00493 00494 static void print_stack_dump(uint32_t stack_start, uint32_t stack_size, uint32_t stack_sp, const mbed_error_ctx *ctx) 00495 { 00496 if (ctx && mbed_error_is_handler(ctx)) { 00497 // Stack dump extra for handler stack which may have accessed MSP. 00498 mbed_fault_context_t *mfc = (mbed_fault_context_t *)ctx->error_value; 00499 uint32_t msp_sp = mfc->MSP; 00500 uint32_t psp_sp = mfc->PSP; 00501 if (mfc && !(mfc->EXC_RETURN & 0x4)) { 00502 // MSP mode. Then SP_reg is more correct. 00503 msp_sp = mfc->SP_reg; 00504 } else { 00505 // PSP mode. Then SP_reg is more correct. 00506 psp_sp = mfc->SP_reg; 00507 } 00508 uint32_t msp_size = MAX(0, (int)INITIAL_SP - (int)msp_sp); 00509 print_stack_dump_core(msp_sp, msp_size, msp_sp, "MSP"); 00510 00511 stack_sp = psp_sp; 00512 } 00513 00514 print_stack_dump_core(stack_start, stack_size, stack_sp, "PSP"); 00515 } 00516 #endif // MBED_STACK_DUMP_ENABLED 00517 00518 #if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT) 00519 /* Prints info of a thread(using osRtxThread_t struct)*/ 00520 static void print_thread(const osRtxThread_t *thread) 00521 { 00522 uint32_t stack_mem = (uint32_t)thread->stack_mem; 00523 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); 00524 00525 #if MBED_STACK_DUMP_ENABLED 00526 print_stack_dump(stack_mem, thread->stack_size, thread->sp, NULL); 00527 #endif 00528 } 00529 00530 /* Prints thread info from a list */ 00531 static void print_threads_info(const osRtxThread_t *threads) 00532 { 00533 while (threads != NULL) { 00534 print_thread(threads); 00535 threads = threads->thread_next; 00536 } 00537 } 00538 #endif 00539 00540 #ifndef NDEBUG 00541 #define GET_TARGET_NAME_STR(tgt_name) #tgt_name 00542 #define GET_TARGET_NAME(tgt_name) GET_TARGET_NAME_STR(tgt_name) 00543 static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg, const char *error_filename, int error_line) 00544 { 00545 int error_code = MBED_GET_ERROR_CODE(ctx->error_status); 00546 int error_module = MBED_GET_ERROR_MODULE(ctx->error_status); 00547 00548 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); 00549 00550 switch (error_code) { 00551 //These are errors reported by kernel handled from mbed_rtx_handlers 00552 case MBED_ERROR_CODE_RTOS_EVENT: 00553 mbed_error_printf("Kernel Error: 0x%" PRIX32 ", ", ctx->error_value); 00554 break; 00555 00556 case MBED_ERROR_CODE_RTOS_THREAD_EVENT: 00557 mbed_error_printf("Thread: 0x%" PRIX32 ", ", ctx->error_value); 00558 break; 00559 00560 case MBED_ERROR_CODE_RTOS_MUTEX_EVENT: 00561 mbed_error_printf("Mutex: 0x%" PRIX32 ", ", ctx->error_value); 00562 break; 00563 00564 case MBED_ERROR_CODE_RTOS_SEMAPHORE_EVENT: 00565 mbed_error_printf("Semaphore: 0x%" PRIX32 ", ", ctx->error_value); 00566 break; 00567 00568 case MBED_ERROR_CODE_RTOS_MEMORY_POOL_EVENT: 00569 mbed_error_printf("MemoryPool: 0x%" PRIX32 ", ", ctx->error_value); 00570 break; 00571 00572 case MBED_ERROR_CODE_RTOS_EVENT_FLAGS_EVENT: 00573 mbed_error_printf("EventFlags: 0x%" PRIX32 ", ", ctx->error_value); 00574 break; 00575 00576 case MBED_ERROR_CODE_RTOS_TIMER_EVENT: 00577 mbed_error_printf("Timer: 0x%" PRIX32 ", ", ctx->error_value); 00578 break; 00579 00580 case MBED_ERROR_CODE_RTOS_MESSAGE_QUEUE_EVENT: 00581 mbed_error_printf("MessageQueue: 0x%" PRIX32 ", ", ctx->error_value); 00582 break; 00583 00584 case MBED_ERROR_CODE_ASSERTION_FAILED: 00585 mbed_error_printf("Assertion failed: "); 00586 break; 00587 00588 default: 00589 //Nothing to do here, just print the error info down 00590 break; 00591 } 00592 mbed_error_puts(error_msg); 00593 mbed_error_printf("\nLocation: 0x%" PRIX32, ctx->error_address); 00594 00595 /* We print the filename passed in, not any filename in the context. This 00596 * avoids the console print for mbed_error being limited to the presence 00597 * and length of the filename storage. Note that although the MBED_ERROR 00598 * macro compiles out filenames unless platform.error-filename-capture-enabled 00599 * is turned on, MBED_ASSERT always passes filenames, and other direct 00600 * users of mbed_error() may also choose to. 00601 */ 00602 if (error_filename) { 00603 mbed_error_puts("\nFile: "); 00604 mbed_error_puts(error_filename); 00605 mbed_error_printf("+%d", error_line); 00606 } 00607 00608 mbed_error_printf("\nError Value: 0x%" PRIX32, ctx->error_value); 00609 #ifdef MBED_CONF_RTOS_PRESENT 00610 bool is_handler = mbed_error_is_handler(ctx); 00611 mbed_error_printf("\nCurrent Thread: %s%s Id: 0x%" PRIX32 " Entry: 0x%" PRIX32 " StackSize: 0x%" PRIX32 " StackMem: 0x%" PRIX32 " SP: 0x%" PRIX32 " ", 00612 name_or_unnamed((osRtxThread_t *)ctx->thread_id), is_handler ? " <handler>" : "", 00613 ctx->thread_id, ctx->thread_entry_address, ctx->thread_stack_size, ctx->thread_stack_mem, ctx->thread_current_sp); 00614 #endif 00615 00616 #if MBED_STACK_DUMP_ENABLED 00617 print_stack_dump(ctx->thread_stack_mem, ctx->thread_stack_size, ctx->thread_current_sp, ctx); 00618 #endif 00619 00620 #if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT) 00621 mbed_error_printf("\nNext:"); 00622 print_thread(osRtxInfo.thread.run.next); 00623 00624 mbed_error_printf("\nReady:"); 00625 print_threads_info(osRtxInfo.thread.ready.thread_list); 00626 00627 mbed_error_printf("\nWait:"); 00628 print_threads_info(osRtxInfo.thread.wait_list); 00629 00630 mbed_error_printf("\nDelay:"); 00631 print_threads_info(osRtxInfo.thread.delay_list); 00632 #endif 00633 #if !defined(MBED_SYS_STATS_ENABLED) 00634 mbed_error_printf("\nFor more info, visit: https://mbed.com/s/error?error=0x%08X&tgt=" GET_TARGET_NAME(TARGET_NAME), ctx->error_status); 00635 #else 00636 mbed_stats_sys_t sys_stats; 00637 mbed_stats_sys_get(&sys_stats); 00638 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); 00639 #endif 00640 00641 mbed_error_printf("\n-- MbedOS Error Info --\n"); 00642 } 00643 #endif //ifndef NDEBUG 00644 00645 00646 #if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED 00647 //Retrieve the error context from error log at the specified index 00648 mbed_error_status_t mbed_get_error_hist_info(int index, mbed_error_ctx *error_info) 00649 { 00650 return mbed_error_hist_get(index, error_info); 00651 } 00652 00653 //Retrieve the error log count 00654 int mbed_get_error_hist_count(void) 00655 { 00656 return mbed_error_hist_get_count(); 00657 } 00658 00659 mbed_error_status_t mbed_save_error_hist(const char *path) 00660 { 00661 mbed_error_status_t ret = MBED_SUCCESS; 00662 mbed_error_ctx ctx = {0}; 00663 int log_count = mbed_error_hist_get_count(); 00664 FILE *error_log_file = NULL; 00665 00666 //Ensure path is valid 00667 if (path == NULL) { 00668 ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_INVALID_ARGUMENT); 00669 goto exit; 00670 } 00671 00672 //Open the file for saving the error log info 00673 if ((error_log_file = fopen(path, "w")) == NULL) { 00674 ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED); 00675 goto exit; 00676 } 00677 00678 //First store the first and last errors 00679 if (fprintf(error_log_file, "\nFirst Error: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n", 00680 (unsigned int)first_error_ctx.error_status, 00681 (unsigned int)first_error_ctx.thread_id, 00682 (unsigned int)first_error_ctx.error_address, 00683 (unsigned int)first_error_ctx.error_value) <= 0) { 00684 ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_WRITE_FAILED); 00685 goto exit; 00686 } 00687 00688 if (fprintf(error_log_file, "\nLast Error: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n", 00689 (unsigned int)last_error_ctx.error_status, 00690 (unsigned int)last_error_ctx.thread_id, 00691 (unsigned int)last_error_ctx.error_address, 00692 (unsigned int)last_error_ctx.error_value) <= 0) { 00693 ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_WRITE_FAILED); 00694 goto exit; 00695 } 00696 00697 //Update with error log info 00698 while (--log_count >= 0) { 00699 mbed_error_hist_get(log_count, &ctx); 00700 //first line of file will be error log count 00701 if (fprintf(error_log_file, "\n%d: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n", 00702 log_count, 00703 (unsigned int)ctx.error_status, 00704 (unsigned int)ctx.thread_id, 00705 (unsigned int)ctx.error_address, 00706 (unsigned int)ctx.error_value) <= 0) { 00707 ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_WRITE_FAILED); 00708 goto exit; 00709 } 00710 } 00711 00712 exit: 00713 fclose(error_log_file); 00714 00715 return ret; 00716 } 00717 #endif
Generated on Tue Jul 12 2022 13:54:33 by
