![]() |
Using the VC++.NET Debugger - Student Primer |
|
| Intro to
the Debugger Introduction Objective Prerequisites The Debugger Debugging Windows Last Modified On 08/10/2002 |
Introduction to the VC++.NET DebuggerIntroductionVisual 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. ObjectiveThis document is intended to help students get started with the Visual C++.NET debugger. It is not intended to be detailed or thoroughjust a helpful introduction to the Visual Studio.NET debugger. PrerequisitesThis 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 DebuggerDebug ToolbarClicking 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. 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. Stop Debugging stops the debugger from executing and returns you to the design environment. Restart restarts the debugger from the beginning resetting all variables. 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. 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. 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. 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. 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. The drop-down box beside the Breakpoints button contains a host of other available windows. Debug MenuThe Visual Studio.NET Debug menu contains all of the above selections for those that prefer using menus over toolbars. DebuggingSteppingStepping 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, Note the yellow arrow left of the code pane, which represents the execution point of the debugger. BreakpointsBreakpoints 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. You can access breakpoint options by <Ctrl><B> or clicking the menu option Debug/Set Breakpoint. Mouse OverA 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. 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. WindowsCall StackThe 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 systems initial, startup call to main()but knowing that this call usually wont 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 WindowThe 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 PaneIn 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. 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. 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 assignede.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). |