468-472.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 237 行
HTML
237 行
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Programming in C</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=26//-->
<!--PAGES=468-472//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="466-468.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="472-474.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">gdb Basic Commands</FONT></H4>
<P>The <TT>gdb</TT> supports many commands that enable you to perform different debugging operations. These commands range in complexity from very simple file-loading commands to complicated commands that allow you to examine the contents of the call stack. Table 26.1 describes the commands that you need to get up and debugging with <TT>gdb</TT>. To get a description of all of the <TT>gdb</TT> commands, refer to the <TT>gdb</TT> manual page.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 26.1.</B> Basic <TT>gdb</TT> commands.
<TR>
<TH COLSPAN="3"><HR>
<TR>
<TH WIDTH="20%" ALIGN="LEFT">Command
<TH WIDTH="80%" ALIGN="LEFT">Description
<TR>
<TH COLSPAN="3"><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 that 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>Enables you to remake the executable program without quitting <TT>gdb</TT> or using another window.
<TR>
<TD><TT>shell</TT>
<TD>Enables you to execute UNIX shell commands without leaving <TT>gdb</TT>.
<TR>
<TD COLSPAN="3"><HR>
</TABLE>
<P>The <TT>gdb</TT> environment supports many of the same command-editing features as do the UNIX shell programs. You can tell <TT>gdb</TT> to complete unique commands by pressing the Tab key just as you do when you are using <TT>bash</TT> or <TT>tcsh</TT>. If what you type is not unique, you can make <TT>gdb</TT> print a list of all the commands that match what you have entered so far by pressing the Tab key again. You can also scroll up and down through the commands that you have entered previously by pressing the up- and down-arrow keys.</P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Sample gdb Session</FONT></H4>
<P>This section goes step by step through a sample <TT>gdb</TT> session. The sample program that is being debugged is quite simple, but it is sufficient to illustrate how <TT>gdb</TT> is typically used.</P>
<P>We will start by showing a listing of the program that is to be debugged. The program is called <TT>greeting</TT> and is supposed to display a simple greeting followed by the greeting printed in reverse order.</P>
<!-- CODE //-->
<PRE>
#include <stdio.h>
main ()
{
char my_string[] = “hello there”;
my_print (my_string);
my_print2 (my_string);
}
void my_print (char *string)
{
printf (“The string is %s\n”, string);
}
void my_print2 (char *string)
{
char *string2;
int size, i;
size = strlen (string);
string2 = (char *) malloc (size + 1);
for (i = 0; i < size; i++)
string2[size - i] = string[i];
string2[size+1] = ’\0’;
printf (“The string printed backward is %s\n”, string2);
}
</PRE>
<!-- END CODE //-->
<P>To compile the preceding program, use the <TT>gcc</TT> command followed by the filename. To rename the generated binary (instead of using the default <TT>a.out</TT> filename), use the <TT>-o</TT> option followed by the binary name, such as</P>
<!-- CODE SNIP //-->
<PRE>
gcc -o test test.c
</PRE>
<!-- END CODE SNIP //-->
<P>The program, when executed, displays the following output:
</P>
<!-- CODE SNIP //-->
<PRE>
The string is hello there
The string printed backward is
</PRE>
<!-- END CODE SNIP //-->
<P>The first line of output displays correctly, but the second line prints something that is unexpected. The second line of output was supposed to be
</P>
<!-- CODE SNIP //-->
<PRE>
The string printed backward is ereht olleh
</PRE>
<!-- END CODE SNIP //-->
<P>For some reason the <TT>my_print2</TT> function is not working properly. Let’s take a look at the problem using <TT>gdb</TT>. First we need to start <TT>gdb</TT>, specifying the <TT>greeting</TT> program as the one to debug. Do this by entering the following command:</P>
<!-- CODE SNIP //-->
<PRE>
gdb greeting
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Tip: </B><BR>Remember that you must compile the <TT>greeting</TT> program with the compiler debug options turned on.<HR></FONT>
</BLOCKQUOTE>
<P>If you forget to pass the program to debug as a parameter to <TT>gdb</TT>, load it in after <TT>gdb</TT> is started by using the <TT>file</TT> command at the <TT>gdb</TT> prompt:</P>
<!-- CODE SNIP //-->
<PRE>
(gdb) file greeting
</PRE>
<!-- END CODE SNIP //-->
<P>This command loads the greeting executable just as if you had told <TT>gdb</TT> to load it on the command line.</P>
<P>Next, let’s run <TT>greeting</TT> by entering the <TT>gdb</TT> run command. When the program is executed from within <TT>gdb</TT>, the result should resemble the following:</P>
<!-- CODE SNIP //-->
<PRE>
(gdb) run
Starting program: /root/greeting
The string is hello there
The string printed backward is
Program exited with code 041
</PRE>
<!-- END CODE SNIP //-->
<P>The output of the greeting program is the same as when we executed the program outside of <TT>gdb</TT>. The question is, why is the backward print not working? To find the problem, let’s set a breakpoint at the line after the <TT>for</TT> statement in the <TT>my_print2</TT> function. To do this, list the source file by entering the <TT>list</TT> command three times at the <TT>gdb</TT> prompt:</P>
<!-- CODE SNIP //-->
<PRE>
(gdb) list
(gdb) list
(gdb) list
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Tip: </B><BR>Pressing Enter by itself at the <TT>gdb</TT> prompt will repeat the last command that was entered.<HR></FONT>
</BLOCKQUOTE>
<P>Enter the <TT>list</TT> command for the first time and the output resembles the following:</P>
<!-- CODE //-->
<PRE>
1 #include <stdio.h>
2
3 main ()
4 {
5 char my_string[] = “hello there”;
6
7 my_print (my_string);
8 my_print2 (my_string);
9 }
10
</PRE>
<!-- END CODE //-->
<P>Press Enter and <TT>gdb</TT> executes the <TT>list</TT> command again, displaying the following output:</P>
<!-- CODE //-->
<PRE>
11 my_print (char *string)
12 {
13 printf (“The string is %s\n”, string);
14 }
15
16 my_print2 (char *string)
17 {
18 char *string2;
19 int size, i;
20
</PRE>
<!-- END CODE //-->
<P>Pressing Enter one more time lists the rest of the <TT>greeting</TT> program:</P>
<!-- CODE SNIP //-->
<PRE>
21 size = strlen (string);
22 string2 = (char *) malloc (size + 1);
23 for (i = 0; i < size; i++)
24 string2[size - i] = string[i];
25 string2[size+1] = ’\0’;
26 printf (“The string printed backward is %s\n”, string2);
26 }
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="466-468.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="472-474.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?