Visual Studio 偵錯工具初探First look at the Visual Studio Debugger

本主題將介紹 Visual Studio 所提供的偵錯工具。This topic introduces the debugger tools provided by Visual Studio. 在 Visual Studio 內容中,當您「偵錯您的應用程式」,通常表示您正在執行附加偵錯工具的應用程式 (也就是在偵錯工具模式下)。In the Visual Studio context, when you debug your app, it usually means that you are running the application with the debugger attached (that is, in debugger mode). 執行此作業時,偵錯工具會提供許多方式來查看您程式碼所執行的功能。When you do this, the debugger provides many ways to see what your code is doing while it runs. 您可以逐步執行程式碼,並查看儲存在變數中的值,您可以設定變數的監看式以查看值何時變更,您可以檢查程式碼的執行路徑,例如 al。如果這是您第一次嘗試進行程式碼的偵錯工具,您可能會想要先閱讀 絕對初學者的偵錯工具 ,再進行本主題。You can step through your code and look at the values stored in variables, you can set watches on variables to see when values change, you can examine the execution path of your code, et al. If this is the first time that you've tried to debug code, you may want to read Debugging for absolute beginners before going through this topic.

此處所述的功能適用於 C#、C++、Visual Basic、JavaScript 及 Visual Studio 支援的其他語言 (除非另外註明)。The features described here are applicable to C#, C++, Visual Basic, JavaScript, and other languages supported by Visual Studio (except where noted).

設定中斷點,並啟動偵錯工具Set a breakpoint and start the debugger

若要偵錯,您必須啟動已將偵錯工具附加到應用程式處理序的應用程式。To debug, you need to start your app with the debugger attached to the app process. F5 鍵 ([偵錯] > [開始偵錯]) 是執行此作業的最常見方式。F5 (Debug > Start Debugging) is the most common way to do that. 不過,目前您可能尚未設定任何中斷點來檢查應用程式程式碼;因此我們會先執行此作業,再開始偵錯。However, right now you may not have set any breakpoints to examine your app code, so we will do that first and then start debugging. 中斷點是可靠偵錯最基本也最重要的功能。Breakpoints are the most basic and essential feature of reliable debugging. 中斷點會指出 Visual Studio 應暫停程式碼執行的地方,如此一來您可以查看變數的值或記憶體的行為,或查看程式碼分支是否正在執行。A breakpoint indicates where Visual Studio should suspend your running code so you can take a look at the values of variables, or the behavior of memory, or whether or not a branch of code is getting run.

如果您已在程式碼編輯器中開啟檔案,透過按一下程式碼行左側的邊界即可設定中斷點。If you have a file open in the code editor, you can set a breakpoint by clicking in the margin to the left of a line of code.

設定中斷點Set a Breakpoint

F5 (Debug > 開始調試 程式) 或 啟動偵錯工具按鈕在 偵錯工具列中 啟動調試 程式,偵錯工具會執行到它遇到的第一個中斷點。Press F5 (Debug > Start Debugging) or the Start Debugging button Start Debugging in the Debug Toolbar, and the debugger runs to the first breakpoint that it encounters. 如果尚未執行應用程式,則 F5 鍵會啟動偵錯工具並在第一個中斷點停止。If the app is not yet running, F5 starts the debugger and stops at the first breakpoint.

如果您知道要詳細檢查的程式碼行或程式碼區段,則中斷點是一個很有用的功能。Breakpoints are a useful feature when you know the line of code or the section of code that you want to examine in detail.

我們提供了大部分命令的鍵盤快速鍵,因為它們可讓應用程式程式碼的巡覽更加快速。We provide the keyboard shortcuts for most commands because they make navigation of your app code quicker. (功能表命令等對等命令會顯示在括弧中)。(Equivalent commands such as menu commands are shown in parentheses.)

若要啟動已附加偵錯工具的應用程式,請按下 F11 鍵 ([偵錯] > [逐步執行])。To start your app with the debugger attached, press F11 (Debug > Step Into). F11 鍵是 逐步執行 命令,可將應用程式執行一次往前推進一個陳述式。F11 is the Step Into command and advances the app execution one statement at a time. 當您使用 F11 鍵啟動應用程式時,偵錯工具會在執行的第一個陳述式上中斷。When you start the app with F11, the debugger breaks on the first statement that gets executed.

F11 逐步執行F11 Step Into

黃色箭號表示偵錯工具暫停時的陳述式,這也表示會在相同的點暫停執行應用程式 (尚未執行此陳述式)。The yellow arrow represents the statement on which the debugger paused, which also suspends app execution at the same point (this statement has not yet executed).

F11 鍵是以最詳細的方式檢查執行流程的好方法F11 is a good way to examine the execution flow in the most detail. (透過程式碼更快地移動,我們也會示範一些其他選項 ) 。根據預設,偵錯工具會略過非使用者程式碼 (如果您需要更多詳細資料,請參閱 Just My Code) 。(To move faster through code, we show you some other options as well.) By default, the debugger skips over non-user code (if you want more details, see Just My Code).

注意

在受控程式碼中,您會看到一個對話方塊,詢問您是否要在自動不進入屬性和運算子 (預設行為) 時收到通知。In managed code, you will see a dialog box asking if you want to be notified when you automatically step over properties and operators (default behavior). 如果您想要在稍後變更此設定,請在 [工具] > [選項] 功能表的 [偵錯] 下方停用 [不進入屬性和運算子] 設定。If you want to change the setting later, disable Step over properties and operators setting in the Tools > Options menu under Debugging.

不進入程式碼以略過函式Step over code to skip functions

當您位在函式或方法呼叫的程式碼行上時,可以按下 F10 鍵 ([偵錯] > [不進入函式]),而不是 F11 鍵。When you are on a line of code that is a function or method call, you can press F10 (Debug > Step Over) instead of F11.

F10 鍵會推進偵錯工具,而不需要逐步執行應用程式程式碼中的函式或方法 (此程式碼仍會執行)。F10 advances the debugger without stepping into functions or methods in your app code (the code still executes). 按下 F10 鍵即可略過您不感興趣的程式碼。By pressing F10, you can skip over code that you're not interested in. 如此一來,您可以快速地取得您更感興趣的程式碼。This way, you can quickly get to code that you are more interested in.

逐步執行屬性Step into a property

如前文所述,根據預設,偵錯工具會略過受控屬性和欄位,但 [逐步執行至特定處] 命令可讓您覆寫這個行為。As mentioned earlier, by default the debugger skips over managed properties and fields, but the Step Into Specific command allows you to override this behavior.

以滑鼠右鍵按一下屬性或欄位,並選擇 [逐步執行至特定處],然後選擇其中一個可用的選項。Right-click on a property or field and choose Step Into Specific, then choose one of the available options.

Visual Studio 偵錯工具的螢幕擷取畫面,其中已反白顯示一行程式碼。

在此範例中,[逐步執行至特定處] 會將我們帶到 Path.set 的程式碼。In this example, Step Into Specific gets us to the code for Path.set.

Visual Studio 偵錯工具的螢幕擷取畫面,其中顯示路徑的程式碼。請設定。

使用滑鼠快速執行至程式碼中的某一點Run to a point in your code quickly using the mouse

在偵錯工具中,將滑鼠停留在程式程式碼上,直到執行 以按一下 (執行至這裡) 按鈕  從 Visual Studio 偵錯工具的 [執行] 按鈕的螢幕擷取畫面。While in the debugger, hover over a line of code until the Run to Click (Run execution to here) button Screenshot of the Run to Click button from the Visual Studio Debugger. 按鈕表示執行應執行至放置按鈕的行。The button indicates that execution should run to the line where the button is placed. 出現在左側。appears on the left.

Visual Studio 偵錯工具的螢幕擷取畫面,其中顯示 [執行到按一下] 按鈕,顯示在 Update 函式的呼叫左邊。

注意

[執行至點選處] (執行到這裡) 按鈕會於 Visual Studio 2017Visual Studio 2017 開始提供。The Run to Click (Run execution to here) button is available starting in Visual Studio 2017Visual Studio 2017.

按一下 [執行至點選處] (執行到這裡) 按鈕。Click the Run to Click (Run execution to here) button. 偵錯工具將前進至您所點選的程式碼行。The debugger advances to the line of code where you clicked.

使用此按鈕類似於設定暫時中斷點。Using this button is similar to setting a temporary breakpoint. 這個命令也方便您在應用程式程式碼的可見區域內快速瀏覽。This command is also handy for getting around quickly within a visible region of app code. 您可以在任何開啟的檔案中使用 [執行至點選處]。You can use Run to Click in any open file.

在目前的函式外推進偵錯工具Advance the debugger out of the current function

有時候,您可能想要繼續偵錯工作階段,但讓偵錯工具一直推進到目前的函式。Sometimes, you might want to continue your debugging session but advance the debugger all the way through the current function.

按下 Shift + F11 (或 [偵錯] > [跳離函式])。Press Shift + F11 (or Debug > Step Out).

此命令會繼續執行應用程式 (並往前推進偵錯工具),直到目前的函式傳回為止。This command resumes app execution (and advances the debugger) until the current function returns.

執行至游標處Run to cursor

當您在偵錯工具) 中編輯程式碼 (而不是暫停時,請以滑鼠右鍵按一下應用程式中的程式程式碼,然後選擇 [執行至游標處] (或按 Ctrl 鍵) 。When you are editing code (rather than paused in the debugger), right-click a line of code in your app and choose Run to Cursor (or press Ctrl to F10). 此命令會開始偵錯,並在目前這一行程式碼上設定暫時中斷點。This command starts debugging and sets a temporary breakpoint on the current line of code.

執行至游標處Run to Cursor

如果您已設定中斷點,偵錯工具會在第一個叫用的中斷點上暫停。If you have set breakpoints, the debugger pauses on the first breakpoint that it hits.

按下 F5 鍵,直到您到達已選取 [執行至游標處] 的程式碼行為止。Press F5 until you reach the line of code where you selected Run to Cursor.

如果您正在編輯程式碼,而且想要快速設定暫時中斷點並同時啟動偵錯工具,此命令很有用。This command is useful when you are editing code and want to quickly set a temporary breakpoint and start the debugger at the same time.

注意

您可以在進行偵錯時,於 [呼叫堆疊] 視窗中使用 [執行至游標處]。You can use Run to Cursor in the Call Stack window while you are debugging.

快速重新啟動您的應用程式Restart your app quickly

按一下 [偵錯工具] 工具列中的 [ 重新 啟動 重新開機應用程式 ] 按鈕 (或按 Ctrl + Shift + F5) 。Click the Restart Restart App button in the Debug Toolbar (or press Ctrl + Shift + F5).

相對於停止應用程式並重新啟動偵錯工具,按下 [重新啟動] 可讓您節省時間。When you press Restart, it saves time versus stopping the app and restarting the debugger. 偵錯工具會在執行程式碼叫用的第一個中斷點處暫停。The debugger pauses at the first breakpoint that is hit by executing code.

如果您想要停止偵錯工具並回到 [程式碼編輯器],您可以按下紅色的停止 [停止 調試 程式] 按鈕,而不是 [ 重新開機]。If you do want to stop the debugger and get back into the code editor, you can press the red stop Stop Debugging button instead of Restart.

編輯您的程式碼並繼續進行 (c #、VB、c + +、XAML) 的偵錯工具Edit your code and continue debugging (C#, VB, C++, XAML)

在 Visual Studio 所支援的大部分語言中,您可以在偵錯工具的過程中編輯程式碼,並繼續進行偵錯工具。In most languages supported by Visual Studio, you can edit your code in the middle of a debugging session and continue debugging. 若要使用這項功能,請在偵錯工具中暫停時,按一下您的程式碼,並進行編輯,然後按 F5F10F11 以繼續進行偵錯工具。To use this feature, click into your code with your cursor while paused in the debugger, make edits, and press F5, F10, or F11 to continue debugging.

編輯後繼續調試Edit and continue debugging

如需使用功能和功能限制的詳細資訊,請參閱 編輯後繼續For more information on using the feature and on feature limitations, see Edit and Continue.

若要在偵錯工具期間修改 XAML 程式碼,請參閱 使用 XAML 熱重新載入撰寫和偵測執行中的 xaml 程式碼。To modify XAML code during a debugging session, see Write and debug running XAML code with XAML Hot Reload.

使用資料提示來檢查變數Inspect variables with data tips

現在您已稍為了解工作環境,接下來正好可以使用偵錯工具開始檢查應用程式狀態 (變數)。Now that you know your way around a little, you have a good opportunity to start inspecting your app state (variables) with the debugger. 可讓您檢查變數的功能是偵錯工具最實用功能之一,而且有不同的方法來完成此作業。Features that allow you to inspect variables are some of the most useful features of the debugger, and there are different ways to do it. 通常當您嘗試偵錯問題時,您會嘗試確定變數是否會儲存您希望其在特定應用程式狀態具有的值。Often, when you try to debug an issue, you are attempting to find out whether variables are storing the values that you expect them to have in a particular app state.

在偵錯工具中暫停時,請使用滑鼠將指標移至物件上方,您會看到它的預設屬性值 (在此範例中,檔案名稱 market 031.jpg 是預設屬性值)。While paused in the debugger, hover over an object with the mouse and you see its default property value (in this example, the file name market 031.jpg is the default property value).

查看資料提示View a Data Tip

展開物件以查看其所有屬性 (例如此範例中的 FullPath 屬性)。Expand the object to see all its properties (such as the FullPath property in this example).

通常在偵錯時,您希望能夠快速地檢查物件的屬性值,而資料提示很適合用來執行此作業。Often, when debugging, you want a quick way to check property values on objects, and the data tips are a good way to do it.

提示

在大部分支援的語言中,您可以在偵錯工作階段期間編輯程式碼。In most supported languages, you can edit code in the middle of a debugging session. 如需詳細資訊,請參閱編輯後繼續For more info, see Edit and Continue.

使用 [自動變數] 和 [區域變數] 視窗來檢查變數Inspect variables with the Autos and Locals windows

進行偵錯時,請查看程式碼編輯器底部的 [自動變數] 視窗。While debugging, look at the Autos window at the bottom of the code editor.

自動變數視窗Autos Window

在 [自動變數] 視窗中,您會看到變數及其目前值和其類型。In the Autos window, you see variables along with their current value and their type. [自動變數] 視窗會顯示目前這一行或上一行中使用的所有變數 (在 C++ 中,此視窗會顯示前三行程式碼中的變數。The Autos window shows all variables used on the current line or the preceding line (In C++, the window shows variables in the preceding three lines of code. 請參閱文件以了解語言特定行為)。Check documentation for language-specific behavior).

注意

在 JavaScript 中,支援 [區域變數] 視窗,但不支援 [自動變數] 視窗。In JavaScript, the Locals window is supported but not the Autos window.

接下來,請查看 [區域變數] 視窗。Next, look at the Locals window. [區域變數] 視窗會顯示位在目前範圍中的變數。The Locals window shows you the variables that are currently in scope.

本機視窗Locals Window

在此範例中,this 物件和 f 物件都在範圍內。In this example, the this object and the object f are in scope. 如需詳細資訊,請參閱在 [自動變數] 和 [區域變數] 視窗中檢查變數For more info, see Inspect Variables in the Autos and Locals Windows.

設定監看式Set a watch

您可以使用 [監看式] 視窗來指定您要留意的變數 (或運算式)。You can use a Watch window to specify a variable (or an expression) that you want to keep an eye on.

進行偵錯時,請以滑鼠右鍵按一下物件,然後選擇 [新增監看式]。While debugging, right-click an object and choose Add Watch.

監看式視窗Watch Window

在此範例中,您已於 f 物件上設定監看式,當您在偵錯工具中移動時,就可以看到其值的變更。In this example, you have a watch set on the f object, and you can see its value change as you move through the debugger. 不同於其他變數視窗,[監看式] 視窗一律會顯示所監看的變數 (它們在超出範圍時會呈現灰色)。Unlike the other variable windows, the Watch windows always show the variables that you are watching (they're grayed out when out of scope).

如需詳細資訊,請參閱使用 [監看式] 及 [快速監看式視窗] 設定監看式For more info, see Set a Watch using the Watch and QuickWatch Windows

檢查呼叫堆疊Examine the call stack

在您進行偵錯時按一下 [呼叫堆疊] 視窗,此視窗預設會在右下方的窗格中開啟。Click the Call Stack window while you are debugging, which is by default open in the lower right pane.

檢查呼叫堆疊Examine the Call Stack

[呼叫堆疊] 視窗會顯示方法和函式的呼叫順序。The Call Stack window shows the order in which methods and functions are getting called. 第一行會顯示目前的函式 (此範例中的 Update 方法)。The top line shows the current function (the Update method in this example). 第二行會顯示已從 Path.set 屬性呼叫 Update,依此類推。The second line shows that Update was called from the Path.set property, and so on. 呼叫堆疊是檢查並了解應用程式執行流程的好方法。The call stack is a good way to examine and understand the execution flow of an app.

注意

[呼叫堆疊] 視窗類似於某些 IDE (例如 Eclipse) 中的 [偵錯] 檢視方塊。The Call Stack window is similar to the Debug perspective in some IDEs like Eclipse.

您可以按兩下某一行的程式碼來查看其原始程式碼,這也會變更偵錯工具所檢查的目前範圍。You can double-click a line of code to go look at that source code and that also changes the current scope being inspected by the debugger. 這不會讓偵錯工具往前推進。This does not advance the debugger.

您也可以從 [呼叫堆疊] 視窗使用滑鼠右鍵功能表來執行其他動作。You can also use right-click menus from the Call Stack window to do other things. 例如,您可以在特定的函式中插入中斷點,使用 [執行至游標處] 重新啟動應用程式,並檢查原始程式碼。For example, you can insert breakpoints into specific functions, restart your app using Run to Cursor, and to go examine source code. 請參閱如何:檢查呼叫堆疊See How to: Examine the Call Stack.

檢查例外狀況Examine an exception

當您的應用程式擲回例外狀況時,偵錯工具會帶您前往擲回例外狀況的程式碼行。When your app throws an exception, the debugger takes you to the line of code that threw the exception.

例外狀況協助程式Exception Helper

在此範例中,例外狀況協助程式 會顯示 System.Argument 例外狀況和錯誤訊息,指出此路徑格式不合法。In this example, the Exception Helper shows you a System.Argument exception and an error message that says that the path is not a legal form. 因此,我們知道方法或函式的引數發生錯誤。So, we know the error occurred on a method or function argument.

在此範例中,DirectoryInfo 呼叫會對儲存在 value 變數中的空字串發出錯誤。In this example, the DirectoryInfo call gave the error on the empty string stored in the value variable.

例外狀況協助程式是可協助您偵錯錯誤的絶佳功能。The Exception Helper is a great feature that can help you debug errors. 您也可以執行檢視錯誤詳細資料,以及從例外狀況協助程式新增監看式等作業。You can also do things like view error details and add a watch from the Exception Helper. 或者;如有必要,您可以變更擲回特定例外狀況的條件。Or, if needed, you can change conditions for throwing the particular exception. 如需如何在程式碼中處理例外狀況的詳細資訊,請參閱偵錯技術和工具For more information on how to handle exceptions in your code, see Debugging techniques and tools.

注意

例外狀況協助程式會取代 Visual Studio 2017Visual Studio 2017 中開始的例外狀況助理。The Exception Helper replaced the Exception Assistant starting in Visual Studio 2017Visual Studio 2017.

請展開 [例外狀況設定] 節點以查看如何處理此例外狀況類型的更多選項,但是您不需要變更此導覽的任何項目!Expand the Exception Settings node to see more options on how to handle this exception type, but you don't need to change anything for this tour!

設定偵錯Configure debugging

您可以將專案設定為以 Debug 或 Release設定的形式建立、設定要進行偵錯工具的專案屬性,或設定要進行偵錯工具的 一般設定You can configure your project to build as a Debug or Release configuration, configure project properties for debugging, or configure general settings for debugging. 此外,您可以設定偵錯工具,以使用 DebuggerDisplay 屬性等功能或 c/c + + 的 NatVis 架構來顯示自訂資訊。In addition, you can configure the debugger to display custom information using features such as DebuggerDisplay attribute or, for C/C++, the NatVis framework.

每個專案類型都有特定的調試屬性。Debugging properties are specific to each project type. 例如,您可以在啟動應用程式時,指定要傳遞給該應用程式的引數。For example, you can specify an argument to pass to the application when you start it. 您可以用滑鼠右鍵按一下方案總管中的專案,然後選取 [ 屬性],來存取專案特定屬性。You can access the project-specific properties by right-clicking the project in Solution Explorer and selecting Properties. 調試屬性通常會出現在 [ 組建 ] 或 [ 調試 ] 索引標籤中,視特定的專案類型而定。Debugging properties typically appear in the Build or Debug tab, depending on the particular project type.

專案屬性Project properties

偵錯 Azure App Service 中的即時 ASP.NET 應用程式Debug live ASP.NET apps in Azure App Service

當您感興趣的程式碼執行時, 快照偵錯工具 會取得生產環境中應用程式的快照集。the Snapshot Debugger takes a snapshot of your in-production apps when code that you are interested in executes. 若要指示偵錯工具擷取快照集,您可以在程式碼中設定快照點和記錄點。To instruct the debugger to take a snapshot, you set snappoints and logpoints in your code. 偵錯工具可讓您清楚了解發生什麼問題,而不會影響實際執行應用程式的流量。The debugger lets you see exactly what went wrong, without impacting traffic of your production application. 快照集偵錯工具可協助您大幅縮短為解決出現在生產環境之問題所花費的時間。The Snapshot Debugger can help you dramatically reduce the time it takes to resolve issues that occur in production environments.

啟動快照偵錯工具Launch the snapshot debugger

快照集合適用於 Azure App Service 中執行的 ASP.NET 應用程式。Snapshot collection is available for ASP.NET applications running in Azure App Service. ASP.NET 應用程式必須在 .NET Framework 4.6.1 或更新版本上執行,而 ASP.NET Core 應用程式必須在 Windows 的 .NET Core 2.0 或更新版本上執行。ASP.NET applications must be running on .NET Framework 4.6.1 or later, and ASP.NET Core applications must be running on .NET Core 2.0 or later on Windows.

如需詳細資訊,請參閱使用快照偵錯工具偵錯即時 ASP.NET 應用程式For more information, see Debug live ASP.NET apps using the Snapshot Debugger.

使用 IntelliTrace 回溯檢視快照集 (Visual Studio Enterprise)View snapshots with IntelliTrace step-back (Visual Studio Enterprise)

IntelliTrace 回溯 會自動擷取應用程式在每個中斷點,以及偵錯工具步驟事件的快照。IntelliTrace step-back automatically takes a snapshot of your application at every breakpoint and debugger step event. 記錄的快照集可讓您回溯到先前的中斷點或步驟,以檢視應用程式過去的狀態。The recorded snapshots enable you to go back to previous breakpoints or steps and view the state of the application as it was in the past. 如果您想要查看先前的應用程式狀態,但不想要重新啟動偵錯或重新建立所需的應用程式狀態,IntelliTrace 回溯可節省您的時間。IntelliTrace step-back can save you time when you want to see the previous application state but don't want to restart debugging or recreate the desired app state.

您可以使用[偵錯] 工具列的 [逐步返回] 和 [逐步前進] 按鈕,來巡覽及檢視快照集。You can navigate and view snapshots by using the Step Backward and Step Forward buttons in the Debug toolbar. 這些按鈕可巡覽出現在 [診斷工具] 視窗之 [事件] 索引標籤中的事件。These buttons navigate the events that appear in the Events tab in the Diagnostic Tools window.

逐步執行和向前按鈕Step Backward and Forward Buttons

如需詳細資訊,請參閱使用 IntelliTrace 檢查先前的應用程式狀態頁面。For more information, see the Inspect previous app states using IntelliTrace page.

Debug 效能問題Debug performance issues

如果您的應用程式執行速度太慢或使用過多的記憶體,您可能需要提早流量分析工具測試您的應用程式。If your app runs too slowly or uses too much memory, you may need to test your app with the profiling tools early on. 如需分析工具的詳細資訊(例如 CPU 使用量工具和記憶體分析器),請參閱程式碼 剖析工具的第一看For more information about profiling tools such as the CPU Usage tool and the Memory Analyzer, see First look at the profiling tools.

下一步Next steps

在本教學課程中,您已快速瀏覽過許多偵錯工具功能。In this tutorial, you've had a quick look at many debugger features. 建議您深入了解這些功能,例如中斷點。You may want a more in-depth look at one of these features, such as breakpoints.