C語言調(diào)試手段:鎖定錯(cuò)誤的實(shí)現(xiàn)方法
更新時(shí)間:2013年05月27日 10:43:26 作者:
本篇文章是對(duì)在C語言調(diào)試中,鎖定錯(cuò)誤的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
在項(xiàng)目開發(fā)工程中,如果能確定哪個(gè)文件下的哪個(gè)函數(shù)下的哪行出錯(cuò)--即鎖定錯(cuò)誤,那該多好啊,該文章就是為此而作的。
首先來了解一下文件默認(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)前時(shí)間
printf ("date : %s\n", __DATE__); //當(dāng)前日期
輸出:
line : 10
filename : test.c
function : main.c
time : 14:13:51
date : Oct 13 2012
理論已足,那就來看看如何鎖定錯(cuò)誤吧:
一、源文件:
[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ù)合并成一個(gè)符號(hào)。
#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)前時(shí)間
printf ("date : %s\n", __DATE__); //當(dāng)前日期
輸出:
line : 10
filename : test.c
function : main.c
time : 14:13:51
date : Oct 13 2012
理論已足,那就來看看如何鎖定錯(cuò)誤吧:
一、源文件:
復(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ù)合并成一個(gè)符號(hào)。
#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)
您可能感興趣的文章:
- C語言中返回錯(cuò)誤信息的相關(guān)函數(shù)用法總結(jié)
- 詳解C語言中的錯(cuò)誤報(bào)告errno與其相關(guān)應(yīng)用方法
- C語言創(chuàng)建鏈表錯(cuò)誤之通過指針參數(shù)申請(qǐng)動(dòng)態(tài)內(nèi)存實(shí)例分析
- C語言初學(xué)者代碼中的常見錯(cuò)誤與問題
- C語言編程時(shí)常犯十八個(gè)錯(cuò)誤小結(jié)
- 深入理解C語言中編譯相關(guān)的常見錯(cuò)誤
- 基于C語言中段錯(cuò)誤的問題詳解
- 詳解C語言中scanf函數(shù)使用的一些注意點(diǎn)
- C語言 volatile與const同時(shí)使用應(yīng)注意的問題
- 基礎(chǔ)C語言編程時(shí)易犯錯(cuò)誤有哪些
相關(guān)文章
C語言 volatile與const同時(shí)使用應(yīng)注意的問題
“volatile”的含義是“請(qǐng)不要做沒譜的優(yōu)化,這個(gè)值可能變掉的”,而并非“你可以修改這個(gè)值”。因此,它們本來就不是矛盾的2013-09-09用C語言實(shí)現(xiàn)自動(dòng)售貨機(jī)
這篇文章主要為大家詳細(xì)介紹了用C語言實(shí)現(xiàn)自動(dòng)售貨機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01C語言實(shí)現(xiàn)圖書管理系統(tǒng)開發(fā)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)圖書管理系統(tǒng)開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08VSCode Linux的C++代碼格式化配置的實(shí)現(xiàn)
動(dòng)格式化代碼容易出現(xiàn)錯(cuò)誤,特別是當(dāng)代碼量較大時(shí),使用自動(dòng)格式化可以減少這種錯(cuò)誤的風(fēng)險(xiǎn),本文主要介紹了VSCode Linux的C++代碼格式化配置的實(shí)現(xiàn),感興趣的可以了解一下2023-10-10C++面試八股文之override和finial關(guān)鍵字有何作用
C++11中的override和final關(guān)鍵字是為了增強(qiáng)代碼的編譯時(shí)類型檢查和面向?qū)ο笤O(shè)計(jì)中的繼承機(jī)制,下面這篇文章主要給大家介紹了關(guān)于C++面試八股文之override和finial關(guān)鍵字有何作用的相關(guān)資料,需要的朋友可以參考下2023-06-06從頭學(xué)習(xí)C語言之switch語句和分支嵌套
這篇文章主要為大家詳細(xì)介紹了C語言之switch語句和分支嵌套,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01