programming4us
programming4us
DESKTOP

Visual Studio 2010 : Understanding Debugging

10/13/2010 5:25:37 PM
When you use debugging, you can stop your code during execution and inspect it. Visual Studio provides a sophisticated Debugger that allows you to
  • Control the execution of your code so that you can peek inside your code while it's running.

  • Test your code while you're designing it.

  • View the data used by your code.

  • Get detailed information about exceptions that occur.

1. Enabling debugging

To use the Visual Studio Debugger with your code, you must build your code by using the Debug build configuration. The Debug build configuration tells the compiler to create a program database (PDB) file for your code. The PDB file stores the data about your source code, such as

  • Source code line numbers

  • Variable names

  • Method names

The Visual Studio Debugger uses the PDB file to know how to access your source code while it's being executed.

NOTE

The data stored in the PDB are symbols.

You should generate PDB files for all builds, including release builds, so that you can debug the build, even if you didn't build it as a debug build.


To set your application to use the Debug build configuration, choose Debug from the Solution Configurations drop-down list on the Standard toolbar.

2. Firing up the Debugger

Your application has two modes in the Debugger: executing or breaking. While your application is executing, everything's A-okay. After your application stops executing, or breaks, then you get busy with the Debugger.

Your application executes until it encounters either of the following:

  • Breakpoint: A marker you set in the code that tells the Debugger to stop execution

  • Exception: An unhandled error generated by your code that causes program execution to stop

While your code is in break mode, you have the features of the Debugger at your disposal for examining your code.

2.1. Controlling execution

One of the primary features of the Visual Studio Debugger is the ability to control your program's execution. Controlling execution allows you to walk through your code, as well as check the values of variables and data.

The Debugger provides the following code execution options:

  • Starting: This starts your code. You must use the Debug build configuration for the Debugger to fire up.

  • Stepping: This allows you to step through your code.

  • Breaking: You can break the code's execution by setting a breakpoint or by manually breaking. Your code breaks automatically when an unhandled exception occurs.

  • Stopping: Execution stops when your program completes execution or you manually stop it.

You have several ways to start the Debugger:

  • Debug menu: From here, choose Start Debug, Step Into, or Step Over to execute your code.

  • Run to Cursor: Right-click an executable line of code and choose Run to Cursor from the shortcut menu. The Debugger starts your code and breaks at the line where your cursor sits.

  • Keyboard shortcuts: Table 1 lists common keyboard shortcuts you can use to control the Debugger.

    You can use the keyboard shortcuts almost exclusively to step through code.


  • Debug toolbar: The Debug toolbar acts like standard VCR buttons for controlling your code's execution. Figure 1 shows the Debug toolbar.

Figure 1. Use the Debug toolbar to control your code's execution.

Table 1. Common Debugging Keyboard Shortcuts
Keyboard ShortcutCommand
F5Start Debugging
Ctrl+F5Start without Debugging
F11Step Into
F10Step Over
Shift+F11Step Out

2.2. Breaking execution

The first step in using the Debugger is breaking your code during execution. To break your code during execution, you can do any one of the following:

  • Click the Break All button on the Debug toolbar.

  • Choose Break All from the Debug menu.

  • Press Ctrl+Alt+Break.

In most cases, you'll likely set a breakpoint in your code before you start execution. By setting a breakpoint, your code stops executing at the line of code where the breakpoint is set. You can then step through the code and watch your variables while the code executes.

Here's how to set a breakpoint:

  1. Go to the line of source code in the Code Editor where you want code execution to break.

  2. From the Debug menu, choose Toggle Breakpoint.

    A solid glyph (red circle) appears in the Code Editor's left gutter. The breakpoint appears in the Breakpoints window. Figure 2 shows the breakpoint.

You can use alternative ways to set breakpoints, such as the following:

  • Click the left gutter in the Code Editor to toggle a breakpoint for an executable line of code.

  • Right-click a line of code and choose Breakpoint from the shortcut menu.

  • Press F9.

Figure 2. Set a breakpoint in the Code Editor.

You can use the Breakpoints window to manage your breakpoints. From here, you can

  • Create new breakpoints.

  • Enable or disable breakpoints.

  • Set conditions on when the breakpoint occurs.

  • Filter the breakpoint for specific machines, processes, and threads.

  • Specify how many times the code should execute before execution breaks.

Here's how to create a new breakpoint via the Breakpoints window:

  1. Press Ctrl+Alt+B to display the Breakpoints window.

  2. Choose New=>Break at Function.

    The New Breakpoint window appears.

  3. In the Function text box, type the name of the function where you want to create the breakpoint.

    For example, type SayHello() to create a breakpoint at a function called SayHello().

  4. Click OK.

    The function appears in the Breakpoints window, as shown in Figure 3.

Figure 3. Use the Breakpoints window to add new breakpoints.

2.3. Stepping through code

Visual Studio provides several options for controlling the execution of your code. Besides just starting, pausing, and stopping code execution, you can step through your code one statement at a time. The three step options are

  • Step Into: Breaks on each line of code

  • Step Over: Breaks on each line of code except functions

    The function executes, and code execution breaks at the first line outside the function.

  • Step Out: Resumes execution on a function and then breaks at the first line of code outside the function

Step Into and Step Over start the Debugger at the next line of code. Step Out allows you to stop debugging a function but then break execution as soon as the function exits.

To step through your code, follow these steps:

  1. Set breakpoints in your code.

    For example, create a form with a button and add a Click event for the button. Set the breakpoint on the button's Click event to break the code's execution when you click the button.

  2. Press F5 to start the Debugger.

    Visual Studio builds your code and starts execution. The code executes until it encounters a breakpoint. Figure 4 shows an example of the Debugger hitting a breakpoint. The Code Editor highlights the code in yellow.

    Figure 4. The Debugger highlights the code statement when it hits a breakpoint.
  3. Press F11 to step into the next line of execution.

  4. Continue to press F11 until you encounter a function.

    • Step into: To follow the execution into the function, press F11 to step into the function.

    • Step over: To execute the function without stepping through it, press F10 to step over the function.

    • Step out: To step out of the function after you enter it, press Shift+F11.

    Figure 5 shows an example of the Debugger stepping into a function. The Debugger hit the breakpoint in line 25 in Figure 5. Press F11 to step through each line. At line 25 is the function SayHello(). Step into the function, and the Debugger jumps to line 26 , where the next statement to execute exists. The arrow in the Code Editor's left gutter shows the current line being executed.

  5. Press Shift+F11 to step out of the function.

    The Debugger executes the function and returns to the calling line of code.

    In the case of the code in Figure 5, pressing Shift + F11 returns you to line 53.

    Figure 5. Use the Debugger to step through your code.
  6. Resume stepping through the code by pressing F11.

    F5 is the shortcut key for the Continue command. Press F5 at any time to resume execution until the code terminates or hits another breakpoint.


The Debug toolbar has three buttons that correspond to the Step Into, Step Over, and Step Out commands, respectively. Refer to Figure 1.

You can use the Run to Cursor command to execute your code to the cursor. Just right-click a line of code in the Code Editor and then choose Run to Cursor from the shortcut menu. Code execution breaks at the line where your cursor is positioned.

To know whether your code is running or debugging, look for Running or Debugging, respectively, in the title bar. Figure 7-10 shows an example.

2.4. Viewing data with the Debugger

The Visual Studio Debugger provides several windows for managing debugging. By default, these windows automatically appear in the bottom of the screen while you're debugging. You can access and open debugging windows by choosing the Debug=>Windows menu selection. Table 2 lists the debugging windows and their functions.

Figure 6. The title bar displays the execution status of your program.

Table 2. Debugging Windows and Their Functions
WindowTypeAccessShortcut KeyFunction
BreakpointsExecution controlDebug menuCtrl+Alt+BCreates a new breakpoint or edits an existing breakpoint
OutputFeedbackDebug menuN/ADisplays results or feedback
ScriptN/ADebug menuCtrl+Alt+NLists scripts used by your Explorer program
WatchVariableDebug menuCtrl+Alt+W, 1Displays variables and expressions you add to the window
AutosVariableDebug menuCtrl+Alt+V, ADisplays variables used in the current and preceding lines of code
LocalsVariableDebug menuCtrl+Alt+V, LDisplays all variables in scope
ImmediateVariableDebug menuCtrl+Alt+IExecutes commands and sets variable values
Call StackMemoryDebug menuCtrl+Alt+CDisplays functions in memory
ThreadsCPUDebug menuCtrl+Alt+HDisplays a set of instructions being executed by your program
ModulesMemoryDebug menuCtrl+Alt+ULists the software modules used by your program
ProcessesMemoryDebug menuCtrl+Alt+ZDisplays processes which your program has launched or attached to
MemoryMemoryDebug menuCtrl+Alt+M, 1Displays contents of memory
DisassemblyCPUDebug menuCtrl+Alt+DDisplays assembly code of your program
RegistersCPUDebug menuCtrl+Alt+GDisplays registers' contents
QuickWatchVariableDebug menuCtrl+Alt+QDisplays values of current in break mode value or expression
ExceptionFeedbackN/AN/ADisplays information about an Assistant exception when an exception occurs
DataTipsVariableHover mouse over variableN/ADisplays value of variable in scope

Figure 7 shows the tabs of the debugging windows at the bottom of a screen. Click a tab to access the window.

Figure 7. Use the debugging windows (bottom of the screen) to debug your code.

Many of the debugging windows allow you to observe the contents of your variables and data structures.

Visual Studio offers the DataTips feature that allows you to view the contents of a variable. Simply hover your mouse over a variable that's active, and a DataTip appears that displays the variable's value. Figure 8 shows a DataTip. Notice that you can click the down arrow next to a DataTip and choose a different visualizer. Visualizers display data in different forms to make it easier to read and interpret.

Figure 8. Hover your mouse over a variable to display its value in a DataTip.

The variable in Figure 8 is a string variable. You can use DataTips to display complex variables, such as DataSets.

Visual Studio also provides the Autos, Locals, and Watch windows for viewing variable values. The Autos and Locals windows populate automatically. You use the Watch window to identify a specific variable that you want to monitor.

To use the Watch window to monitor a DataSet, follow these steps:

  1. Set a breakpoint in your code where you wish to monitor a variable.

  2. Press F5 to start the Debugger.

  3. When code execution breaks, choose Debug=>Windows=>Watch 1.

    The Watch window appears.

    NOTE

    The Debugger must be in break mode to open the Watch window. Four Watch windows are available, and each window has a number from 1 to 4 appended to its Watch name.

  4. Right-click the object that you want to add to the Watch window.

  5. Choose Expression=>Add Watch from the shortcut menu.

    The object appears in the Watch window.

    NOTE

    Be sure to click the object — not a property of the object.

  6. Press F5 to resume execution.

    The code executes until it hits a breakpoint.

    When the variable or object is in scope (part of the currently executing code), its values appear in the Watch window, as you can see in Figure 9.

Figure 9. Use the Watch window to view the values of a variable or object.

You can add objects to the Watch window by typing their names in the Name field. For example, to add a Watch using a DataSet with the name ds

  1. Open the Watch window (using the preceding steps).

  2. Type ds in the Name field.

    IntelliSense lists the properties and methods that you can select.

  3. Select a member of the DataSet to watch.

    Figure 10 shows the Watch window with DataSet members added.

Figure 10. Use IntelliSense to add members to the Watch window.

3. Exception handling in native C++ applications

Structure exception handling isn't limited to only applications built on the .NET framework. You can also take advantage of structured exception handling and the powerful Visual Studio debugger for your native C++ applications. Many C++ libraries that are built into Visual Studio provide structured exceptions. Some common exceptions provided by built-in libraries appear in Table 3.

Table 3. Native C++ Exception Classes
Exception ClassLibraryPurpose
std::exceptionStandard Template Library (STL)Base class for all STL exceptions
std::bad_allocStandard Template Library (STL)Thrown when an STL function can't allocate required memory
CAtlExceptionActive Template Library (ATL)Base class for all ATL exceptions
CExceptionMicrosoft Foundation Classes (MFC)Base class for all MFC exceptions
CMemoryExceptionMicrosoft Foundation Classes (MFC)Out-of-memory exception
CFileExceptionMicrosoft Foundation Classes (MFC)File exceptions

Consider the simple console application code in Figure 11 that attempts to open a file and output the contents to the console window.

Figure 11. Native C++ application with structured exception handling.

In the code in Figure 11, the file foo.txt doesn't exist, so the code flows into the structured exception handler in the catch() block. As you can see, the Visual Studio debugger provides you the same features as it does for debugging .NET applications. Figure 12 shows an example of debugging a native C++ application.

Figure 12. Debugging native C++ applications.

4. Debugging for those special circumstances

The Visual Studio Debugger is sophisticated, and you can use it to debug very complex scenarios. Using the Debugger in a single-tier Windows application is about as easy as it gets. In the real world, however, applications aren't quite that simple.

You can use the Visual Studio Debugger to debug any of these items:

  • ASP.NET Web sites

  • Managed stored procedures

  • Scripts

  • Code on remote servers

NOTE

Going through the setup processes for each of these is beyond the scope of this book. See the Visual Studio documentation for more information.

After you get the Debugger set up to work in your scenario, controlling execution and watching variables are basically the same.

5. Debugging generated code

Visual Studio generates a lot of code for you. Sometimes, you'll want to step through that code with the Debugger so you can see how the code works. To step into code that is not yours, however, you must disable the Debugger's Just My Code feature.

To disable the feature, follow these steps:

  1. Choose Tools=>Options.

    The Options window appears.

  2. Click Debugging.

  3. Clear the Enable Just My Code check box.

  4. Click OK.

Now, you can hit breakpoints inside generated code. To see generated code, click the Show All Files button on the Solution Explorer toolbar.

Other  
 
Video
PS4 game trailer XBox One game trailer
WiiU game trailer 3ds game trailer
Top 10 Video Game
-   Rise of Incarnates [PC] Zeus Trailer
-   Heroes Reborn | The Extraordinary Among Us (Preview)
-   Battleborn | E3 2015 Gameplay Demo
-   Fortnite [PC] Mac Showcase Trailer
-   Overwatch [PC] Zarya Gameplay Trailer
-   Tony Hawk's Pro Skater 5 [PS3/PS4/X360/XOne] THPS Is Back Trailer
-   Bombing Busters Trailer
-   Blade & Soul 'What is Blade & Soul?' Trailer
-   Cast of the Seven Godsends 'Plague Armour' Trailer
-   Guncraft X360 Trailer
-   Disgaea 5: Alliance of Vengeance | Official Trailer
-   XCOM 2 [PC] E3 2015 Gameplay Trailer
-   RONIN | Turn-Based Action Platformer
-   Balance Benny | Trailer
-   We Happy Few | An Uncle Jack Episode - Nighty Night, The Pied Piper of Hamlyn, Part1
Game of War | Kate Upton Commercial
programming4us
 
 
programming4us