diff --git a/include/rtdebug.h b/include/rtdebug.h index 8fd07b5b7689d78e67ef56789d5313ef3fee373f..a324a29623f55eb4e014804fcbab97df78d3dc87 100644 --- a/include/rtdebug.h +++ b/include/rtdebug.h @@ -84,8 +84,13 @@ while (0) if (!(EX)) \ { \ volatile char dummy = 0; \ - rt_kprintf("(%s) assert failed at %s:%d \n", #EX, __FUNCTION__, __LINE__);\ - while (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__); \ + } \ } /* Macro to check current context */ diff --git a/include/rtthread.h b/include/rtthread.h index 9bb02abedefe70194d472f88f747d845488c355f..705795b5220846fce4f029ab290fec522bc9e9b3 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -515,6 +515,11 @@ rt_uint32_t rt_strcasecmp(const char *a, const char *b); 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)); +#endif /* RT_DEBUG */ + /*@}*/ #ifdef __cplusplus diff --git a/src/kservice.c b/src/kservice.c index ab15917bbe33aeca65f047986df13facb1677568..e8076eb2378922dd7947e39923d1cd87dd37f89e 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -1237,6 +1237,19 @@ int __rt_ffs(int value) } #endif +#ifdef RT_DEBUG +/* RT_ASSERT(EX)'s hook */ +void (*rt_assert_hook)(const char* ex, const char* func, rt_size_t line); +/** + * This function will set a hook function to RT_ASSERT(EX). It will run when the expression is false. + * + * @param hook the hook function + */ +void rt_assert_set_hook(void (*hook)(const char* ex, const char* func, rt_size_t line)) { + rt_assert_hook = hook; +} +#endif /* RT_DEBUG */ + #if !defined (RT_USING_NEWLIB) && defined (RT_USING_MINILIBC) && defined (__GNUC__) #include void *memcpy(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memcpy")));