introduction to debugging

50
S Introduction to debugging Appendix C of Introduction to C Programming Author: Peter PH Chang

Upload: peter-chang

Post on 19-May-2015

928 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Introduction to debugging

S

Introduction to debugging

Appendix C of Introduction to C ProgrammingAuthor: Peter PH Chang

Page 2: Introduction to debugging

S

你是怎麼 Debug的?

Page 3: Introduction to debugging

經典的除錯方式

printf debug

IDE裡的除錯工具

gdb

其他的除錯方式

Page 4: Introduction to debugging

S

printf Debug

Page 5: Introduction to debugging

什麼是 printf debug

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

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

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

Page 6: Introduction to debugging

進階 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)

Page 7: Introduction to debugging

bug-1.c

Page 8: Introduction to debugging

bug-2.c

Page 9: Introduction to debugging

bug-3.c

Page 10: Introduction to debugging

bug-4.c

Page 11: Introduction to debugging

Demo

Page 12: Introduction to debugging

S

IDE裡的除錯工具

Page 13: Introduction to debugging

除錯時會想知道的東西?

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

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

程式崩潰在哪一行?

Page 14: Introduction to debugging

IDE裡的除錯工具

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

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

Page 15: Introduction to debugging

Where is it?

Page 16: Introduction to debugging

How to use?

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

Page 17: Introduction to debugging

How to use?

再按下 F5啟動除錯模式

Page 18: Introduction to debugging

進入除錯模式

Page 19: Introduction to debugging

Stop here

Page 20: Introduction to debugging

Its control interface

Page 21: Introduction to debugging

Add watch value

Page 22: Introduction to debugging

Add watch point

Page 23: Introduction to debugging

Add watch point

Page 24: Introduction to debugging

單步執行

Page 25: Introduction to debugging

單步執行

Page 26: Introduction to debugging

單步執行

Page 27: Introduction to debugging

單步執行

Page 28: Introduction to debugging

單步執行

Page 29: Introduction to debugging

單步執行

Page 30: Introduction to debugging

單步執行

Page 31: Introduction to debugging

單步執行

Page 32: Introduction to debugging

單步執行

Page 33: Introduction to debugging

單步執行

Page 34: Introduction to debugging

單步執行

Page 35: Introduction to debugging

單步執行

Page 36: Introduction to debugging

單步執行

Page 37: Introduction to debugging

單步執行

Page 38: Introduction to debugging

單步執行

Page 39: Introduction to debugging

單步執行

Page 40: Introduction to debugging

單步執行

Page 41: Introduction to debugging

其他指令

Page 42: Introduction to debugging

CPU Status

Page 43: Introduction to debugging

其他指令

Page 44: Introduction to debugging

bug-2.c

Page 45: Introduction to debugging

bug-3.c

Page 46: Introduction to debugging

Demo

Page 47: Introduction to debugging

S

GDB

Page 48: Introduction to debugging

gdb

GNU Debugger

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

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

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

debugcgdb.html

Page 49: Introduction to debugging

S

其他的除錯方式

Page 50: Introduction to debugging

其他的除錯方式

Hardware debugger

純粹利用 Hardware

Debug by simulator

Trace code

Talk to your friend!