Lab 13: Debugging the STUpload Application

In this section, you will use the Visual C++ debugger to investigate the STUpload application as it runs. The lab exercises implement the debugger's Variables, Watch, Call Stack, and Disassembly windows, and demonstrate how these windows all contribute to the debugging effort. You will also set breakpoints at various locations and examine the effects of data breakpoints.

Estimated lesson time: 15 minutes

Running STUpload in the Debugger

In this exercise, you will run STUpload in the debugger until the MFC framework calls the application's CMainFrame::OnCreate() function. This function executes early in the application's run cycle, before documents and views are created and before the STUpload window appears on the screen. This process provides the opportunity to single-step through OnCreate() for an inside view of how an MFC program begins life.

The first step of the exercise is to build a debug version of the program, which is necessary to run the debugger. If you have already created a debug version of STUpload for labs in earlier chapters, skip to the second procedure.

  • To build a debug version
    1. Open the STUpload project.
    2. In the larger of the two text boxes on the Build toolbar, select Win32 Debug. Alternatively, you can click the Set Active Configuration command on the Build menu and select Win32 Debug.
    3. Click the Build button on the Build toolbar, or click the Build command on the Build menu to compile and link the application.

  • To stop execution at CMainFrame::OnCreate()
    1. On the FileView tab of the Visual C++ Workspace window, expand the list of source files. Double-click MainFrm.cpp in the list to open the file.
    2. Scroll down to find the beginning of the OnCreate() function, which looks like this in the source code:
    3. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

    4. Click the function's opening brace to set the insertion point there. This action identifies the line at which the program should stop.
    5. Launch the debugger by choosing Start Debug from the Build menu and clicking the Run to Cursor command.

    The debugger begins first, then automatically runs the STUpload program. When CMainFrame::OnCreate() gains control, execution halts at the line containing the insertion point. The instruction has not yet executed, but is next in line to do so when the program resumes.

    Although no C++ code appears associated with the function's opening brace, code nevertheless exists before the first C++ instruction, as you can see by clicking the Disassembly tool in the debugger toolbar. The assembly language instructions that appear in the source window are the function's prologue code, in which a stack frame is created and the class's this pointer is stored as a local variable. Click the Disassembly tool again to restore the normal Source window.

    Stepping Through Code

    This exercise demonstrates how to single-step through the OnCreate() function using the Step Into, Step Over, and Step Out tools. The exercise also shows how to read information in the debugger's Variables and Call Stack windows while stepping through code.

  • To see the Variables and Call Stack windows
    1. If the Variables window does not appear on your screen, click the Variables tool on the debugger toolbar to expose the window. In its undocked state, it appears as shown in Figure 13.13.
    2. click to view at full size.

      Figure 13.13 The Visual C++ debugger's Variables window

    3. Click the plus icon adjacent to the lpCreateStruct name in the Variables window to expand the listing. Because the function's parameter is the only variable on the current function's stack frame, lpCreateStruct is the only variable listed in the window. It points to a CREATESTRUCT structure, the various fields of which appear in the Variables window when the view is expanded. Expanding the view shows that, for example, the structure's lpszName field contains a pointer to the application's name.
    4. Click the debugger's Call Stack tool to expose the Call Stack window (Figure 13.14). This window shows the course taken to reach the OnCreate() function, which heads the list. The second function in the call stack list identifies the function that has called OnCreate(), and to which control will return when OnCreate() finishes. In this case, the caller is CWnd::OnWndMsg(), part of the MFC framework.
    5. click to view at full size.

      Figure 13.14 The Visual C++ debugger's Call Stack window

    6. Click the Call Stack tool again to hide the Call Stack window.

  • To single-step through OnCreate()
    1. Click the Step Over tool or press the F10 key to step through the first two or three instructions of OnCreate(). Each time you invoke the Step Over command, execution halts at the next instruction as indicated by the yellow instruction arrow. Notice how the Variables window shows the application's data changing with each instruction.
    2. Continue single-stepping until the instruction arrow points to the line that sets up docking for the application's toolbar:
    3. m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);

    4. Click the Step Into tool to step into the framework's CControlBar:: EnableDocking() function. The debugger automatically locates the func-tion's source file Bardock.cpp and opens it. Execution halts at the function's first instruction.
    5. Single-step through CControlBar::EnableDocking() if you want, then click the debugger's Step Out tool to leave. The rest of the function executes, and when control returns to CMainFrame::OnCreate(), execution halts at the instruction following the call to CControlBar::EnableDocking():
    6. EnableDocking(CBRS_ALIGN_ANY);

    Setting Breakpoints

    This exercise demonstrates how to set breakpoints in another source file. Breakpoints are most commonly set before launching the debugger, but as you will see in this exercise you can set or clear breakpoints at any time, even while the debugger is active.

  • To use a breakpoint to interrupt when importing a file
    1. Open the STUploadDoc.cpp source file and scroll down to locate the CSTUploadDoc::LoadData() function:
    2. BOOL CSTUploadDoc::LoadData(CStdioFile &infile)

    3. In the LoadData() function, locate the while loop that reads each line of a .dat file, as described in Chapter 4:
    4. while(infile.ReadString(strTemp))      {        BOOL bValidDate = FALSE;        CString strFund;        CString strDate;        .        .        .

    5. Click anywhere on the while instruction to set the insertion point there.
    6. Press F9 to set a location breakpoint. A small red dot appears in the selection margin to indicate the line has been tagged with a breakpoint.
    7. Click the Go tool on the debugger's toolbar or press the F5 key. The STUpload application resumes execution, finishing the CMainFrame::OnCreate() function that had been interrupted. The STUpload main window now appears. At this point, the STUpload program is active, not the debugger. The debugger does not become active again until the program encounters a breakpoint.
    8. On the program's Data menu, click Import and browse for the Test.dat file in the \Chapter 4\Data folder. When you open the file, control stops at the breakpoint set in the CSTUploadDoc::LoadData() function.
    9. Right-click the following line and click either Remove Breakpoint or Disable Breakpoint from the shortcut menu. The first command removes the breakpoint; the second makes the breakpoint unavailable without removing it.
    10. while(infile.ReadString(strTemp))

    11. Single-step through a few instructions, noting in the Variables window how variables such as StrDate are correctly initialized.

  • To set a data breakpoint
    1. On the Edit menu, click Breakpoints, or press CTRL+B to invoke the Breakpoints dialog box.
    2. On the Data tab, type bFirstLine in the text box labeled Enter the expression to be evaluated. This action establishes a data breakpoint that triggers only when the function's bFirstLine variable changes in value.
    3. Click OK to close the Breakpoints dialog box.
    4. Click the Go command again to resume execution.

    You will probably experience a short pause as the while loop continues because of the drag on execution speed exacted by data breakpoints. The program soon stops, however, and the debugger displays a message box saying that it has detected a change in the value of bFirstLine. Clicking the message box's OK button updates the source window showing the yellow instruction arrow that points to the instruction following the one that has just cleared the bFirstLine flag.

    Setting data breakpoints this way is not very helpful for simple variables like bFirstLine that change value only once. However, data breakpoints can be invaluable when tracking down obscure bugs in which a variable is incorrectly altered during execution, and the location in which the variable was incorrectly altered is indeterminable.

  • To resume execution and terminate the program
    1. Press CTRL+B again to display the Breakpoints dialog box, and click the Remove All button at the bottom of the dialog box. This action deletes the data breakpoint attached to bFirstLine, ensuring that the program now runs at normal speed.
    2. Click the Go command, or press F5 to continue running the STUpload program, and terminate the program normally through its Exit command. When the program stops, so does the debugger.


    Microsoft Press - Desktop Applications with Microsoft Visual C++ 6. 0. MCSD Training Kit
    Desktop Applications with Microsoft Visual C++ 6.0 MCSD Training Kit
    ISBN: 0735607958
    EAN: 2147483647
    Year: 1999
    Pages: 95

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net