diff --git a/include/rtdebug.h b/include/rtdebug.h index a324a29623f55eb4e014804fcbab97df78d3dc87..69a33b391f7fe4dd15f5771d7a3efaf27f976a0f 100644 --- a/include/rtdebug.h +++ b/include/rtdebug.h @@ -83,14 +83,7 @@ while (0) #define RT_ASSERT(EX) \ if (!(EX)) \ { \ - volatile char dummy = 0; \ - if (rt_assert_hook == RT_NULL) \ - { \ - rt_kprintf("(%s) assert failed at %s:%d \n", #EX, __FUNCTION__, __LINE__);\ - while (dummy == 0); \ - } else { \ - rt_assert_hook(#EX, __FUNCTION__, __LINE__); \ - } \ + rt_assert_handler(#EX, __FUNCTION__, __LINE__); \ } /* Macro to check current context */ diff --git a/include/rtthread.h b/include/rtthread.h index 705795b5220846fce4f029ab290fec522bc9e9b3..c7f35d163e0585757b11e640216cd60a1e442fe7 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -518,6 +518,8 @@ void rt_show_version(void); #ifdef RT_DEBUG extern void (*rt_assert_hook)(const char* ex, const char* func, rt_size_t line); void rt_assert_set_hook(void (*hook)(const char* ex, const char* func, rt_size_t line)); + +void rt_assert_handler(const char* ex, const char* func, rt_size_t line); #endif /* RT_DEBUG */ /*@}*/ diff --git a/src/kservice.c b/src/kservice.c index e8076eb2378922dd7947e39923d1cd87dd37f89e..0e15610e5103c1a8c948e893f8e52326558bdc91 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -30,6 +30,7 @@ * 2012-12-22 Bernard fix rt_kprintf issue, which found by Grissiom. * 2013-06-24 Bernard remove rt_kprintf if RT_USING_CONSOLE is not defined. * 2013-09-24 aozima make sure the device is in STREAM mode when used by rt_kprintf. + * 2015-07-06 Bernard Add rt_assert_handler routine. */ #include @@ -1248,6 +1249,42 @@ void (*rt_assert_hook)(const char* ex, const char* func, rt_size_t line); void rt_assert_set_hook(void (*hook)(const char* ex, const char* func, rt_size_t line)) { rt_assert_hook = hook; } + +/** + * The RT_ASSERT function. + * + * @param ex the assertion condition string + * @param func the function name when assertion. + * @param line the file line number when assertion. + */ +void rt_assert_handler(const char* ex_string, const char* func, rt_size_t line) +{ + volatile char dummy = 0; + + if (rt_assert_hook == RT_NULL) + { +#ifdef RT_USING_MODULE + if (rt_module_self() != RT_NULL) + { + /* unload assertion module */ + rt_module_unload(rt_module_self()); + + /* re-schedule */ + rt_schedule(); + } + else +#endif + { + rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line); + while (dummy == 0); + } + } + else + { + rt_assert_hook(ex_string, func, line); + } +} +RTM_EXPORT(rt_assert_handler); #endif /* RT_DEBUG */ #if !defined (RT_USING_NEWLIB) && defined (RT_USING_MINILIBC) && defined (__GNUC__)