Skip to content

Commit baa44eb

Browse files
committed
Limit error filename capture to 64 chars, wrapping tests with right configs and astyle fixes.
1 parent dcdd616 commit baa44eb

File tree

5 files changed

+59
-54
lines changed

5 files changed

+59
-54
lines changed

TESTS/mbed_platform/error_handling/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ void test_error_hook()
261261
TEST_ASSERT(sem_status > 0);
262262
}
263263

264-
#ifdef MBED_TEST_SIM_BLOCKDEVICE
264+
#if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED && defined(MBED_TEST_SIM_BLOCKDEVICE)
265265

266266
// test configuration
267267
#ifndef MBED_TEST_FILESYSTEM
@@ -356,7 +356,7 @@ Case cases[] = {
356356
#if MBED_CONF_RTOS_PRESENT
357357
Case("Test error handling multi-threaded", test_error_logging_multithread),
358358
#endif //MBED_CONF_RTOS_PRESENT
359-
#ifdef MBED_TEST_SIM_BLOCKDEVICE
359+
#if MBED_CONF_PLATFORM_ERROR_HIST_ENABLED && defined(MBED_TEST_SIM_BLOCKDEVICE)
360360
Case("Test error save log", test_save_error_log),
361361
#endif //MBED_TEST_SIM_BLOCKDEVICE
362362
#endif //MBED_CONF_ERROR_HIST_DISABLED

platform/mbed_error.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#define GET_CURRENT_SP(sp) \
3434
{ \
3535
/*If in Handler mode we are always using MSP*/ \
36-
if( __get_IPSR() != 0U ) { \
36+
if ( __get_IPSR() != 0U ) { \
3737
sp = __get_MSP(); \
3838
} else { \
3939
/*Look into CONTROL.SPSEL value*/ \
@@ -95,7 +95,7 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
9595
mbed_error_ctx current_error_ctx;
9696

9797
//Error status should always be < 0
98-
if(error_status >= 0) {
98+
if (error_status >= 0) {
9999
//This is a weird situation, someone called mbed_error with invalid error code.
100100
//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
101101
error_status = MBED_ERROR_INVALID_ARGUMENT;
@@ -141,7 +141,7 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
141141
#endif
142142

143143
//Capture the fist system error and store it
144-
if(error_count == 1) { //first error
144+
if (error_count == 1) { //first error
145145
memcpy(&first_error_ctx, &current_error_ctx, sizeof(mbed_error_ctx));
146146
}
147147

@@ -154,7 +154,7 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
154154
#endif
155155

156156
//Call the error hook if available
157-
if(error_hook != NULL) {
157+
if (error_hook != NULL) {
158158
error_hook(&last_error_ctx);
159159
}
160160

@@ -194,7 +194,7 @@ mbed_error_status_t mbed_warning(mbed_error_status_t error_status, const char *e
194194
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)
195195
{
196196
//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))
198198
return MBED_ERROR_FAILED_OPERATION;
199199

200200
//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
208208
mbed_error_status_t mbed_set_error_hook(mbed_error_hook_t error_hook_in)
209209
{
210210
//register the new hook/callback
211-
if( error_hook_in != NULL ) {
211+
if ( error_hook_in != NULL ) {
212212
error_hook = error_hook_in;
213213
return MBED_SUCCESS;
214214
}
@@ -236,17 +236,17 @@ mbed_error_status_t mbed_make_error(mbed_error_type_t error_type, mbed_module_ty
236236
switch(error_type)
237237
{
238238
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)
240240
return -error_code;
241241
break;
242242

243243
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)
245245
return MAKE_MBED_ERROR(MBED_ERROR_TYPE_SYSTEM, entity, error_code);
246246
break;
247247

248248
case MBED_ERROR_TYPE_CUSTOM:
249-
if(error_code >= MBED_CUSTOM_ERROR_BASE)
249+
if (error_code >= MBED_CUSTOM_ERROR_BASE)
250250
return MAKE_MBED_ERROR(MBED_ERROR_TYPE_CUSTOM, entity, error_code);
251251
break;
252252

@@ -291,7 +291,7 @@ static void print_thread(osRtxThread_t *thread)
291291
/* Prints thread info from a list */
292292
static void print_threads_info(osRtxThread_t *threads)
293293
{
294-
while(threads != NULL) {
294+
while (threads != NULL) {
295295
print_thread( threads );
296296
threads = threads->thread_next;
297297
}
@@ -347,7 +347,7 @@ static void print_error_report(mbed_error_ctx *ctx, const char *error_msg)
347347
mbed_error_printf("\nLocation: 0x%X", ctx->error_address);
348348

349349
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED && !defined(NDEBUG)
350-
if((NULL != ctx->error_filename[0]) && (ctx->error_line_number != 0)) {
350+
if ((NULL != ctx->error_filename[0]) && (ctx->error_line_number != 0)) {
351351
//for string, we must pass address of a ptr which has the address of the string
352352
mbed_error_printf("\nFile:%s+%d", ctx->error_filename, ctx->error_line_number);
353353
}
@@ -404,19 +404,19 @@ mbed_error_status_t mbed_save_error_hist(const char *path)
404404
FILE *error_log_file = NULL;
405405

406406
//Ensure path is valid
407-
if(path==NULL) {
407+
if (path==NULL) {
408408
ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_INVALID_ARGUMENT);
409409
goto exit;
410410
}
411411

412412
//Open the file for saving the error log info
413-
if((error_log_file = fopen( path, "w" ) ) == NULL){
413+
if ((error_log_file = fopen( path, "w" )) == NULL){
414414
ret = MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED);
415415
goto exit;
416416
}
417417

418418
//First store the first and last errors
419-
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",
420420
(unsigned int)first_error_ctx.error_status,
421421
(unsigned int)first_error_ctx.thread_id,
422422
(unsigned int)first_error_ctx.error_address,
@@ -425,7 +425,7 @@ mbed_error_status_t mbed_save_error_hist(const char *path)
425425
goto exit;
426426
}
427427

428-
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",
429429
(unsigned int)last_error_ctx.error_status,
430430
(unsigned int)last_error_ctx.thread_id,
431431
(unsigned int)last_error_ctx.error_address,
@@ -435,10 +435,10 @@ mbed_error_status_t mbed_save_error_hist(const char *path)
435435
}
436436

437437
//Update with error log info
438-
while(--log_count >= 0) {
438+
while (--log_count >= 0) {
439439
mbed_error_hist_get(log_count, &ctx);
440440
//first line of file will be error log count
441-
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",
442442
log_count,
443443
(unsigned int)ctx.error_status,
444444
(unsigned int)ctx.thread_id,

platform/mbed_error.h

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ extern "C" {
3939

4040
#ifndef MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN
4141
#define MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN 16
42+
#else //MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN
43+
#if MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN > 64
44+
//We have to limit this to 64 bytes since we use mbed_error_printf for error reporting
45+
//and mbed_error_vfprintf uses 128bytes internal buffer which may not be sufficient for anything
46+
//longer that 64 bytes with the current implementation.
47+
#error "Unsupported error filename buffer length detected, max supported length is 64 chars. Please change MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN or max-error-filename-len in configuration."
48+
#endif
4249
#endif
4350

4451
#define MBED_ERROR_STATUS_CODE_MASK (0x0000FFFF)
@@ -144,16 +151,16 @@ typedef int mbed_error_status_t;
144151
*
145152
*/
146153
#ifdef NDEBUG
147-
#define MBED_WARNING1( error_status, error_msg, error_value ) mbed_warning( error_status, (const char *)NULL, (uint32_t)error_value, NULL, 0 )
148-
#define MBED_WARNING( error_status, error_msg ) mbed_warning( error_status, (const char *)NULL, (uint32_t)0, NULL, 0 )
149-
#else
150-
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
151-
#define MBED_WARNING1( error_status, error_msg, error_value ) mbed_warning( error_status, (const char *)error_msg, (uint32_t)error_value, (const char *)MBED_FILENAME, __LINE__ )
152-
#define MBED_WARNING( error_status, error_msg ) mbed_warning( error_status, (const char *)error_msg, (uint32_t)0 , (const char *)MBED_FILENAME, __LINE__ )
153-
#else
154-
#define MBED_WARNING1( error_status, error_msg, error_value ) mbed_warning( error_status, (const char *)error_msg, (uint32_t)error_value, NULL, 0 )
155-
#define MBED_WARNING( error_status, error_msg ) mbed_warning( error_status, (const char *)error_msg, (uint32_t)0, NULL, 0 )
156-
#endif
154+
#define MBED_WARNING1( error_status, error_msg, error_value ) mbed_warning( error_status, (const char *)NULL, (uint32_t)error_value, NULL, 0 )
155+
#define MBED_WARNING( error_status, error_msg ) mbed_warning( error_status, (const char *)NULL, (uint32_t)0, NULL, 0 )
156+
#else //NDEBUG
157+
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
158+
#define MBED_WARNING1( error_status, error_msg, error_value ) mbed_warning( error_status, (const char *)error_msg, (uint32_t)error_value, (const char *)MBED_FILENAME, __LINE__ )
159+
#define MBED_WARNING( error_status, error_msg ) mbed_warning( error_status, (const char *)error_msg, (uint32_t)0 , (const char *)MBED_FILENAME, __LINE__ )
160+
#else //MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
161+
#define MBED_WARNING1( error_status, error_msg, error_value ) mbed_warning( error_status, (const char *)error_msg, (uint32_t)error_value, NULL, 0 )
162+
#define MBED_WARNING( error_status, error_msg ) mbed_warning( error_status, (const char *)error_msg, (uint32_t)0, NULL, 0 )
163+
#endif
157164
#endif
158165

159166
/**
@@ -176,16 +183,16 @@ typedef int mbed_error_status_t;
176183
*
177184
*/
178185
#ifdef NDEBUG
179-
#define MBED_ERROR1( error_status, error_msg, error_value ) mbed_error( error_status, (const char *)NULL, (uint32_t)error_value, NULL, 0 )
180-
#define MBED_ERROR( error_status, error_msg ) mbed_error( error_status, (const char *)NULL, (uint32_t)0 , NULL, 0 )
181-
#else
182-
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
183-
#define MBED_ERROR1( error_status, error_msg, error_value ) mbed_error( error_status, (const char *)error_msg, (uint32_t)error_value, (const char *)MBED_FILENAME, __LINE__ )
184-
#define MBED_ERROR( error_status, error_msg ) mbed_error( error_status, (const char *)error_msg, (uint32_t)0 , (const char *)MBED_FILENAME, __LINE__ )
185-
#else
186-
#define MBED_ERROR1( error_status, error_msg, error_value ) mbed_error( error_status, (const char *)error_msg, (uint32_t)error_value, NULL, 0 )
187-
#define MBED_ERROR( error_status, error_msg ) mbed_error( error_status, (const char *)error_msg, (uint32_t)0 , NULL, 0 )
188-
#endif
186+
#define MBED_ERROR1( error_status, error_msg, error_value ) mbed_error( error_status, (const char *)NULL, (uint32_t)error_value, NULL, 0 )
187+
#define MBED_ERROR( error_status, error_msg ) mbed_error( error_status, (const char *)NULL, (uint32_t)0 , NULL, 0 )
188+
#else //NDEBUG
189+
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
190+
#define MBED_ERROR1( error_status, error_msg, error_value ) mbed_error( error_status, (const char *)error_msg, (uint32_t)error_value, (const char *)MBED_FILENAME, __LINE__ )
191+
#define MBED_ERROR( error_status, error_msg ) mbed_error( error_status, (const char *)error_msg, (uint32_t)0 , (const char *)MBED_FILENAME, __LINE__ )
192+
#else //MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
193+
#define MBED_ERROR1( error_status, error_msg, error_value ) mbed_error( error_status, (const char *)error_msg, (uint32_t)error_value, NULL, 0 )
194+
#define MBED_ERROR( error_status, error_msg ) mbed_error( error_status, (const char *)error_msg, (uint32_t)0 , NULL, 0 )
195+
#endif
189196
#endif
190197

191198
//Error Type definition
@@ -253,8 +260,7 @@ typedef enum _mbed_error_type_t
253260
\endverbatim
254261
*
255262
*/
256-
typedef enum _mbed_module_type
257-
{
263+
typedef enum _mbed_module_type {
258264
MBED_MODULE_APPLICATION = 0,
259265
MBED_MODULE_PLATFORM,
260266
MBED_MODULE_KERNEL,
@@ -566,8 +572,7 @@ typedef enum _mbed_module_type
566572
\endverbatim
567573
*/
568574

569-
typedef enum _mbed_error_code
570-
{
575+
typedef enum _mbed_error_code {
571576
//Below are POSIX ERROR CODE definitions, which starts at MBED_POSIX_ERROR_BASE(=0)
572577
//POSIX ERROR CODE definitions starts at offset 0(MBED_POSIX_ERROR_BASE) to align them with actual Posix Error Code
573578
//defintions in mbed_retarget.h

platform/mbed_error_hist.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ static int error_log_count = -1;
3030
mbed_error_status_t mbed_error_hist_put(mbed_error_ctx *error_ctx)
3131
{
3232
//Return error if error_ctx is NULL
33-
if(NULL == error_ctx) {
33+
if (NULL == error_ctx) {
3434
return MBED_ERROR_INVALID_ARGUMENT;
3535
}
3636

3737
core_util_critical_section_enter();
3838
error_log_count++;
39-
memcpy(&mbed_error_ctx_log[error_log_count % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], error_ctx, sizeof(mbed_error_ctx) );
39+
memcpy(&mbed_error_ctx_log[error_log_count % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], error_ctx, sizeof(mbed_error_ctx));
4040
core_util_critical_section_exit();
4141

4242
return MBED_SUCCESS;
@@ -45,17 +45,17 @@ mbed_error_status_t mbed_error_hist_put(mbed_error_ctx *error_ctx)
4545
mbed_error_status_t mbed_error_hist_get(int index, mbed_error_ctx *error_ctx)
4646
{
4747
//Return error if index is more than max log size
48-
if(index >= MBED_CONF_PLATFORM_ERROR_HIST_SIZE) {
48+
if (index >= MBED_CONF_PLATFORM_ERROR_HIST_SIZE) {
4949
return MBED_ERROR_INVALID_ARGUMENT;
5050
}
5151

5252
core_util_critical_section_enter();
5353
//calculate the index where we want to pick the ctx
54-
if(error_log_count >= MBED_CONF_PLATFORM_ERROR_HIST_SIZE) {
54+
if (error_log_count >= MBED_CONF_PLATFORM_ERROR_HIST_SIZE) {
5555
index = (error_log_count + index + 1) % MBED_CONF_PLATFORM_ERROR_HIST_SIZE;
5656
}
5757
core_util_critical_section_exit();
58-
memcpy(error_ctx, &mbed_error_ctx_log[index % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], sizeof(mbed_error_ctx) );
58+
memcpy(error_ctx, &mbed_error_ctx_log[index % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], sizeof(mbed_error_ctx));
5959

6060
return MBED_SUCCESS;
6161
}
@@ -72,11 +72,11 @@ mbed_error_ctx *mbed_error_hist_get_entry(void)
7272

7373
mbed_error_status_t mbed_error_hist_get_last_error(mbed_error_ctx *error_ctx)
7474
{
75-
if(-1 == error_log_count) {
75+
if (-1 == error_log_count) {
7676
return MBED_ERROR_ITEM_NOT_FOUND;
7777
}
7878
core_util_critical_section_enter();
79-
memcpy(error_ctx, &mbed_error_ctx_log[error_log_count % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], sizeof(mbed_error_ctx) );
79+
memcpy(error_ctx, &mbed_error_ctx_log[error_log_count % MBED_CONF_PLATFORM_ERROR_HIST_SIZE], sizeof(mbed_error_ctx));
8080
core_util_critical_section_exit();
8181

8282
return MBED_SUCCESS;

rtos/TARGET_CORTEX/TARGET_CORTEX_M/mbed_rtx_fault_handler.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ MBED_NOINLINE void print_context_info(void)
9999
,SCB->HFSR, (0xFF & SCB->CFSR), ((0xFF00 & SCB->CFSR) >> 8), ((0xFFFF0000 & SCB->CFSR) >> 16), SCB->DFSR, SCB->AFSR );
100100

101101
//Print MMFAR only if its valid as indicated by MMFSR
102-
if((0xFF & SCB->CFSR) & 0x80) {
102+
if ((0xFF & SCB->CFSR) & 0x80) {
103103
mbed_error_printf("\nMMFAR: %08X",SCB->MMFAR);
104104
}
105105
//Print BFAR only if its valid as indicated by BFSR
106-
if(((0xFF00 & SCB->CFSR) >> 8) & 0x80) {
106+
if (((0xFF00 & SCB->CFSR) >> 8) & 0x80) {
107107
mbed_error_printf("\nBFAR : %08X",SCB->BFAR);
108108
}
109109
#endif
110110

111111
//Print Mode
112-
if(mbed_fault_context.EXC_RETURN & 0x8) {
112+
if (mbed_fault_context.EXC_RETURN & 0x8) {
113113
mbed_error_printf("\nMode : Thread");
114114
//Print Priv level in Thread mode - We capture CONTROL reg which reflects the privilege.
115115
//Note that the CONTROL register captured still reflects the privilege status of the
@@ -124,7 +124,7 @@ MBED_NOINLINE void print_context_info(void)
124124
mbed_error_printf("\nPriv : Privileged");
125125
}
126126
//Print Return Stack
127-
if(mbed_fault_context.EXC_RETURN & 0x4) {
127+
if (mbed_fault_context.EXC_RETURN & 0x4) {
128128
mbed_error_printf("\nStack: PSP");
129129
} else {
130130
mbed_error_printf("\nStack: MSP");

0 commit comments

Comments
 (0)