linux下C語言__FILE__,__LINE__,FUNCTION__實現程式碼跟蹤除錯
__FILE__,__LINE__,FUNCTION__實現程式碼跟蹤除錯(linux下C語言程式設計
)先看下簡單的初始程式碼:注意其編譯執行後的結果。
[email protected]:~/cpropram/2# cat global.h //標頭檔案
#ifndef CLOBAL_H
#define GLOBAL_H
#include <stdio.h>
int funca(void);
int funcb(void);
#endif
[email protected]:~/cpropram/2# cat funca.c //函式a
#include "global.h"
int funca(void)
{
printf ("this is function\n");
return 0;
}
#include "global.h"
int funcb(void)
{
printf ("this is function\n");
return 0;
}
[email protected]:~/cpropram/2# gcc -Wall funca.c funcb.c main.c //聯合編譯
[email protected]:~/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中
#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;
}
[email protected]:~/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程式碼中進行巨集定義
[email protected]:~/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
[email protected]:~/cpropram/2# cat funca.c
#include "global.h"
int funca(void)
{
printf (DEBUGFMT " this is function\n",DEBUGARGS);
return 0;
}
[email protected]:~/cpropram/2# cat funcb.c
#include "global.h"
int funcb(void)
{
printf (DEBUGFMT " this is function\n",DEBUGARGS);
return 0;
}
[email protected]:~/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;
}
[email protected]:~/cpropram/2# gcc -Wall funca.c funcb.c main.c
[email protected]:~/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
[email protected]:~/cpropram/2#
這就是通過定義__FILE__,__LINE__,FUNCTION__的巨集來簡單實現程式碼的跟蹤除錯:)
下面是一個可供除錯用的標頭檔案
#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);
/*除錯巨集, 用於暫停*/
#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
//標頭檔案內容
#endif
02: 重新定義一些型別,防止由於各種平臺和編譯器的不同,而產生的型別位元組數差異,方便移植。
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: 得到指定地址上的一個位元組或字
#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) 的定義則在任何情況下都不會出錯。