免費(fèi)建立網(wǎng)站的軟件任務(wù)推廣引流平臺
1 導(dǎo)言
在軟件開發(fā)周期中,測試和修正缺陷(defect,defect與bug的區(qū)別:Bug是缺陷的一種表現(xiàn)形式,而一個(gè)缺陷是可以引起多種Bug的)的時(shí)間遠(yuǎn)多于寫代碼的時(shí)間。通常,debug是指發(fā)現(xiàn)缺陷并改正的過程。修正缺陷緊隨debug之后,或者說二者是相關(guān)的。如果代碼中存在缺陷,我們首先要識別造成缺陷的根本原因(root cause),這個(gè)過程就稱作調(diào)試(debugging)。找到根本原因后,就可以修正缺陷。
那么如何調(diào)試代碼呢?Visual Studio提供了很多用于調(diào)試的工具。有時(shí)調(diào)試需要花費(fèi)大量時(shí)間去識別root cause。VS提供了許多輔助調(diào)試的便捷的工具。調(diào)試器(Debugger)包含錯誤列表、添加斷點(diǎn)、可視化的程序流程、控制執(zhí)行流程、Data Tips、監(jiān)視窗口(watch windows)、多線程調(diào)試、線程窗口、并行調(diào)試概覽以及IntelliTrace調(diào)試概覽。我希望本文能夠?qū)φ{(diào)試功能的使用者有所裨益。請注意,本文使用VS2010。某些功能在舊版本中也是一致的,但是VS2010新增了許多features(Labeling breakpoint, Pinned DataTip, Multithreaded Debugging, Parallel debugging and IntelliTrace)。
注釋: ? ? ? ?先CTRL+K,然后CTRL+C
取消注釋:?先CTRL+K,然后CTRL+U
2 如何啟動調(diào)試?
圖 啟動調(diào)試(Start Debugging)
“附加到進(jìn)程(Attach to Process)”是另一種啟動調(diào)試的方法。Attach Process會為應(yīng)用程序啟動一個(gè)調(diào)試會話。可能我們更熟悉ASP.NET Web應(yīng)用的Attach Process調(diào)試。我發(fā)了另外兩篇相關(guān)的帖子。如下:
- Debug Your ASP.NET Application that Hosted on IIS
- Remote IIS Debugging: Debug your ASP.NET Application which is hosted on "Remote IIS Server"
通常我們通過在可能存在問題代碼處加斷點(diǎn)來啟動調(diào)試。因此,我們從斷點(diǎn)開始講起。
3 斷點(diǎn)(Breakpoints)
圖 設(shè)置斷點(diǎn)(Set Breakpoint)
3.1 使用斷點(diǎn)進(jìn)行調(diào)試
在中斷模式下,你有多條可使用的命令,使用相應(yīng)命令進(jìn)行進(jìn)一步的調(diào)試。
圖 斷點(diǎn)工具條(Breakpoint Toolbar)
3.1.1 逐過程(Step Over)

3.1.2 逐語句(Step Into)

3.1.3 跳出(Step Out)
3.1.4 繼續(xù)(Continue)
3.1.5 設(shè)置下一語句(Set Next Statement)

3.1.6 顯示下一語句(Show Next Statement [Ctrl+*])
3.2 斷點(diǎn)標(biāo)簽(Labeling in Break Point)
- class?Program??
- ????{??
- ????????static?void?Main(string[]?args)??
- ????????{??
- ????????????string[]?strNames?=?{?"Name1",?"Name2",?"Name3",?"Name4",?"Name5",?"Name6"?};??
- ??
- ????????????foreach?(string?name?in?strNames)??
- ????????????{??
- ????????????????Console.WriteLine(name);???//?BreakPoint??
- ????????????}??
- ????????????int?temp?=?4;??
- ????????????for?(int?i?=?1;?i?<=?10;?i++)??
- ????????????{??
- ????????????????if?(i?>?6)??
- ????????????????????temp?=?5;??
- ????????????}??
- ????????}??
- ??
- ????????public?static?void?Method1()??
- ????????{??
- ????????????Console.WriteLine("Break?Point?in?Method1");???//?BreakPoint??
- ????????}??
- ??
- ????????public?static?void?Method2()??
- ????????{??
- ????????????Console.WriteLine("Break?Point?in?Method2");??//?BreakPoint??
- ????????????Console.WriteLine("Break?Point?in?Method2");??//?BreakPoint??
- ????????}??
- ??
- ????????public?static?void?Method3()??
- ????????{??
- ????????????Console.WriteLine("Break?Point?in?Method3");??//?Breakpoint??
- ????????}??
- ????}??




3.3 條件斷點(diǎn)(Conditional Breakpoint)


- class?Program??
- ????{??
- ????????static?void?Main(string[]?args)??
- ????????{??
- ?????????string?[]?strNames?=?{?"Name1","Name2",?"Name3",?"Name4",?"Name5",?"Name6"};??
- ??
- ????????????foreach(string?name?in?strNames)??
- ????????????{??
- ?????????????????Console.WriteLine(name);?//?Breakpoint?is?here??
- ????????????}??
- ????????}??
- ????}??




- Is True
- Has Changed
3.4 導(dǎo)入/導(dǎo)出斷點(diǎn)(Import / Export Breakpoint)
3.5 斷點(diǎn)命中計(jì)數(shù)(Breakpoint Hit Count)
3.6 Breakpoint When Hit
3.7 斷點(diǎn)篩選器(Breakpoint Filter)

4 數(shù)據(jù)便簽(Data Tip)

4.1 Pin Inspect Value During Debugging
4.2 Drag-Drop Pin Data Tip
4.3 Adding Comments
4.4 Last Session Debugging Value
4.5 Import Export Data Tips
4.6 Change Value Using Data Tips
4.7 Clear Data Tips
5 監(jiān)視窗口(Watch Windows)
5.1 局部變量(Locals)

5.2 自動窗口(Autos)

5.3 監(jiān)視(Watch)

有4個(gè)可同時(shí)使用的監(jiān)視窗口。

圖:多個(gè)監(jiān)視窗口

5.3.1 Creating Object ID


5.4?
6 即時(shí)窗口(Immediate Window)

7 調(diào)用堆棧(Call Stack)
8 調(diào)試多線程程序(Debugging Multithreaded Program)
8.1 Exploring Threads Window
8.2 Flag Just My Code
8.3 Break Point Filter - Multithread Debugging
9 調(diào)試并行程序(Debugging Parallel Program)
9.1 Parallel Task and Parallel Stacks
10 Debugging with IntelliTrace
10.1 Overview
10.2 Mapping with IntelliTrace
10.3 Filter IntelliTrace Data
11 調(diào)試常用快捷鍵(Useful Shortcut Keys For VS Debugging)
Shortcut Keys | Descriptions |
Ctrl-Alt-V, A | Displays the Auto window |
Ctrl-Alt-B | Displays the Breakpoints dialog |
Ctrl-Alt-C | Displays the Call Stack |
Ctrl-Shift-F9 | Clears all of the breakpoints in the project |
Ctrl-F9 | Enables or disables the breakpoint on the current line of code |
Ctrl-Alt-E | Displays the Exceptions dialog |
Ctrl-Alt-I | Displays the Immediate window |
Ctrl-Alt-V, L | Displays the Locals window |
Ctrl-Alt-Q | Displays the Quick Watch dialog |
Ctrl-Shift-F5 | Terminates the current debugging session, rebuilds if necessary, and starts a new debugging session. |
Ctrl-F10 | Starts or resumes execution of your code and then halts execution when it reaches the selected statement. |
Ctrl-Shift-F10 | Sets the execution point to the line of code you choose |
Alt-NUM * | Highlights the next statement |
F5 | If not currently debugging, this runs the startup project or projects and attaches the debugger. |
Ctrl-F5 | Runs the code without invoking the debugger |
F11 | Step Into |
Shift-F11 | Executes the remaining lines out from procedure |
F10 | Executes the next line of code but does not step into any function calls |
Shift-F5 | Available in break and run modes, this terminates the debugging session |
Ctrl-Alt-H | Displays the Threads window to view all of the threads for the current process |
F9 | Sets or removes a breakpoint at the current line |
Ctrl-Alt-W, 1 | Displays the Watch 1 window to view the values of variables or watch expressions |
Ctrl-Alt-P | Displays the Processes dialog, which allows you to attach or detach the debugger to one or more running processes |
Ctrl-D,V | IntelliTrace Event |
12 深入學(xué)習(xí)(Further Study)
- Debugging Task-Based Parallel Applications in Visual Studio 2010 By Daniel Moth and Stephen Toub
- Debugging With IntelliTrace
13 總結(jié)(Summary)
“自動窗口”(Autos):當(dāng)前使用的變量
“局部窗口”(Locals):在范圍內(nèi)的所有變量
“監(jiān)視N”(Watch):可定制(N從1到4)
Step Into(逐語句):執(zhí)行并移動到下一條語句(實(shí)際上,跳入上一條語句的代碼塊,此代碼塊的第一條)
Step Over(逐過程):執(zhí)行并跳到下一條語句,但不進(jìn)入上一條語句的代碼塊
Step Out(跳出):執(zhí)行到代碼塊結(jié)尾
命令窗口(Command)
即時(shí)窗口(Immediate):主要用于計(jì)算表達(dá)式