SlideShare uma empresa Scribd logo
1 de 50
Introduction to debugging
     Appendix C of Introduction to C Programming
              Author: Peter PH Chang




                                                   S
你是怎麼Debug的?




              S
經典的除錯方式


S printf debug

S IDE裡的除錯工具

S gdb

S 其他的除錯方式
printf Debug




               S
什麼是printf debug


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

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

S 基本中的基本,不論如何一定要會
進階printf debug


S 用fprintf把輸入內容導到檔案

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

S Macro
  S #define errexit(format,arg...) exit(printf(format,##arg))
  S #define errexit2(format,arg...) do{printf(“In
     %sn”, __FUNCTION__); printf(format,##arg);
     exit(1);}while(0)
bug-1.c
bug-2.c
bug-3.c
bug-4.c
Dem
o
IDE裡的除錯工具




            S
除錯時會想知道的東西?


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

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

S 程式崩潰在哪一行?
IDE裡的除錯工具


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

S Code::Block和MS Visual Studio也都有自己的除錯工具,
  有興趣的同學可以自行研究
Where is it?
How to use?

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


S 再按下F5啟動除錯模式
進入除錯模式
Stop here
Its control interface
Add watch value
Add watch point
Add watch point
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
單步執行
其他指令
CPU Status
其他指令
bug-2.c
bug-3.c
Dem
o
GDB




      S
gdb


S GNU Debugger

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

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

S 可參考此網頁教學基本gdb使用:
  S http://nthusslab.blogspot.tw/2011/09/debugcgdb.html
其他的除錯方式




          S
其他的除錯方式


S Hardware debugger

S 純粹利用Hardware

S Debug by simulator

S Trace code

S Talk to your friend!

Mais conteúdo relacionado

Semelhante a Introduction to debugging

2006 recycle opensourceprojects
2006 recycle opensourceprojects2006 recycle opensourceprojects
2006 recycle opensourceprojects
George Ang
 
Recycle Open Source Projects
Recycle Open Source ProjectsRecycle Open Source Projects
Recycle Open Source Projects
George Ang
 
第7章预编译命令
第7章预编译命令第7章预编译命令
第7章预编译命令
summerfeng
 

Semelhante a Introduction to debugging (9)

Java script 全面逆襲!使用 node.js 打造桌面環境!
Java script 全面逆襲!使用 node.js 打造桌面環境!Java script 全面逆襲!使用 node.js 打造桌面環境!
Java script 全面逆襲!使用 node.js 打造桌面環境!
 
2006 recycle opensourceprojects
2006 recycle opensourceprojects2006 recycle opensourceprojects
2006 recycle opensourceprojects
 
Recycle Open Source Projects
Recycle Open Source ProjectsRecycle Open Source Projects
Recycle Open Source Projects
 
Java 基礎入門1
Java 基礎入門1Java 基礎入門1
Java 基礎入門1
 
第7章预编译命令
第7章预编译命令第7章预编译命令
第7章预编译命令
 
Software Development via A.I.
Software Development via A.I.Software Development via A.I.
Software Development via A.I.
 
C 1 c
C 1 cC 1 c
C 1 c
 
C 1 c
C 1 cC 1 c
C 1 c
 
C 1 c
C 1 cC 1 c
C 1 c
 

Introduction to debugging