Merge pull request #6983 from SenRamakri/sen_ErrorHandling_Push2

Standardized Error Handling and Error Codes
pull/7011/head
Cruz Monrreal 2018-05-24 10:57:00 -05:00 committed by GitHub
commit 527f9a12fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 2241 additions and 211 deletions

View File

@ -0,0 +1,365 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "greentea-client/test_env.h"
#include "utest/utest.h"
#include "unity/unity.h"
#include "mbed.h"
#include <LittleFileSystem.h>
#include "HeapBlockDevice.h"
using utest::v1::Case;
/** Test error count and reset functionality
*/
void test_error_count_and_reset()
{
int count = 7;
//Log multiple errors and get the error count and make sure its 15
for(int i=0; i<count; i++) {
MBED_WARNING1(MBED_ERROR_OUT_OF_MEMORY, "Out of memory", i);
}
TEST_ASSERT_EQUAL_INT(count, mbed_get_error_count());
//clear the errors and error count to 0
mbed_clear_all_errors();
//Now the error count should be 0
TEST_ASSERT_EQUAL_INT(0, mbed_get_error_count());
}
/** Test error type encoding and test capturing of system, custom, posix errors
* and ensure the status/error code/type/error value is correct
*/
void test_error_capturing()
{
uint32_t error_value = 0xAA11BB22;
mbed_error_ctx error_ctx = {0};
//first clear all errors and start afresh
MBED_WARNING1(MBED_ERROR_OUT_OF_RESOURCES, "System type error", 0x1100 );
mbed_error_status_t lastError = mbed_get_last_error();
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_SYSTEM, MBED_GET_ERROR_TYPE(lastError));
TEST_ASSERT_EQUAL_UINT(MBED_MODULE_UNKNOWN, MBED_GET_ERROR_MODULE(lastError));
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_OUT_OF_RESOURCES, MBED_GET_ERROR_CODE(lastError));
mbed_error_status_t error = MBED_MAKE_ERROR(MBED_MODULE_DRIVER_SERIAL, MBED_ERROR_CODE_OUT_OF_RESOURCES);
MBED_WARNING1(error, "Error Serial", 0xAA );
lastError = mbed_get_last_error();
TEST_ASSERT_EQUAL_UINT(error, lastError);
error = MBED_MAKE_CUSTOM_ERROR(MBED_MODULE_APPLICATION, MBED_ERROR_CODE_UNKNOWN);
MBED_WARNING1(error, "Custom Error Unknown", 0x1234 );
lastError = mbed_get_last_error();
TEST_ASSERT_EQUAL_UINT(error, lastError);
MBED_WARNING1(MBED_ERROR_EPERM, "Posix Error Eperm", 0x1234 );
lastError = mbed_get_last_error();
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_EPERM, lastError);
error = MBED_MAKE_CUSTOM_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_CREATE_FAILED);
MBED_WARNING1(error, "Custom Error Type", error_value);
lastError = mbed_get_last_error();
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_CUSTOM, MBED_GET_ERROR_TYPE(lastError));
TEST_ASSERT_EQUAL_UINT(MBED_MODULE_PLATFORM, MBED_GET_ERROR_MODULE(lastError));
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_CREATE_FAILED, MBED_GET_ERROR_CODE(lastError));
mbed_error_status_t status = mbed_get_last_error_info( &error_ctx );
TEST_ASSERT(status == MBED_SUCCESS);
TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
error_value = 0xAABBCC;
MBED_WARNING1(MBED_ERROR_EACCES, "Posix Error", error_value );
lastError = mbed_get_last_error();
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_POSIX, MBED_GET_ERROR_TYPE(lastError));
TEST_ASSERT_EQUAL_UINT(MBED_MODULE_UNKNOWN, MBED_GET_ERROR_MODULE(lastError));
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_EACCES, MBED_GET_ERROR_CODE(lastError));
status = mbed_get_last_error_info( &error_ctx );
TEST_ASSERT(status == MBED_SUCCESS);
TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
error_value = 0;
error = MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_UNKNOWN);
MBED_WARNING1(error, "HAL Entity error", error_value );
lastError = mbed_get_last_error();
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TYPE_SYSTEM, MBED_GET_ERROR_TYPE(lastError));
TEST_ASSERT_EQUAL_UINT(MBED_MODULE_HAL, MBED_GET_ERROR_MODULE(lastError));
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_CODE_UNKNOWN, MBED_GET_ERROR_CODE(lastError));
status = mbed_get_last_error_info( &error_ctx );
TEST_ASSERT(status == MBED_SUCCESS);
TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
MBED_WARNING1(MBED_ERROR_MUTEX_LOCK_FAILED, "Mutex lock failed", 0x4455 );
error = mbed_get_last_error();
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_MUTEX_LOCK_FAILED, error);
error = mbed_get_first_error();
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_OUT_OF_RESOURCES, error);
}
/** Test error context capture
*/
void test_error_context_capture()
{
uint32_t error_value = 0xABCD;
mbed_error_ctx error_ctx = {0};
MBED_WARNING1(MBED_ERROR_INVALID_ARGUMENT, "System type error", error_value );
mbed_error_status_t status = mbed_get_last_error_info( &error_ctx );
TEST_ASSERT(status == MBED_SUCCESS);
TEST_ASSERT_EQUAL_UINT(error_value, error_ctx.error_value);
TEST_ASSERT_EQUAL_UINT(osThreadGetId(), error_ctx.thread_id);
//Capture thread info and compare
osRtxThread_t *current_thread = osRtxInfo.thread.run.curr;
TEST_ASSERT_EQUAL_UINT((uint32_t)current_thread->thread_addr, error_ctx.thread_entry_address);
TEST_ASSERT_EQUAL_UINT((uint32_t)current_thread->stack_size, error_ctx.thread_stack_size);
TEST_ASSERT_EQUAL_UINT((uint32_t)current_thread->stack_mem, error_ctx.thread_stack_mem);
#ifdef MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED
TEST_ASSERT_EQUAL_STRING(MBED_FILENAME, error_ctx.error_filename);
#endif
}
#ifndef MBED_CONF_ERROR_HIST_DISABLED
/** Test error logging functionality
*/
void test_error_logging()
{
mbed_error_ctx error_ctx = {0};
//clear the current errors first
mbed_clear_all_errors();
//log 3 errors and retrieve them to ensure they are correct
MBED_WARNING1(MBED_ERROR_INVALID_ARGUMENT, "Invalid argument", 1 );
MBED_WARNING1(MBED_ERROR_INVALID_SIZE, "Invalid size", 2 );
MBED_WARNING1(MBED_ERROR_INVALID_FORMAT, "Invalid format", 3 );
mbed_error_status_t status = mbed_get_error_hist_info( 0, &error_ctx );
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_INVALID_ARGUMENT, error_ctx.error_status);
TEST_ASSERT_EQUAL_UINT(1, error_ctx.error_value);
status = mbed_get_error_hist_info( 1, &error_ctx );
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_INVALID_SIZE, error_ctx.error_status);
TEST_ASSERT_EQUAL_UINT(2, error_ctx.error_value);
status = mbed_get_error_hist_info( 2, &error_ctx );
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_INVALID_FORMAT, error_ctx.error_status);
TEST_ASSERT_EQUAL_UINT(3, error_ctx.error_value);
//Log a bunch of errors to overflow the error log and retrieve them
MBED_WARNING1(MBED_ERROR_INVALID_ARGUMENT, "Invalid argument", 6 );
MBED_WARNING1(MBED_ERROR_INVALID_SIZE, "Invalid size", 7 );
MBED_WARNING1(MBED_ERROR_INVALID_FORMAT, "Invalid format", 8 );
MBED_WARNING1(MBED_ERROR_NOT_READY, "Not ready error", 9 );
//Last 4 entries
MBED_WARNING1(MBED_ERROR_TIME_OUT, "Timeout error", 10 );
MBED_WARNING1(MBED_ERROR_ALREADY_IN_USE, "Already in use error", 11 );
MBED_WARNING1(MBED_ERROR_UNSUPPORTED, "Not supported", 12 );
MBED_WARNING1(MBED_ERROR_ACCESS_DENIED, "Access denied", 13 );
status = mbed_get_error_hist_info( 0, &error_ctx );
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_TIME_OUT, error_ctx.error_status);
TEST_ASSERT_EQUAL_UINT(10, error_ctx.error_value);
status = mbed_get_error_hist_info( 1, &error_ctx );
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_ALREADY_IN_USE, error_ctx.error_status);
TEST_ASSERT_EQUAL_UINT(11, error_ctx.error_value);
status = mbed_get_error_hist_info( 2, &error_ctx );
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_UNSUPPORTED, error_ctx.error_status);
TEST_ASSERT_EQUAL_UINT(12, error_ctx.error_value);
status = mbed_get_error_hist_info( 3, &error_ctx );
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_ACCESS_DENIED, error_ctx.error_status);
TEST_ASSERT_EQUAL_UINT(13, error_ctx.error_value);
//Try an index which is invalid, we should get ERROR_INVALID_ARGUMENT back
status = mbed_get_error_hist_info( 99, &error_ctx );
TEST_ASSERT_EQUAL_UINT(MBED_ERROR_INVALID_ARGUMENT, status);
}
#define NUM_TEST_THREADS 5
//Error logger threads
void err_thread_func(mbed_error_status_t *error_status)
{
MBED_WARNING1(*error_status, "Error from Multi-Threaded error logging test", *error_status );
}
/** Test error logging multithreaded
*/
void test_error_logging_multithread()
{
mbed_error_ctx error_ctx = {0};
int i=0;
Thread *errThread[NUM_TEST_THREADS];
mbed_error_status_t error_status[NUM_TEST_THREADS] = {
MBED_ERROR_INVALID_ARGUMENT, MBED_ERROR_INVALID_DATA_DETECTED, MBED_ERROR_INVALID_FORMAT, MBED_ERROR_INVALID_SIZE, MBED_ERROR_INVALID_OPERATION
};
for(; i<NUM_TEST_THREADS; i++) {
errThread[i] = new Thread(osPriorityNormal1, 512, NULL, NULL);
errThread[i]->start(callback(err_thread_func, &error_status[i]));
}
wait(2.0);
for(i=0; i<NUM_TEST_THREADS; i++) {
errThread[i]->join();
}
i = mbed_get_error_hist_count()-1;
for(;i>=0;--i) {
mbed_error_status_t status = mbed_get_error_hist_info( i, &error_ctx );
if(status != MBED_SUCCESS) {
TEST_FAIL();
}
TEST_ASSERT_EQUAL_UINT((unsigned int)error_ctx.error_value, (unsigned int)error_ctx.error_status);
}
}
#endif
static Semaphore callback_sem;
void MyErrorHook(const mbed_error_ctx *error_ctx)
{
callback_sem.release();
}
/** Test error hook
*/
void test_error_hook()
{
if( MBED_SUCCESS != mbed_set_error_hook(MyErrorHook)) {
TEST_FAIL();
}
MBED_WARNING1(MBED_ERROR_INVALID_ARGUMENT, "Test for error hook", 1234);
int32_t sem_status = callback_sem.wait(5000);
TEST_ASSERT(sem_status > 0);
}
#ifdef MBED_TEST_SIM_BLOCKDEVICE
// test configuration
#ifndef MBED_TEST_FILESYSTEM
#define MBED_TEST_FILESYSTEM LittleFileSystem
#endif
#ifndef MBED_TEST_FILESYSTEM_DECL
#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
#endif
#ifndef MBED_TEST_BLOCK_COUNT
#define MBED_TEST_BLOCK_COUNT 64
#endif
#ifndef MBED_TEST_SIM_BLOCKDEVICE_DECL
#define MBED_TEST_SIM_BLOCKDEVICE_DECL MBED_TEST_SIM_BLOCKDEVICE fd(MBED_TEST_BLOCK_COUNT*512, 1, 1, 512)
#endif
// declarations
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define INCLUDE(x) STRINGIZE(x.h)
#include INCLUDE(MBED_TEST_FILESYSTEM)
#include INCLUDE(MBED_TEST_SIM_BLOCKDEVICE)
MBED_TEST_FILESYSTEM_DECL;
MBED_TEST_SIM_BLOCKDEVICE_DECL;
/** Test save error log
*/
void test_save_error_log()
{
//Log some errors
MBED_WARNING1(MBED_ERROR_TIME_OUT, "Timeout error", 1 );
MBED_WARNING1(MBED_ERROR_ALREADY_IN_USE, "Already in use error", 2 );
MBED_WARNING1(MBED_ERROR_UNSUPPORTED, "Not supported error", 3 );
MBED_WARNING1(MBED_ERROR_ACCESS_DENIED, "Access denied error", 4 );
MBED_WARNING1(MBED_ERROR_ITEM_NOT_FOUND, "Not found error", 5 );
int error = 0;
error = MBED_TEST_FILESYSTEM::format(&fd);
if(error < 0) {
TEST_FAIL_MESSAGE("Failed formatting");
}
error = fs.mount(&fd);
if(error < 0) {
TEST_FAIL_MESSAGE("Failed mounting fs");
}
if(MBED_SUCCESS != mbed_save_error_hist("/fs/errors.log")) {
TEST_FAIL_MESSAGE("Failed saving error log");
}
FILE *error_file = fopen("/fs/errors.log", "r");
if(error_file == NULL) {
TEST_FAIL_MESSAGE("Unable to find error log in fs");
}
char buff[64] = {0};
while (!feof(error_file)){
int size = fread(&buff[0], 1, 15, error_file);
fwrite(&buff[0], 1, size, stdout);
}
fclose(error_file);
error = fs.unmount();
if(error < 0) {
TEST_FAIL_MESSAGE("Failed unmounting fs");
}
}
#endif
utest::v1::status_t test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(120, "default_auto");
return utest::v1::verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("Test error counting and reset", test_error_count_and_reset),
Case("Test error encoding, value capture, first and last errors", test_error_capturing),
Case("Test error context capture", test_error_context_capture),
Case("Test error hook", test_error_hook),
#ifndef MBED_CONF_ERROR_HIST_DISABLED
Case("Test error logging", test_error_logging),
Case("Test error handling multi-threaded", test_error_logging_multithread),
#ifdef MBED_TEST_SIM_BLOCKDEVICE
Case("Test error save log", test_save_error_log),
#endif
#endif
};
utest::v1::Specification specification(test_setup, cases);
int main()
{
return !utest::v1::Harness::run(specification);
}

View File

@ -51,6 +51,12 @@ Semaphore sync_sem(0, 1);
void error(const char* format, ...) {
(void) format;
}
//Override the set_error function to trap the errors
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)
{
return MBED_SUCCESS;
}
#endif
template<uint32_t flags, uint32_t wait_ms>

View File

@ -85,6 +85,11 @@ void error(const char* format, ...)
{
(void) format;
}
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)
{
return MBED_SUCCESS;
}
#endif
/** Test one-shot not restarted when elapsed

View File

@ -55,6 +55,11 @@ struct Sync {
void error(const char* format, ...) {
(void) format;
}
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)
{
return MBED_SUCCESS;
}
#endif

View File

@ -57,14 +57,14 @@ int ReadOnlyBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
int ReadOnlyBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
{
error("ReadOnlyBlockDevice::program() not allowed");
return 0;
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_BLOCK_DEVICE, MBED_ERROR_CODE_WRITE_PROTECTED), "ReadOnlyBlockDevice::program() not allowed", addr);
return MBED_ERROR_WRITE_PROTECTED;
}
int ReadOnlyBlockDevice::erase(bd_addr_t addr, bd_size_t size)
{
error("ReadOnlyBlockDevice::erase() not allowed");
return 0;
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_BLOCK_DEVICE, MBED_ERROR_CODE_WRITE_PROTECTED), "ReadOnlyBlockDevice::erase() not allowed", addr);
return MBED_ERROR_WRITE_PROTECTED;
}
bd_size_t ReadOnlyBlockDevice::get_read_size() const

View File

@ -29,7 +29,7 @@ void pinmap_pinout(PinName pin, const PinMap *map) {
}
map++;
}
error("could not pinout");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PINMAP_INVALID), "could not pinout", pin);
}
uint32_t pinmap_merge(uint32_t a, uint32_t b) {
@ -44,7 +44,7 @@ uint32_t pinmap_merge(uint32_t a, uint32_t b) {
return a;
// mis-match error case
error("pinmap mis-match");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PINMAP_INVALID), "pinmap mis-match", a);
return (uint32_t)NC;
}
@ -64,7 +64,7 @@ uint32_t pinmap_peripheral(PinName pin, const PinMap* map) {
return (uint32_t)NC;
peripheral = pinmap_find_peripheral(pin, map);
if ((uint32_t)NC == peripheral) // no mapping available
error("pinmap not found for peripheral");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PINMAP_INVALID), "pinmap not found for peripheral", peripheral);
return peripheral;
}
@ -84,6 +84,6 @@ uint32_t pinmap_function(PinName pin, const PinMap* map) {
return (uint32_t)NC;
function = pinmap_find_function(pin, map);
if ((uint32_t)NC == function) // no mapping available
error("pinmap not found for function");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PINMAP_INVALID), "pinmap not found for function", function);
return function;
}

View File

@ -161,7 +161,7 @@ void sleep_manager_lock_deep_sleep_internal(void)
core_util_critical_section_enter();
if (deep_sleep_lock == USHRT_MAX) {
core_util_critical_section_exit();
error("Deep sleep lock would overflow (> USHRT_MAX)");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_OVERFLOW), "DeepSleepLock overflow (> USHRT_MAX)", deep_sleep_lock);
}
core_util_atomic_incr_u16(&deep_sleep_lock, 1);
core_util_critical_section_exit();
@ -172,7 +172,7 @@ void sleep_manager_unlock_deep_sleep_internal(void)
core_util_critical_section_enter();
if (deep_sleep_lock == 0) {
core_util_critical_section_exit();
error("Deep sleep lock would underflow (< 0)");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_UNDERFLOW), "DeepSleepLock underflow (< 0)", deep_sleep_lock);
}
core_util_atomic_decr_u16(&deep_sleep_lock, 1);
core_util_critical_section_exit();

View File

@ -69,7 +69,7 @@ public:
sleep_manager_lock_deep_sleep();
}
if (0 == count) {
error("DeepSleepLock overflow (> USHRT_MAX)");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OVERFLOW), "DeepSleepLock overflow (> USHRT_MAX)", count);
}
}
@ -83,7 +83,7 @@ public:
}
if (count == USHRT_MAX) {
core_util_critical_section_exit();
error("DeepSleepLock underflow (< 0)");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNDERFLOW), "DeepSleepLock underflow (< 0)", count);
}
}
};

View File

@ -29,7 +29,7 @@ Stream::Stream(const char *name) : FileLike(name), _file(NULL) {
if (_file) {
mbed_set_unbuffered_stream(_file);
} else {
error("Stream obj failure, errno=%d\r\n", errno);
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED), "Stream obj failure", _file);
}
}

View File

@ -16,14 +16,55 @@
#include <stdlib.h>
#include <stdarg.h>
#include "device.h"
#include "platform/mbed_toolchain.h"
#include "platform/mbed_critical.h"
#include "platform/mbed_error.h"
#include "platform/mbed_error_hist.h"
#include "platform/mbed_interface.h"
#ifdef MBED_CONF_RTOS_PRESENT
#include "rtx_os.h"
#endif
#if DEVICE_STDIO_MESSAGES
#include <stdio.h>
#endif
//Helper macro to get the current SP
#define GET_CURRENT_SP(sp) \
{ \
/*If in Handler mode we are always using MSP*/ \
if( __get_IPSR() != 0U ) { \
sp = __get_MSP(); \
} else { \
/*Look into CONTROL.SPSEL value*/ \
if ((__get_CONTROL() & 2U) == 0U) { \
sp = __get_MSP();/*Read MSP*/ \
} else { \
sp = __get_PSP();/*Read PSP*/ \
} \
} \
}
static uint8_t error_in_progress = 0;
static int error_count = 0;
static mbed_error_ctx first_error_ctx = {0};
static mbed_error_ctx last_error_ctx = {0};
static mbed_error_hook_t error_hook = NULL;
static void print_error_report(mbed_error_ctx *ctx, const char *);
//Helper function to halt the system
static void mbed_halt_system(void)
{
//If not in ISR context exit, otherwise spin on WFI
if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
for(;;) {
__WFI();
}
} else {
//exit eventually calls mbed_die
exit(1);
}
}
WEAK void error(const char* format, ...) {
@ -37,7 +78,360 @@ WEAK void error(const char* format, ...) {
va_list arg;
va_start(arg, format);
mbed_error_vfprintf(format, arg);
MBED_ERROR(MBED_ERROR_UNKNOWN, "Fatal Run-time Error");
va_end(arg);
#endif
exit(1);
}
//Set an error status with the error handling system
mbed_error_status_t handle_error(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number)
{
mbed_error_ctx current_error_ctx;
//Error status should always be < 0
if(error_status >= 0) {
//This is a weird situation, someone called mbed_error with invalid error code.
//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
error_status = MBED_ERROR_INVALID_ARGUMENT;
}
//Prevent corruption by holding out other callers
//and we also need this until we remove the "error" call completely
while (error_in_progress == 1);
//Use critsect here, as we don't want inadvertant modification of this global variable
core_util_critical_section_enter();
error_in_progress = 1;
core_util_critical_section_exit();
//Increment error count
error_count++;
//Clear the context capturing buffer
memset(&current_error_ctx, sizeof(mbed_error_ctx), 0);
//Capture error information
current_error_ctx.error_status = error_status;
current_error_ctx.error_address = (uint32_t)MBED_CALLER_ADDR();
current_error_ctx.error_value = error_value;
#ifdef MBED_CONF_RTOS_PRESENT
//Capture thread info
osRtxThread_t *current_thread = osRtxInfo.thread.run.curr;
current_error_ctx.thread_id = (uint32_t)current_thread;
current_error_ctx.thread_entry_address = (uint32_t)current_thread->thread_addr;
current_error_ctx.thread_stack_size = current_thread->stack_size;
current_error_ctx.thread_stack_mem = (uint32_t)current_thread->stack_mem;
#ifdef TARGET_CORTEX_M
GET_CURRENT_SP(current_error_ctx.thread_current_sp);
#endif //TARGET_CORTEX_M
#endif //MBED_CONF_RTOS_PRESENT
#ifdef MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED
//Capture filename/linenumber if provided
//Index for tracking error_filename
int idx = 0;
if(NULL != filename) {
while(idx < MBED_CONF_MAX_ERROR_FILENAME_LEN && (filename[idx] != '\0')) {
current_error_ctx.error_filename[idx] = filename[idx];
idx++;
}
current_error_ctx.error_line_number = line_number;
}
#endif
//Capture the fist system error and store it
if(error_count == 1) { //first error
memcpy(&first_error_ctx, &current_error_ctx, sizeof(mbed_error_ctx));
}
//copy this error to last error
memcpy(&last_error_ctx, &current_error_ctx, sizeof(mbed_error_ctx));
#ifndef MBED_CONF_ERROR_HIST_DISABLED
//Log the error with error log
mbed_error_hist_put(&current_error_ctx);
#endif
//Call the error hook if available
if(error_hook != NULL) {
error_hook(&last_error_ctx);
}
error_in_progress = 0;
return MBED_SUCCESS;
}
//Return the first error
mbed_error_status_t mbed_get_first_error(void)
{
//return the first error recorded
return first_error_ctx.error_status;
}
//Return the last error
mbed_error_status_t mbed_get_last_error(void)
{
//return the last error recorded
return last_error_ctx.error_status;
}
//Gets the current error count
int mbed_get_error_count(void)
{
//return the current error count
return error_count;
}
//Sets a fatal error
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)
{
return handle_error(error_status, error_msg, error_value, filename, line_number);
}
//Sets a fatal error
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)
{
//set the error reported and then halt the system
if( MBED_SUCCESS != handle_error(error_status, error_msg, error_value, filename, line_number) )
return MBED_ERROR_FAILED_OPERATION;
//On fatal errors print the error context/report
print_error_report(&last_error_ctx, error_msg);
mbed_halt_system();
return MBED_ERROR_FAILED_OPERATION;
}
//Register an application defined callback with error handling
mbed_error_status_t mbed_set_error_hook(mbed_error_hook_t error_hook_in)
{
//register the new hook/callback
if( error_hook_in != NULL ) {
error_hook = error_hook_in;
return MBED_SUCCESS;
}
return MBED_ERROR_INVALID_ARGUMENT;
}
//Retrieve the first error context from error log
mbed_error_status_t mbed_get_first_error_info (mbed_error_ctx *error_info)
{
memcpy(error_info, &first_error_ctx, sizeof(first_error_ctx));
return MBED_SUCCESS;
}
//Retrieve the last error context from error log
mbed_error_status_t mbed_get_last_error_info (mbed_error_ctx *error_info)
{
memcpy(error_info, &last_error_ctx, sizeof(mbed_error_ctx));
return MBED_SUCCESS;
}
//Makes an mbed_error_status_t value
mbed_error_status_t mbed_make_error(mbed_error_type_t error_type, mbed_module_type_t entity, mbed_error_code_t error_code)
{
switch(error_type)
{
case MBED_ERROR_TYPE_POSIX:
if(error_code >= MBED_POSIX_ERROR_BASE && error_code <= MBED_SYSTEM_ERROR_BASE)
return -error_code;
break;
case MBED_ERROR_TYPE_SYSTEM:
if(error_code >= MBED_SYSTEM_ERROR_BASE && error_code <= MBED_CUSTOM_ERROR_BASE)
return MAKE_MBED_ERROR(MBED_ERROR_TYPE_SYSTEM, entity, error_code);
break;
case MBED_ERROR_TYPE_CUSTOM:
if(error_code >= MBED_CUSTOM_ERROR_BASE)
return MAKE_MBED_ERROR(MBED_ERROR_TYPE_CUSTOM, entity, error_code);
break;
default:
break;
}
//If we are passed incorrect values return a generic system error
return MAKE_MBED_ERROR(MBED_ERROR_TYPE_SYSTEM, MBED_MODULE_UNKNOWN, MBED_ERROR_CODE_UNKNOWN);
}
/**
* Clears all the last error, error count and all entries in the error log.
* @return 0 or MBED_SUCCESS on success.
*
*/
mbed_error_status_t mbed_clear_all_errors(void)
{
mbed_error_status_t status = MBED_SUCCESS;
//Make sure we dont multiple clients resetting
core_util_critical_section_enter();
//Clear the error and context capturing buffer
memset(&last_error_ctx, sizeof(mbed_error_ctx), 0);
//reset error count to 0
error_count = 0;
#ifndef MBED_CONF_ERROR_HIST_DISABLED
status = mbed_error_hist_reset();
#endif
core_util_critical_section_exit();
return status;
}
#ifndef MBED_CONF_ERROR_HIST_DISABLED
//Retrieve the error context from error log at the specified index
mbed_error_status_t mbed_get_error_hist_info (int index, mbed_error_ctx *error_info)
{
return mbed_error_hist_get(index, error_info);
}
//Retrieve the error log count
int mbed_get_error_hist_count(void)
{
return mbed_error_hist_get_count();
}
mbed_error_status_t mbed_save_error_hist(const char *path)
{
mbed_error_status_t ret = MBED_SUCCESS;
mbed_error_ctx ctx = {0};
int log_count = mbed_error_hist_get_count();
FILE *error_log_file = NULL;
//Ensure path is valid
if(path==NULL) {
ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_INVALID_ARGUMENT);
goto exit;
}
//Open the file for saving the error log info
if((error_log_file = fopen( path, "w" ) ) == NULL){
ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED);
goto exit;
}
//First store the first and last errors
if(fprintf(error_log_file, "\nFirst Error: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n",
(unsigned int)first_error_ctx.error_status,
(unsigned int)first_error_ctx.thread_id,
(unsigned int)first_error_ctx.error_address,
(unsigned int)first_error_ctx.error_value) <= 0) {
ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_WRITE_FAILED);
goto exit;
}
if(fprintf(error_log_file, "\nLast Error: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n",
(unsigned int)last_error_ctx.error_status,
(unsigned int)last_error_ctx.thread_id,
(unsigned int)last_error_ctx.error_address,
(unsigned int)last_error_ctx.error_value) <= 0) {
ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_WRITE_FAILED);
goto exit;
}
//Update with error log info
while(--log_count >= 0) {
mbed_error_hist_get(log_count, &ctx);
//first line of file will be error log count
if(fprintf(error_log_file, "\n%d: Status:0x%x ThreadId:0x%x Address:0x%x Value:0x%x\n",
log_count,
(unsigned int)ctx.error_status,
(unsigned int)ctx.thread_id,
(unsigned int)ctx.error_address,
(unsigned int)ctx.error_value) <= 0) {
ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_WRITE_FAILED);
goto exit;
}
}
exit:
fclose(error_log_file);
return ret;
}
static void print_error_report(mbed_error_ctx *ctx, const char *error_msg)
{
uint32_t error_code = MBED_GET_ERROR_CODE(ctx->error_status);
uint32_t error_module = MBED_GET_ERROR_MODULE(ctx->error_status);
mbed_error_printf("\n\n++ MbedOS Error Info ++\nError Status: 0x%x Code: %d Entity: %d\nError Message: ", ctx->error_status, error_code, error_module);
//Report error info based on error code, some errors require different
//error_vals[1] contains the error code
if(error_code == MBED_ERROR_CODE_HARDFAULT_EXCEPTION ||
error_code == MBED_ERROR_CODE_MEMMANAGE_EXCEPTION ||
error_code == MBED_ERROR_CODE_BUSFAULT_EXCEPTION ||
error_code == MBED_ERROR_CODE_USAGEFAULT_EXCEPTION ) {
mbed_error_printf(error_msg);
mbed_error_printf("\nLocation: 0x%x\n", ctx->error_value);
} else {
switch (error_code) {
//These are errors reported by kernel handled from mbed_rtx_handlers
case MBED_ERROR_CODE_RTOS_EVENT:
mbed_error_printf("Kernel Error: 0x%x, ", ctx->error_value);
break;
case MBED_ERROR_CODE_RTOS_THREAD_EVENT:
mbed_error_printf("Thread: 0x%x, ", ctx->error_value);
break;
case MBED_ERROR_CODE_RTOS_MUTEX_EVENT:
mbed_error_printf("Mutex: 0x%x, ", ctx->error_value);
break;
case MBED_ERROR_CODE_RTOS_SEMAPHORE_EVENT:
mbed_error_printf("Semaphore: 0x%x, ", ctx->error_value);
break;
case MBED_ERROR_CODE_RTOS_MEMORY_POOL_EVENT:
mbed_error_printf("MemoryPool: 0x%x, ", ctx->error_value);
break;
case MBED_ERROR_CODE_RTOS_EVENT_FLAGS_EVENT:
mbed_error_printf("EventFlags: 0x%x, ", ctx->error_value);
break;
case MBED_ERROR_CODE_RTOS_TIMER_EVENT:
mbed_error_printf("Timer: 0x%x, ", ctx->error_value);
break;
case MBED_ERROR_CODE_RTOS_MESSAGE_QUEUE_EVENT:
mbed_error_printf("MessageQueue: 0x%x, ", ctx->error_value);
break;
default:
//Nothing to do here, just print the error info down
break;
}
mbed_error_printf(error_msg, NULL);
mbed_error_printf("\nLocation: 0x%x", ctx->error_address);
#ifdef MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED
if(NULL != error_ctx->error_filename) {
//for string, we must pass address of a ptr which has the address of the string
uint32_t *file_name = (uint32_t *)&error_ctx->error_filename[0];
mbed_error_printf("\nFile:%s", &file_name);
mbed_error_printf("+0x%x", ctx->error_line_number);
}
#endif
#ifdef TARGET_CORTEX_M
mbed_error_printf("\nError Value: 0x%x\nCurrent Thread: Id: 0x%x Entry: 0x%x StackSize: 0x%x StackMem: 0x%x SP: 0x%x ",
ctx->error_value, ctx->thread_id, ctx->thread_entry_address, ctx->thread_stack_size, ctx->thread_stack_mem, ctx->thread_current_sp);
#else
//For Cortex-A targets we dont have support to capture the current SP
mbed_error_printf("\nError Value: 0x%x\nCurrent Thread: Id: 0x%x Entry: 0x%x StackSize: 0x%x StackMem: 0x%x ",
ctx->error_value, ctx->thread_id, ctx->thread_entry_address, ctx->thread_stack_size, ctx->thread_stack_mem);
#endif //TARGET_CORTEX_M
}
mbed_error_printf("\n-- MbedOS Error Info --");
}
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,99 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <stdarg.h>
#include "device.h"
#include "platform/mbed_error.h"
#include "platform/mbed_toolchain.h"
#include "platform/mbed_critical.h"
#include "platform/mbed_interface.h"
#ifndef MBED_CONF_ERROR_HIST_DISABLED
#include "platform/mbed_error_hist.h"
static mbed_error_ctx mbed_error_ctx_log[MBED_CONF_ERROR_HIST_SIZE] = {0};
static int error_log_count = -1;
mbed_error_status_t mbed_error_hist_put(mbed_error_ctx *error_ctx)
{
//Return error if error_ctx is NULL
if(NULL == error_ctx) {
return MBED_ERROR_INVALID_ARGUMENT;
}
core_util_critical_section_enter();
error_log_count++;
memcpy(&mbed_error_ctx_log[error_log_count % MBED_CONF_ERROR_HIST_SIZE], error_ctx, sizeof(mbed_error_ctx) );
core_util_critical_section_exit();
return MBED_SUCCESS;
}
mbed_error_status_t mbed_error_hist_get(int index, mbed_error_ctx *error_ctx)
{
//Return error if index is more than max log size
if(index >= MBED_CONF_ERROR_HIST_SIZE) {
return MBED_ERROR_INVALID_ARGUMENT;
}
core_util_critical_section_enter();
//calculate the index where we want to pick the ctx
if(error_log_count >= MBED_CONF_ERROR_HIST_SIZE) {
index = (error_log_count + index + 1) % MBED_CONF_ERROR_HIST_SIZE;
}
core_util_critical_section_exit();
memcpy(error_ctx, &mbed_error_ctx_log[index % MBED_CONF_ERROR_HIST_SIZE], sizeof(mbed_error_ctx) );
return MBED_SUCCESS;
}
mbed_error_ctx *mbed_error_hist_get_entry(void)
{
core_util_critical_section_enter();
error_log_count++;
mbed_error_ctx *ctx = &mbed_error_ctx_log[error_log_count % MBED_CONF_ERROR_HIST_SIZE];
core_util_critical_section_exit();
return ctx;
}
mbed_error_status_t mbed_error_hist_get_last_error(mbed_error_ctx *error_ctx)
{
if(-1 == error_log_count) {
return MBED_ERROR_ITEM_NOT_FOUND;
}
core_util_critical_section_enter();
memcpy(error_ctx, &mbed_error_ctx_log[error_log_count % MBED_CONF_ERROR_HIST_SIZE], sizeof(mbed_error_ctx) );
core_util_critical_section_exit();
return MBED_SUCCESS;
}
int mbed_error_hist_get_count()
{
return (error_log_count >= MBED_CONF_ERROR_HIST_SIZE? MBED_CONF_ERROR_HIST_SIZE:error_log_count+1);
}
mbed_error_status_t mbed_error_hist_reset()
{
core_util_critical_section_enter();
error_log_count = -1;
core_util_critical_section_exit();
return MBED_SUCCESS;
}
#endif

116
platform/mbed_error_hist.h Normal file
View File

@ -0,0 +1,116 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_ERROR_HIST_H
#define MBED_ERROR_HIST_H
#ifndef MBED_CONF_ERROR_HIST_SIZE
#define MBED_CONF_ERROR_HIST_SIZE 4
#else
#if MBED_CONF_ERROR_HIST_SIZE == 0
#define MBED_CONF_ERROR_HIST_SIZE 1
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
* Puts/Adds an error entry into the error history list
*
* @param error_ctx pointer to the mbed_error_ctx struct with the error context
* @return 0 or MBED_SUCCESS on success.
* MBED_ERROR_WRITE_FAILED if writing to file failed
* MBED_ERROR_INVALID_ARGUMENT if path is not valid
*
*
*/
mbed_error_status_t mbed_error_hist_put(mbed_error_ctx *error_ctx);
/*
* Reads the error entry from the error list with the specified index
*
* @param index Index of the error context to be retrieved. It starts from 0 and 0 is the oldest.
* @param error_ctx pointer to the mbed_error_ctx struct where the error context will be filled, this should be allocated by the caller
* @return 0 or MBED_SUCCESS on success.
* MBED_ERROR_WRITE_FAILED if writing to file failed
* MBED_ERROR_INVALID_ARGUMENT if path is not valid
*
*
*/
mbed_error_status_t mbed_error_hist_get(int index, mbed_error_ctx *error_ctx);
/*
* Gets a reference to the next error entry in the error log where in the error ctx can be filled in.
* Its like reserving the next error entry to fill in the error info
*
* @return Returns the pointer to the next error ctx entry
*
*
*/
mbed_error_ctx *mbed_error_hist_get_entry(void);
/*
* Reads the last(latest) error entry from the error history
*
* @param error_ctx pointer to the mbed_error_ctx struct where the error context will be filled, this should be allocated by the caller
* @return 0 or MBED_SUCCESS on success.
* MBED_ERROR_WRITE_FAILED if writing to file failed
* MBED_ERROR_INVALID_ARGUMENT if path is not valid
*
*
*/
mbed_error_status_t mbed_error_hist_get_last_error(mbed_error_ctx *error_ctx);
/*
* Returns the number of error entries in the error history list
*
* @return Number of entries in the history list
*
*
*/
int mbed_error_hist_get_count(void);
/*
* Resets the error log by resetting the number of errors to 0 and clears all previous errors in the history list
*
* @return 0 or MBED_SUCCESS on success.
* MBED_ERROR_WRITE_FAILED if writing to file failed
* MBED_ERROR_INVALID_ARGUMENT if path is not valid
*
*
*/
mbed_error_status_t mbed_error_hist_reset(void);
/*
* Saves the error log information to a file
*
* @param path path to the file in the filesystem
* @return 0 or MBED_SUCCESS on success.
* MBED_ERROR_WRITE_FAILED if writing to file failed
* MBED_ERROR_INVALID_ARGUMENT if path is not valid
*
* @note Filesystem support is required in order for this function to work.
*
*/
mbed_error_status_t mbed_save_error_hist(const char *path);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -538,7 +538,7 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
#if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED && defined(MBED_CONF_RTOS_PRESENT)
if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
error("Error - writing to a file in an ISR or critical section\r\n");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PROHIBITED_IN_ISR_CONTEXT), "Error - writing to a file in an ISR or critical section\r\n", fh);
}
#endif
@ -646,7 +646,7 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
#if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED && defined(MBED_CONF_RTOS_PRESENT)
if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
error("Error - reading from a file in an ISR or critical section\r\n");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PROHIBITED_IN_ISR_CONTEXT), "Error - reading from a file in an ISR or critical section\r\n", fh);
}
#endif
@ -1088,7 +1088,7 @@ extern "C" int statvfs(const char *path, struct statvfs *buf) {
#include "mbed_error.h"
namespace __gnu_cxx {
void __verbose_terminate_handler() {
error("Exception");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_CLIB_EXCEPTION),"Exception", 0);
}
}
extern "C" WEAK void __cxa_pure_virtual(void);
@ -1374,7 +1374,7 @@ extern "C" void __cxa_guard_abort(int *guard_object_p)
#endif
#if defined(MBED_MEM_TRACING_ENABLED) && (defined(__CC_ARM) || defined(__ICCARM__) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)))
#if defined(MBED_MEM_TRACING_ENABLED) && (defined(__CC_ARM) || defined(__ICCARM__))
// If the memory tracing is enabled, the wrappers in mbed_alloc_wrappers.cpp
// provide the implementation for these. Note: this needs to use the wrappers
@ -1387,7 +1387,7 @@ void *operator new(std::size_t count)
{
void *buffer = malloc_wrapper(count, MBED_CALLER_ADDR());
if (NULL == buffer) {
error("Operator new out of memory\r\n");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Operator new out of memory\r\n", count);
}
return buffer;
}
@ -1431,7 +1431,7 @@ void *operator new(std::size_t count)
{
void *buffer = malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
if (NULL == buffer) {
error("Operator new out of memory\r\n");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Operator new out of memory\r\n", count);
}
return buffer;
}
@ -1440,7 +1440,7 @@ void *operator new[](std::size_t count)
{
void *buffer = malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
if (NULL == buffer) {
error("Operator new[] out of memory\r\n");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Operator new out of memory\r\n", count);
}
return buffer;
}
@ -1471,7 +1471,7 @@ void *operator new(std::size_t count)
{
void *buffer = malloc(count);
if (NULL == buffer) {
error("Operator new out of memory\r\n");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Operator new out of memory\r\n", count);
}
return buffer;
}
@ -1480,7 +1480,7 @@ void *operator new[](std::size_t count)
{
void *buffer = malloc(count);
if (NULL == buffer) {
error("Operator new[] out of memory\r\n");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Operator new[] out of memory\r\n", count);
}
return buffer;
}

View File

@ -15,104 +15,230 @@
*/
#include "rtx_os.h"
#include "mbed_rtx.h"
#include "mbed_rtx_fault_handler.h"
#include "device.h"
#include "platform/mbed_error.h"
#include "hal/serial_api.h"
#include "hal/itm_api.h"
#ifndef MBED_FAULT_HANDLER_DISABLED
#include "mbed_rtx_fault_handler.h"
#ifdef DEVICE_SERIAL
extern int stdio_uart_inited;
extern serial_t stdio_uart;
#endif
//Functions Prototypes
void print_context_info(void);
//Global for populating the context in exception handler
mbed_fault_context_t mbed_fault_context;
//Structure to capture the context
void fault_print_init(void);
void fault_print_str(char *fmtstr, uint32_t *values);
void hex_to_str(uint32_t value, char *hex_star);
void print_context_info(void);
void print_threads_info(osRtxThread_t *);
void print_thread(osRtxThread_t *thread);
void print_register(char *regtag, uint32_t regval);
/* Converts a uint32 to hex char string */
static void value_to_hex_str(uint32_t value, char *hex_str)
{
char hex_char_map[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int i = 0;
//Return without converting if hex_str is not provided
if(hex_str == NULL) return;
for(i=7; i>=0; i--) {
hex_str[i] = hex_char_map[(value & (0xf << (i * 4))) >> (i * 4)];
}
}
static void fault_print_init(void)
{
#if DEVICE_SERIAL
extern int stdio_uart_inited;
extern serial_t stdio_uart;
/* Initializes std uart if not init-ed yet */
if (!stdio_uart_inited) {
serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
}
#endif
#if DEVICE_ITM
/*Initialize ITM interfaces*/
mbed_itm_init();
#endif
}
static void fault_putc(char ch)
{
#if DEVICE_SERIAL
serial_putc(&stdio_uart, ch);
#endif
#if DEVICE_ITM
/*Initialize ITM interfaces*/
mbed_itm_send(ITM_PORT_SWO, ch);
#endif
}
/* Limited print functionality which prints the string out to
stdout/uart without using stdlib by directly calling serial-api
and also uses less resources
The fmtstr contains the format string for printing and for every %
found in that it fetches a uint32 value from values buffer
and prints it in hex format.
*/
static void fault_print(char *fmtstr, uint32_t *values)
{
#if DEVICE_SERIAL || DEVICE_ITM
int i = 0;
int idx = 0;
int vidx = 0;
char num_str[9]={0};
char *str=NULL;
//Init error reporting interfaces
fault_print_init();
while(fmtstr[i] != '\0') {
if(fmtstr[i]=='%') {
i++;
if(fmtstr[i]=='x') {
//print the number in hex format
value_to_hex_str(values[vidx++],num_str);
for(idx=7; idx>=0; idx--) {
fault_putc(num_str[idx]);
}
}
else if(fmtstr[i]=='s') {
//print the string
str = (char *)((uint32_t)values[vidx++]);
while(*str != '\0') {
fault_putc(*str);
str++;
}
str = NULL;
} else {
//Do not handle any other % formatting and keep going
}
} else {
//handle carriage returns
if (fmtstr[i] == '\n') {
fault_putc('\r');
}
fault_putc(fmtstr[i]);
}
i++;
}
#endif
}
#ifdef MBED_CONF_RTOS_PRESENT
/* Prints info of a thread(using osRtxThread_t struct)*/
static void print_thread(osRtxThread_t *thread)
{
uint32_t data[5];
data[0]=thread->state;
data[1]=thread->thread_addr;
data[2]=thread->stack_size;
data[3]=(uint32_t)thread->stack_mem;
data[4]=thread->sp;
fault_print("\nState: 0x%x Entry: 0x%x Stack Size: 0x%x Mem: 0x%x SP: 0x%x", data);
}
/* Prints thread info from a list */
static void print_threads_info(osRtxThread_t *threads)
{
while(threads != NULL) {
print_thread( threads );
threads = threads->thread_next;
}
}
#endif
//This is a handler function called from Fault handler to print the error information out.
//This runs in fault context and uses special functions(defined in mbed_rtx_fault_handler.c) to print the information without using C-lib support.
__NO_RETURN void mbed_fault_handler (uint32_t fault_type, void *mbed_fault_context_in, void *osRtxInfoIn)
{
fault_print_init();
fault_print_str("\n++ MbedOS Fault Handler ++\n\nFaultType: ",NULL);
mbed_error_status_t faultStatus = MBED_SUCCESS;
fault_print("\n++ MbedOS Fault Handler ++\n\nFaultType: ",NULL);
switch( fault_type ) {
case HARD_FAULT_EXCEPTION:
fault_print_str("HardFault",NULL);
fault_print("HardFault",NULL);
faultStatus = MBED_ERROR_HARDFAULT_EXCEPTION;
break;
case MEMMANAGE_FAULT_EXCEPTION:
fault_print_str("MemManageFault",NULL);
fault_print("MemManageFault",NULL);
faultStatus = MBED_ERROR_MEMMANAGE_EXCEPTION;
break;
case BUS_FAULT_EXCEPTION:
fault_print_str("BusFault",NULL);
fault_print("BusFault",NULL);
faultStatus = MBED_ERROR_BUSFAULT_EXCEPTION;
break;
case USAGE_FAULT_EXCEPTION:
fault_print_str("UsageFault",NULL);
fault_print("UsageFault",NULL);
faultStatus = MBED_ERROR_USAGEFAULT_EXCEPTION;
break;
default:
fault_print_str("Unknown Fault",NULL);
fault_print("Unknown Fault",NULL);
faultStatus = MBED_ERROR_UNKNOWN;
break;
}
fault_print_str("\n\nContext:",NULL);
fault_print("\n\nContext:",NULL);
print_context_info();
fault_print_str("\n\nThread Info:\nCurrent:",NULL);
fault_print("\n\nThreads Info:\nCurrent:",NULL);
print_thread(((osRtxInfo_t *)osRtxInfoIn)->thread.run.curr);
fault_print_str("\nNext:",NULL);
fault_print("\nNext:",NULL);
print_thread(((osRtxInfo_t *)osRtxInfoIn)->thread.run.next);
fault_print_str("\nWait Threads:",NULL);
fault_print("\nWait:",NULL);
osRtxThread_t *threads = ((osRtxInfo_t *)osRtxInfoIn)->thread.wait_list;
print_threads_info(threads);
fault_print_str("\nDelay Threads:",NULL);
fault_print("\nDelay:",NULL);
threads = ((osRtxInfo_t *)osRtxInfoIn)->thread.delay_list;
print_threads_info(threads);
fault_print_str("\nIdle Thread:",NULL);
fault_print("\nIdle:",NULL);
threads = ((osRtxInfo_t *)osRtxInfoIn)->thread.idle;
print_threads_info(threads);
fault_print_str("\n\n-- MbedOS Fault Handler --\n\n",NULL);
/* Just spin here, we have already crashed */
for (;;) {}
fault_print("\n\n-- MbedOS Fault Handler --\n\n",NULL);
//Now call mbed_error, to log the error and halt the system
mbed_error( MBED_MAKE_ERROR( MBED_MODULE_UNKNOWN, faultStatus ), "System encountered an unrecoverable fault excaption, halting system.", mbed_fault_context.PC_reg, NULL, 0 );
/* In case we return, just spin here, we have already crashed */
for (;;) {
__WFI();
}
}
void print_context_info()
void print_context_info(void)
{
//Context Regs
fault_print_str( "\nR0 : %"
"\nR1 : %"
"\nR2 : %"
"\nR3 : %"
"\nR4 : %"
"\nR5 : %"
"\nR6 : %"
"\nR7 : %"
"\nR8 : %"
"\nR9 : %"
"\nR10 : %"
"\nR11 : %"
"\nR12 : %"
"\nSP : %"
"\nLR : %"
"\nPC : %"
"\nxPSR : %"
"\nPSP : %"
"\nMSP : %", (uint32_t *)&mbed_fault_context);
fault_print("\nR0 : %x"
"\nR1 : %x"
"\nR2 : %x"
"\nR3 : %x"
"\nR4 : %x"
"\nR5 : %x"
"\nR6 : %x"
"\nR7 : %x"
"\nR8 : %x"
"\nR9 : %x"
"\nR10 : %x"
"\nR11 : %x"
"\nR12 : %x"
"\nSP : %x"
"\nLR : %x"
"\nPC : %x"
"\nxPSR : %x"
"\nPSP : %x"
"\nMSP : %x", (uint32_t *)&mbed_fault_context);
//Capture CPUID to get core/cpu info
fault_print_str("\nCPUID: %",(uint32_t *)&SCB->CPUID);
fault_print("\nCPUID: %x",(uint32_t *)&SCB->CPUID);
#if !defined(TARGET_M0) && !defined(TARGET_M0P)
//Capture fault information registers to infer the cause of exception
@ -126,121 +252,43 @@ void print_context_info()
FSR[4] = SCB->DFSR;
FSR[5] = SCB->AFSR;
FSR[6] = SCB->SHCSR;
fault_print_str("\nHFSR : %"
"\nMMFSR: %"
"\nBFSR : %"
"\nUFSR : %"
"\nDFSR : %"
"\nAFSR : %"
"\nSHCSR: %",FSR);
fault_print("\nHFSR : %x"
"\nMMFSR: %x"
"\nBFSR : %x"
"\nUFSR : %x"
"\nDFSR : %x"
"\nAFSR : %x"
"\nSHCSR: %x",FSR);
//Print MMFAR only if its valid as indicated by MMFSR
if(FSR[1] & 0x80) {
fault_print_str("\nMMFAR: %",(uint32_t *)&SCB->MMFAR);
fault_print("\nMMFAR: %x",(uint32_t *)&SCB->MMFAR);
}
//Print BFAR only if its valid as indicated by BFSR
if(FSR[2] & 0x80) {
fault_print_str("\nBFAR : %",(uint32_t *)&SCB->BFAR);
fault_print("\nBFAR : %x",(uint32_t *)&SCB->BFAR);
}
#endif
//Print Mode
if(mbed_fault_context.EXC_RETURN & 0x8) {
fault_print_str("\nMode : Thread", NULL);
fault_print("\nMode : Thread", NULL);
//Print Priv level in Thread mode - We capture CONTROL reg which reflects the privilege.
//Note that the CONTROL register captured still reflects the privilege status of the
//thread mode eventhough we are in Handler mode by the time we capture it.
if(mbed_fault_context.CONTROL & 0x1) {
fault_print_str("\nPriv : User", NULL);
fault_print("\nPriv : User", NULL);
} else {
fault_print_str("\nPriv : Privileged", NULL);
fault_print("\nPriv : Privileged", NULL);
}
} else {
fault_print_str("\nMode : Handler", NULL);
fault_print_str("\nPriv : Privileged", NULL);
fault_print("\nMode : Handler", NULL);
fault_print("\nPriv : Privileged", NULL);
}
//Print Return Stack
if(mbed_fault_context.EXC_RETURN & 0x4) {
fault_print_str("\nStack: PSP", NULL);
fault_print("\nStack: PSP", NULL);
} else {
fault_print_str("\nStack: MSP", NULL);
}
}
/* Prints thread info from a list */
void print_threads_info(osRtxThread_t *threads)
{
while(threads != NULL) {
print_thread( threads );
threads = threads->thread_next;
}
}
/* Prints info of a thread(using osRtxThread_t struct)*/
void print_thread(osRtxThread_t *thread)
{
uint32_t data[5];
data[0]=thread->state;
data[1]=thread->thread_addr;
data[2]=thread->stack_size;
data[3]=(uint32_t)thread->stack_mem;
data[4]=thread->sp;
fault_print_str("\nState: % EntryFn: % Stack Size: % Mem: % SP: %", data);
}
/* Initializes std uart for spitting the info out */
void fault_print_init()
{
#if DEVICE_SERIAL
if (!stdio_uart_inited) {
serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
}
#endif
}
/* Limited print functionality which prints the string out to
stdout/uart without using stdlib by directly calling serial-api
and also uses less resources
The fmtstr contains the format string for printing and for every %
found in that it fetches a uint32 value from values buffer
and prints it in hex format.
*/
void fault_print_str(char *fmtstr, uint32_t *values)
{
#if DEVICE_SERIAL
int i = 0;
int idx = 0;
int vidx = 0;
char hex_str[9]={0};
while(fmtstr[i] != '\0') {
if(fmtstr[i]=='%') {
hex_to_str(values[vidx++],hex_str);
for(idx=7; idx>=0; idx--) {
serial_putc(&stdio_uart, hex_str[idx]);
}
} else {
if (fmtstr[i] == '\n') {
serial_putc(&stdio_uart, '\r');
}
serial_putc(&stdio_uart, fmtstr[i]);
}
i++;
}
#endif
}
/* Converts a uint32 to hex char string */
void hex_to_str(uint32_t value, char *hex_str)
{
char hex_char_map[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int i = 0;
//Return without converting if hex_str is not provided
if(hex_str == NULL) return;
for(i=7; i>=0; i--) {
hex_str[i] = hex_char_map[(value & (0xf << (i * 4))) >> (i * 4)];
fault_print("\nStack: MSP", NULL);
}
}

View File

@ -13,27 +13,30 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_RTX_FAULT_HANDLER_H
#define MBED_RTX_FAULT_HANDLER_H
//Fault context struct
//WARNING: DO NOT CHANGE THIS STRUCT WITHOUT MAKING CORRESPONDING CHANGES in except.S files.
//Offset of these registers are used by fault handler in except.S
typedef struct {
uint32_t R0;
uint32_t R1;
uint32_t R2;
uint32_t R3;
uint32_t R4;
uint32_t R5;
uint32_t R6;
uint32_t R7;
uint32_t R8;
uint32_t R9;
uint32_t R10;
uint32_t R11;
uint32_t R12;
uint32_t SP;
uint32_t LR;
uint32_t PC;
uint32_t R0_reg;
uint32_t R1_reg;
uint32_t R2_reg;
uint32_t R3_reg;
uint32_t R4_reg;
uint32_t R5_reg;
uint32_t R6_reg;
uint32_t R7_reg;
uint32_t R8_reg;
uint32_t R9_reg;
uint32_t R10_reg;
uint32_t R11_reg;
uint32_t R12_reg;
uint32_t SP_reg;
uint32_t LR_reg;
uint32_t PC_reg;
uint32_t xPSR;
uint32_t PSP;
uint32_t MSP;
@ -52,3 +55,4 @@ typedef struct {
//This runs in fault context and uses special functions(defined in mbed_rtx_fault_handler.c) to print the information without using C-lib support.
__NO_RETURN void mbed_fault_handler (uint32_t fault_type, void *mbed_fault_context_in, void *osRtxInfoIn);
#endif

View File

@ -323,7 +323,7 @@ void mbed_start_main(void)
_main_thread_attr.name = "main_thread";
osThreadId_t result = osThreadNew((osThreadFunc_t)pre_main, NULL, &_main_thread_attr);
if ((void *)result == NULL) {
error("Pre main thread not created");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_INITIALIZATION_FAILED), "Pre main thread not created", &_main_thread_attr);
}
osKernelStart();

View File

@ -19,6 +19,7 @@
#include "rtx_evr.h"
#include "mbed_rtx.h"
#include "mbed_error.h"
#include "mbed_interface.h"
#include "RTX_Config.h"
#ifdef RTE_Compiler_EventRecorder
@ -46,31 +47,27 @@ __NO_RETURN uint32_t osRtxErrorNotify (uint32_t code, void *object_id)
case osRtxErrorStackUnderflow:
// Stack underflow detected for thread (thread_id=object_id)
// Note: "overflow" is printed instead of "underflow" due to end user familiarity with overflow errors
error("CMSIS-RTOS error: Stack overflow (status: 0x%X, task ID: 0x%X, task name: %s)\n\r",
code, object_id, osThreadGetName(object_id));
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_STACK_OVERFLOW), "CMSIS-RTOS error: Stack overflow", code);
break;
case osRtxErrorISRQueueOverflow:
// ISR Queue overflow detected when inserting object (object_id)
error("CMSIS-RTOS error: ISR Queue overflow (status: 0x%X, task ID: 0x%X, object ID: 0x%X)\n\r",
code, tid, object_id);
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_ISR_QUEUE_OVERFLOW), "CMSIS-RTOS error: ISR Queue overflow", code);
break;
case osRtxErrorTimerQueueOverflow:
// User Timer Callback Queue overflow detected for timer (timer_id=object_id)
error("CMSIS-RTOS error: User Timer Callback Queue overflow (status: 0x%X, task ID: 0x%X, timer ID: 0x%X)\n\r",
code, tid, object_id);
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_TIMER_QUEUE_OVERFLOW), "CMSIS-RTOS error: User Timer Callback Queue overflow", code);
break;
case osRtxErrorClibSpace:
// Standard C/C++ library libspace not available: increase OS_THREAD_LIBSPACE_NUM
error("CMSIS-RTOS error: STD C/C++ library libspace not available (status: 0x%X, task ID: 0x%X)\n\r",
code, tid);
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_CLIB_SPACE_UNAVAILABLE), "CMSIS-RTOS error: STD C/C++ library libspace not available", code);
break;
case osRtxErrorClibMutex:
// Standard C/C++ library mutex initialization failed
error("CMSIS-RTOS error: STD C/C++ library mutex initialization failed (status: 0x%X, task ID: 0x%X)\n\r",
code, tid);
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_CLIB_MUTEX_INIT_FAILURE), "CMSIS-RTOS error: STD C/C++ library mutex initialization failed", code);
break;
default:
error("CMSIS-RTOS error: Unknown (status: 0x%X, task ID: 0x%X)\n\r", code, tid);
//Unknown error flagged from kernel
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_UNKNOWN), "CMSIS-RTOS error: Unknown", code);
break;
}
@ -102,27 +99,27 @@ static const char* error_msg(int32_t status)
void EvrRtxKernelError (int32_t status)
{
error("Kernel error %i: %s\r\n", status, error_msg(status));
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_EVENT), error_msg(status), status);
}
void EvrRtxThreadError (osThreadId_t thread_id, int32_t status)
{
error("Thread %p error %i: %s\r\n", thread_id, status, error_msg(status));
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_THREAD_EVENT), error_msg(status), thread_id);
}
void EvrRtxTimerError (osTimerId_t timer_id, int32_t status)
{
error("Timer %p error %i: %s\r\n", timer_id, status, error_msg(status));
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_TIMER_EVENT), error_msg(status), timer_id);
}
void EvrRtxEventFlagsError (osEventFlagsId_t ef_id, int32_t status)
{
error("Event %p error %i: %s\r\n", ef_id, status, error_msg(status));
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_EVENT_FLAGS_EVENT), error_msg(status), ef_id);
}
void EvrRtxMutexError (osMutexId_t mutex_id, int32_t status)
{
error("Mutex %p error %i: %s\r\n", mutex_id, status, error_msg(status));
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_MUTEX_EVENT), error_msg(status), mutex_id);
}
void EvrRtxSemaphoreError (osSemaphoreId_t semaphore_id, int32_t status)
@ -132,17 +129,17 @@ void EvrRtxSemaphoreError (osSemaphoreId_t semaphore_id, int32_t status)
return;
}
error("Semaphore %p error %i\r\n", semaphore_id, status);
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_SEMAPHORE_EVENT), error_msg(status), semaphore_id);
}
void EvrRtxMemoryPoolError (osMemoryPoolId_t mp_id, int32_t status)
{
error("Memory Pool %p error %i\r\n", mp_id, status);
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_MEMORY_POOL_EVENT), error_msg(status), mp_id);
}
void EvrRtxMessageQueueError (osMessageQueueId_t mq_id, int32_t status)
{
error("Message Queue %p error %i\r\n", mq_id, status);
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_RTOS_MESSAGE_QUEUE_EVENT), error_msg(status), mq_id);
}
#endif

View File

@ -78,13 +78,13 @@ void Thread::constructor(Callback<void()> task,
switch (start(task)) {
case osErrorResource:
error("OS ran out of threads!\n");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_RESOURCES), "OS ran out of threads!\n", task);
break;
case osErrorParameter:
error("Thread already running!\n");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_ALREADY_IN_USE), "Thread already running!\n", task);
break;
case osErrorNoMemory:
error("Error allocating the stack memory\n");
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Error allocating the stack memory\n", task);
default:
break;
}