前言
本文对lwip中debug.h
文件里的调试相关宏进行分析。
正文
debug.h中有3个重要的调试相关宏:
LWIP_ASSERT(message, assertion)
LWIP_ERROR(message, expression, handler)
LWIP_DEBUGF(debug, message)
断言
LWIP_ASSERT(message, assertion)
源代码为:
#define LWIP_ASSERT(message, assertion) do {
if (!(assertion)) {
\
LWIP_PLATFORM_ASSERT(message); }} while(0)
message
为断言触发时输出的字符串,assertion
为一个布尔值,展开后:
if (!(assertion))
{
printf("Assertion \"%s\" failed at line %d in %s\n",
message, __LINE__, __FILE__);
fflush(NULL); //更新文件系统
abort(); //程序中止运行
}
当判断条件不为真时,打印断言原因,输出断言所在文件名和文件中行数,更新文件系统 ,中止程序运行。
例子:
err_t
ethernetif_init(struct netif *netif)
{
struct ethernetif *ethernetif;
LWIP_ASSERT("netif != NULL", (netif != NULL));
。。。。。。
/* initialize the hardware */
low_level_init(netif);
return ERR_OK;
}
网卡初始化部分代码,当传入指针为空时,触发断言。
错误处理
LWIP_ERROR(message, expres