Hướng dẫn debug trong visual studio 2023 năm 2024

Để trở thành lập trình viên thì chúng ta phải học code và những môn bổ trợ. Để trở thành lập trình viên giỏi thì ta cần phải biết nhiều thứ khác nhau, debug cũng là một thứ mà một lập trình viên giỏi nhất định phải thành thạo.

Trước khi khi học ở trường đại học thì tôi không biết về nó nên khi đi làm thì tôi gặp rất nhiều khó khăn, vì vậy tôi viết bài này chia sẻ về debug, hy vọng sẽ giúp các bạn nhiều hơn về con đường lập trình.

1. Debug là gì?

Debug là một công cụ gỡ lỗi mà một ngôn ngữ lập trình nào cũng phải có, nói đến debug thì mọi người sẽ nghĩ đến chương trình bị lỗi thì sẽ sử dụng công cụ này để tìm lỗi, tuy nhiên đó chỉ là một phần đúng mà thôi. Ngoài chức năng gỡ lỗi của nó thì nó còn cho chúng ta biết những thứ khác, những thứ đó là gì thì chúng ta cùng tìm hiểu các ví dụ thực tế nhé.

2. Debug trong ngôn ngữ javascript

Để hiểu về debug thì bạn cần biết được bốn thứ quan trọng đó là: breakpoint, step over, step into, watch.

2.1 Breakpoint là gì?

Breakpoints đó là điểm dừng, bất kể bạn làm việc trên javascript hay bất kỳ một ngôn ngữ nào khác cũng đều có breakpoints, khi bạn debug thì cần đặt một điểm dừng ở trong source code, nơi mà bạn đang nghi ngờ chỗ đó phát sinh bug.

Quan sát hình bên trên, vùng khoanh màu đỏ [dòng 8] thì tôi có đặt một breakpoints, khi chương trình của tôi chạy tới dòng 8 thì tự động chương trình sẽ dừng lại và không chạy tiếp cho đến khi tôi tác động.

2.2 Step over là gì?

Step over là di chuyển xuống một dòng, sau vị trí đặt breakpoints

Quan sát hình bên trên thì ta thấy vị trí ban đầu đã di chuyển xuống dòng 9, còn breakpoints của dòng 8 đã đặt trước đó thì vẫn sẽ giữ nguyên như cũ để cho lần chạy tiếp theo.

2.3 Step into là gì?

Step into là di chuyển vị trí debug vào trong function con, bạn hãy nhìn hình bên trên sẽ thấy function getLocalStorage[]. Step into sẽ di chuyển debug vào hàm này.

2.4 Watch là gì?

Watch là nơi mà hiển thị các thông tin của biến, giá trị trả về của function, để chúng ta xem và phán đoán bug, đồng thời có thể đưa ra được cách giải quyết cái bug của mình.

Quan sát hình bên trên thì tôi có thể xem được các dữ liệu của biến key truyền vào function getLocalStorage[key] là gì. Và kết quả trả về của getLocalStorage là gì.

2.5 Cách debug trong javascript

Để debug javascript thì tôi sẽ dùng công cụ developer tool của google chrome. Để mở công cụ này lên thì nhấn F12 thôi, khá là đơn giản, khi đó giao diện của bạn sẽ như thế này.

Khi mở lên thì chọn tab Source, nơi đây sẽ chứa những file html, js, css, image dự án của bạn.

2.5.1 Đặt một breakpoints

Để đặt một điểm dừng cho debug thì tôi sẽ nhấn chuột trái vào vùng khoanh màu đỏ.

2.5.2 Step over

Để di chuyển sang dòng tiếp theo thì nhấn F10, hoặc nút được khoanh màu đỏ hình ở bên dưới.

2.5.3 Step into

Để di chuyển vào trong hàm con của getLocalStorage thì nhấn F11, hoặc phấn vào nút được khoanh màu đỏ ở hình bên dưới.

2.5.4 Watch

Để xem dữ liệu của các kiến hay giá trị trả về thì bôi đen giá trị muốn xem và nhấn chuột phải tại giá trị đó và chọn “Add selected text to watches”.

Nhấn F8 để nhảy đến vị trí đặt breakpoints kế tiếp, nếu không có vị trí đặt breakpoints kế tiếp thì chương trình sẽ tiếp tục chạy.

Trên đây là những cái cơ bản nhất của debug, có thể các bạn xem xong phần này nhưng vẫn sẽ thắc mắc vì chưa hình dung được khi vào dự án thực tế thì bug sẽ như thế nào, cách giải quyết ra sao. Nếu bạn thắc mắc như vậy thì đó là điều hiển nhiên, vì bài này tôi viết chỉ ở mức cơ bản, tiếp cận về debug mà thôi, ở bài viết sau tôi sẽ viết những bài nâng cao hơn, tôi sẽ tự đặt ra những ví dụ thực tế về những cái mà tôi đã gặp phải và cách giải quyết chúng. Lúc đó bạn sẽ hiểu rõ thêm về sức mạnh của debug là gì.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Debugging with Visual Studio for Mac

  • Article
  • 08/30/2023

In this article

Important

Visual Studio for Mac is scheduled for retirement on August 31, 2024 in accordance with Microsoft’s Modern Lifecycle Policy. While you can continue to work with Visual Studio for Mac, there are several other options for developers on Mac such as the preview version of the new C# Dev Kit extension for VS Code.

Learn more about support timelines and alternatives.

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

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 uses the Mono Soft Debugger to debug managed [C# or F#] code in all Xamarin applications. The Mono Soft debugger is different from regular debuggers in that it's 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. 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. It can still be useful when debugging Xamarin applications if you need to debug native iOS or Android code.

For .NET Core and ASP.NET Core applications, Visual Studio for Mac uses the .NET Core debugger. 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

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:

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 will display four buttons, allowing you to run and step through the code. In Visual Studio for Mac, they'll look like:

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 isn't 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 select 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.

Caution

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. 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 to single step from the debugger to inspect how things are working under the hood.

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

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:

Chủ Đề