__FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

转自http://www.cnitblog.com/zouzheng/archive/2007/08/31/32691.html

先看下简单的初始代码:注意其编译运行后的结果。

root@xuanfei-desktop:~/cpropram/2# cat global.h //头文件
#ifndef CLOBAL_H
        #define GLOBAL_H
        #include <stdio.h>
        int funca(void);
        int funcb(void);
#endif
root@xuanfei-desktop:~/cpropram/2# cat funca.c //函数a
#include "global.h"
int funca(void)
{
printf ("this is function/n");
return 0;
}
root@xuanfei-desktop:~/cpropram/2# cat funcb.c //函数b
#include "global.h"
int funcb(void)
{
printf ("this is function/n");
return 0;
}
root@xuanfei-desktop:~/cpropram/2# gcc -Wall funca.c funcb.c main.c //联合编译
root@xuanfei-desktop:~/cpropram/2# ./a.out //运行
this is main
this is function
this is main
this is function
this is main

相同结果很难让人看出那里出错,下面我们用用 __FILE__,__LINE__,__FUNCTION__加入代码,看看有什么区别吗.
把 __FILE__,__LINE__,__FUNCTION__加入到mail.c中
root@xuanfei-desktop:~/cpropram/2# cat main.c
#include "global.h"
int main(int argc, char **argv)
{
    printf("%s(%d)-%s: this is main/n",__FILE__,__LINE__,__FUNCTION__);
    funca();
    printf("%s(%d)-%s: this is main/n",__FILE__,__LINE__,__FUNCTION__);
    funcb();
    printf("%s(%d)-%s: this is main/n",__FILE__,__LINE__,__FUNCTION__);
    return 0;
}
root@xuanfei-desktop:~/cpropram/2# gcc -Wall funca.c funcb.c main.c
root@xuanfei-desktop:~/cpropram/2# ./a.out
main.c(4)-main: this is main
this is function
main.c(6)-main: this is main
this is function
main.c(8)-main: this is main

上面的结果main.c(4)-main:this is main 表示在mian.c源代码的第四行main函数里边打印出来的 this is main
那样的话就很方便的让程序员对自己的程序进行排错!
为了更方便的使用它我们可以通过在global.h代码中进行宏定义
root@xuanfei-desktop:~/cpropram/2# cat global.h
#ifndef CLOBAL_H
        #define GLOBAL_H
        #include <stdio.h>
        int funca(void);
        int funcb(void);
        #define DEBUGFMT  "%s(%d)-%s"
        #define DEBUGARGS __FILE__,__LINE__,__FUNCTION__
#endif
root@xuanfei-desktop:~/cpropram/2# cat funca.c
#include "global.h"
int funca(void)
{
printf (DEBUGFMT " this is function/n",DEBUGARGS);
return 0;
}
root@xuanfei-desktop:~/cpropram/2# cat funcb.c
#include "global.h"
int funcb(void)
{
printf (DEBUGFMT " this is function/n",DEBUGARGS);
return 0;
}
root@xuanfei-desktop:~/cpropram/2# cat main.c
#include "global.h"
int main(int argc, char **argv)
{
    printf(DEBUGFMT "this is main/n", DEBUGARGS);
    funca();
    printf(DEBUGFMT "this is main/n", DEBUGARGS);
    funcb();
    printf(DEBUGFMT "this is main/n", DEBUGARGS);
    return 0;
}
root@xuanfei-desktop:~/cpropram/2# gcc -Wall funca.c funcb.c main.c
root@xuanfei-desktop:~/cpropram/2# ./a.out
main.c(4)-mainthis is main
funca.c(4)-funca this is function
main.c(6)-mainthis is main
funcb.c(4)-funcb this is function
main.c(8)-mainthis is main
root@xuanfei-desktop:~/cpropram/2#

这就是通过定义__FILE__,__LINE__,FUNCTION__的宏来简单实现代码的跟踪调试:)

下面是一个可供调试用的头文件
#ifndef _GOLD_DEBUG_H
#define _GOLD_DEBUG_H

 

#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif

//#define GI_DEBUG

#ifdef GI_DEBUG

#define GI_DEBUG_POINT()   printf("/n/n[File:%s Line:%d] Fun:%s/n/n", __FILE__, __LINE__, __FUNCTION__)
#define dbg_printf(arg...)   printf(arg);

#define GI_ASSERT(expr)                                     /
    do{                                                     /
        if (!(expr)) { /
            printf("/nASSERT failed at:/n  >File name: %s/n  >Function : %s/n  >Line No. : %d/n  >Condition: %s/n", /
                    __FILE__,__FUNCTION__, __LINE__, #expr);/
        } /
    }while(0);


#define GI_DEBUG_PAUSE()           /
 do               /
 {               /
  GI_DEBUG_POINT();          /
  printf("pause for debug, press 'q' to exit!/n");  /
  char c;             /
  while( ( c = getchar() ) )        /
   {             /
    if('q' == c)         /
     {           /
      getchar();        /
      break;         /
     }           /
   }             /
 }while(0);
#define GI_DEBUG_PAUSE_ARG(arg...)          /
  do               /
  {               /
   printf(arg);           /
   GI_DEBUG_PAUSE()          /
  }while(0);


#define GI_DEBUG_ASSERT(expression_r_r)      /
if(!(expression_r_r))                        /
{                                  /
    printf("[ASSERT],%s,%s:%d/n", __FILE__,  __FUNCTION__, __LINE__);/
    exit(-1);             /
}
#else
#define GI_ASSERT(expr)
#define GI_DEBUG_PAUSE()
#define GI_DEBUG_PAUSE_ARG(arg...)
#define GI_DEBUG_POINT()
#define dbg_printf(arg...)
#define GI_DEBUG_ASSERT(expression_r_r)

#endif

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif


#endif

 

C语言常用宏定义

01: 防止一个头文件被重复包含
#ifndef COMDEF_H
#define COMDEF_H
//头文件内容
#endif
02: 重新定义一些类型,防止由于各种平台和编译器的不同,而产生的类型字节数差异,方便移植。
typedef  unsigned char      boolean;    
typedef  unsigned long int  uint32;     
typedef  unsigned short     uint16;     
typedef  unsigned char      uint8;      
typedef  signed long int    int32;      
typedef  signed short       int16;      
typedef  signed char        int8;       

//下面的不建议使用
typedef  unsigned char     byte;        
typedef  unsigned short    word;        
typedef  unsigned long     dword;       
typedef  unsigned char     uint1;       
typedef  unsigned short    uint2;       
typedef  unsigned long     uint4;       
typedef  signed char       int1;        
typedef  signed short      int2;        
typedef  long int          int4;        
typedef  signed long       sint31;      
typedef  signed short      sint15;      
typedef  signed char       sint7;       

03: 得到指定地址上的一个字节或字
#define  MEM_B(x) (*((byte *)(x)))
#define  MEM_W(x) (*((word *)(x)))

04: 求最大值和最小值
#define  MAX(x,y) (((x)>(y)) ? (x) : (y))
#define  MIN(x,y) (((x) < (y)) ? (x) : (y))

05: 得到一个field在结构体(struct)中的偏移量
#define FPOS(type,field) ((dword)&((type *)0)->field)

06: 得到一个结构体中field所占用的字节数
#define FSIZ(type,field) sizeof(((type *)0)->field)

07: 按照LSB格式把两个字节转化为一个Word
#define FLIPW(ray) ((((word)(ray)[0]) * 256) + (ray)[1])

08: 按照LSB格式把一个Word转化为两个字节
#define FLOPW(ray,val) (ray)[0] = ((val)/256); (ray)[1] = ((val) & 0xFF)

09: 得到一个变量的地址(word宽度)
#define B_PTR(var)  ((byte *) (void *) &(var))
#define W_PTR(var)  ((word *) (void *) &(var))

10: 得到一个字的高位和低位字节
#define WORD_LO(xxx)  ((byte) ((word)(xxx) & 255))
#define WORD_HI(xxx)  ((byte) ((word)(xxx) >> 8))

11: 返回一个比X大的最接近的8的倍数
#define RND8(x) ((((x) + 7)/8) * 8)

12: 将一个字母转换为大写
#define UPCASE(c) (((c)>='a' && (c) <= 'z') ? ((c) - 0x20) : (c))

13: 判断字符是不是10进值的数字
#define  DECCHK(c) ((c)>='0' && (c)<='9')

14: 判断字符是不是16进值的数字
#define HEXCHK(c) (((c) >= '0' && (c)<='9') ((c)>='A' && (c)<= 'F') /
((c)>='a' && (c)<='f'))

15: 防止溢出的一个方法
#define INC_SAT(val) (val=((val)+1>(val)) ? (val)+1 : (val))

16: 返回数组元素的个数
#define ARR_SIZE(a)  (sizeof((a))/sizeof((a[0])))

17: 返回一个无符号数n尾的值MOD_BY_POWER_OF_TWO(X,n)=X%(2^n)
#define MOD_BY_POWER_OF_TWO( val, mod_by ) ((dword)(val) & (dword)((mod_by)-1))

18: 对于IO空间映射在存储空间的结构,输入输出处理
#define inp(port) (*((volatile byte *)(port)))
#define inpw(port) (*((volatile word *)(port)))
#define inpdw(port) (*((volatile dword *)(port)))
#define outp(port,val) (*((volatile byte *)(port))=((byte)(val)))
#define outpw(port, val) (*((volatile word *)(port))=((word)(val)))
#define outpdw(port, val) (*((volatile dword *)(port))=((dword)(val)))

19: 使用一些宏跟踪调试
ANSI标准说明了五个预定义的宏名。它们是:
__LINE__
__FILE__
__DATE__
__TIME__
__STDC__
C++中还定义了 __cplusplus

如果编译器不是标准的,则可能仅支持以上宏名中的几个,或根本不支持。记住编译程序也许还提供其它预定义的宏名。

__LINE__ 及 __FILE__ 宏指示,#line指令可以改变它的值,简单的讲,编译时,它们包含程序的当前行数和文件名。

__DATE__ 宏指令含有形式为月/日/年的串,表示源文件被翻译到代码时的日期。
__TIME__ 宏指令包含程序编译的时间。时间用字符串表示,其形式为: 分:秒
__STDC__ 宏指令的意义是编译时定义的。一般来讲,如果__STDC__已经定义,编译器将仅接受不包含任何非标准扩展的标准C/C++代码。如果实现是标准的,则宏__STDC__含有十进制常量1。如果它含有任何其它数,则实现是非标准的。
__cplusplus 与标准c++一致的编译器把它定义为一个包含至少6为的数值。与标准c++不一致的编译器将使用具有5位或更少的数值。


可以定义宏,例如:
当定义了_DEBUG,输出数据信息和所在文件所在行
#ifdef _DEBUG
#define DEBUGMSG(msg,date) printf(msg);printf(“%d%d%d”,date,_LINE_,_FILE_)
#else
#define DEBUGMSG(msg,date)
#endif
 

20: 宏定义防止错误使用小括号包含。
例如:
有问题的定义:#define DUMP_WRITE(addr,nr) {memcpy(bufp,addr,nr); bufp += nr;}
应该使用的定义: #difne DO(a,b) do{a+b;a++;}while(0)
例如:
if(addr)
    DUMP_WRITE(addr,nr);
else
    do_somethong_else();
宏展开以后变成这样:
if(addr)
    {memcpy(bufp,addr,nr); bufp += nr;};
else
    do_something_else();

gcc 在碰到else前面的“;”时就认为if语句已经结束,因而后面的else不在if语句中。而采用do{} while(0)的定义,在任何情况下都没有问题。而改为 #difne DO(a,b) do{a+b;a++;}while(0) 的定义则在任何情况下都不会出错。

linux C/C++ 宏定义 __FILE__、__LINE__、__func__、__TIME__ 一、用例测试环境 整篇文章测试用例使用到的环境如下,并不是说一定要在如下环境中才有效,只是指明下本文的测试用例环境。 输入: cat /etc/redhat-release 系统环境:CentOS Linux release 7.5.1804 (Core) 测试文件名: test.c 二、宏定义 2.1 __FILE__ 作用:表示当前源文件名,类型为字符串常量; #include <stdio.h> int main() { printf("%... 阅读详情

相关推荐

C++ 中 __FILE__,__LINE__,__FUNCTION__ 含义

c/c++ 中__LINE__,FUNCTION 含义 LINE,FUNCTION 为内置的宏定义 __LINE__ // 是内置宏,代表该行代码的所在行号 __FUNCTION__ // 是内置宏,代表代码所在的函数 测试: #include <iostream> using namespace std; void func() { printf ( "source line %d, in function %s" ,__LINE__,__FUNCTION__); } int

weixin_39681471的博客 5087

Linux下__LINE__用法(C语言)

测试代码 ANSI C标准中有几个标准预定义宏,其中 __ LINE __ 表示在源代码中插入当前源代码行号,通过如下代码可以__LINE__在程序中行号输出情况: #include <stdio.h> int PRINT2(int line) { printf("PRINT2: %d\n", line); return 0; } int PRINT1() { printf("PRINT1: %d\n", __LINE__); return 0; } int main(void)

C/C++攻城狮 4283

linux下的头文件 及C/C++头文件 一览表

linux下编程常用头文件一览 ==============================================================================================          验证程序断言            cpio归档值            字符类型          目录项            出错码    

a2796749的专栏 4075

linux 中C语言便于调试的宏定义编写及 __FILE__,__FUNCTION__, __LINE__参数使用

linux编程中,当文件数量变的众多之后,使用gdb调试就是一场灾难。因此在程序中加入合理的打印信息,定位错误出现的文件名,函数名,行号等信息,能更高效的定位到问题的所在。      下面定义了宏,分别是WARNING,INFO,ERROR,SHOW_TIME,DEBUG等。利用了 __FILE__,_FUNCTION__, __LINE__等变量。。。       _FILE__,__FU

AddyLee的专栏 1万+

linux下C语言__FILE__,__LINE__,FUNCTION__实现代码跟踪调试

_FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux下C语言编程 )先看下简单的初始代码:注意其编译运行后的结果。 root@xuanfei-desktop:~/cpropram/2# cat global.h //头文件 #ifndef CLOBAL_H #define GLOBAL_H #include <stdio.h&gt...

yilongdashi的博客 564

__FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux下c语言编程

__FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux下c语言编程http://bbs.linux-cn.com/archiver/tid-13793.html (come from)[color=#295200][size=14pt][b]__FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux下c语言编程 )[/b][/size][/c

shark0001的专栏 3002

linux下使用__FILE__,__LINE__,FUNCTION__实现代码跟踪调试

先看下简单的初始代码:注意其编译运行后的结果。 root@xuanfei-desktop:~/cpropram/2# cat global.h //头文件 #ifndef CLOBAL_H         #define GLOBAL_H         #include         int funca(void);         int funcb(void); #en

w_k的专栏 585

__FILE__,__LINE__,FUNCTION__实现代码跟踪调试(Linux C)

<br />先看下简单的初始代码:注意其编译运行后的结果。<br /><br />root@xuanfei-desktop:~/cpropram/2# cat global.h //头文件<br />#ifndef CLOBAL_H<br />        #define GLOBAL_H<br />        #include <stdio.h><br />        int funca(void);<br />        int funcb(void);<br />#endif<br />

yuanxuran0101的专栏 617

__FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux下c语言编程 )

root@xuanfei-desktop:~/cpropram/2# cat global.h //头文件#ifndef CLOBAL_H #define GLOBAL_H #include <stdio.h> int funca(void); int funcb(void);#endifroot@xuanfei-desktop...

SurpassLi的专栏 54

linux多个函数调试,__FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux下c语言编程)...

__FILE__,__LINE__,FUNCTION__实现代码跟踪调试(linux下c语言编程 )先看下简单的初始代码:注意其编译运行后的结果。root@xuanfei-desktop:~/cpropram/2# cat global.h //头文件#ifndef CLOBAL_H#define GLOBAL_H#include int funca(void);int funcb(void);#...

weixin_33394460的博客 398

FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux下c语言编程 )

先看下简单的初始代码:注意其编译运行后的结果。root@xuanfei-desktop:~/cpropram/2# cat global.h //头文件#ifndef CLOBAL_H        #define GLOBAL_H        #include         int funca(void);        int funcb(void);#endifroot@xuanfei-

linuxerhqt的专栏 692

_FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux下c语言编程)

http://hi.baidu.com/925566297/item/4661483d83df4800ceb9fec6 __FILE__,__LINE__,FUNCTION__实现代码跟踪调试(下c语言编程 )先看下简单的初始代码:注意其编译运行后的结果。 root@xuanfei-desktop:~/cpropram/2# cat global.h //头文件 #ifndef

chengfangang的专栏 954

[转]__FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux下c语言编程)

原文地址:http://www.cnitblog.com/zouzheng/archive/2007/08/31/32691.html __FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux下c语言编程) __FILE__,__LINE__,FUNCTION__实现代码跟踪调试linux下c语言编程 )先看下简单的初始代码:注意其编译运行后的结果

giskook的专栏 981
上一篇: Address already in use的解决方法
下一篇: 一个问题
sandflee
博客等级 码龄18年 40粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值