vs_net-comp.gif (3966 bytes) etsu1.jpg (6093 bytes)

Using the VC++.NET Debugger - Student Primer

Intro to the Debugger
Introduction
Objective
Prerequisites

The Debugger
Debug Toolbar
Debug Menu

Debugging
Stepping
Breakpoints
Mouse Over

Windows
Call Stack
Watch Window
Executing Code Pane

etsulogo.gif (5198 bytes)

acm_logo.gif (7101 bytes)

Last Modified On 08/10/2002

Written by the faculty and staff of ETSU


Introduction to the VC++.NET Debugger

Introduction

Visual Studio.NET contains a very capable and easy to use debugger. It is recommended that the student becomes very familiar with debugging techniques and the use of the Visual C++.NET debugger.

Objective

This document is intended to help students get started with the Visual C++.NET debugger. It is not intended to be detailed or thorough—just a helpful introduction to the Visual Studio.NET debugger.

Prerequisites

This document assumes you already have installed all necessary .NET Framework components as well as Visual Studio.NET. You are expected to have at least a basic understanding of C++ language constructs as well as how to create projects and compile code. ( see VC++ Primer )

The Debugger

Debug Toolbar

Clicking the Visual Studio.NET menu selection View/Toolbars reveals all the toolbars available to the user. After clicking Debug you should see the following toolbar.

dbtoolbar.JPG (3867 bytes)

Here is a brief description of the more useful functions of the Debug toolbar.


The Start button will result in your being asked if it should re-compile/re-build if you have edited anything since the last successful compile/build.

dbtbstart.JPG (4277 bytes)


Stop Debugging stops the debugger from executing and returns you to the design environment.

dbtbstop.JPG (5027 bytes)


Restart restarts the debugger from the beginning resetting all variables.

dbtbrestart.JPG (4676 bytes)


Step Into allows you to step through the code line-by-line into a function that is called. This means that you can watch the execution of called functions as well.

dbtbstepinto.JPG (4864 bytes)


Step Over allows you to step through the code line-by-line, but runs any functions called at full speed.  This is used when you want to watch a function execute, but you want to treat function calls as if they were single statements and not watch the called functions execute.

dbtbstepover.JPG (4563 bytes)


Step Out is useful if you step into a function you would rather not execute line-by-line. This is quite often used when accidentally stepping into a system function that is little help to you during your debugging session.

dbtbstepout.JPG (4577 bytes)


Hexadecimal Display converts all decimal variable values that are in the watch window, local window, and other windows into hexadecimal values. It also converts mouse over values to hexadecimal. More on this later.

dbtbhexdisp.JPG (5180 bytes)


Breakpoints opens the breakpoint window and integrates it along with other windows at the bottom of Visual Studio.NET environment. This window contains information concerning all breakpoints that are currently set.

dbtbbreakpts.JPG (4794 bytes)


The drop-down box beside the Breakpoints button contains a host of other available windows.

dbtbothers.JPG (23301 bytes)

Debug Menu

The Visual Studio.NET Debug menu contains all of the above selections for those that prefer using menus over toolbars.

dbmenu.JPG (19937 bytes)

Debugging

Stepping

Stepping through your code is as simple as clicking the Step Into or Step Over toolbar button or <Ctrl><F11> and <Ctrl><F10> respectively. For example, after you have compiled and linked your code, you can simply hit the <Ctrl><F11> keys to begin your debug session. You will see something like the figure below,

dbsession.JPG (99996 bytes)

Note the yellow arrow left of the code pane, which represents the execution point of the debugger.

Breakpoints

Breakpoints are set by clicking in the left margin in the code pane on the line of code you want to halt execution on. All breakpoints will be listed in the breakpoints window at the bottom if you choose to display the breakpoint window. See below for an example.

dbsession2.JPG (107928 bytes)

You can access breakpoint options by <Ctrl><B> or clicking the menu option Debug/Set Breakpoint.

Mouse Over

A very nice feature of many development environments is seeing the value of a variable by simply holding the mouse cursor over the variable name. See below.

dbsession3.JPG (15022 bytes)

Also, if the Hex button on the Debug toolbar is activated, note what happens to all decimal variables including what is in the watch window and mouse over display tool-tip window below.

dbsession4.JPG (40496 bytes)

Windows

Call Stack

callstack.JPG (88369 bytes)

The bottom right pane in the above figure contains the Call Stack.  This pane shows that the current line is line 19 of the function named Stack :: mPop was called from line 17 of the main function. (The bottom right pane also shows the system’s initial, “startup” call to main()—but knowing that this call usually won’t matter in your debugging.)  <usually, since argc/argv/env may be important to check in some circumstances> Call Stack information can be important for determining “how the program got where it is.”

Watch Window

watch.JPG (16779 bytes)

The bottom left pane shows Visual Studio’s Watch Window.  The Watch Window shows the values of a program’s variables and data objects. Note that the nSum, nOldValue, strName, and strSSN variables represent “values” of these variable at this instance.

watch2.JPG (46456 bytes)

The length of the string strName is currently 10 characters as determined by its current value. If an item in a watch represents an object, it may be expanded as shown here. Primitive types of variables such as int or float variables cannot be expanded (of course), but their values are shown directly in the watch window.

Executing Code Pane

In the code pane, we see the executing source code.  The next line to be executed is marked with the arrow in the margin.  This means that the previous line just executed.

execwindow.JPG (18348 bytes)

In addition to showing the executing code, this pane may be used to get a Quick Watch on a variable in the code. If you simply place the cursor over a variable and leave it there momentarily, the current value of that variable is displayed in a tool tip window next to the cursor. 

This Quick Watch is illustrated with the variable named ptrMyStack->mnLastValuePushed in the above figure. Note the current value of mnLastValuePushed is shown in the tool tip window as 20.

Note: If a compound variable reference, such as above, is intended as the subject of a Quick Watch, it may be necessary to highlight the entire reference since both components identify the item in question. 

execwindow2.JPG (11426 bytes)

Important: the Quick Watch window displays the values that variables currently have.  The window does not show any values that a variable is about to be assigned—e.g., any values on the current line. In the example above, the value of the variable nOldValue has not been set by the main function but the value of nNewValue has been set to 0 (or initialized).