Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Using the DevStudio IDE Debugger

Before you begin to debug your program, remember that there are two main compiler configurations: Debug and Release mode. You can see the modes in the upper-left corner of the Project Settings dialog box (Alt+F7), and you can toggle between the modes there.

You can select from three choices after clicking the 'Settings For'drop-down box: Win32 Release, Win32 Debug, and All Configurations. The choice selected in this box when the program is built determines the mode in which it is built. Building the project with All Configurations selected builds both a Win32 release and a Win32 debug version.

Both the debug and release configurations are supplied whenever a new project is created. You can debug code only in Debug mode.

Compiler Warning Levels

On the C++ tab of the Project Settings dialog box, a Warning Level drop-down box offers various levels of warnings:

NONE No warnings
Level 1 Returns only the most severe warnings
Level 2 Returns less-severe warnings but ignores the most common ones
Level 3 Returns most general warnings (default setting)
Level 4 Extremely sensitive; returns the most warnings

Debug Info Settings

The Debug Info settings follow:

NONE No debugging data is generated; used mainly for Release modes
Line Numbers Only Creates line numbers to reference functions and global variables
C7 Compatible Produces debugging information compatible with Microsoft C 7.0
Program Database Creates a file with a .pdb extension that holds the maximum level of debugging information
Program Database for Generates a .pdb file with the maximum
Edit and Continue level of debugging information that can be used with the Edit and Continue feature

Using the Source Browser Tool

The Source Browser tool is an excellent tool for inspecting source code in great detail. You must enable the Generate Browse Info setting to use this tool. The check box is located in the Project Settings dialog box (Alt+F7), on the C++ tab, below the Optimizations drop-down box. The first time this runs for a project, it pops up a message box saying

Browse information is not available for this project. Do you want your build
settings altered (if necessary) and your project rebuilt to generate browse
information?

Click Yes to continue.

After you confirm that you want the build settings altered, the Browse dialog box appears. Here, you can enter an identifier for it to browse for. The identifier can be one of the following:

The Select Query box shows the options for displaying information about the identifier:

Breakpoints and Single-Stepping

Using breakpoints is an excellent method of debugging. Combined with single-stepping (stepping through the code one line at a time), this method is very effective at pinpointing exactly where a problem occurs.

You can set a breakpoint at any line of code in the program. After the program is started, it stops at the line of code that has the breakpoint and does not execute it unless you single step forward or continue running the program.

To add a breakpoint, select the line of code in which you want the breakpoint to appear. Then press F9, or click the hand icon on the Build minibar. A red dot appears next to the selected line of code. To remove a breakpoint, press F9 or click the hand while your cursor is positioned in the line of code.

A more involved way to add breakpoints is to select the Breakpoints option from the Edit menu. A Breakpoints dialog box appears, showing all current breakpoints that exist in the code. This box gives you the option to remove all breakpoints.

You can have multiple breakpoints throughout your program.

When the program is stopped at a breakpoint, you can view the contents of any variables by moving the mouse pointer over the variable text. Dragging any variable into the Watch window reveals more information on the variable.

A few options are available for traversing the program after it has stopped. The following sections examine these options.

Apply Code Changes

If program changes were made while debugging the program, this option allows the code to be recompiled. The shortcut key is Alt+F10. This is a new feature in Visual C++ 6.0.

Break Execution

Stops the program from running.

Go

Continues on with running the program after the program has been stopped by a breakpoint. The shortcut key is F5.

Restart

The program is restarted to the very beginning. The cursor stops at the first line of code to be executed. The shortcut key is Ctrl+Shift+F5.

Run to Cursor

Continues on with running the program and stops at the cursor position. The cursor position becomes a breakpoint when this program is run. The shortcut key is Ctrl+F10.

Step Into

The current line will be executed. The cursor can be followed inside function call one step at a time. The shortcut key is F11.

Step Over

The current line will be executed. If the cursor encounters a function call, it runs the function call at normal speed and returns the cursor to the position after the function. The shortcut key is F10.

Step Out

The program will run the function in which the cursor currently resides at normal speed. The program will stop again when it returns from the function. The shortcut key is Shift+F11.

Stop Debugging

Stops the debugger. The shortcut key is Shift+F5.

Debugger Windows

The DevStudio IDE comes with quite a few useful windows to assist in debugging. A few of the important ones are covered here.

Variables Window

The Variables window provides a quick-and-easy view to variables involved in the debugged program.

There are three window tabs:

A Context box also exists in the Variables window. This box enables you to select a context to specify the scope of the variables being displayed. A drop-down list displays the current call stack.

Although you can expand and collapse the variables display, you cannot add variables to this box (unlike the Watch window).

A powerful feature of the Context box is that it enables you to enter new values into any local variable that is not a constant. With this capability, you can perform on-the-fly value testing, as well as fix a value that crashed the program.

To modify the value of a local variable, do the following:

  1. Select the Auto tab or Locals tab in the Variables window.
  2. Choose the line of code containing the local variable that needs to be changed.
  3. Double-click the value, or move to the insertion point with the Tab key.
  4. Enter the new value and press Return.

Note that string variables cannot be lengthened and must be changed one character at a time.

Watch Window

You can use the Watch window to specify local or global variables that are to be monitored while the program is being debugged. The Watch window, like the Variables window, can change the values of local variables.

The Watch window contains four tabs: Watch1, Watch2, Watch3, and Watch4. Each tab can display a specified list of variables.

To add a variable to the Watch window, follow these steps:

  1. Open the Watch window through the View menu.
  2. Select a tab for the variable.
  3. Enter the variable name into the Name column on the tab.
  4. Press Return.

To view type information for a variable, right-click on a selected variable in the Watch window and choose Properties. Or, choose Properties from the View menu.

To remove a variable from the Watch window, do the following:

  1. Select the line with the variable you want to remove.
  2. Press the Delete key.

To modify the value of a variable, double-click the value in the Watch window. Or, you can tab down to the value and press Return. You cannot modify date/time, image, or text data types.

QuickWatch Window

You use the QuickWatch window to quickly find the values of SQL variables and parameters. You also can use the QuickWatch window to add a variable to the Watch window or to modify the value of a local variable. The same restrictions apply for changing values that apply for the Watch window.

Registers Window

The Registers window displays the contents of the CPU Registers, floating-point stack, and flags. You can use the Registers window to add Registers to the Watch window. You also can change Register and flag values during a debug session.

To view the value in a Register, open the Registers window. Any Register values that have changed recently appear in red.

To change the value of a Register, select the Register value you want to change and type the new value followed by Return.

Table 15.1 lists the Register flags and the set values for Intelx86 processors.

Table 15.1. Register Window Flags

Flag Set
Overflow O = 1
Direction D = 1
Interrupt I = 1
Sign S = 1
Zero Z = 1
Auxiliary carry A = 1
Parity P = 1
Carry C = 1

Call Stack Window

The Call Stack window displays the stack of function calls that are still active. Whenever a function is called, it is pushed onto the stack. Whenever a function returns, it is popped off the stack.

The top of the Call Stack window displays the current function being executed; the bottom of the window displays previous function calls. The window also displays the parameter types and values for each function call.

Disassembly Window

The Disassembly window displays code instructions in disassembled form. You can set the breakpoint on any instruction and step through the code just like regular code.

The Disassembly window is extremely useful for stepping through code that contains multiple instructions separated by semicolons—for example,

a = 1; b = 2, c = 3;

The regular debugger treats this line of code as one step through the code. The Disassembly window treats the line one instruction at a time. Stepping through and viewing code one instruction at a time is very beneficial when dealing with optimized code.

Memory Window

You use the Memory window to view large amounts of data that are not fully displayed in the Watch and Variables windows. Such data includes buffers, large strings, and other large data objects.

Note that the default is to display numbers in decimal format.

Share ThisShare This

Informit Network