__FILE__,__LINE__,FUNCTION__實現(xiàn)代碼跟蹤調(diào)試(linux下c語言編程 )先看下簡單的初始代碼:注意其編譯運行后的結(jié)果。
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 //函數(shù)a
#include "global.h"
int funca(void)
{
printf ("this is function\n");
return 0;
}
root@xuanfei-desktop:~/cpropram/2# cat funcb.c //函數(shù)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 //聯(lián)合編譯
root@xuanfei-desktop:~/cpropram/2# ./a.out //運行
this is main
this is function
this is main
this is function
this is main
相同結(jié)果很難讓人看出那里出錯,下面我們用用 __FILE__,__LINE__,__FUNCTION__加入代碼,看看有什么區(qū)別嗎.
把 __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
上面的結(jié)果main.c(4)-main:this is main 表示在mian.c源代碼的第四行main函數(shù)里邊打印出來的 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__的宏來簡單實現(xiàn)代碼的跟蹤調(diào)試:)
下面是一個可供調(diào)試用的頭文件
#ifndef _GOLD_DEBUG_H
#define _GOLD_DEBUG_H
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
//#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);
/*調(diào)試宏, 用于暫停*/
#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) \
if(!(expression)) \
{ \
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)
#endif
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif
C語言常用宏定義
01: 防止一個頭文件被重復包含
#ifndef COMDEF_H
#define COMDEF_H
//頭文件內(nèi)容
#endif
02: 重新定義一些類型,防止由于各種平臺和編譯器的不同,而產(chǎn)生的類型字節(jié)數(shù)差異,方便移植。
typedef unsigned char boolean; /* Boolean value type. */
typedef unsigned long int uint32; /* Unsigned 32 bit value */
typedef unsigned short uint16; /* Unsigned 16 bit value */
typedef unsigned char uint8; /* Unsigned 8 bit value */
typedef signed long int int32; /* Signed 32 bit value */
typedef signed short int16; /* Signed 16 bit value */
typedef signed char int8; /* Signed 8 bit value */
//下面的不建議使用
typedef unsigned char byte; /* Unsigned 8 bit value type. */
typedef unsigned short word; /* Unsinged 16 bit value type. */
typedef unsigned long dword; /* Unsigned 32 bit value type. */
typedef unsigned char uint1; /* Unsigned 8 bit value type. */
typedef unsigned short uint2; /* Unsigned 16 bit value type. */
typedef unsigned long uint4; /* Unsigned 32 bit value type. */
typedef signed char int1; /* Signed 8 bit value type. */
typedef signed short int2; /* Signed 16 bit value type. */
typedef long int int4; /* Signed 32 bit value type. */
typedef signed long sint31; /* Signed 32 bit value */
typedef signed short sint15; /* Signed 16 bit value */
typedef signed char sint7; /* Signed 8 bit value */
03: 得到指定地址上的一個字節(jié)或字
#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在結(jié)構(gòu)體(struct)中的偏移量
#define FPOS(type,field) ((dword)&((type *)0)->field)
06: 得到一個結(jié)構(gòu)體中field所占用的字節(jié)數(shù)
#define FSIZ(type,field) sizeof(((type *)0)->field)
07: 按照LSB格式把兩個字節(jié)轉(zhuǎn)化為一個Word
#define FLIPW(ray) ((((word)(ray)[0]) * 256) + (ray)[1])
08: 按照LSB格式把一個Word轉(zhuǎn)化為兩個字節(jié)
#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: 得到一個字的高位和低位字節(jié)
#define WORD_LO(xxx) ((byte) ((word)(xxx) & 255))
#define WORD_HI(xxx) ((byte) ((word)(xxx) >> 8))
11: 返回一個比X大的最接近的8的倍數(shù)
#define RND8(x) ((((x) + 7)/8) * 8)
12: 將一個字母轉(zhuǎn)換為大寫
#define UPCASE(c) (((c)>='a' && (c) <= 'z') ? ((c) - 0x20) : (c))
13: 判斷字符是不是10進值的數(shù)字
#define DECCHK(c) ((c)>='0' && (c)<='9')
14: 判斷字符是不是16進值的數(shù)字
#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: 返回數(shù)組元素的個數(shù)
#define ARR_SIZE(a) (sizeof((a))/sizeof((a[0])))
17: 返回一個無符號數(shù)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空間映射在存儲空間的結(jié)構(gòu),輸入輸出處理
#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: 使用一些宏跟蹤調(diào)試
ANSI標準說明了五個預定義的宏名。它們是:
__LINE__
__FILE__
__DATE__
__TIME__
__STDC__
C++中還定義了 __cplusplus
如果編譯器不是標準的,則可能僅支持以上宏名中的幾個,或根本不支持。記住編譯程序也許還提供其它預定義的宏名。
__LINE__ 及 __FILE__ 宏指示,#line指令可以改變它的值,簡單的講,編譯時,它們包含程序的當前行數(shù)和文件名。
__DATE__ 宏指令含有形式為月/日/年的串,表示源文件被翻譯到代碼時的日期。
__TIME__ 宏指令包含程序編譯的時間。時間用字符串表示,其形式為: 分:秒
__STDC__ 宏指令的意義是編譯時定義的。一般來講,如果__STDC__已經(jīng)定義,編譯器將僅接受不包含任何非標準擴展的標準C/C++代碼。如果實現(xiàn)是標準的,則宏__STDC__含有十進制常量1。如果它含有任何其它數(shù),則實現(xiàn)是非標準的。
__cplusplus 與標準c++一致的編譯器把它定義為一個包含至少6為的數(shù)值。與標準c++不一致的編譯器將使用具有5位或更少的數(shù)值。
可以定義宏,例如:
當定義了_DEBUG,輸出數(shù)據(jù)信息和所在文件所在行
#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語句已經(jīng)結(jié)束,因而后面的else不在if語句中。而采用do{} while(0)的定義,在任何情況下都沒有問題。而改為 #difne DO(a,b) do{a+b;a++;}while(0) 的定義則在任何情況下都不會出錯。