📄 chxc.htm
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Appendix C</TITLE>
<LINK REL="Next" HREF="chxd.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/chxd.htm">
<LINK REL="Previous" HREF="chxb.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/chxb.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><B>Appendix C</B></H2>
<H2><B>Debugging</B></H2>
<hr>
<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 <A HREF="index24.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index24.htm" target="text">Chapter 24</A>, "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 <A HREF="indexb.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/indexb.htm" target="text">Reference B</A>, "The Developer Studio Interface."</P>
<ul>
<li> <B>Debugging Vocabulary</B></P>
<P> What's a breakpoint? What's the difference between Go and Restart? You'll see.</P>
<li> <B>Debugging Commands and Windows</B></P>
<P> There's a lot of information available to you when you're debugging an application. Get a handle on it all.</P>
<li> <B>Tracing</B></P>
<P> The system will tell you what's happening if you just ask it to.</P>
<li> <B>Dump functions</B></P>
<P> In case of an error, get your objects to describe themselves so you can see what's wrong.</P>
</ul>
<H3><B>Debugging Vocabulary</B></H3>
<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 gets executed, or whether control
transfers inside a certain <font color="#008000">if</font> statement, or whether a function even gets 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 to move through your code a line or so at a time. You may want to know the values of some of your variables, or see how control transferred to this point by examining the call stack.</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 all over again from the beginning.</P>
<li> <I>Step Over</I>--Execute just the next statement, 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 all 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 lines from now, where the cursor is positioned.</P>
</ul>
<P>Most of the information made available to you by the debugger is in the form of new windows. These are discussed in the following sections.</P>
<H3><B>Debugging Commands and Windows</B></H3>
<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><B>Menu Items</B></P>
<P>The user interface for debugging starts with items on some ordinary menus that are used only in debugging and were not discussed in <A HREF="indexb.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/indexb.htm" target="text">Reference B</A>, "The Developer Studio Interface." These include:</P>
<ul>
<li> <U>E</U>dit,Brea<U>k</U>points</P>
<li> <U>V</U>iew, <U>D</U>ebug Windows, W<U>a</U>tch</P>
<li> <U>V</U>iew,<U> D</U>ebug Windows,<U> C</U>all Stack</P>
<li> <U>V</U>iew,<U> D</U>ebug Windows,<U> M</U>emory</P>
<li> <U>V</U>iew,<U> D</U>ebug Windows,<U> V</U>ariables</P>
<li> <U>V</U>iew,<U> D</U>ebug Windows,<U> R</U>egisters</P>
<li> <U>V</U>iew,<U> D</U>ebug Windows,<U> D</U>isassembly</P>
<li> <U>B</U>uild, Start <U>D</U>ebug, <U>G</U>o</P>
<li> <U>B</U>uild, Start <U>D</U>ebug, Step <U>I</U>nto</P>
<li> <U>B</U>uild, Start <U>D</U>ebug, Run to <U>C</U>ursor</P>
<li> <U>B</U>uild, Start <U>D</U>ebug, <U>A</U>ttach to Process</P>
<li> <U>B</U>uild, Debugger Remote Co<U>n</U>nection</P>
</ul>
<P>These are not the only menu items you will use, of course. For example, the <U>E</U>dit, <U>G</U>o To dialog box can be used to scroll the editor to a specific breakpoint as easily as a line, bookmark, or address. Many of the menu items you already
learned about are useful during debugging.</P>
<P>Once you have started debugging, the <U>B</U>uild menu disappears and a <U>D</U>ebug menu appears. The items on that menu are as follows:</P>
<ul>
<li> <U>D</U>ebug, <U>G</U>o</P>
<li> <U>D</U>ebug, <U>R</U>estart</P>
<li> <U>D</U>ebug, Stop <U>D</U>ebugging</P>
<li> <U>D</U>ebug, <U>B</U>reak</P>
<li> <U>D</U>ebug, Step <U>I</U>nto</P>
<li> <U>D</U>ebug, Step <U>O</U>ver</P>
<li> <U>D</U>ebug, Step O<U>u</U>t</P>
<li> <U>D</U>ebug, Run to <U>C</U>ursor</P>
<li> <U>D</U>ebug, Ste<U>p</U> Into Specific Function</P>
<li> <U>D</U>ebug, <U>E</U>xceptions</P>
<li> <U>D</U>ebug, <U>T</U>hreads</P>
<li> <U>D</U>ebug, Show <U>N</U>ext Statement</P>
<li> <U>D</U>ebug, <U>Q</U>uickWatch</P>
</ul>
<P>As you can see, some of the items from the Build, Start Debug cascading menu are also on the Debug menu, along with many other items. The individual items will be discussed in the sections that follow.</P>
<P><B>Setting Breakpoints</B></P>
<P>Probably the simplest way to set a simple 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 Mini-bar, 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 C.1.</P>
<A HREF="GGfig01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxc/GGfig01.gif"><b>Fig. C.1</b></A>
<P><I>The F9 key toggles a breakpoint on the line containing the cursor.</I></P>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>The application being debugged throughout this reference chapter is ShowString, as built in <A HREF="index09.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index09.htm" target="text">Chapter 9</A>, "Building a Complete Application: ShowString."</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>Choosing <U>E</U>dit, Brea<U>k</U>points 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 C.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 below.</P>
<A HREF="GGfig02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxc/GGfig02.gif"><b>Fig. C.2</b></A>
<P><I>You can arrange for execution to pause whenever a variable or expression </I><I>changes value.</I></P>
<P>You can also set conditional breakpoints, such as "break on this line when <font color="#008000">i</font> exceeds 100," that spare you from mindlessly clicking Go, Go, Go until you have been through a loop 100 times.</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -