Skip to content

Commit 50cd664

Browse files
author
Cruz Monrreal
authored
Merge pull request #7050 from SenRamakri/sen_ErrorHandlingFilenameFix
Fix for filename capture not working
2 parents abed3df + 576bd61 commit 50cd664

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

platform/mbed_error.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static mbed_error_ctx first_error_ctx = {0};
5151
static mbed_error_ctx last_error_ctx = {0};
5252
static mbed_error_hook_t error_hook = NULL;
5353
static void print_error_report(mbed_error_ctx *ctx, const char *);
54+
static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsigned int error_value, const char *filename, int line_number);
5455

5556
//Helper function to halt the system
5657
static void mbed_halt_system(void)
@@ -72,20 +73,23 @@ WEAK void error(const char* format, ...) {
7273
if (error_in_progress) {
7374
return;
7475
}
76+
77+
//Call handle_error/print_error_report permanently setting error_in_progress flag
78+
handle_error(MBED_ERROR_UNKNOWN, 0, NULL, 0);
79+
print_error_report(&last_error_ctx, "Fatal Run-time error");
7580
error_in_progress = 1;
7681

7782
#ifndef NDEBUG
7883
va_list arg;
7984
va_start(arg, format);
8085
mbed_error_vfprintf(format, arg);
81-
MBED_ERROR(MBED_ERROR_UNKNOWN, "Fatal Run-time Error");
8286
va_end(arg);
8387
#endif
8488
exit(1);
8589
}
8690

8791
//Set an error status with the error handling system
88-
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)
92+
static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsigned int error_value, const char *filename, int line_number)
8993
{
9094
mbed_error_ctx current_error_ctx;
9195

@@ -129,16 +133,10 @@ mbed_error_status_t handle_error(mbed_error_status_t error_status, const char *e
129133

130134
#ifdef MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED
131135
//Capture filename/linenumber if provided
132-
//Index for tracking error_filename
133-
int idx = 0;
134-
135-
if(NULL != filename) {
136-
while(idx < MBED_CONF_MAX_ERROR_FILENAME_LEN && (filename[idx] != '\0')) {
137-
current_error_ctx.error_filename[idx] = filename[idx];
138-
idx++;
139-
}
140-
current_error_ctx.error_line_number = line_number;
141-
}
136+
//Index for tracking error_filename
137+
memset(&current_error_ctx.error_filename, 0, MBED_CONF_MAX_ERROR_FILENAME_LEN);
138+
strncpy(current_error_ctx.error_filename, filename, MBED_CONF_MAX_ERROR_FILENAME_LEN);
139+
current_error_ctx.error_line_number = line_number;
142140
#endif
143141

144142
//Capture the fist system error and store it
@@ -188,14 +186,14 @@ int mbed_get_error_count(void)
188186
//Sets a fatal error
189187
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)
190188
{
191-
return handle_error(error_status, error_msg, error_value, filename, line_number);
189+
return handle_error(error_status, error_value, filename, line_number);
192190
}
193191

194192
//Sets a fatal error
195193
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)
196194
{
197195
//set the error reported and then halt the system
198-
if( MBED_SUCCESS != handle_error(error_status, error_msg, error_value, filename, line_number) )
196+
if( MBED_SUCCESS != handle_error(error_status, error_value, filename, line_number) )
199197
return MBED_ERROR_FAILED_OPERATION;
200198

201199
//On fatal errors print the error context/report
@@ -359,7 +357,7 @@ static void print_error_report(mbed_error_ctx *ctx, const char *error_msg)
359357
uint32_t error_code = MBED_GET_ERROR_CODE(ctx->error_status);
360358
uint32_t error_module = MBED_GET_ERROR_MODULE(ctx->error_status);
361359

362-
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);
360+
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);
363361

364362
//Report error info based on error code, some errors require different
365363
//error_vals[1] contains the error code
@@ -410,12 +408,10 @@ static void print_error_report(mbed_error_ctx *ctx, const char *error_msg)
410408
}
411409
mbed_error_printf(error_msg, NULL);
412410
mbed_error_printf("\nLocation: 0x%x", ctx->error_address);
413-
#ifdef MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED
414-
if(NULL != error_ctx->error_filename) {
411+
#if defined(MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED) && !defined(NDEBUG)
412+
if(NULL != ctx->error_filename) {
415413
//for string, we must pass address of a ptr which has the address of the string
416-
uint32_t *file_name = (uint32_t *)&error_ctx->error_filename[0];
417-
mbed_error_printf("\nFile:%s", &file_name);
418-
mbed_error_printf("+0x%x", ctx->error_line_number);
414+
mbed_error_printf("\nFile:%s+%d", ctx->error_filename, ctx->error_line_number);
419415
}
420416
#endif
421417

@@ -429,7 +425,7 @@ static void print_error_report(mbed_error_ctx *ctx, const char *error_msg)
429425
#endif //TARGET_CORTEX_M
430426
}
431427

432-
mbed_error_printf("\n-- MbedOS Error Info --");
428+
mbed_error_printf("\n-- MbedOS Error Info --\n");
433429
}
434430

435431

platform/mbed_error.h

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,17 @@ typedef int mbed_error_status_t;
143143
* Since this macro is a wrapper for mbed_warning API callers should process the return value from this macro which is the return value from calling mbed_error API.
144144
*
145145
*/
146-
#ifdef MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED
147-
#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__ )
148-
#define MBED_WARNING( error_status, error_msg ) mbed_warning( error_status, (const char *)error_msg, (uint32_t)0 , (const char *)MBED_FILENAME, __LINE__ )
146+
#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 )
149149
#else
150-
#define MBED_WARNING1( error_status, error_msg, error_value ) mbed_warning( error_status, (const char *)error_msg, (uint32_t)error_value, NULL, 0 )
151-
#define MBED_WARNING( error_status, error_msg ) mbed_warning( error_status, (const char *)error_msg, (uint32_t)0, NULL, 0 )
150+
#if defined(MBED_CONF_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
152157
#endif
153158

154159
/**
@@ -170,13 +175,18 @@ typedef int mbed_error_status_t;
170175
* Since this macro is a wrapper for mbed_error API callers should process the return value from this macro which is the return value from calling mbed_error API.
171176
*
172177
*/
173-
#ifdef MBED_CONF_ERROR_FILENAME_CAPTURE_ENABLED
174-
#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__ )
175-
#define MBED_ERROR( error_status, error_msg ) mbed_error( error_status, (const char *)error_msg, (uint32_t)0 , (const char *)MBED_FILENAME, __LINE__ )
178+
#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 )
176181
#else
177-
#define MBED_ERROR1( error_status, error_msg, error_value ) mbed_error( error_status, (const char *)error_msg, (uint32_t)error_value, NULL, 0 )
178-
#define MBED_ERROR( error_status, error_msg ) mbed_error( error_status, (const char *)error_msg, (uint32_t)0 , NULL, 0 )
179-
#endif
182+
#if defined(MBED_CONF_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
189+
#endif
180190

181191
//Error Type definition
182192
/** mbed_error_type_t definition

0 commit comments

Comments
 (0)