?? apd.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Appendix D -- Debugging</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../apc/apc.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ape/ape.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1>- D -</H1>
</CENTER>
<CENTER>
<H1>Debugging</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Debugging Vocabulary</A>
<LI><A HREF="#Heading2">Debugging Commands and Windows</A>
<UL>
<LI><A HREF="#Heading3">Menu Items</A>
<LI><A HREF="#Heading4">Setting Breakpoints</A>
<LI><A HREF="#Heading5">Examining Variable Values</A>
<LI><A HREF="#Heading6">Stepping Through Code</A>
<LI><A HREF="#Heading7">Edit and Continue</A>
<LI><A HREF="#Heading8">Other Debug Windows</A>
</UL>
<LI><A HREF="#Heading9">Using MFC Tracer</A>
<LI><A HREF="#Heading10">Defining a Dump Member Function</A>
<UL>
<LI><A HREF="#Heading11">An Example Using CDumpContext, CFile, and axfDump</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<P>Debugging is a vital part of programming. Whenever a program doesn't do what you
expect, even if it doesn't blow up, you should turn to the debugger to see what's
really going on. Some of the philosophies and techniques of debugging have been explained
elsewhere in this book, especially in Chapter 24, "Improving Your Application's
Performance." This appendix concentrates on the nuts and bolts of how to use
the debugger: the menus, toolbars, and windows that were not covered in Appendix
C, "The Visual Studio User Interface, Menus, and Toolbars."</P>
<P>
<H2><A NAME="Heading1"></A>Debugging Vocabulary</H2>
<P>Probably the most important word in debugging is <I>breakpoint</I>. A breakpoint
is a spot in your program, a single line of code, where you would like to pause.
Perhaps you are wondering how many times a loop is executed, whether control transfers
inside a certain if statement, or whether a function is even called. Setting a breakpoint
on a line will make execution stop when that line is about to be executed. At that
point you may want the program to be off and running again or want to move through
your code a line or so at a time. You may want to know some of your variables' values
or see how control transferred to this point by examining the call stack. Often,
you'll spot the cause of a bug and correct your code on the spot.</P>
<P>When it's time to move along, there are a number of ways you might like execution
to resume. These are explained in the following list:</P>
<UL>
<LI><I>Go</I>--Execute to the next breakpoint or, if there are no more breakpoints,
until the program completes.
<P>
<LI><I>Restart</I>--Start again from the beginning.
<P>
<LI><I>Step Over</I>--Execute only the next statement, and then pause again. If it
is a function call, run the whole function and pause after returning from it.
<P>
<LI><I>Step Into</I>--Execute just the next statement, but if it is a function, go
into it and pause before executing the first statement in the function.
<P>
<LI><I>Step Out</I>--Execute the rest of the current function and pause in the function
that called this one.
<P>
<LI><I>Run to Cursor</I>--Start running and stop a few (or many) lines from here,
where the cursor is positioned.
</UL>
<P>Most information made available to you by the debugger is in the form of new windows.
These are discussed in the following sections.</P>
<P>
<H2><A NAME="Heading2"></A>Debugging Commands and Windows</H2>
<P>Developer Studio has a powerful debugger with a rich interface. There are menu
items, toolbar buttons, and windows (output areas) that are used only when debugging.</P>
<P>
<H3><A NAME="Heading3"></A>Menu Items</H3>
<P>The user interface for debugging starts with items on some ordinary menus that
are used only in debugging and are not discussed in Appendix C. These include</P>
<UL>
<LI>Edit, Breakpoints
<P>
<LI>View, Debug Windows, Watch
<P>
<LI>View, Debug Windows, Call Stack
<P>
<LI>View, Debug Windows, Memory
<P>
<LI>View, Debug Windows, Variables
<P>
<LI>View, Debug Windows, Registers
<P>
<LI>View, Debug Windows, Disassembly
<P>
<LI>Build, Start Debug, Go
<P>
<LI>Build, Start Debug, Step Into
<P>
<LI>Build, Start Debug, Run to Cursor
<P>
<LI>Build, Start Debug, Attach to Process
<P>
<LI>Build, Debugger Remote Connection
</UL>
<P>These are not the only menu items you'll use, of course. For example, the Edit,
Go To dialog box can be used to scroll the editor to a specific breakpoint as easily
as a line, bookmark, or address. Many menu items you've already learned about are
useful during debugging.</P>
<P>When you start debugging, the Build menu disappears and a Debug menu appears.
The items on that menu are as follows:</P>
<UL>
<LI>Debug, Go
<P>
<LI>Debug, Restart
<P>
<LI>Debug, Stop Debugging
<P>
<LI>Debug, Break
<P>
<LI>Debug, Apply Code Changes
<P>
<LI>Debug, Step Into
<P>
<LI>Debug, Step Over
<P>
<LI>Debug, Step Out
<P>
<LI>Debug, Run to Cursor
<P>
<LI>Debug, Step into Specific Function
<P>
<LI>Debug, Exceptions
<P>
<LI>Debug, Threads
<P>
<LI>Debug, Show Next Statement
<P>
<LI>Debug, QuickWatch
</UL>
<P>As you can see, some items from the Build, Start Debug cascading menu are also
on the Debug menu, along with many other items. The sections that follow discuss
the individual items.</P>
<P>
<H3><A NAME="Heading4"></A>Setting Breakpoints</H3>
<P>Probably the simplest way to set a breakpoint is to place the cursor on the line
of code where you would like to pause. Then, toggle a breakpoint by pressing F9 or
by clicking the Insert/Remove Breakpoint button on the Build MiniBar, which looks
like an upraised hand (you're supposed to think "Stop!"). A red dot appears
in the margin to indicate you have placed a breakpoint here, as shown in Figure D.1.</P>
<P><A HREF="javascript:popUp('xduvc01.gif')"><B>FIG. D.1</B></A><B> </B><I>The F9
key toggles a breakpoint on the line containing the cursor.</I></P>
<P>
<BLOCKQUOTE>
<P>
<HR>
<strong>NOTE:</strong> The application being debugged throughout this appendix is ShowString,
as built in Chapter 8, "Building a Complete Application: ShowString." n
<HR>
</BLOCKQUOTE>
<P>Choosing Edit, Breakpoints displays a tabbed dialog box to set simple or conditional
breakpoints. For example, you may want to pause whenever a certain variable's value
changes. Searching through your code for lines that change that variable's value
and setting breakpoints on them all is tiresome. Instead, use the Data tab of the
Breakpoints dialog box, shown in Figure D.2. When the value of the variable changes,
a message box tells you why execution is pausing; then you can look at code and variables,
as described next.</P>
<P>You can also set conditional breakpoints, such as break on this line when i exceeds
100, that spare you from mindlessly clicking Go, Go, Go until you have been through
a loop 100 times.</P>
<P><A HREF="javascript:popUp('xduvc02.gif')"><B>FIG. D.2</B></A><B> </B><I>You can
arrange for execution to pause whenever a variable or expression changes value.</I></P>
<H3><I></I></H3>
<H3><A NAME="Heading5"></A>Examining Variable Values</H3>
<P>When you set a breakpoint and debug the program, everything proceeds normally
until the breakpoint line of code is about to execute. Then Developer Studio comes
up on top of your application, with some extra windows in the display and a yellow
arrow in the red margin dot that indicates your breakpoint, as shown in Figure D.3.
This shows you the line of code that is about to execute.</P>
<P><A HREF="javascript:popUp('xduvc03.gif')"><B>FIG. D.3</B></A><B> </B><I>A yellow
arrow indicates the line of code about to execute.</I></P>
<P>Move the mouse over a variable name, like color or horizcenter. A DataTip appears,
telling you the current value of this variable. You can check as many local variables
as you want like this, then continue executing, and check them again. There are other
ways, though, to examine variable values.</P>
<P>You could click on the variable (or move the cursor to it some other way) and
choose Debug, QuickWatch or click the QuickWatch button (a pair of glasses) on the
toolbar. This brings up the QuickWatch window, which shows you the value of a variable
or expression and lets you add it to the Watch window, if you want. You're probably
wondering why anyone uses this feature now that DataTips will show you a variable's
value without even clicking. DataTips can't handle expressions, even simple ones
like dlg.m_horizcenter, but QuickWatch can, as you see in Figure D.4. You can also
change a variable's value with this dialog box to recover from horrible errors and
see what happens.</P>
<P><A HREF="javascript:popUp('xduvc04.gif')"><B>FIG. D.4</B></A><B> </B><I>The QuickWatch
dialog box evaluates expressions. You add them to the Watch window by clicking Add
Watch.</I></P>
<P>Figure D.5 shows a debug session after running forward a few lines from the original
breakpoint (you'll see how to do this in a moment). The Watch and Variable windows
have been undocked to show more clearly which is which, and two watches have been
added: one for horizcenter and one for dlg.m_horizcenter. The program is paused immediately
after the user clicks OK on the Options dialog, and in this case the user changed
the string, the color, and both kinds of centering.</P>
<P>The Watch window simply shows the values of the two variables that were added
to it. horizcenter is still TRUE (1) because the line of code that sets it has not
yet been executed. dlg.m_horizcenter is FALSE (0) because the user deselected the
check box associated with the member variable. (Dialogs, controls, and associating
controls with member variables are discussed in Chapter 2, "Dialogs and Controls.")</P>
<P>The Variables window has a lot more information in it, which sometimes makes it
harder to use. The local variable dlg and the pointer to the object for whom this
member function was invoked, this, are both in the Variables window in tree form:
Click on a + to expand the tree and on a - to collapse it. In addition, the return
value from DoModal(), 1, is displayed.</P>
<P>At the top of the Variables window is a drop-down box labeled <I>Context</I>.
Dropping it down shows how control got here: It lists the names of a series of functions.
The top entry is the function in which the line about to be executed is contained,
CShowStringDoc::OnToolsOptions(). The second entry is the function that called this
one, _AfxDispatchCmdMsg(), which dispatches command messages. Chapter 3, "Messages
and Commands," introduces commands and messages and discusses the way that control
passes to a message-handling function like OnToolsOptions(). Here, the debugger gives
proof of this process right before your eyes.</P>
<P><A HREF="javascript:popUp('xduvc05.gif')"><B>FIG. D.5</B></A><B> </B><I>The Watch
window and the Variable window make it easy to know the values of all your variables.</I></P>
<P>Click on any function name in the drop-down box and the code for that function
is displayed. You can look at variables local to that function, and so on.</P>
<P>The Call Stack window, shown in Figure D.6, is easier to examine than the drop-down
box in the Variables window, and it shows the same information. As well as the function
names, you can see the parameters that were passed to each function. You may notice
the number 32771 recurring in most of the function calls. Choose View, Resource Symbols,
and you'll see that 32771 means ID_TOOLS_OPTIONS, the resource ID associated with
the menu item Tools, Options in ShowString (see Figure D.7).</P>
<P><A HREF="javascript:popUp('xduvc06.gif')"><B>FIG. D.6</B></A><B> </B><I>The Call
Stack window shows how you arrived here.</I></P>
<P>
<H3><A NAME="Heading6"></A>Stepping Through Code</H3>
<P>Double-clicking a function name in the call stack or the context drop-down box
of the Variables window doesn't make any code execute: It simply gives you a chance
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -