編程調試技巧

來源:互聯網
上載者:User
 printf(核心態為printk)是我覺得最好的調試工具,我碰到的大部分問題也是通過在代碼中列印調試資訊來分析錯誤源的位置,但當我們寫的代碼需要發布時,這些調試資訊則是多餘的,而當我們再次發現bug時,可能又需要加入一些調試資訊,於是我們可能想尋求一種方法可以控制print函數是否列印調試資訊,預先處理宏可協助我們實現這一功能。   

#undef PDEBUG /* undef it, just in case */
#ifdef DNFS_DEBUG
# ifdef __KERNEL__
/* This one if debugging is on, and kernel space */
# define PDEBUG(fmt, args...) printk( KERN_DEBUG "scull: " fmt, ##

args)
# else
/* This one for user space */
# define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)
# endif
#else
# define PDEBUG(fmt, args...) /* not debugging: nothing */
#endif

加入上述代碼,則可通過是否定義DNFS_DEBUG宏來決定是否列印調試資訊,去年這個時候看wcw師兄寫的代碼第一次發現這個方法,後來編程過程中發現這種做法很是方便,代碼看起來也更加規整。

可在makefile中加入輔助資訊,更方便的實現調試資訊的列印,而不用修改原始碼中的宏定義,debug變數是否為y決定了DNFS_DEBUG是否被定義,從而決定了PDEBUG最終的宏定義。

# Comment/uncomment the following line to disable/enable debugging
DEBUG = y
# Add your debugging flag (or not) to CFLAGS
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -DDNFS_DEBUG   # "-O" is needed to expand inlines
else
DEBFLAGS = -O2
endif
CFLAGS += $(DEBFLAGS)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.