rhl27.htm

来自「linux的初学电子书」· HTM 代码 · 共 2,014 行 · 第 1/4 页

HTM
2,014 行
字号

<BR>

<PRE>

<FONT COLOR="#000080">1 #include &lt;stdio.h&gt;

2

3 void main ()

4 {

5 void my_print(char *);

6 void my_print2(char *);

7

8 char my_string[] = &quot;hello there&quot;;

9

10 my_print (my string)</FONT></PRE>

<P>If you press Enter, gdb will execute the list command again, giving you the following output:

<BR>

<PRE>

<FONT COLOR="#000080">11 my_print2 (my string);

12 {

13

14 void my_print (char *string)

15 {

16 printf (&quot;The string is %s\n&quot;, string);

17 }

18

19 void my_print2 (char *string)

20 {</FONT></PRE>

<P>Pressing Enter one more time will list the rest of the greeting program:

<BR>

<PRE>

<FONT COLOR="#000080">21 char *string2;

22 int size, i;

23

24 size = strlen (string);

25 string2 = (char *) malloc (size + 1);

26 for (i = 0; i &lt; size; i++)

27 string2[size - i] = string[i];

28 string2[size+1] = '\0';

29 printf (&quot;The string printed backward is %s\n&quot;, string2);

30 }</FONT></PRE>

<P>By listing the file you can see that the place where you want to set the breakpoint is line 27. Now, to set the breakpoint, type the following command at the gdb command prompt:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080"> (gdb) break 27</FONT></PRE>

<P>gdb should now print a response resembling the following:

<BR>

<PRE>

<FONT COLOR="#000080">Breakpoint 1 at 0x8000570: file greeting.c, line 27

 (gdb)</FONT></PRE>

<P>Now you can run the program again by typing the run command. This command will generate the following output:

<BR>

<PRE>

<FONT COLOR="#000080">Starting program: /root/greeting

The string is hello there

Breakpoint 1, my_print2 (string = 0xbffffe14 &quot;hello there&quot;) at greeting.c:27

27 string2[size - i]=string[i]</FONT></PRE>

<P>You can see what is actually going wrong with your program by setting a watch to tell you the value of the string2[size - i] variable expression.

<BR>

<P>To do this, type

<BR>

<BR>

<PRE>

<FONT COLOR="#000080"> (gdb) watch string2[size - i]</FONT></PRE>

<P>gdb will return the following acknowledgment:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">Hardware watchpoint 2: string2[size - i]</FONT></PRE>

<P>Now that a watch has been set, gdb will halt the program and display the new value of variable string2[size - i] each time it changes. But because we already have a breakpoint established at the line where string2[size - i] will be updated, two breaks 
for each pass through the loop will actually be generated: once for the breakpoint and again for the watch. To eliminate this redundancy, eliminate the breakpoint by typing disable 1 at the gdb prompt.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>Breakpoints are reset by disabling the number of the breakpoint assigned to the code line. For example, when we previously typed break 27, breakpoint 1 was assigned to that line of code. So to clear the breakpoint, we disable breakpoint number 1 by 
typing disable 1.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>Now you can step through the execution of the for loop using the cont (short for &quot;continue&quot;) command:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080"> (gdb) cont</FONT></PRE>

<P>After the first time through the loop, gdb tells us that string2[size - i] is 'h'. gdb informs you of this by writing the following message on the screen:

<BR>

<PRE>

<FONT COLOR="#000080">Hardware watchpoint 2, string2[size - i]

Old value = 0 '\000'

New value = 104 'h'

my_print2(string = 0xbffffe14 &quot;hello there&quot;) at greeting.c:26

26 for (i=0; i&lt;size; i++)</FONT></PRE>

<P>This is the value that you expected. Continuing through the loop several more times reveals similar results. Everything appears to be functioning normally. When you get to the point where i=10, the value of the string2[size - i] expression is equal to 
'e', the value of the size - i expression is equal to 1, and the program is at the last character that is to be copied over into the new string.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>You can see the current value of a variable or expression at any time from the gdb prompt by using the print EXP command. For example, try print i or print (size - i) while continuing through the loop.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>If you continue through the loop one more time, you see that there was not a value assigned to string2[0], which is the first character of the string. Because the malloc function initializes the memory it assigns to null, the first character in string2 
is the null character. This explains why nothing was being printed when you tried to print string2.

<BR>

<P>Now that you have found the problem, it should be quite easy to fix. You must write the code so that the first character going into string2 is being put into string2 at offset size - 1 instead of string2 at offset size. This is because the size of 
string2 is 12, but it starts numbering at offset zero. The characters in the string should start at offset 0 and go to offset 10, with offset 11 being reserved for the null character.

<BR>

<P>There are many ways to modify this code so that it will work. One way is to keep a separate size variable that is one smaller than the real size of the original string. This solution is shown in the following code:

<BR>

<PRE>

<FONT COLOR="#000080">#include &lt;stdio.h&gt;

void main ()

{

void my_print(char *);

void my_print2(char *);

 char my_string[] = &quot;hello there&quot;;

my_print (my_string);

my_print2 (my_string);

}

void my_print (char *string)

{

printf (&quot;The string is %s\n&quot;, string);

}

void my_print2 (char *string)

{

char *string2;

int size, size2, i;

size = strlen (string);

size2 = size -1;

string2 = (char *) malloc (size + 1);

for (i = 0; i &lt; size; i++)

string2[size2 - i] = string[i];

string2[size] = '\0';

printf (&quot;The string printed backward is %s\n&quot;, string2);

}</FONT></PRE>

<BR>

<A NAME="E68E224"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Additional C Programming Tools</B></FONT></CENTER></H3>

<BR>

<P>The Red Hat Linux distribution includes a number of C development tools that have not yet been described. This section describes many of these additional tools and their typical uses.

<BR>

<BR>

<A NAME="E69E339"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>xxgdb</B></FONT></CENTER></H4>

<BR>

<P>xxgdb is an X Window system&#151;based graphical user interface to gdb. All of the features that exist in the command-line version of gdb are present in xxgdb. xxgdb enables you to perform many of the most commonly used gdb commands by pressing buttons 
instead of typing in commands. It also graphically represents where you have placed breakpoints.

<BR>

<P>You can start xxgdb by typing the following into an Xterm window.

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">xxgdb</FONT></PRE>

<P>When you initiate xxgdb you can specify any of the command-line options that were available with gdb. xxgdb also has some of its own command-line options. These are described in Table 27.2.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 27.2. The </B><B>xxgdb</B><B> command-line options.</B></FONT></CENTER>

<BR>



<TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Option</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Description</I></FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

=db_name

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Specifies the name of the debugger to be used. The default is gdb.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

=db_prompt

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Specifies the debugger prompt. The default is gdb.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

=gdbinit

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Specifies the filename of the initial gdb command file. The default is .gdbinit.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

=nx

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Tells xxgdb not to execute the .gdbinit file.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

=bigicon

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Uses a large icon size for the xxgdb icon.</FONT>

</TABLE><P>When you start xxgdb, a window, depicted in Figure 27.1, opens on your screen.

<BR>

<P><B> <A HREF="27rhl01.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/27rhl01.gif">Figure 27.1. The </B><B>xxgdb</B><B> debugging window.</A></B>

<BR>

<P>The bottom pane of the window contains a message that is similar to the one displayed on the screen when you started the command-line version of gdb. Use this pane to enter commands to the xxgdb debugger just as you would in gdb. To give the pane focus, 
left-click anywhere in its region and type:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">file greeting</FONT></PRE>

<P>This should open the file for debugging and display its source code in the upper pane of the window, as shown in Figure 27.2.

<BR>

<P><B> <A HREF="27rhl02.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/27rhl02.gif">Figure 27.2. The </B><B>xxgdb</B><B> window with debug source </B><B>open.</A></B>

<BR>

<P>You could have accomplished the same thing by using the File button shown in the center of the window. In addition, you can use the run, break, and cont buttons much in the same way that we have used them before in gdb, only now graphically. To watch a 
variable (as long as it is within context) you can use the display button.

<BR>

<P>Try experimenting with the different features that this program provides and refer to the xxgdb and gdb manual pages for information on additional capabilities these tools provide.

<BR>

<BR>

<A NAME="E69E340"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>calls</B></FONT></CENTER></H4>

<BR>

<P>calls is a program that is not included on the Linux CD-ROM accompanying this book, but you can obtain a copy from the sunsite FTP site under the directory /pub/Linux/devel/lang/c/calls.tar.Z. Some older CD-ROM distributions of Linux include this file. 
Because it is a useful tool, we will cover it here. If you think it will be of use to you, obtain a copy from an FTP or BBS site or another CD-ROM. calls runs the GCC preprocessor on the files that are passed to it on the command line, and displays a 
function call tree for the functions that are in those files.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>To install calls on your system, perform the following steps while you are logged in as root:</NOTE>

<BR>

<OL>

<LI>FTP the file <A HREF="tppmsgs/msgs1.htm#115" tppabs="ftp://sunsite.unc.edu/pub/Linux/devel/lang/c/calls.tar.z">sunsite.unc.edu/pub/Linux/devel/lang/c/calls.tar.z</A>.

<BR>

<LI>Uncompress and untar the file.

<BR>

<LI>cd into the calls subdirectory that was created by untarring the file.

<BR>

<LI>Move the file named calls to the /usr/bin directory.

<BR>

<LI>Move the file named calls.1 to the /usr/man/man1 directory.

<BR>

</OL>

<BR>

<NOTE>This will install the calls program and man page on your system.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>When calls prints out the call trace, it includes the filename in which the function was found in brackets after the function name:

⌨️ 快捷键说明

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