⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 http:^^www.cs.wisc.edu^~cs367-3^gdb.html

📁 This data set contains WWW-pages collected from computer science departments of various universities
💻 HTML
📖 第 1 页 / 共 3 页
字号:
the program is executing. </p>
<pre><code><samp>
(gdb) b main
Breakpoint 1 at 0x22f8: file bug2.C, line 6.
(gdb) run
Starting program: /afs/cs.wisc.edu/p/course/cs367-reps/private/GDB/bug2 

Breakpoint 1, main () at bug2.C:6
6         int a[10] = {1,8,5,3,3,9,8,4,4,10};
(gdb) n
7         int cnt = 0; /* how many adjacent elems are equal ? */
(gdb) 
10  for (i = 0; i &lt; 9; i++)</samp></code></pre>
<p>The numbers as the extreme left are line numbers. It looks like the program is incrementing variable <code>cnt</code> during each iteration, but why? Let's print out the 
value of <code>i</code>, <code>a[i]</code>, and <code>a[i+1]</code>. </p>
<pre><code><samp>(gdb) p i
$1 = 2
(gdb) p a[i]
$2 = 5
(gdb) p a[i+1]
$3 = 3</samp></code></pre>
<p>These values look ok. </p>
<p>At a breakpoint, the line that <code>gdb</code> shows you is the <em>next</em> line to be executed, so we are just about to execute &quot;<code>if (a[i] = a[i+1])</code>&quot;. We'll let the if 
execute and then we'll look at the values again: </p>
<pre><code><samp>(gdb) n
14            cnt++;
(gdb) p i
$4 = 2
(gdb) p a[i]
$5 = 3
(gdb) p a[i+1]
$6 = 3</samp></code></pre>
<p>Our intention was that the true-branch of the if, which increments variable <code>cnt</code>, should execute only if <code>a[i]</code> equals <code>a[i+1]</code>; however, as a result of executing 
just the condition <code>a[i]</code> changed value! </p>
<p>That is the clue. The equality operator in C++ is <code>==</code>, not <code>=</code>; the operator <code>=</code> is the assignment operator! Now we understand what has been happening: Each 
time the test has been performed, we have been assigning the value of <code>a[i+1]</code> to <code>a[i]</code> and then treating that value (<code>a[i+1]</code>) as a boolean value for the test! 
Since none of the values in <code>a</code> are <code>0</code>, they are all treated as true. </p>
<p>The way to correct the program is to change <code>=</code> to <code>==</code> in line 12. </p>
<h2><a name="additional_commands">Additional Gdb Commands </a></h2>
<p>Single-stepping a large program can be very tedious. Another strategy is to set breakpoints at key statements that modify critical data. At these points, we 
can look at values, or single-step a bit to see where the program is going. In our example program <code>bug2.C</code>, line 14 would be a good place to set a 
breakpoint, since it is where variable <code>cnt</code> is incremented. In this case, we would have seen that <code>cnt</code> was being incremented on each iteration, and looking at 
the values of <code>i</code> and <code>a</code> would soon have showed us the unintentional assignment in the condition of the if statement. </p>
<p>A breakpoint can be cleared with the <code>clear</code> command, naming the line or function from which a breakpoint is to be cleared. </p>
<p>In our example, it was obvious that line 14 was the only place where the value of <code>cnt</code> was being changed (and hence it was a good place to put a 
breakpoint). In large programs, it is sometimes not obvious where a given variable is being set. The command &quot;<code>watch</code> <i>expr</i>&quot; -- where <i>expr</i> is a C++ 
expression -- allows you to ask <code>gdb</code> to stop whenever <i>expr</i> changes value. Thus, the command </p>
<pre><code><samp>watch cnt
</samp></code></pre>
<p>would have instructed <code>gdb</code> to stop whenever <code>cnt</code> changed value; execution would have stopped at the statement just after the change, and <code>gdb</code> would have 
showed us both the old and new values. </p>
<p><code>Watch</code> commands slow <code>gdb</code> down a lot, so use them judiciously. Note that to watch a variable <code>v</code>, you must be within the scope that contains <code>v</code>. You can set a 
breakpoint at the start of <code>main</code> or some other function, then set a watch involving variables in the function. Here is how we might have used <code>watch</code> on 
program <code>bug2</code>: </p>
<pre><code><samp>&gt; gdb bug2
   . . .
(gdb) b main
Breakpoint 1 at 0x22f8: file bug2.C, line 6.
(gdb) run
Starting program: /afs/cs.wisc.edu/p/course/cs367-reps/private/GDB/bug2 

Breakpoint 1, main () at bug2.C:6
6         int a[10] = {1,8,5,3,3,9,8,4,4,10};
(gdb) watch cnt
Watchpoint 2: cnt
(gdb) continue
Continuing.
Watchpoint 2: cnt

Old value = 0
New value = 1
main () at bug2.C:16
16        }</samp></code></pre>
<p>Another useful <code>gdb</code> command is &quot;<code>commands</code>&quot;. This allows you to give a collection of <code>gdb</code> commands that are executed (by <code>gdb</code>) each time a certain 
breakpoint is encountered. This is especially useful when you are trying to keep an eye on a number of variables; you just tell <code>gdb</code> to print <em>all</em> their values at 
the breakpoint, rather than typing in <code>print</code> commands by hand every time <code>gdb</code> stops at the breakpoint. For instance, in our example of debugging program 
<code>bug2</code>, we could have had the values of <code>cnt</code>, <code>a[i]</code>, and <code>a[i+1]</code> printed out each time the breakpoint at line 14 was encountered: </p>
<pre><code><samp>&gt; gdb bug2
   . . .
(gdb) b main
Breakpoint 1 at 0x22f8: file bug2.C, line 6.
(gdb) run
Starting program: /afs/cs.wisc.edu/p/course/cs367-reps/private/GDB/bug2 

Breakpoint 1, main () at bug2.C:6
6         int a[10] = {1,8,5,3,3,9,8,4,4,10};
(gdb) b 14
Breakpoint 2 at 0x2438: file bug2.C, line 14.
(gdb) commands 2
Type commands for when breakpoint 2 is hit, one per line.
End with a line saying just &quot;end&quot;.
p cnt
p a[i]
p a[i+1]
end
(gdb) continue
Continuing.

Breakpoint 2, main () at bug2.C:14
14            cnt++;
$1 = 0
$2 = 8
$3 = 8
(gdb) continue
Continuing.

Breakpoint 2, main () at bug2.C:14
14            cnt++;
$4 = 1
$5 = 5
$6 = 5</samp></code></pre>
<p>Another feature of <code>gdb</code> that cuts down on how much you have to type is the <em>command editing</em> feature: You can scroll through past commands using the 
<code><b>up-arrow</b></code> and <code><b>down-arrow</b></code> keys; once you've found a similar command to the one you want to issue, you can edit the text of the command using 
<code><b>left-arrow</b></code>, <code><b>right-arrow</b></code>, <code><b>backspace</b></code>, etc. </p>
<p>At this point you should copy the program <code>bug2.C</code> (from <!WA10><a href="http://www.cs.wisc.edu/~cs367-3/gdb/bug2.C">here</a>) and compile it with <code>g++</code> (remember the <code>-g</code> option). Enter <code>gdb</code> and experiment with 
breakpoints, single-stepping, watches, and other commands. Remember that if you forget the details of a command, you should say <code>help command</code> and <code>gdb</code> 
will give you some information about the command. </p>
<h2><a name="command_summary">Summary of Gdb Commands </a></h2>
<p><code>Gdb</code> has many commands we have not discussed. The document <i>Using GDB</i> details all that's available. This document is long (almost 200 pages) and very 
detailed. Fortunately with just the commands we've discussed you can make very effective use of <code>gdb</code>. Let's review the commands we've seen and their 
effect: </p>
<pre><samp>Command          Effect

quit             Terminate gdb
where            Show the call stack where execution has been halted
p                Print the value of a variable or expression
up               Refocus gdb up one function in the call stack
down             Refocus gdb down one function in the call stack
help             Get help for a command
run              Start execution of a program
b                Set a breakpoint at a line or function
clear            Clear a breakpoint from a line or function
commands         Set commands to be executed when a breakpoint is hit
s                Execute one more line (possibly in a subroutine)
n                Execute to next line of current function
continue         Continue execution to next breakpoint
watch            Watch for a change in an expression (this can be slow)
list             List source lines of a function
</samp></pre>
<p>Here are some other commands that we did not discuss but which are very useful: </p>
<pre><samp>Command          Effect

info b           Show what breakpoints are set
delete </samp><samp><i>breakpoint-#</i></samp><samp>
                 Remove a single breakpoint (use &quot;info b&quot; to find breakpoint numbers)
cond </samp><samp><i>#</i></samp><samp> </samp><samp><i>condition</i></samp><samp>
                 Convert a breakpoint into a conditional breakpoint;
                 # is the breakpoint number and [cond] is any C++ expression.
                 For example:  cond 1 (x == 0)
set </samp><samp><i>var</i></samp><samp> = </samp><samp><i>expr</i></samp><samp>
                 Set the given variable to have the value of the given expression
until            Execute until the program reaches a source line greater than
                 a specified line or function.  Execution will also stop upon
                 exit from the current stack frame.
</samp></pre>
<p>Learning a new tool like <code>gdb</code> can be tedious. However, once you've mastered <code>gdb</code>, it will greatly ease debugging. You'll soon wonder how you ever got by 
without it. </p>
</body>

</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -