C語言調(diào)試手段:鎖定錯誤的實現(xiàn)方法
更新時間:2013年05月27日 10:43:26 作者:
本篇文章是對在C語言調(diào)試中,鎖定錯誤的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
在項目開發(fā)工程中,如果能確定哪個文件下的哪個函數(shù)下的哪行出錯--即鎖定錯誤,那該多好啊,該文章就是為此而作的。
首先來了解一下文件默認(rèn)的輸出信息的函數(shù)吧:
文件信息函數(shù):
printf("line : %d\n", __LINE__); //當(dāng)前行數(shù)
printf("filename : %s\n", __FILE__); //當(dāng)前文件名
printf("function : %s\n", __FUNCTION__); //當(dāng)前函數(shù)
printf("time : %s\n", __TIME__); //當(dāng)前時間
printf ("date : %s\n", __DATE__); //當(dāng)前日期
輸出:
line : 10
filename : test.c
function : main.c
time : 14:13:51
date : Oct 13 2012
理論已足,那就來看看如何鎖定錯誤吧:
一、源文件:
[root@localhost for_test]# cat erroutput.c
#include <stdio.h>
#include <assert.h>
#define _DEBUG(msg...) printf("[ %s,%s, %d ]=>",__FILE__, __FUNCTION__, __LINE__); printf(msg);printf("\r\n")
#define _ERROR(msg...) printf("[ error: %s, %d]=>", __FILE__, __LINE__);printf(msg); printf("\r\n")
#define _ASSERT(exp) \
do {\
if (!(exp)) {\
printf( "[ %s ] ",#exp);printf("\r\n");\
assert(exp);\
}\
} while (0)
int main(void)
{
char *p = NULL;
_DEBUG("DEBUG!");
_ERROR("ERROR!");
_ASSERT(NULL != p);
return 0;
}
二、輸出:
[root@localhost for_test]# gcc erroutput.c
[root@localhost for_test]# ./a.out
[ erroutput.c,main, 17 ]=>DEBUG!
[ error: erroutput.c, 18]=>ERROR!
[ NULL != p ]
a.out: erroutput.c:19: main: Assertion `((void *)0) != p' failed.
已放棄
TI處理:
#ifdef DEBUG
#define DBG(fmt, args...) printf("Debug " fmt, ##args)// ##運(yùn)算符用于把參數(shù)連接到一起。預(yù)處理程序把出現(xiàn)在##兩側(cè)的參數(shù)合并成一個符號。
#else
#define DBG(fmt, args...)
#endif
#define ERR(fmt, args...) printf("Error " fmt, ##args)
[root@localhost for_test]# cat debug_err.c
#include <stdio.h>
//#define DEBUG
int main(void)
{
DBG("xxxx\n");
ERR("xxxx\n");
return 0;
}
[root@localhost for_test]# ./a.out
Error xxxx
#ifdef __DEBUG
#define DBG(fmt, args...) fprintf(stderr,"Encode Debug: " fmt, ## args)
#else
#define DBG(fmt, args...)
#endif
#define ERR(fmt, args...) fprintf(stderr,"Encode Error: " fmt, ## args)
首先來了解一下文件默認(rèn)的輸出信息的函數(shù)吧:
文件信息函數(shù):
復(fù)制代碼 代碼如下:
printf("line : %d\n", __LINE__); //當(dāng)前行數(shù)
printf("filename : %s\n", __FILE__); //當(dāng)前文件名
printf("function : %s\n", __FUNCTION__); //當(dāng)前函數(shù)
printf("time : %s\n", __TIME__); //當(dāng)前時間
printf ("date : %s\n", __DATE__); //當(dāng)前日期
輸出:
line : 10
filename : test.c
function : main.c
time : 14:13:51
date : Oct 13 2012
理論已足,那就來看看如何鎖定錯誤吧:
一、源文件:
復(fù)制代碼 代碼如下:
[root@localhost for_test]# cat erroutput.c
#include <stdio.h>
#include <assert.h>
#define _DEBUG(msg...) printf("[ %s,%s, %d ]=>",__FILE__, __FUNCTION__, __LINE__); printf(msg);printf("\r\n")
#define _ERROR(msg...) printf("[ error: %s, %d]=>", __FILE__, __LINE__);printf(msg); printf("\r\n")
#define _ASSERT(exp) \
do {\
if (!(exp)) {\
printf( "[ %s ] ",#exp);printf("\r\n");\
assert(exp);\
}\
} while (0)
int main(void)
{
char *p = NULL;
_DEBUG("DEBUG!");
_ERROR("ERROR!");
_ASSERT(NULL != p);
return 0;
}
二、輸出:
復(fù)制代碼 代碼如下:
[root@localhost for_test]# gcc erroutput.c
[root@localhost for_test]# ./a.out
[ erroutput.c,main, 17 ]=>DEBUG!
[ error: erroutput.c, 18]=>ERROR!
[ NULL != p ]
a.out: erroutput.c:19: main: Assertion `((void *)0) != p' failed.
已放棄
TI處理:
復(fù)制代碼 代碼如下:
#ifdef DEBUG
#define DBG(fmt, args...) printf("Debug " fmt, ##args)// ##運(yùn)算符用于把參數(shù)連接到一起。預(yù)處理程序把出現(xiàn)在##兩側(cè)的參數(shù)合并成一個符號。
#else
#define DBG(fmt, args...)
#endif
#define ERR(fmt, args...) printf("Error " fmt, ##args)
[root@localhost for_test]# cat debug_err.c
#include <stdio.h>
//#define DEBUG
int main(void)
{
DBG("xxxx\n");
ERR("xxxx\n");
return 0;
}
[root@localhost for_test]# ./a.out
Error xxxx
#ifdef __DEBUG
#define DBG(fmt, args...) fprintf(stderr,"Encode Debug: " fmt, ## args)
#else
#define DBG(fmt, args...)
#endif
#define ERR(fmt, args...) fprintf(stderr,"Encode Error: " fmt, ## args)
相關(guān)文章
C++代碼改造為UTF-8編碼問題的總結(jié)(最新推薦)
本文總結(jié)了如何將C++程序代碼改造為UTF-8編碼,包括操作系統(tǒng)、編譯器和終端等各方面的設(shè)置,在實際操作中,可以通過漸進(jìn)式更新的方式,只在新的代碼項目中使用UTF-8編碼,避免大規(guī)模修改舊代碼,感興趣的朋友一起看看吧2025-02-02
VisualStudio Community2019在安裝的過程中無法進(jìn)入安裝界面的解決方法
這篇文章主要介紹了VisualStudio Community2019在安裝的過程中無法進(jìn)入安裝界面的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
C語言數(shù)據(jù)結(jié)構(gòu)之學(xué)生信息管理系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細(xì)介紹了C語言數(shù)據(jù)結(jié)構(gòu)之學(xué)生信息管理系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
C語言實現(xiàn)學(xué)生檔案管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)學(xué)生檔案管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05

