乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      掌握VS2010調(diào)試

       corefashion 2015-05-24

      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)包含錯(cuò)誤列表、添加斷點(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)。

      2 如何啟動調(diào)試?

      可以通過VS的調(diào)試(Debug)菜單啟動調(diào)試。點(diǎn)擊調(diào)試菜單下的“啟動調(diào)試”或者按F5鍵啟動。如果你已經(jīng)在代碼中加入了斷點(diǎn),那么執(zhí)行會自動開始。


      圖 啟動調(diào)試(Start Debugging)

      “附加到進(jìn)程(Attach to Process)”是另一種啟動調(diào)試的方法。Attach Process會為應(yīng)用程序啟動一個(gè)調(diào)試會話??赡芪覀兏煜SP.NET Web應(yīng)用的Attach Process調(diào)試。我發(fā)了另外兩篇相關(guān)的帖子。如下:

      通常我們通過在可能存在問題代碼處加斷點(diǎn)來啟動調(diào)試。因此,我們從斷點(diǎn)開始講起。

      3 斷點(diǎn)(Breakpoints)

      斷點(diǎn)用于通知調(diào)試器何時(shí)何處暫停程序的執(zhí)行。通過點(diǎn)擊左邊欄或者按F9鍵在當(dāng)前行添加斷點(diǎn)。在加斷點(diǎn)之前,你需要知道你的代碼將會出現(xiàn)什么錯(cuò)誤,在什么地方停止執(zhí)行。當(dāng)調(diào)試器執(zhí)行到斷點(diǎn)處時(shí),你可以使用其他的調(diào)試工具核對代碼何處出現(xiàn)錯(cuò)誤。


      圖 設(shè)置斷點(diǎn)(Set Breakpoint)

      3.1 使用斷點(diǎn)進(jìn)行調(diào)試

      你已經(jīng)在你想要暫停執(zhí)行的地方設(shè)置了斷點(diǎn)?,F(xiàn)在按F5鍵啟動調(diào)試,當(dāng)程序執(zhí)行到斷點(diǎn)處時(shí),自動暫停執(zhí)行。此時(shí)你有多種方式來檢查代碼。命中斷點(diǎn)(hit the breakpoint)后,加斷點(diǎn)的行變?yōu)辄S色,意指下一步將執(zhí)行此行。

      在中斷模式下,你有多條可使用的命令,使用相應(yīng)命令進(jìn)行進(jìn)一步的調(diào)試。


      圖 斷點(diǎn)工具條(Breakpoint Toolbar)

      3.1.1 逐過程(Step Over)

      調(diào)試器執(zhí)行到斷點(diǎn)后,你可能需要一條一條的執(zhí)行代碼。”Step Over“[F10]命令用于一條一條的執(zhí)行代碼。這將執(zhí)行當(dāng)前高亮的行,然后暫停。如果在一條方法調(diào)用語句高亮?xí)r按F10,執(zhí)行會停在調(diào)用語句的下一條語句上。Step Over會一次整個(gè)方法。
      debug51
      圖: 逐過程(Step Over - F10)

      3.1.2 逐語句(Step Into)

      它與Step Over相似。唯一的不同是,如果當(dāng)前高亮語句是方法調(diào)用,調(diào)試器會進(jìn)入方法內(nèi)部??旖萱I是”F11“
      debug52
      圖: 逐語句(Step Into - F11)

      3.1.3 跳出(Step Out)

      當(dāng)你在一個(gè)方法內(nèi)部調(diào)試時(shí)會用到它。如果你在當(dāng)前方法內(nèi)按Shift - F11,調(diào)試器會完成此方法的執(zhí)行,之后在調(diào)用此方法的語句的下一條語句處暫停。

      3.1.4 繼續(xù)(Continue)

      它像是重新執(zhí)行你的程序。它會繼續(xù)程序的執(zhí)行直到遇到下一個(gè)斷點(diǎn)??旖萱I是”F5“

      3.1.5 設(shè)置下一語句(Set Next Statement)

      這是一個(gè)非常有趣的特性。設(shè)置下一語句允許你在調(diào)試的時(shí)候改變程序的執(zhí)行路徑。如果你的程序在某一行處暫停而且你想改變執(zhí)行路徑,跳到指定行,在這一行上右擊,在右擊菜單中選擇”設(shè)置下一語句“。這樣程序就會轉(zhuǎn)到哪一行執(zhí)行而不執(zhí)行先前的代碼。這在如下情況中非常有用:當(dāng)你發(fā)現(xiàn)代碼中某些行可能會導(dǎo)致程序的中斷(break)而你不想讓程序在那個(gè)時(shí)候中斷??旖萱I是Ctrl + Shift + F10。
      debug3_small
      圖: 設(shè)置下一語句(Set Next Statement)

      3.1.6 顯示下一語句(Show Next Statement [Ctrl+*])

      這一行用黃色箭頭標(biāo)記。這行是程序繼續(xù)執(zhí)行時(shí)下一條將執(zhí)行的語句。

      3.2 斷點(diǎn)標(biāo)簽(Labeling in Break Point)

      這是VS2010提供的新特征(feature)。用于更好的管理斷點(diǎn)。它使得我們能夠更好的分組和過濾斷點(diǎn)。這像是對斷點(diǎn)的歸類。如果我們添加了與某一功能相關(guān)的不同類型的斷點(diǎn),我們可以根據(jù)需要使能(enable)、取消(disable)、過濾(filter)這些斷點(diǎn)。例如,假設(shè)我們要調(diào)試一下代碼塊。
      1. class Program  
      2.     {  
      3.         static void Main(string[] args)  
      4.         {  
      5.             string[] strNames = { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6" };  
      6.   
      7.             foreach (string name in strNames)  
      8.             {  
      9.                 Console.WriteLine(name);   // BreakPoint  
      10.             }  
      11.             int temp = 4;  
      12.             for (int i = 1; i <= 10; i++)  
      13.             {  
      14.                 if (i > 6)  
      15.                     temp = 5;  
      16.             }  
      17.         }  
      18.   
      19.         public static void Method1()  
      20.         {  
      21.             Console.WriteLine("Break Point in Method1");   // BreakPoint  
      22.         }  
      23.   
      24.         public static void Method2()  
      25.         {  
      26.             Console.WriteLine("Break Point in Method2");  // BreakPoint  
      27.             Console.WriteLine("Break Point in Method2");  // BreakPoint  
      28.         }  
      29.   
      30.         public static void Method3()  
      31.         {  
      32.             Console.WriteLine("Break Point in Method3");  // Breakpoint  
      33.         }  
      34.     }  
      執(zhí)行程序?qū)⑼T诘谝粋€(gè)斷點(diǎn)處。下圖給出了斷點(diǎn)列表。
      debug11_small.png
      圖: 斷點(diǎn)列表
      上圖中Labels列都為空。下面介紹如何給斷點(diǎn)設(shè)置標(biāo)簽(label)以及如何使用標(biāo)簽。只需在特定代碼行的斷點(diǎn)符號上右擊(①)或者在斷點(diǎn)窗口中設(shè)置(②)即可對任何斷點(diǎn)設(shè)置標(biāo)簽。
      debug12_small
      圖: 設(shè)置斷點(diǎn)標(biāo)簽(Setting Breakpoint Label)
      右擊斷點(diǎn),點(diǎn)擊編輯標(biāo)簽(Edit Labels),即可對任意斷點(diǎn)添加標(biāo)簽。對于示例代碼,我為所有斷點(diǎn)的標(biāo)簽起了易于理解的名字。
      debug13
      圖: 添加斷點(diǎn)標(biāo)簽(Adding Breakpoint Label)
      這些標(biāo)簽如何輔助我們調(diào)試呢?現(xiàn)在,所有斷點(diǎn)都是使能的(enabled)。如果你不想調(diào)試method2,一般情況下你必須去對應(yīng)的方法中一個(gè)一個(gè)的取消斷點(diǎn),但這里你可以通過標(biāo)簽名過濾或者搜索它們,然后選中它們以方便的取消它們。
      debug14_small
      圖: 使用標(biāo)簽過濾斷點(diǎn)(Filter Breakpoint Using Labels)
      斷點(diǎn)標(biāo)簽到此介紹完畢。我舉的例子非常簡單,但是斷點(diǎn)標(biāo)簽在你調(diào)試大量代碼,多個(gè)工程等情況下非常有用。

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

      假設(shè)你在多次迭代(循環(huán))處理數(shù)據(jù)而你只想調(diào)試其中某幾次迭代。這意味著你想根據(jù)某些特定條件暫停你的程序。Visual Studio斷點(diǎn)允許你設(shè)置條件斷點(diǎn)。當(dāng)且僅當(dāng)條件滿足時(shí),調(diào)試器才會停住。
      首先,你需要在你想暫停執(zhí)行處設(shè)置斷點(diǎn)。然后右擊紅色的斷點(diǎn)圖標(biāo)。右鍵菜單中點(diǎn)擊”條件“。
      debug5_small
      圖: 設(shè)置斷點(diǎn)條件(Set Breakpoint Condition)
      點(diǎn)擊右鍵菜單中的”條件“后,會彈出下面的對話框設(shè)置斷點(diǎn)的條件。
      debug6
      圖: 斷點(diǎn)條件設(shè)置
      假設(shè)你要調(diào)試下面的代碼塊:
      1. class Program  
      2.     {  
      3.         static void Main(string[] args)  
      4.         {  
      5.          string [] strNames = { "Name1","Name2", "Name3", "Name4", "Name5", "Name6"};  
      6.   
      7.             foreach(string name in strNames)  
      8.             {  
      9.                  Console.WriteLine(name); // Breakpoint is here  
      10.             }  
      11.         }  
      12.     }  
      你在Console.WriteLine()語句處設(shè)置了斷點(diǎn)。當(dāng)執(zhí)行程序時(shí),每次for-each循環(huán)都會停住。如果你想讓代碼只在name="Name3"時(shí)停住,該怎么辦呢?非常簡單,你只需使用條件name.Equals("Name3")。
      debug7
      圖: 設(shè)置斷點(diǎn)條件
      查看斷點(diǎn)符號。它應(yīng)該看上去像是一個(gè)加(+)號在斷點(diǎn)符號內(nèi)部,這表示該斷點(diǎn)是條件斷點(diǎn)。
      debug9
      圖: 條件斷點(diǎn)符號(Conditional Breakpoint Symbol)
      設(shè)置斷點(diǎn)的條件之后,在調(diào)試程序,調(diào)試器只會在滿足給定條件時(shí)才停住。
      debug10
      圖: 條件斷點(diǎn)命中(Conditional Breakpoint hit)
      條件輸入框的自動補(bǔ)全(intellisense):上面給出的斷點(diǎn)條件非常簡單,可以非常容易的寫到條件文本框中。有時(shí)你可能需要定義很大很復(fù)雜的條件。不必?fù)?dān)心,VS為條件文本輸入框也提供了自動補(bǔ)全功能。因此,在條件框中輸入就像是在編輯器中一樣方便。如下圖。
      debug8_small
      圖: 條件文本框的自動補(bǔ)全(intellisense in condition textbox)
      我?guī)缀踔v解了條件斷點(diǎn)的所有內(nèi)容。除了下面這點(diǎn)。在條件窗口中有兩個(gè)選項(xiàng):
      1. Is True
      2. Has Changed
      我們已經(jīng)看到”Is True“選項(xiàng)的用途了。”Has Changed“用在當(dāng)你想在某些值變?yōu)槟承┨囟ㄖ档臅r(shí)候停住。

      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)

      你可以限制斷點(diǎn)只對特定進(jìn)程或線程有效。這在進(jìn)行多線程程序的調(diào)試時(shí)非常有用。右擊斷點(diǎn)選”篩選器“即可打開篩選器窗口。
      debug40
      圖: 斷點(diǎn)篩選器(Breakpoint Filter)
      在篩選規(guī)則中,你可以設(shè)置進(jìn)程名,進(jìn)程Id,機(jī)器名,線程ID等。我會在多線程調(diào)試小節(jié)中詳述其用法。

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

      數(shù)據(jù)便簽是應(yīng)用程序調(diào)試期間用于查看對象和變量的一種高級便簽消息。當(dāng)調(diào)試器執(zhí)行到斷點(diǎn)時(shí),將鼠標(biāo)移到對象或者變量上方時(shí),你會看到它們的當(dāng)前值。你甚至可以看到一些復(fù)雜對象(如dataset,datatable等等)的細(xì)節(jié)。數(shù)據(jù)便簽左上角有一個(gè)“+”號用于展開它的子對象或者值。
      debug20
      圖: 調(diào)試時(shí)的數(shù)據(jù)便簽(DataTips During Debugging)
      幾個(gè)月前,我發(fā)過一篇關(guān)于VS 2010 DataTip Debugging Tips的文章。
      下面是一些在調(diào)試時(shí)有用的特性。

      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)

      列出當(dāng)前方法中的所有變量。當(dāng)調(diào)試器停在某特定斷點(diǎn)并打開Autos窗口時(shí),將展示當(dāng)前范圍中與此值相關(guān)的變量。
      debug30_small
      圖:Local Variables

      5.2 自動窗口(Autos)

      這些變量由VS調(diào)試器在調(diào)試的時(shí)候自動檢測。VS檢測與當(dāng)前語句相關(guān)的對象或變量,基于此列出Autos變量。Autos Variable的快捷鍵是Ctrl + D + A。
      debug31_small
      圖:Autos - Ctrl + D, A

      5.3 監(jiān)視(Watch)

      Watch窗口用于添加變量。你可以添加任意多個(gè)變量。添加方法是,右擊變量并選擇“Add to Watch”。
      debug32_small
      圖:Watch - Ctrl + D, W
      也可以使用拖放(Drag and Drop)將變量添加到監(jiān)視窗口中。從監(jiān)視窗口中刪除變量的方法是,右擊變量并選擇“Delete Watch”。通過調(diào)試窗口,也可以在運(yùn)行時(shí)編輯這些變量值。

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

      若果變量中含有對象實(shí)例,左邊會有一個(gè)“+”號用于查看對象的屬性和成員。
      debug34_small
      圖:展開監(jiān)視變量

      5.3.1 Creating Object ID

      Visual Studio調(diào)試器提供另外一個(gè)強(qiáng)大的功能,支持我們?yōu)閷ο蟮娜魏我粋€(gè)特定實(shí)例創(chuàng)建一個(gè)對象ID(object ID)。這可以用于在任何時(shí)間監(jiān)控任意對象,甚至是該對象位于范圍(scope)之外。在監(jiān)視窗口(watch window)右擊特定對象變量,再單擊“Make Object ID”即可創(chuàng)建Object ID。
      debug35_small
      圖: 創(chuàng)建Object ID
      在對特定對象變量創(chuàng)建Object ID之后,Visual Studio會給這個(gè)對象添加一個(gè)數(shù)碼和“#”號,用來表示。
      debug36_small
      圖:添加Object ID后

      5.4 

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

      即時(shí)窗口是開發(fā)人員常用的功能。它可以在不改變當(dāng)前調(diào)試步驟的情況下修改變量值或者執(zhí)行一些語句。我們可以通過菜單調(diào)試 > 窗口 > 即時(shí)(Debug > Window > Immediate Window)打開即時(shí)窗口。即時(shí)窗口支持一組命令,可在調(diào)試的任何時(shí)刻執(zhí)行。它也支持Intellisense。在調(diào)試期間,我們可以在即時(shí)窗口中執(zhí)行任何命令或者代碼語句。
      debug37
      圖:基本即時(shí)窗口(Basic Immediate Window)
      這是對所有開發(fā)人員來說最為常用的特性,因此我就不一一介紹即時(shí)窗口的每一條命令了。

      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
      到此本文結(jié)束。希望你喜歡本文。請分享你的反饋和建議。

      12 深入學(xué)習(xí)(Further Study)

      13 總結(jié)(Summary)

      本文介紹了調(diào)試過程的基本內(nèi)容。介紹了如何使用VS調(diào)試一個(gè)應(yīng)用程序。我解釋了幾乎所有重要的工具以及它們的使用方法。對于并行程序調(diào)試,我只講了基礎(chǔ)部分。在深入學(xué)習(xí)小節(jié)中,深入講解了并行調(diào)試過程。如果你感興趣,請閱讀。我的主要目的是涵蓋Visual Studio中提供的幾乎所有調(diào)試工具。希望你從本文中學(xué)到了一些新知識。

      “自動窗口”(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á)式

      參考資料:

      [1] Mastering Debugging in Visual Studio 2010 - A Beginner's Guide

      [2] bug和缺陷的區(qū)別


        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多