总结嵌入式C语言程序调试和宏使用技巧

1. 调试相关的宏
    在linux使用gcc编译程序的时候,对于调试的语句还具有一些特殊的语法。gcc编译的过程中,会生成一些宏,可以使用这些宏分别打印当前源文件的信息,主要内容是当前的文件、当前运行的函数和当前的程序行。
    具体宏如下:
__file__ 当前程序源文件 (char*)__function__ 当前运行的函数 (char*)__line__ 当前的函数行 (int)    这些宏不是程序代码定义的,而是有编译器产生的。这些信息都是在编译器处理文件的时候动态产生的。
测试示例:#include int main(void){ printf(file: %s, __file__); printf(function: %s, __function__); printf(line: %d, __line__); return 0;}2. # 字符串化操作符
在gcc的编译系统中,可以使用#将当前的内容转换成字符串。
程序示例:#include #define dprint(expr) printf(%s = %d, #expr, expr);int main(void){ int x = 3; int y = 5; dprint(x / y); dprint(x + y); dprint(x * y); return 0;}    执行结果:deng@itcast:~/tmp$ gcc test.c deng@itcast:~/tmp$ ./a.out x / y = 0x + y = 8x * y = 15      #expr表示根据宏中的参数(即表达式的内容),生成一个字符串。该过程同样是有编译器产生的,编译器在编译源文件的时候,如果遇到了类似的宏,会自动根据程序中表达式的内容,生成一个字符串的宏。
这种方式的优点是可以用统一的方法打印表达式的内容,在程序的调试过程中可以方便直观的看到转换字符串之后的表达式。具体的表达式的内容是什么,有编译器自动写入程序中,这样使用相同的宏打印所有表达式的字符串。//打印字符#define debugc(expr) printf( %s = %c, #expr, expr)//打印浮点数#define debugf(expr) printf( %s = %f, #expr, expr)//按照16进制打印整数#define debugx(expr) printf( %s = 0x%x, #expr, expr);      由于#expr本质上市一个表示字符串的宏,因此在程序中也可以不适用%s打印它的内容,而是可以将其直接与其它的字符串连接。因此,上述宏可以等价以下形式://打印字符#define debugc(expr) printf( #expr = %c, expr)//打印浮点数#define debugf(expr) printf( #expr = %f, expr)//按照16进制打印整数#define debugx(expr) printf( #expr = 0x%x, expr);  总结
#是c语言预处理阶段的字符串化操作符,可将宏中的内容转换成字符串。
3. ## 连接操作符
在gcc的编译系统中,##是c语言中的连接操作符,可以在编译的预处理阶段实现字符串连接的操作。
程序示例:#include #define test(x) test##xvoid test1(int a){ printf(test1 a = %d, a);}void test2(char *s){ printf(test2 s = %s, s);}int main(void){ test(1)(100); test(2)(hello world); return 0;}      上述程序中,test(x)宏被定义为test##x, 他表示test字符串和x字符串的连接。     在程序的调试语句中,##常用的方式如下:#define debug(fmt, args...) printf(fmt, ##args)      替换的方式是将参数的两个部分以##连接。##表示连接变量代表前面的参数列表。使用这种形式可以将宏的参数传递给一个参数。args…是宏的参数,表示可变的参数列表,使用##args将其传给printf函数。
总结
##是c语言预处理阶段的连接操作符,可实现宏参数的连接。
4. 调试宏第一种形式
一种定义的方式:#define debug(fmt, args...) { printf(file:%s function: %s line: %d , __file__, __function__, __line__); printf(fmt, ##args); }      程序示例:#include #define debug(fmt, args...) { printf(file:%s function: %s line: %d , __file__, __function__, __line__); printf(fmt, ##args); }int main(void){ int a = 100; int b = 200; char *s = hello world; debug(a = %d b = %d, a, b); debug(a = %x b = %x, a, b); debug(s = %s, s); return 0;}  总结
上面的debug定义的方式是两条语句的组合,不可能在产生返回值,因此不能使用它的返回值。
5. 调试宏的第二种定义方式
调试宏的第二种定义方式。#define debug(fmt, args...) printf(file:%s function: %s line: %d fmt, __file__, __function__, __line__, ##args)      程序示例:#include #define debug(fmt, args...) printf(file:%s function: %s line: %d fmt, __file__, __function__, __line__, ##args)int main(void){ int a = 100; int b = 200; char *s = hello world; debug(a = %d b = %d, a, b); debug(a = %x b = %x, a, b); debug(s = %s, s); return 0;}  总结
fmt必须是一个字符串,不能使用指针,只有这样才可以实现字符串的功能。
6. 对调试语句进行分级审查
即使定义了调试的宏,在工程足够大的情况下,也会导致在打开宏开关的时候在终端出现大量的信息。而无法区分哪些是有用的。这个时候就要加入分级检查机制,可以定义不同的调试级别,这样就可以对不同重要程序和不同的模块进行区分,需要调试哪一个模块就可以打开那一个模块的调试级别。
一般可以利用配置文件的方式显示,其实linux内核也是这么做的,它把调试的等级分成了7个不同重要程度的级别,只有设定某个级别可以显示,对应的调试信息才会打印到终端上。
可以写出一下配置文件。[debug]debug_level=xxx_module      解析配置文件使用标准的字符串操作库函数就可以获取xxx_module这个数值。int show_debug(int level){ if (level == xxx_module) { #define debug(fmt, args...) printf(file:%s function: %s line: %d fmt, __file__, __function__, __line__, ##args) } else if (...) { .... }}  7. 条件编译调试语句
在实际的开发中,一般会维护两种源程序,一种是带有调试语句的调试版本程序,另外一种是不带有调试语句的发布版本程序。然后根据不同的条件编译选项,编译出不同的调试版本和发布版本的程序。
在实现过程中,可以使用一个调试宏来控制调试语句的开关。#ifdef use_debug #define debug(fmt, args...) printf(file:%s function: %s line: %d fmt, __file__, __function__, __line__, ##args) #else #define debug(fmt, args...)#endif      如果use_debug被定义,那么有调试信息,否则debug就为空。
如果需要调试信息,就只需要在程序中更改一行就可以了。#define use_debug#undef use_debug      定义条件编译的方式使用一个带有值的宏。#if use_debug #define debug(fmt, args...) printf(file:%s function: %s line: %d fmt, __file__, __function__, __line__, ##args) #else #define debug(fmt, args...)#endif      可以使用如下方式进行条件编译。#ifndef use_debug#define use_debug 0#endif  8. 使用do…while的宏定义
使用宏定义可以将一些较为短小的功能封装,方便使用。宏的形式和函数类似,但是可以节省函数跳转的开销。如何将一个语句封装成一个宏,在程序中常常使用do…while(0)的形式。#define hello(str) do { printf(hello: %s, str); }while(0)      程序示例:int cond = 1;if (cond) hello(true);else hello(false);  9. 代码剖析
对于比较大的程序,可以借助一些工具来首先把需要优化的点清理出来。接下来我们来看看在程序执行过程中获取数据并进行分析的工具:代码剖析程序。
测试程序:#include #define t 100000void call_one(){ int count = t * 1000; while(count--);}void call_two(){ int count = t * 50; while(count--);}void call_three(){ int count = t * 20; while(count--);}int main(void){ int time = 10; while(time--) { call_one(); call_two(); call_three(); } return 0;}      编译的时候加入-pg选项。deng@itcast:~/tmp$ gcc -pg test.c -o test      执行完成后,在当前文件中生成了一个gmon.out文件。deng@itcast:~/tmp$ ./test deng@itcast:~/tmp$ lsgmon.out test test.cdeng@itcast:~/tmp$     使用gprof剖析主程序。 deng@itcast:~/tmp$ gprof testflat profile:each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls ms/call ms/call name 95.64 1.61 1.61 10 160.68 160.68 call_one 3.63 1.67 0.06 10 6.10 6.10 call_two 2.42 1.71 0.04 10 4.07 4.07 call_three  
    其中主要的信息有两个,一个是每个函数执行的时间占程序总时间的百分比,另外一个就是函数被调用的次数。通过这些信息,可以优化核心程序的实现方式来提高效率。
    当然这个剖析程序由于它自身特性有一些限制,比较适用于运行时间比较长的程序,因为统计的时间是基于间隔计数这种机制,所以还需要考虑函数执行的相对时间,如果程序执行时间过短,那得到的信息是没有任何参考意义的。
将上述程序时间缩短。
#include #define t 100void call_one(){ int count = t * 1000; while(count--);}void call_two(){ int count = t * 50; while(count--);}void call_three(){ int count = t * 20; while(count--);}int main(void){ int time = 10; while(time--) { call_one(); call_two(); call_three(); } return 0;}      剖析结果如下。 deng@itcast:~/tmp$ gcc -pg test.c -o testdeng@itcast:~/tmp$ ./test deng@itcast:~/tmp$ gprof testflat profile:each sample counts as 0.01 seconds. no time accumulated % cumulative self self total time seconds seconds calls ts/call ts/call name 0.00 0.00 0.00 10 0.00 0.00 call_one 0.00 0.00 0.00 10 0.00 0.00 call_three 0.00 0.00 0.00 10 0.00 0.00 call_two    因此该剖析程序对于越复杂、执行时间越长的函数也适用。
那么是不是每个函数执行的绝对时间越长,剖析显示的时间就真的越长呢?可以再看如下的例子。#include #define t 100void call_one(){ int count = t * 1000; while(count--);}void call_two(){ int count = t * 100000; while(count--);}void call_three(){ int count = t * 20; while(count--);}int main(void){ int time = 10; while(time--) { call_one(); call_two(); call_three(); } return 0;}      剖析结果如下。 deng@itcast:~/tmp$ gcc -pg test.c -o testdeng@itcast:~/tmp$ ./test deng@itcast:~/tmp$ gprof testflat profile:each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls ms/call ms/call name 101.69 0.15 0.15 10 15.25 15.25 call_two 0.00 0.15 0.00 10 0.00 0.00 call_one  0.00      0.15     0.00       10     0.00     0.00  call_three总结
在使用gprof工具的时候,对于一个函数进行gprof方式的剖析,实质上的时间是指除去库函数调用和系统调用之外,纯碎应用部分开发的实际代码运行的时间,也就是说time一项描述的时间值不包括库函数printf、系统调用system等运行的时间。这些实用库函数的程序虽然运行的时候将比最初的程序实用更多的时间,但是对于剖析函数来说并没有影响。  


稳压二极管的工作原理及应用,Zener diode
什么是高压开关管?
小燕科技为消费者提供了统一于智能生活套装的智能生活体验
亚马逊、谷歌、苹果联手制定新的标准
通过智能化技术,智能健身将越来越便捷
总结嵌入式C语言程序调试和宏使用技巧
反激变换器的整流二极管上面为什么要并联电容和电阻?
思特威推出高端ADAS应用8.3MP高分辨率车规级图像传感器新品SC850AT,赋能高级辅助驾驶与自动驾驶应用
工业物联网能耗监测系统应用
韩国电信公司推出了一款名叫N bot的服务机器人
5G云网融合赋能XR产业新发展
说一下模拟信号与数字信号的区别
魅族Pro7什么时候上市?最新消息:魅族Pro7难产是因为没有骁龙835吗?没有骁龙835还要卖3299?
攀升电脑迎来促销,主机搭载i5-9400F处理器
介绍一种紧凑型DIN导轨安装式交流/直流电源
彩电行业的商业模式发生变化 海信电视凭借超强竞争力迎接2020
单片机的组成、工作原理、分类、特点以及发展趋势
6G运存手机大比拼:三星C9 Pro、OPPOR9s Plus、一加3T、VivoX9 Plus、360N5,性价比超高!
大深教你对对射式光电传感器安装注意事项
云智能正在推动云计算应用