From 697bf139b2385daac4968bac3c4f551a47c00d10 Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Wed, 29 Jun 2022 14:21:21 +0800 Subject: [PATCH] [enhancement] Add string information for error (#3186) * [enhancement]Add string information for error * Update src/kservice.c * Update src/kservice.c Co-authored-by: Man, Jianting (Meco) <920369182@qq.com> * remove %m Signed-off-by: a1012112796 <1012112796@qq.com> Co-authored-by: Meco Man <920369182@qq.com> --- components/finsh/cmd.c | 7 +++---- include/rtthread.h | 1 + src/kservice.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/components/finsh/cmd.c b/components/finsh/cmd.c index f60473bcbc..70cd934f64 100644 --- a/components/finsh/cmd.c +++ b/components/finsh/cmd.c @@ -229,15 +229,14 @@ long list_thread(void) thread->error); #else ptr = (rt_uint8_t *)thread->stack_addr; - while (*ptr == '#')ptr ++; - - rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n", + while (*ptr == '#') ptr ++; + rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %s\n", thread->stack_size + ((rt_ubase_t)thread->stack_addr - (rt_ubase_t)thread->sp), thread->stack_size, (thread->stack_size - ((rt_ubase_t) ptr - (rt_ubase_t) thread->stack_addr)) * 100 / thread->stack_size, thread->remaining_tick, - thread->error); + rt_strerror(thread->error)); #endif } } diff --git a/include/rtthread.h b/include/rtthread.h index fa72384a1d..c55720b7d1 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -592,6 +592,7 @@ rt_device_t rt_console_get_device(void); rt_err_t rt_get_errno(void); void rt_set_errno(rt_err_t no); int *_rt_errno(void); +const char *rt_strerror(rt_err_t error); #if !defined(RT_USING_NEWLIB) && !defined(_WIN32) #ifndef errno #define errno *_rt_errno() diff --git a/src/kservice.c b/src/kservice.c index 4e6dc22675..dfa0da9cd6 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -53,6 +53,40 @@ RT_WEAK void rt_hw_us_delay(rt_uint32_t us) "Please consider implementing rt_hw_us_delay() in another file.")); } +static const char* rt_errno_strs[] = +{ + "OK", + "ERROR", + "ETIMOUT", + "ERSFULL", + "ERSEPTY", + "ENOMEM", + "ENOSYS", + "EBUSY", + "EIO", + "EINTRPT", + "EINVAL", + "EUNKNOW" +}; + +/** + * This function return a pointer to a string that contains the + * message of error. + * + * @param error the errorno code + * @return a point to error message string + */ +const char *rt_strerror(rt_err_t error) +{ + if (error < 0) + error = -error; + + return (error > RT_EINVAL + 1) ? + rt_errno_strs[RT_EINVAL + 1] : + rt_errno_strs[error]; +} +RTM_EXPORT(rt_strerror); + /** * This function gets the global errno for the current thread. *