Webocreation

Thursday, November 19, 2009

Using Developer Studio Debugger

Using Developer Studio Debugger

The debugging tools in Developer Studio enable you to test your C++, MFC, and mixed-language applications. You can set and manage breakpoints, view and modify variables, step through your application, and interpret the call stack. The Visual C++ browse windows described later in this section enable you to view the relationships between symbols, variables, and classes. "Just-in-time" debugging, described later in this section, enables you to run your application outside the Developer Studio environment.

During debugging, you can stop the process to examine the state of an executable application, a dynamic-link library (DLL), threads, or an ActiveX control, at nearly any point during application execution.

Debugging is a two-step process. First, you correct compile-time errors that prevent you from building your project, such as incorrect syntax, misspelled keywords, or type mismatches. Then you use the Developer Studio debugger to detect and correct logic errors and errors in sequencing, branching, and interaction between application components.

This section describes the debugging tools and techniques used to resolve common programming problems with MFC projects written in Visual C++. This section includes the following topics:

Setting and Managing Breakpoints

Use the Breakpoints dialog box to set, remove, disable, enable, or view breakpoints. On the Edit menu, click Breakpoints. The Breakpoints dialog box will appear.

Setting Breakpoints

The breakpoints you set will be saved as a part of your project. You can set breakpoints in the following places:

® At a source-code line

® At the beginning of a function

® At the return point of a function

® At a label

To set a breakpoint at a source-code line

1. In a source window, move the insertion point to the line where you want the program to break.

2. Click the Insert/Remove Breakpoint toolbar button (a hand) on the Build MiniBar toolbar.

A red dot appears in the left margin, indicating that the breakpoint has been set. To see an illustration that shows an example of code with a breakpoint set, click this icon.

Note If you want to set a breakpoint on a source-code statement extending across two or more lines, you must set the breakpoint on the last line of the statement.

To set a breakpoint at the beginning of a function

1. In the Find box on the Standard toolbar, type the function name.

2. Click the Insert/Remove Breakpoint toolbar button.

In the source code, a red dot appears in the left margin at the beginning of the function, indicating that the breakpoint has been set.
To set a breakpoint at the return point of a function

1. On the View menu, point to Debug Windows, and then click Call Stack.

2. In the Call Stack window, move the insertion point to the function where you want the program to break.

3. Click the Insert/Remove Breakpoint toolbar button.

A red dot appears in the left margin, indicating that the breakpoint has been set.

To set a breakpoint at a label

1. In the Find box on the Standard toolbar, type the name of the label.

2. Click the Insert/Remove Breakpoint toolbar button.

A red dot appears in the left margin at the line containing the label, indicating that the breakpoint has been set.

If you set more than one location breakpoint on a line, and some breakpoints are disabled while others are enabled, a gray dot will appear in the left margin. The first time you click the Enable/Disable Breakpoint toolbar button, all breakpoints on the line will become disabled, and the gray dot will change to a hollow circle. If you click the Enable/Disable Breakpoint button again, all breakpoints on the line will become enabled, and the hollow circle will change to a red dot.

An asterisk (*) in the Breakpoint check box indicates that the breakpoint is not supported on the current platform.

Viewing Breakpoints

You can view the list of current breakpoints or you can view the source code or disassembled code where a breakpoint is set.

To view the list of current breakpoints

1. On the Edit menu, click Breakpoints.

2. Use the scroll bar to move up or down the Breakpoints list.

To view the source code or disassembled code where a breakpoint is set

1. In the Breakpoints list, select a line-number breakpoint.

2. Click Edit Code.

You will see the source code for the breakpoint set at the line number.

Disabling Breakpoints

You can disable an individual breakpoint, or you can disable all breakpoints in an application.

To disable a single breakpoint

1. For a location breakpoint in a source-code window or in the Call Stack or Disassembly window, move the insertion point to the line containing the breakpoint you want to disable.

2. Click the Enable/Disable Breakpoint toolbar button, or click the right mouse button, and click Disable Breakpoint on the shortcut menu.

– or –

1. In the Breakpoints dialog box, find the breakpoint you want to disable in the Breakpoints list.

2. Clear the check box corresponding to the breakpoint that you want to disable, and then click OK.

When a location breakpoint is disabled, the red dot in the left margin changes to a hollow circle.

– or –

— Use the SPACEBAR to toggle the state of one or more breakpoints in the Breakpoints list.
To disable all breakpoints

— Click the Disable All Breakpoints toolbar button.

The red dots in the left margin change to hollow circles.

Stepping Through an Application

The Developer Studio debugger enables you to step through your application in two ways. You can:

® Run the application and execute the next statement (Step Into).

® Step into a specific function.


To run the application and execute the next statement (Step Into)

1. While the application is paused at a breakpoint, click Step Into on the Debug menu.

The debugger executes the next statement, then pauses execution. If the next statement is a function call, the debugger steps into that function, then pauses execution at the beginning of the function.

2. Repeat Step 1 to continue executing the application one statement at a time.

If you step into a nested function call, the debugger steps into the most deeply nested function. For more information and examples about debugging nested functions, see "Stepping Into Functions" in the Developer Studio online documentation.

To step into a specific function

1. Set a breakpoint just before the nested function call, or use the Step Into, Step Over, or Run To Cursor command to advance the application execution to that point.

2. In a source window, select the function that you want to step into.

3. On the Debug menu, or on the shortcut menu associated with a source window, click Step Into Name, where Name is the selected function.

The debugger executes the function call and pauses execution at the beginning of the selected function.

Step Into Specific Function works for any number of nesting levels. You can sometimes use Step Into Specific Function to step into a member function — for example, MemberFn in the line of C++ code CMyClass::MemberFn();.

Viewing and Altering Variables

You can use the Developer Studio debugger to view and alter variables.

Viewing Variables

The Developer Studio debugger gives you several options for viewing variables and expressions. You can view:

® The value of a variable or expression.

® The value of a variable using QuickWatch.

® A variable or expression in the Watch window.

® Type information for a variable in the Watch window.

® A variable in the Variables window.

® Type information for a variable in the Variables window.

To view the value of a variable or expression

— Pause the mouse pointer over the variable or expression until a pop-up window displays the variable's value.

To view the value of a variable using QuickWatch

1. When the debugger is stopped at a breakpoint, switch to a source window and right-click a variable (Var, for example).

2. On the shortcut menu, click QuickWatch.

3. Click Recalculate.

The value will be displayed in the spreadsheet field.

4. Click Close.


To view a variable or expression in the Watch window

1. On the View menu, point to Debug Windows, and then click Watch.

2. Select a tab for the variable or expression.

3. Type, paste, or drag the variable name or expression into the Name column on the tab. If you typed the variable name, press ENTER.

The Watch window evaluates the variable or expression immediately and will display the value or an error message.

If you add an array or object variable to the Watch window, plus sign (+) or minus sign () boxes will appear in the Name column. Use these boxes to expand or collapse your view of the variable. You can change the display format (to display Unicode characters, for example) of variables in the QuickWatch dialog box or in the Watch window using formatting symbols. For more information, see "Symbols for Watch Variables" in the Developer Studio online documentation.

To view type information for a variable in the Watch window

1. In the Watch window, select the line containing the variable whose type you want to see.

2. On the View menu, click Properties.

To view a variable in the Variables window

1. On the View menu, point to Debug Windows, and then click Variables.

2. Click the Auto tab, Locals tab, or This tab, according to the type of variables you want to see.

To view type information for a variable in the Variables window

1. In the Variables window, click the Auto tab, Locals tab, or This tab.

2. Select the line containing the variable whose type you want to see.

3. On the View menu, click Properties.

Altering Variables

The Developer Studio debugger gives you several options for altering variables and expressions. You can modify the value of a variable:

® Using QuickWatch.

® Using the Watch window.

® In the Variables window.

To modify the value of a variable using QuickWatch

1. On the Debug menu, click QuickWatch.

2. In the Expression text box, type the variable name.

3. Click Recalculate.

4. If the variable is an array or object, use the plus sign (+) box to expand the view until you see the value you want to modify.

5. Use the TAB key to move to the value you want to modify.

6. Type the new value, and then press ENTER.

7. Click Close.

Note To change the value of an array, modify the individual fields or elements. You cannot edit an entire array at once.

To modify the value of a variable using the Watch window

1. In the Watch window, double-click the value, or use the TAB key to move the insertion point to the value you want to modify.

2. If the variable is an array or object, use the plus sign (+) box to expand the view until you see the value you want to modify.

3. Type the new value, and press ENTER.

To modify the value of a variable in the Variables window

1. In the Variables window, click the Auto tab, Locals tab, or This tab.

2. Select the line containing the value you want to modify.

3. If the variable is an array or object, use the plus sign (+) box to expand the view until you see the value you want to modify.

4. Double-click the value, or use the TAB key to move the insertion point to the value you want to modify.

5. Type the new value, and press ENTER.


  • Viewing the Call Stack

You can use the Developer Studio debugger to view and change the call stack for a function.


To view the call stack for a function

1. Place the insertion point in the function.

2. On the Debug menu, click Run to Cursor to execute your application to the location of the insertion point.

The Locals tab of the Variables window is updated automatically to display the local variables for the function or procedure.

3. On the View menu, point to Debug Windows, and then click Call Stack.

The calls are listed in the calling order, with the current function (the most deeply nested) at the top. To jump to the location in the code where a function is implemented, double-click the function name in the call stack. To see an illustration that shows how an application, a call stack, and the floating debugger can be displayed together, click this icon.


Note To run the application to the return address, select the function in the Call Stack window, and click Run to Cursor on the Debug menu.

To set or remove a breakpoint at a function return address, select the function in the Call Stack window, and click the Insert/Remove Breakpoint toolbar button.

To change the call stack display


1. On the Tools menu, click Options.

2. Click the Debug tab.

3. In the Call Stack window, select Parameter Values or Parameter Types, according to the information you want to display.

Note The Context box at the top of the Variables window contains a drop-down list of call stack functions. If you select one of these, the debugger window views will change accordingly. You cannot use the call stack to trace back through Windows messages.

Running to a Specific Location

When debugging your application, you can choose to run the application until you reach a specific location. You can:

® Run until a breakpoint is reached.

® Run to the cursor location.

® Run to the cursor location in object code.

® Run to the cursor location in the call stack.

® Run until reaching a specified function.

® Set the statement you want to run next.

To run until a breakpoint is reached

1. Set a breakpoint.

2. On the Build menu, point to Start Debug, and then click Go.

To run to the cursor location

1. Open a source file, and move the insertion point to the location where you want the debugger to break.

2. On the Build menu, point to Start Debug, and then click Run to Cursor.

To run to the cursor location in object code

1. On the View menu, point to Debug Windows, and then click Disassembly.

2. In the Disassembly window, move the insertion point to the location where you want the debugger to break.

3. On the Debug menu, click Run to Cursor.

To run to the cursor location in the call stack

1. On the View menu, point to Debug Windows, and then click Call Stack.

2. In the Call Stack window, select the function name.

3. On the Debug menu, click Run to Cursor.

To run until reaching a specified function

1. In the Find box on the Standard toolbar, type the function name.

2. On the Build menu, point to Start Debug, and then click Run to Cursor.


Note You can use the Run to Cursor command to return to an earlier statement to retest your application, using different values for variables.

To set the statement you want to run next

1. In a source window, move the insertion point to the statement or instruction that you want to run next.

2. Right-click the mouse, then click Set Next Statement on the shortcut menu.

Using Visual C++ Browse Windows

The Visual C++ browse windows display information about the symbols (classes, functions, data, and macros) in a program. If you turn on browse information when building a project, the compiler creates .sbr files with information about each program file in the project. The BSCMAKE utility (BSCMake.exe) assembles these .sbr files into a single browse information file that includes the project's base name and the extension .bsc.

You view browse information in browse windows that have different appearances and different controls, depending on the type of information displayed. By using browse commands, you can examine:

® Information about all the symbols in any source file.

® The source-code line in which a symbol is defined.

® Each source-code line where there is a reference to a symbol.

® The relationships between base classes and derived classes.

® The relationships between calling functions and called functions.

When you open a project workspace, the project browse file will open automatically. You can enable the browser, disable the browser, and open or close the browse information files.

To enable or disable creation of the .sbr files at compile time

1. Open the project if it is not open.

2. On the Project menu, click Settings. To see an illustration that shows the Project Settings dialog box, click this icon.





3. Click the C/C++ tab.

4. Select or clear the Generate browse info check box, and then click OK.


To enable or disable updating of the .bsc file at compile time

1. Open the project if it is not open.

2. On the Project menu, click Settings.

3. Click the Browse Info tab.

4. Select or clear the Build browse info file check box, and then click OK.

Note Once browse files have been generated for the project, you can access a floating Browse menu. Right-click anywhere on the Developer Studio toolbar to display a pop-up menu. Click Browse, and the Browse toolbar will appear. Use the Browse toolbar buttons to obtain browse information for your project.

If you do not need browse information, you can speed up the build process by turning off browse information. When the browse option is off, .sbr files are not generated, and the .bsc file is not updated.

To speed up your builds and update your browse information file quickly, turn on creation of .sbr files and turn off updating of the .bsc file. When you want to update your browse information file, turn on .bsc file updating, and then build your project.

To open or close the browse information file

— On the Tools menu, click Source Browser to open browse files or Close Source Browser File to close browse files, and then click OK.

Note When you use a browse information file, the .bsc file stays open for the duration of the session unless you close it. If you run NMAKE outside the development environment, you should close the .bsc file to allow updating. As long as the .bsc file remains open, it cannot be updated.

Enabling Just-in-Time Debugging

In Developer Studio, you can edit, compile, link, debug, and test an application in a single integrated environment. However, sometimes you may want to test an application outside the Developer Studio environment. With just-in-time debugging, you can run an application outside Developer Studio; when an application error occurs, just-in-time debugging calls the Developer Studio debugger.

To use just-in-time debugging, set the just-in-time debugging option before you execute your program. If you do not set this option, the debugger cannot respond to errors.
To enable just-in-time debugging

1. On the Tools menu, click Options.

2. Click the Debug tab.

3. Select the Just-In-Time Debugging check box, and then click OK.

4. On the Build menu, click Build <projectname>.exe.

Note If you are running Windows NT, you must have Administrator privileges to set the just-in-time option

No comments:

Post a Comment