Cách Debug Trong Visual Studio 2017

  -  

In this article

Visual Studio for Mac has debuggers with support for .NET Core, .NET Framework, Unity, và Xamarin applications.

Bạn đang xem: Cách debug trong visual studio 2017

Visual Studio for Mac uses the Mono Soft Debugger, which is implemented inlớn the Mono runtime, allowing Visual Studio for Mac to debug managed code across all platforms.

The Debugger

Visual Studio for Mac uses the Mono Soft Debugger to lớn debug managed (C# or F#) code in all Xamarin applications. 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 và Mono runtime cooperate with the IDE lớn provide a debugging experience. The Mono runtime exposes the debugging functionality through a wire protocol, which you can read more about in the Mono documentation.

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.

For .NET Chip Core and ASPhường.NET Vi xử lý Core applications, Visual Studio for Mac uses the .NET Vi xử lý Core debugger. This debugger is also a cooperative debugger & works with the .NET runtime.

Using the debugger

To start debugging any application, always ensure that the configuration is phối khổng lồ Debug. The debug configuration provides a helpful mix of tools to lớn support debugging, such as breakpoints, using data visualizers, and viewing the Điện thoại tư vấn stack:

*

Setting a breakpoint

To set a breakpoint in your IDE, cliông xã on the margin area of your editor, next to the line number of the code where you wish khổng lồ break:

*

You can view all the breakpoints that have been mix in your code by going to the Breakpoints Window:

*

Start debugging

To start debugging, select the target browser, device, or simulator/emulator:

*
*

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 mix 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-clichồng on the editor margin, to the left of the line number for the code you wish khổng lồ phối a breakpoint on, and select New Breakpoint:

*

To add a condition lớn an existing breakpoint, right-clichồng 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 lớn occur:

*

Stepping through code

When a breakpoint has been reached, the Debug tools enable you lớn get control over the program"s execution. Visual Studio for Mac will display four buttons, allowing you lớn run and step through the code. 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 gọi, Step Over will exedễ thương the function, and will stop at the next line of code after the function.

Xem thêm: {Bí Kíp} Cách Chơi Vạn Bảo Các Vlcm 2021, Hướng Dẫn Cách Chơi Vạn Bảo Các Vlcm

Step Into - This will also exeđáng yêu the next line of code. If the next line is a function gọi, Step Inkhổng lồ will stop at the first line of the function, allowing you lớn 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 lớn 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 cliông xã và drag the arrow lớn a different line of code lớn change which statement will be executed. You can achieve the same thing by also right-clicking on a line of code và selecting Set Next Statement from the context menu.

*


Caution

Changing the current line of execution can cause unexpected behavior in an application. There are also some conditions where changing the next statement lớn exedễ thương is not possible. For example, dragging the arrow from one method khổng lồ another method will not work. 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.


Debugging Mono"s class libraries

Xamarin products ship with the source code for Mono"s class libraries, and you can use this khổng lồ single step from the debugger lớn inspect how things are working under the hood.

Xem thêm: 30 Loại Bánh Bột Mì, Cách Làm Bánh Từ Bột Mì Trứng Sữa, Bột Năng Bột Nếp, Bột Bắp Không Cần Lò Nướng Tại Nhà

Since this feature consumes more memory during debugging, it is turned off by default.

To enable this feature, browse to Visual Studio for Mac > Preferences > Debugger và ensure that the "Step into lớn external code" option is selected, as illustrated below: