Introduction to debugging

Preview:

DESCRIPTION

Introduce how to use Dev-C++ 5.3.0 to debug source code

Citation preview

S

Introduction to debugging

Appendix C of Introduction to C ProgrammingAuthor: Peter PH Chang

S

你是怎麼 Debug的?

經典的除錯方式

printf debug

IDE裡的除錯工具

gdb

其他的除錯方式

S

printf Debug

什麼是 printf debug

就是插入 printf,把你想要印的資訊印出來而已

看似無用,但是很多時候真的只能用 printf來除錯

基本中的基本,不論如何一定要會

進階 printf debug

用 fprintf把輸入內容導到檔案

__FUNCTION__ printf(“in %s function, it crashed.\n”, __FUNCTION__);

Macro #define errexit(format,arg...)

exit(printf(format,##arg)) #define errexit2(format,arg...) do{printf(“In %s\n”,

__FUNCTION__); printf(format,##arg); exit(1);}while(0)

bug-1.c

bug-2.c

bug-3.c

bug-4.c

Demo

S

IDE裡的除錯工具

除錯時會想知道的東西?

某某變數現在的值是多少?

程式到底是怎麼走的? 程式永遠不會照你所想的走,只會照你所寫的走

程式崩潰在哪一行?

IDE裡的除錯工具

以MS Windows下的 Dev C++ 5.3.0.1為例

Code::Block和MS Visual Studio也都有自己的除錯工具,有興趣的同學可以自行研究

Where is it?

How to use?

先用 F9或 F11編譯過,再於行號處點一下滑鼠左鍵,插入Break Point,讓程式執行之後,可以在此中斷,先暫停在此

How to use?

再按下 F5啟動除錯模式

進入除錯模式

Stop here

Its control interface

Add watch value

Add watch point

Add watch point

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

單步執行

其他指令

CPU Status

其他指令

bug-2.c

bug-3.c

Demo

S

GDB

gdb

GNU Debugger

GNU GPLv2授權開放原始碼除錯器

Dev C++、 Code::Block、 Eclipse甚至 Xcode的除錯器都是 gdb

可參考此網頁教學基本 gdb使用: http://nthusslab.blogspot.tw/2011/09/

debugcgdb.html

S

其他的除錯方式

其他的除錯方式

Hardware debugger

純粹利用 Hardware

Debug by simulator

Trace code

Talk to your friend!

Recommended