1.1.1 assert 介绍
assert()
是一个调试程序时经常使用的宏,在程序运行时它计算括号内的表达式:
- 如果表达式为
false (0)
, 程序将报告错误,并终止执行; - 如果表达式不为
0
,则继续执行后面的语句。
这个宏通常用来判断程序中是否出现了明显非法的数据,如果出现了,则终止程序以免导致严重后果,同时也便于查找错误。
下面以 RT-Thread 中的 assert 为例进行简单介绍:
assert 宏定义:
rt-thread/include/rtdebug.h
#define RT_ASSERT(EX) \
if (!(EX)) \
{ \
rt_assert_handler(#EX, __FUNCTION__, __LINE__); \
}
1.1.2 assert 的处理
通过继续跟踪 rt_assert_handler 函数:
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)
{
{
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);
从上面这个函数可以看出来,对于 assert 的流程处理分为两种情况:
- 如果没有配置 hook 函数,也即 assert 之后的回调函数,这个时候通常是打印一句 log,然后
cpu while(1)
循环停住; - 如果配置了 hook 函数,那么就去调用对应 hook 函数来进行处理,hook函数的配置通常是在系统初始化的时候完成的:
void rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line))
{
rt_assert_hook = hook;
}
int rt_cm_backtrace_init(void) {
static rt_bool_t is_init = RT_FALSE;
if (is_init)
{
return 0;
}
cm_backtrace_init("rtthread","1.0","1.0");
rt_hw_exception_install(exception_hook);
rt_assert_set_hook(assert_hook);
is_init = RT_TRUE;
return 0;
}
在 rtthread 中是在 cm_backtrace 中配置完成的,那么 hook 函数到底做了什么呢 ?
void rt_cm_backtrace_assert_hook(const char* ex, const char* func, rt_size_t line)
{
rt_enter_critical();
#ifdef RT_USING_FINSH
extern long list_thread(void);
list_thread();
#endif
cmb_println("");
cmb_println("(%s) has assert failed at %s:%ld.", ex, func, line);
cm_backtrace_assert(cmb_get_sp());
cmb_println("Current system tick: %ld", rt_tick_get());
}
RT_WEAK rt_err_t exception_hook(void *context) {
volatile uint8_t _continue = 1;
rt_cm_backtrace_exception_hook(context);
while (_continue == 1);
return RT_EOK;
}
通过上面内容可以看到 hook 函数最后是进行 堆栈及相关寄存器信息的打印,最后通过 while(1)
停住。