使用 Visual Studio for Mac 進行調試Debugging with Visual Studio for Mac

Visual Studio for Mac 具有可支援 .NET Core、.NET Framework、Unity 和 Xamarin 應用程式的偵錯工具。Visual Studio for Mac has debuggers with support for .NET Core, .NET Framework, Unity, and Xamarin applications.

Visual Studio for Mac 使用 Mono Soft Debugger,它實作到 Mono 執行階段之中,讓 Visual Studio for Mac 能跨所有平台進行 Managed 程式碼的偵錯。Visual Studio for Mac uses the Mono Soft Debugger, which is implemented into the Mono runtime, allowing Visual Studio for Mac to debug managed code across all platforms.

偵錯工具The Debugger

Visual Studio for Mac 使用 Mono Soft Debugger 針對所有 Xamarin 應用程式進行 Managed (C# 或 F#) 程式碼的偵錯。Visual Studio for Mac uses the Mono Soft Debugger to debug managed (C# or F#) code in all Xamarin applications. Mono 軟偵錯工具與一般偵錯工具不同,因為它是內建于 Mono 執行時間的合作式偵錯工具。產生的程式碼和 Mono 執行時間會與 IDE 合作,以提供偵錯工具體驗。The Mono Soft debugger is different from regular debuggers in that it is a cooperative debugger that is built into the Mono runtime; the generated code and Mono runtime cooperate with the IDE to provide a debugging experience. Mono 執行階段會透過網路通訊協定公開偵錯功能,您可以在 Mono 文件中深入了解。The Mono runtime exposes the debugging functionality through a wire protocol, which you can read more about in the Mono documentation.

硬式偵錯工具,例如 LLDBGDB,會控制程式,而被偵錯的程式不必知情或合作,但在您需要偵錯原生 iOS 或 Android 程式碼,偵錯 Xamarin 應用程式時仍然十分有用。Hard debuggers, such as LLDB or GDB, control a program without the knowledge or cooperation from the debugged program, but can still be useful when debugging Xamarin applications in the event that you need to debug native iOS or Android code.

若是 .NET Core 和 ASP.NET Core 應用程式,Visual Studio for Mac 使用 .NET Core 偵錯工具。For .NET Core and ASP.NET Core applications, Visual Studio for Mac uses the .NET Core debugger. 此偵錯工具也是合作的偵錯工具,可搭配 .NET 執行時間使用。This debugger is also a cooperative debugger and works with the .NET runtime.

使用偵錯工具Using the debugger

若要開始進行任何應用程式的偵錯,請務必確定組態已設為 [偵錯]。To start debugging any application, always ensure that the configuration is set to Debug. 偵錯組態會提供有用的一組工具來支援偵錯,例如中斷點、使用資料的視覺化檢視,以及檢視呼叫堆疊:The debug configuration provides a helpful set of tools to support debugging, such as breakpoints, using data visualizers, and viewing the call stack:


設定中斷點Setting a breakpoint

若要在您的 IDE 中設定中斷點,請在編輯器按一下您要中斷之程式碼行號旁邊的邊界區域:To set a breakpoint in your IDE, click on the margin area of your editor, next to the line number of the code where you wish to break:


您可以前往 [ 中斷點] 視窗 來查看程式碼中已設定的所有中斷點:You can view all the breakpoints that have been set in your code by going to the Breakpoints Window :


開始偵錯Start debugging

若要啟動偵錯工具,請選取目標瀏覽器、裝置或模擬器/模擬器:To start debugging, select the target browser, device, or simulator/emulator:

Debug configuration  選取目標裝置Debug configuration Select target device

然後按 [播放] 按鈕,或 Cmd + return 部署您的應用程式。Then deploy your application by pressing the Play button, or Cmd + return. 當您到達中斷點時,程式碼反白顯示為黃色:When you hit a breakpoint, the code will be highlighted yellow:


偵錯工具,例如用來檢查物件值的工具,可在此時用來取得程式碼中發生情況的詳細資訊:Debugging tools, such as the one used to inspect the values of objects, can be used at this point to get more information about what is happening in your code:


條件式中斷點Conditional breakpoints

您也可以設定規則,指出應該發生中斷點的情況,這就是新增「條件中斷點」。You can also set rules dictating the circumstances under which a breakpoint should occur, this is known as adding a conditional breakpoint. 若要設定條件中斷點,請存取 [中斷點屬性] 視窗,這可以透過兩種方式進行:To set a conditional breakpoint, access the Breakpoint Properties window , which can be done in two ways:

  • 若要新增條件中斷點,請在您想要設定中斷點的程式碼行號左邊,以滑鼠右鍵按一下編輯器邊界,然後選取 [新增中斷點]:To add a new conditional breakpoint, right-click on the editor margin, to the left of the line number for the code you wish to set a breakpoint on, and select New Breakpoint:

[中斷點] 操作功能表

  • 若要將條件加入至現有的中斷點,請以滑鼠右鍵按一下中斷點,然後選取 [ 中斷點屬性 ],或在 [ 中斷點] 視窗 中,選取 [編輯中斷點] 按鈕,如下所示:To add a condition to an existing breakpoint, right-click on the breakpoint and select Breakpoint Properties , or, in the Breakpoints Window , select the Edit Breakpoint button illustrated below:


然後,您可以輸入想要中斷點發生的條件:You can then enter the condition under which you want the breakpoint to occur:


逐步執行程式碼Stepping through code

當已到達中斷點時,偵錯工具可讓您控制程式的執行。When a breakpoint has been reached, the Debug tools enable you to get control over the program's execution. Visual Studio for Mac 會顯示四個按鈕,可讓您執行與逐步執行程式碼。Visual Studio for Mac will display four buttons, allowing you to run and step through the code. 在 Visual Studio for Mac 中,它們看起來如下所示:In Visual Studio for Mac, they will look like the following:


下面是四個按鈕:Here are the four buttons:

  • 播放 - 這會開始執行程式碼,直到下一個中斷點。Play - This will begin executing the code, until the next breakpoint.
  • 不進入一 -這會執行下一行程式碼。Step Over - This will execute the next line of code. 如果下一行是函式呼叫,不進入函式會執行該函式,並在函式「之後」的下一行程式碼停止。If the next line is a function call, Step Over will execute the function, and will stop at the next line of code after the function.
  • 逐步執行 - 這也會執行下一行程式碼。Step Into - This will also execute the next line of code. 如果下一行是函式呼叫,逐步執行會停止在函式的第一行,讓您繼續一行一行地進行函式的偵錯。If the next line is a function call, Step Into will stop at the first line of the function, allowing you to continue line-by-line debugging of the function. 如果下一行不是函式,它的行為與「不進入函式」相同。If the next line is not a function, it will behave the same as Step Over.
  • 跳出 -這會回到呼叫目前函式的行。Step Out - This will return to the line where the current function was called.

變更接下來要執行的語句Change which statement is executed next

當偵錯工具暫停時,邊界上的箭號會指出接下來所要執行的程式碼行。While the debugger is paused, an arrow in the margin shows which line of code will be executed next. 您可以按一下該箭號,將其拖曳至不同的程式碼行來變更所要執行的陳述式。You can click and drag the arrow to a different line of code to change which statement will be executed. 您也可以在程式程式碼上按一下滑鼠右鍵,然後從操作功能表選取 [設定下一個語句] ,來達到相同的目的。You can achieve the same thing by also right-clicking on a line of code and selecting Set Next Statement from the context menu.



變更目前的執行行可能會導致應用程式發生未預期的行為。Changing the current line of execution can cause unexpected behavior in an application. 此外,也有一些條件是無法變更下一個要執行的語句。There are also some conditions where changing the next statement to execute is not possible. 例如,將箭號從某個方法拖曳到另一個方法將無法運作。For example, dragging the arrow from one method to another method will not work. 在這些不受支援的情況下,Visual Studio for Mac 將會顯示一個對話方塊,讓您知道不可能變更目前執行的行。In these unsupported cases, Visual Studio for Mac will display a dialog to let you know it was not possible to change the current line of execution.

Mono 類別庫偵錯Debugging Mono's class libraries

Xamarin 產品隨附 Mono 類別庫的原始程式碼,您可以使用它從偵錯工具逐步檢查一切背後運作的情況。Xamarin products ship with the source code for Mono's class libraries, and you can use this to single step from the debugger to inspect how things are working under the hood.

因為這項功能會在偵錯期間消耗較多的記憶體,所以它預設會關閉。Since this feature consumes more memory during debugging, it is turned off by default.

若要啟用這項功能,請流覽至 Visual Studio for Mac > 喜好設定 > 偵錯工具 ,並確認已選取 [ 逐步執行外部程式碼 ] 選項,如下 示:To enable this feature, browse to Visual Studio for Mac > Preferences > Debugger and ensure that the " Step into external code " option is selected , as illustrated below:


請參閱See also