📄 debug.html
字号:
<html>
<head>
<title>Debugging</title>
<meta name="description" content="Intro to debugging">
<meta name="keywords" content="debugger, debugging, step, stack">
<link rel="stylesheet" href="../../rs.css">
</head>
<body background="../../images/margin.gif" bgcolor="#ffffe0">
<!-- Main Table -->
<table width="100%" cellpadding="6">
<tr>
<td width="78"> </td>
<td>
<h2>Debugging</h2>
<p>Wouldn't it be nice to have a tool that would show you, step by step, how your program is executing? Luckily, such tools exist and are called <b>debuggers</b>. There are many implementations of debuggers, but they all offer the same basic funtionality. There is a debugger built into Microsoft Developer's Studio a.k.a. Visual C++, so I'll use it as an example.
<p>Once you have your program built, you <b>step into it</b>. In VC++ you do it by pressing F11. What happens next is that your program starts executing, but it is stopped immediately after entering <var>main</var>. You have just <i>stepped into main</i>. You can continue stepping through your program, line by line, by pressing the <b>step over</b> button. When you are positioned at a method call, <i>stepping over</i> will execute the method and position you right after the call. If, on the other hand, you'd like to follow the execution of this method, you should press the <b>step into</b> button. The debugger will jump into the method that's being called and show it to you.
<p>All through the debugging session you'll have additional panes showing the state of the program (if you don't have these panes, go to the <i>View</i> menu and select <i>Debug Windows</i>). One pane shows the values of all the local variables in the current scope. Another pane shows the call stack, or who called whom, called whom... The top of the call stack shows the place where you are currently stopped.
<p>The great thing about the call stack is that you can double-click on any of the callers and the debugger will show you where in the calling method the call was made. At the same time, the local-variable window will be updated to show the values of local variables of this particular caller.
<p>Look at the screen shot below. I first stepped into <var>main</var> by pressing F11. Then I did a few step-overs to get to the line in <var>main</var> that calls <var>num.AddInput()</var>. (Incidentally, when stepping over input statements, you have to go to your application window and actually type in the numbers before you can continue stepping.) Instead of stepping over this call, I chose to <i>step into</i> <var>AddInput</var>. I continued stepping over until I got to the line that calls <var>aNum.GetValue()</var> at which point I stepped into <var>GetValue()</var>. You can see where I stopped inside <var>GetValue()</var>--there's an arrow pointing at the appropriate line.
<p>Then I double clicked on <var>InputNum::AddInput()</var> in the call stack in order to see the values of local variables at the time <var>GetValue()</var> was called. You can see the second (triangular) arrow pointing at the very point where the call was made. The variable window was updated appropriately, showing the values of <var>aNum</var> and <var>this</var>. "This" is the <i>current</i> object--the object in whose context the call was made. Notice that, since the variables here are objects of the class <var>InputNum</var>, you can expand them to see the values of their member variables. In this case, you see the values of <var>_num</var> for both, <var>aNum</var> and <var>this</var>. They are, respectively, 11 and 5. (Your debugger might display them in hexadecimal notation as 0xb and 0x5.)
</table>
<!-- End Main Table -->
<img src="images/debugwin.gif" width=772 height=699 border=2 alt="Debug Window">
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -