Skip to content

Commit 29e9619

Browse files
author
Cruz Monrreal
authored
Merge pull request #8441 from kjbracey-arm/error_puts
More mbed_error refinements
2 parents 69904ea + 500e37f commit 29e9619

File tree

3 files changed

+70
-63
lines changed

3 files changed

+70
-63
lines changed

platform/mbed_board.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,35 @@ void mbed_error_printf(const char *format, ...)
5656

5757
void mbed_error_vprintf(const char *format, va_list arg)
5858
{
59-
core_util_critical_section_enter();
6059
char buffer[132];
6160
int size = vsnprintf(buffer, sizeof buffer, format, arg);
6261
if (size >= sizeof buffer) {
63-
/* Output was truncated - indicate by overwriting last 4 bytes of buffer
64-
* with ellipsis and newline.
65-
* (Note that although vsnprintf always leaves a NUL terminator, we
66-
* don't need a terminator and can use the entire buffer)
62+
/* Output was truncated - indicate by overwriting tail of buffer
63+
* with ellipsis, newline and null terminator.
6764
*/
68-
memcpy(&buffer[sizeof buffer - 4], "...\n", 4);
69-
size = sizeof buffer;
65+
static const char ellipsis[] = "...\n";
66+
memcpy(&buffer[sizeof buffer - sizeof ellipsis], ellipsis, sizeof ellipsis);
67+
}
68+
if (size > 0) {
69+
mbed_error_puts(buffer);
7070
}
71+
}
72+
73+
void mbed_error_puts(const char *str)
74+
{
75+
core_util_critical_section_enter();
7176
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES || MBED_CONF_PLATFORM_STDIO_CONVERT_TTY_NEWLINES
7277
char stdio_out_prev = '\0';
73-
for (int i = 0; i < size; i++) {
74-
if (buffer[i] == '\n' && stdio_out_prev != '\r') {
78+
for (; *str != '\0'; str++) {
79+
if (*str == '\n' && stdio_out_prev != '\r') {
7580
const char cr = '\r';
7681
write(STDERR_FILENO, &cr, 1);
7782
}
78-
write(STDERR_FILENO, &buffer[i], 1);
79-
stdio_out_prev = buffer[i];
83+
write(STDERR_FILENO, str, 1);
84+
stdio_out_prev = *str;
8085
}
8186
#else
82-
write(STDERR_FILENO, buffer, size);
87+
write(STDERR_FILENO, str, strlen(str));
8388
#endif
8489
core_util_critical_section_exit();
8590
}

platform/mbed_error.c

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,18 @@
2929
#include <stdio.h>
3030
#endif
3131

32-
//Helper macro to get the current SP
33-
#define GET_CURRENT_SP(sp) \
34-
{ \
35-
/*If in Handler mode we are always using MSP*/ \
36-
if ( __get_IPSR() != 0U ) { \
37-
sp = __get_MSP(); \
38-
} else { \
39-
/*Look into CONTROL.SPSEL value*/ \
40-
if ((__get_CONTROL() & 2U) == 0U) { \
41-
sp = __get_MSP();/*Read MSP*/ \
42-
} else { \
43-
sp = __get_PSP();/*Read PSP*/ \
44-
} \
45-
} \
46-
}
47-
4832
#ifndef NDEBUG
49-
#define ERROR_REPORT(ctx, error_msg) print_error_report(ctx, error_msg)
33+
#define ERROR_REPORT(ctx, error_msg, error_filename, error_line) print_error_report(ctx, error_msg, error_filename, error_line)
34+
static void print_error_report(const mbed_error_ctx *ctx, const char *, const char *error_filename, int error_line);
5035
#else
51-
#define ERROR_REPORT(ctx, error_msg) ((void) 0)
36+
#define ERROR_REPORT(ctx, error_msg, error_filename, error_line) ((void) 0)
5237
#endif
5338

5439
static uint8_t error_in_progress = 0;
5540
static int error_count = 0;
5641
static mbed_error_ctx first_error_ctx = {0};
5742
static mbed_error_ctx last_error_ctx = {0};
5843
static mbed_error_hook_t error_hook = NULL;
59-
static void print_error_report(mbed_error_ctx *ctx, const char *);
6044
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);
6145

6246
//Helper function to halt the system
@@ -83,7 +67,7 @@ WEAK void error(const char *format, ...)
8367

8468
//Call handle_error/print_error_report permanently setting error_in_progress flag
8569
handle_error(MBED_ERROR_UNKNOWN, 0, NULL, 0, MBED_CALLER_ADDR());
86-
ERROR_REPORT(&last_error_ctx, "Fatal Run-time error");
70+
ERROR_REPORT(&last_error_ctx, "Fatal Run-time error", NULL, 0);
8771
error_in_progress = 1;
8872

8973
#ifndef NDEBUG
@@ -132,16 +116,12 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
132116
current_error_ctx.thread_entry_address = (uint32_t)current_thread->thread_addr;
133117
current_error_ctx.thread_stack_size = current_thread->stack_size;
134118
current_error_ctx.thread_stack_mem = (uint32_t)current_thread->stack_mem;
135-
#ifdef TARGET_CORTEX_M
136-
GET_CURRENT_SP(current_error_ctx.thread_current_sp);
137-
#endif //TARGET_CORTEX_M
138-
119+
current_error_ctx.thread_current_sp = (uint32_t)&current_error_ctx; // Address local variable to get a stack pointer
139120
#endif //MBED_CONF_RTOS_PRESENT
140121

141122
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
142123
//Capture filename/linenumber if provided
143124
//Index for tracking error_filename
144-
memset(&current_error_ctx.error_filename, 0, MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN);
145125
strncpy(current_error_ctx.error_filename, filename, MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN);
146126
current_error_ctx.error_line_number = line_number;
147127
#endif
@@ -205,7 +185,7 @@ WEAK mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char
205185
}
206186

207187
//On fatal errors print the error context/report
208-
ERROR_REPORT(&last_error_ctx, error_msg);
188+
ERROR_REPORT(&last_error_ctx, error_msg, filename, line_number);
209189
mbed_halt_system();
210190

211191
return MBED_ERROR_FAILED_OPERATION;
@@ -290,15 +270,20 @@ mbed_error_status_t mbed_clear_all_errors(void)
290270
return status;
291271
}
292272

273+
static const char *name_or_unnamed(const char *name)
274+
{
275+
return name ? name : "<unnamed>";
276+
}
277+
293278
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
294279
/* Prints info of a thread(using osRtxThread_t struct)*/
295-
static void print_thread(osRtxThread_t *thread)
280+
static void print_thread(const osRtxThread_t *thread)
296281
{
297-
mbed_error_printf("\nState: 0x%08X Entry: 0x%08X Stack Size: 0x%08X Mem: 0x%08X SP: 0x%08X", thread->state, thread->thread_addr, thread->stack_size, (uint32_t)thread->stack_mem, thread->sp);
282+
mbed_error_printf("\n%s State: 0x%X Entry: 0x%08X Stack Size: 0x%08X Mem: 0x%08X SP: 0x%08X", name_or_unnamed(thread->name), thread->state, thread->thread_addr, thread->stack_size, (uint32_t)thread->stack_mem, thread->sp);
298283
}
299284

300285
/* Prints thread info from a list */
301-
static void print_threads_info(osRtxThread_t *threads)
286+
static void print_threads_info(const osRtxThread_t *threads)
302287
{
303288
while (threads != NULL) {
304289
print_thread(threads);
@@ -308,7 +293,7 @@ static void print_threads_info(osRtxThread_t *threads)
308293
#endif
309294

310295
#ifndef NDEBUG
311-
static void print_error_report(mbed_error_ctx *ctx, const char *error_msg)
296+
static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg, const char *error_filename, int error_line)
312297
{
313298
uint32_t error_code = MBED_GET_ERROR_CODE(ctx->error_status);
314299
uint32_t error_module = MBED_GET_ERROR_MODULE(ctx->error_status);
@@ -357,41 +342,41 @@ static void print_error_report(mbed_error_ctx *ctx, const char *error_msg)
357342
//Nothing to do here, just print the error info down
358343
break;
359344
}
360-
mbed_error_printf(error_msg);
345+
mbed_error_puts(error_msg);
361346
mbed_error_printf("\nLocation: 0x%X", ctx->error_address);
362347

363-
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED && !defined(NDEBUG)
364-
if ((NULL != ctx->error_filename[0]) && (ctx->error_line_number != 0)) {
365-
//for string, we must pass address of a ptr which has the address of the string
366-
mbed_error_printf("\nFile:%s+%d", ctx->error_filename, ctx->error_line_number);
348+
/* We print the filename passed in, not any filename in the context. This
349+
* avoids the console print for mbed_error being limited to the presence
350+
* and length of the filename storage. Note that although the MBED_ERROR
351+
* macro compiles out filenames unless platform.error-filename-capture-enabled
352+
* is turned on, MBED_ASSERT always passes filenames, and other direct
353+
* users of mbed_error() may also choose to.
354+
*/
355+
if (error_filename) {
356+
mbed_error_puts("\nFile: ");
357+
mbed_error_puts(error_filename);
358+
mbed_error_printf("+%d", error_line);
367359
}
368-
#endif
369360

370361
mbed_error_printf("\nError Value: 0x%X", ctx->error_value);
371-
#ifdef TARGET_CORTEX_M
372-
mbed_error_printf("\nCurrent Thread: Id: 0x%X Entry: 0x%X StackSize: 0x%X StackMem: 0x%X SP: 0x%X ",
362+
#ifdef MBED_CONF_RTOS_PRESENT
363+
mbed_error_printf("\nCurrent Thread: %s Id: 0x%X Entry: 0x%X StackSize: 0x%X StackMem: 0x%X SP: 0x%X ",
364+
name_or_unnamed(((osRtxThread_t *)ctx->thread_id)->name),
373365
ctx->thread_id, ctx->thread_entry_address, ctx->thread_stack_size, ctx->thread_stack_mem, ctx->thread_current_sp);
374-
#else
375-
//For Cortex-A targets we dont have support to capture the current SP
376-
mbed_error_printf("\nCurrent Thread: Id: 0x%X Entry: 0x%X StackSize: 0x%X StackMem: 0x%X ",
377-
ctx->thread_id, ctx->thread_entry_address, ctx->thread_stack_size, ctx->thread_stack_mem);
378-
#endif //TARGET_CORTEX_M
366+
#endif
379367

380368
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
381369
mbed_error_printf("\nNext:");
382370
print_thread(osRtxInfo.thread.run.next);
383371

372+
mbed_error_printf("\nReady:");
373+
print_threads_info(osRtxInfo.thread.ready.thread_list);
374+
384375
mbed_error_printf("\nWait:");
385-
osRtxThread_t *threads = (osRtxThread_t *)&osRtxInfo.thread.wait_list;
386-
print_threads_info(threads);
376+
print_threads_info(osRtxInfo.thread.wait_list);
387377

388378
mbed_error_printf("\nDelay:");
389-
threads = (osRtxThread_t *)&osRtxInfo.thread.delay_list;
390-
print_threads_info(threads);
391-
392-
mbed_error_printf("\nIdle:");
393-
threads = (osRtxThread_t *)&osRtxInfo.thread.idle;
394-
print_threads_info(threads);
379+
print_threads_info(osRtxInfo.thread.delay_list);
395380
#endif
396381
mbed_error_printf(MBED_CONF_PLATFORM_ERROR_DECODE_HTTP_URL_STR, ctx->error_status);
397382
mbed_error_printf("\n-- MbedOS Error Info --\n");

platform/mbed_interface.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ void mbed_die(void);
127127
* handling a crash.
128128
*
129129
* @note Synchronization level: Interrupt safe
130+
* @note This uses an internal 128-byte buffer to format the string,
131+
* so the output may be truncated. If you need to write a potentially
132+
* long string, use mbed_error_puts.
130133
*
131134
* @param format C string that contains data stream to be printed.
132135
* Code snippets below show valid format.
@@ -149,6 +152,20 @@ void mbed_error_printf(const char *format, ...);
149152
*/
150153
void mbed_error_vprintf(const char *format, va_list arg);
151154

155+
/** Print out an error message. This is typically called when
156+
* handling a crash.
157+
*
158+
* Unlike mbed_error_printf, there is no limit to the maximum output
159+
* length. Unlike standard puts, but like standard fputs, this does not
160+
* append a '\n' character.
161+
*
162+
* @note Synchronization level: Interrupt safe
163+
*
164+
* @param str C string that contains data stream to be printed.
165+
*
166+
*/
167+
void mbed_error_puts(const char *str);
168+
152169
/** @deprecated Renamed to mbed_error_vprintf to match functionality */
153170
MBED_DEPRECATED_SINCE("mbed-os-5.11",
154171
"Renamed to mbed_error_vprintf to match functionality.")

0 commit comments

Comments
 (0)