491-494.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 154 行

HTML
154
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Programming in C&#43;&#43;</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=27//-->

<!--PAGES=491-494//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="488-491.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="494-497.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H3><A NAME="Heading8"></A><FONT COLOR="#000077">Debugging C&#43;&#43; Applications</FONT></H3>

<P>A very important part of developing C&#43;&#43; programs is being able to debug them efficiently. The GNU debug application that was introduced in Chapter 26 can also be used to debug C&#43;&#43; applications. This section describes some of the differences between debugging C applications and debugging C&#43;&#43; applications.

</P>

<P>The basic <TT>gdb</TT> commands that were introduced in Chapter 26 are listed again for your convenience in Table 27.2.</P>

<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 27.2.</B> Basic <TT>gdb</TT> commands.

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TH WIDTH="30%" ALIGN="LEFT">Command

<TH WIDTH="70%" ALIGN="LEFT">Description

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD><TT>file</TT>

<TD>Loads the executable file that is to be debugged.

<TR>

<TD><TT>kill</TT>

<TD>Terminates the program you are currently debugging.

<TR>

<TD VALIGN="TOP"><TT>list</TT>

<TD>Lists sections of the source code used to generate the executable file.

<TR>

<TD VALIGN="TOP"><TT>next</TT>

<TD>Advances one line of source code in the current function, without stepping into other functions.

<TR>

<TD VALIGN="TOP"><TT>step</TT>

<TD>Advances one line of source code in the current function, and does step into other functions.

<TR>

<TD><TT>run</TT>

<TD>Executes the program that is currently being debugged.

<TR>

<TD><TT>quit</TT>

<TD>Terminates <TT>gdb</TT>.

<TR>

<TD VALIGN="TOP"><TT>watch</TT>

<TD>Enables you to examine the value of a program variable whenever the value changes.

<TR>

<TD VALIGN="TOP"><TT>break</TT>

<TD>Sets a breakpoint in the code; this causes the execution of the program to be suspended whenever this point is reached.

<TR>

<TD VALIGN="TOP"><TT>make</TT>

<TD>This command enables you to remake the executable program without quitting <TT>gdb</TT> or using another window.

<TR>

<TD VALIGN="TOP"><TT>shell</TT>

<TD>Enables you to execute UNIX shell commands without leaving <TT>gdb</TT>.

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

<P>From the programmer&#146;s perspective, you have more details to be aware of when debugging C&#43;&#43; code than when you are debugging C code. This is because of the C&#43;&#43; features such as virtual functions and exception handling. <TT>gdb</TT> has added features to support debugging both of these C&#43;&#43; specific features.</P>

<H3><A NAME="Heading9"></A><FONT COLOR="#000077">Debugging Virtual Functions</FONT></H3>

<P>As described in the &#147;Polymorphism&#148; section of this chapter, virtual functions are C&#43;&#43;&#146;s way of implementing polymorphism. This means that there may be more than one function in a program with the same name. The only way to tell these functions apart is by their signatures. The signature of a function is composed of the types of all the arguments to the function. For example, a function with the following prototype has a signature of <TT>int,real</TT>:</P>

<!-- CODE SNIP //-->

<PRE>

void func(int, real);

</PRE>

<!-- END CODE SNIP //-->

<P>You can see how this could cause the <TT>gdb</TT> a few problems. For example, if you define a class that has a virtual function called <TT>calculate</TT>, and two objects with different definitions for this function are created, how do you set a breakpoint to trigger on this function? You set breakpoints in C by specifying the function name as an argument to the <TT>gdb break</TT> command, as follows:</P>

<!-- CODE SNIP //-->

<PRE>

(gdb) break calculate

</PRE>

<!-- END CODE SNIP //-->

<P>This does not work in the case of a virtual function because the debugger is not able to tell which <TT>calculate</TT> you want the breakpoint to be set on. <TT>gdb</TT> was extended in a few ways so that it could handle virtual functions. The first way to solve the problem is to enter the function name by specifying its prototype, as well. This is done in the following way:</P>

<!-- CODE SNIP //-->

<PRE>

break &#146;calculate (float)&#146;

</PRE>

<!-- END CODE SNIP //-->

<P>This gives <TT>gdb</TT> enough information to determine which function the breakpoint was meant for. A second solution that <TT>gdb</TT> supports is using a breakpoint menu. Breakpoint menus allow you to specify the function name of a function. If there is more than one function definition for that function, it gives you a menu of choices. The first choice in the menu is to abort the <TT>break</TT> command. The second choice is to set a breakpoint on all the functions that the <TT>break</TT> command matches. The remaining choices correspond to each function that matches the <TT>break</TT> command. The following code shows an example of a breakpoint menu:</P>

<!-- CODE //-->

<PRE>

 (gdb) break shape::calculate

[0] cancel

[1] all

[2] file: shapes.C: line number: 153

[3] file: shapes.C: line number: 207

[4] file: shapes.C: line number: 247

&gt; 2 3

Breakpoint 1 at 0xb234: file shapes.C, line 153

Breakpoint 2 at 0xa435: file shapes.C, line 207

Multiple breakpoints were set

Use the &#147;delete&#148; command to delete unwanted breakpoints

(gdb)

</PRE>

<!-- END CODE //-->

<H3><A NAME="Heading10"></A><FONT COLOR="#000077">Debugging Exception Handlers</FONT></H3>

<P>Exceptions are errors that occur within your program. Exception handlers are pieces of code that are written to handle errors and potential errors. For example, if you were writing a C program and calling the <TT>malloc</TT> function to get a block of memory, you would typically check <TT>malloc</TT>&#146;s return code to make sure the memory allocation was successful. If C supported exception handling, you could specify a function that would receive or catch exceptions, and the <TT>malloc</TT> function would send or throw an exception to your function if one occurred.</P>

<P>The <TT>gdb</TT> added two new commands to support C&#43;&#43; exception handling: the <TT>catch</TT> command and the <TT>catch info</TT> command. The <TT>catch</TT> command is used to set a breakpoint in active exception handlers. The syntax of this command is as follows:</P>

<!-- CODE SNIP //-->

<PRE>

catch exceptions



exceptions is a list of the exceptions to catch.



The catch info command is used to display all the active exception

 handlers.

</PRE>

<!-- END CODE SNIP //-->

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="488-491.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="494-497.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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