rhl27.htm

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

HTM
2,014
字号

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

<BR>

<P>When you compile C code with GCC, it tries to compile the code in the least amount of time and also tries to create compiled code that is easy to debug. Making the code easy to debug means that the sequence of the compiled code is the same as the 
sequence of the source code, and no code gets optimized out of the compile. There are many options that you can use to tell GCC to create smaller, faster executable programs at the cost of compile time and ease of debugging. Of these options the two that 
you will typically use are the -o and the -O2 options.

<BR>

<P>The -o option tells GCC to perform basic optimizations on the source code. These optimizations will in most cases make the code run faster. The -O2 option tells GCC to make the code as fast and small as it can. The -O2 option will cause the compilation 
speed to be slower than it is when using the -o option, but will typically result in code that executes more quickly.

<BR>

<P>In addition to the -o and -O2 optimization options, there are a number of lower-level options that can be used to make the code faster. These options are very specific and should only be used if you fully understand the consequences that using these 
options will have on the compiled code. For a detailed description of these options, refer to the GCC manual page by typing man gcc on the command line.

<BR>

<BR>

<A NAME="E69E335"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Debugging and Profiling Options</B></FONT></CENTER></H4>

<BR>

<P>GCC supports several debugging and profiling options. Of these options, the two that you are most likely to use are the -g option and the -pg option.

<BR>

<P>The -g option tells GCC to produce debugging information that the GNU debugger (gdb) can use to help you to debug your program. GCC provides a feature that many other C compilers do not have. With GCC you can use the -g option in conjunction with the -o 
option (which generates optimized code). This can be very useful if you are trying to debug code that is as close as possible to what will exist in the final product. When you are using these two options together you should be aware that some of the code 
that you have written will probably be changed by GCC when it optimizes it. For more information on debugging your C programs, refer to the &quot;Debugging GCC Programs with gdb&quot; section in this chapter.

<BR>

<P>The -pg option tells GCC to add extra code to your program that will, when executed, generate profile information that can be used by the gprof program to display timing information about your program. For more information on gprof, refer to the 
&quot;gprof&quot; section in this chapter.

<BR>

<BR>

<A NAME="E68E223"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Debugging GCC Programs with </B><B>gdb</B></FONT></CENTER></H3>

<BR>

<P>Linux includes the GNU debugging program called gdb. gdb is a very powerful debugger that can be used to debug C and C++ programs. It enables you to see the internal structure or the memory that is being used by a program while it is executing. Some of 
the functions that gdb provides for you are these:

<BR>

<UL>

<LI>It enables you to monitor the value of variables that are contained in your program.

<BR>

<BR>

<LI>It enables you to set breakpoints that will stop the program at a specific line of code.

<BR>

<BR>

<LI>It enables you to step through the code, line by line.

<BR>

<BR>

</UL>

<P>You can run gdb by typing gdb on the command line and pressing Enter. If your system is configured properly, gdb should start and you will see a screen that resembles the following:

<BR>

<PRE>

<FONT COLOR="#000080">GDB is free software and you are welcome to distribute copies of it

under certain conditions; type &quot;show copying&quot; to see the conditions.

There is absolutely no warranty for GDB; type &quot;show warranty&quot; for details.

GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc.

 (gdb)</FONT></PRE>

<P>When you start gdb, there are a number of options that you can specify on the command line. You will probably run gdb in the following way:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">gdb &lt;fname&gt;</FONT></PRE>

<P>When you invoke gdb in this way, you are specifying the executable file that you want to debug. This tells gdb to load the executable file with the name fname. There are also ways of starting gdb that tell it to inspect a core file that was created by 
the executable file being examined, or to attach gdb to a currently running process. To get a listing and brief description of each of these other options, you can refer to the gdb man page or type gdb -h at the command line.

<BR>

<BR>

<A NAME="E69E336"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Compiling Code for Debugging</B></FONT></CENTER></H4>

<BR>

<P>To get gdb to work properly, you must compile your programs so that debugging information will be generated by the compiler. The debugging information that is generated contains the types for each of the variables in your program as well as the mapping 
between the addresses in the executable program and the line numbers in the source code. gdb uses this information to relate the executable code to the source code.

<BR>

<P>To compile a program with the debugging information turned on, use the -g compiler option.

<BR>

<BR>

<A NAME="E69E337"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>gdb</B><B> Basic Commands</B></FONT></CENTER></H4>

<BR>

<P>The gdb 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 
27.1 describes the commands that you will need to get up and debugging with gdb. To get a description of all of the gdb commands, refer to the gdb manual page.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 27.1. Basic </B><B>gdb</B><B> commands.</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>Command</I>

</FONT>

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

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

<TR>

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

file

</FONT>

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

Loads the executable file that is to be debugged</FONT>

<TR>

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

kill

</FONT>

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

Terminates the program that you are currently debugging</FONT>

<TR>

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

list

</FONT>

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

Lists sections of the source code that was used to generate the executable file</FONT>

<TR>

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

next

</FONT>

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

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

<TR>

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

step

</FONT>

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

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

<TR>

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

run

</FONT>

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

Executes the program that is currently being debugged</FONT>

<TR>

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

quit

</FONT>

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

Terminates gdb</FONT>

<TR>

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

watch

</FONT>

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

Enables you to examine the value of a program variable whenever the value changes</FONT>

<TR>

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

break

</FONT>

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

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

<TR>

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

make

</FONT>

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

Enables you to remake the executable program without quitting out of gdb or using another window</FONT>

<TR>

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

shell

</FONT>

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

Enables you to execute UNIX shell commands without leaving gdb</FONT>

</TABLE><P>The gdb environment supports many of the same command-editing features as do the UNIX shell programs. You can tell gdb to complete unique commands by pressing the Tab key just as you do when you are using bash or tcsh. If what you have typed in 
is not unique, you can make gdb print a list of all the commands that match what you have typed in 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.

<BR>

<BR>

<A NAME="E69E338"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Sample </B><B>gdb</B><B> Session</B></FONT></CENTER></H4>

<BR>

<P>This section takes you step by step through a sample gdb session. The sample program that is being debugged is quite simple, but it is sufficient to illustrate how gdb is typically used.

<BR>

<P>We will start by showing a listing of the program that is to be debugged. The program is called greeting and is supposed to display a simple greeting followed by the greeting printed in reverse order.

<BR>

<PRE>

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

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, i;

size = strlen (string);

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

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

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

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

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

}</FONT></PRE>

<P>You should compile the preceding program using the gcc command followed by the filename. To rename the generated binary (instead of using the default a.out filename), use the -o option followed by the binary name:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">gcc -o greeting greeting.c</FONT></PRE>

<P>The program, when executed, displays the following output:

<BR>

<PRE>

<FONT COLOR="#000080">The string is hello there

The string printed backward is</FONT></PRE>

<P>The first line of output comes out correctly, but the second line prints something that was unexpected. We intended the second line of output to be

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">The string printed backward is ereht olleh</FONT></PRE>

<P>For some reason the my_print2 function is not working properly. Let's take a look at the problem using gdb. First you need to start gdb, specifying the greeting program as the one to debug. You do this by typing the following command:

<BR>

<BR>

<PRE>

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

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>Remember that you must compile the greeting program with the compiler debug options turned on, by typing gcc -q -o greeting greeting.c.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>If you forget to pass the program to debug as a parameter to gdb, you can load it in after gdb is started by using the file command at the gdb prompt:

<BR>

<BR>

<PRE>

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

<P>This command will load the greeting executable just as if you had told gdb to load it on the command line.

<BR>

<P>You can now run greeting by entering the run command from the gdb prompt. When the program is executed from within gdb, the result should resemble the following:

<BR>

<PRE>

<FONT COLOR="#000080"> (gdb) run

Starting program: /root/greeting

The string is hello there

The string printed backward is

Program exited with code 040</FONT></PRE>

<P>The output of the greeting program is the same as when we executed the program outside of gdb. The question is, why is the backward print not working? To find the problem we can set a breakpoint at the line after the for statement in the my_print2 
function. To do this, list the source file by entering the list command at the gdb prompt:

<BR>

<BR>

<PRE>

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

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>Pressing Enter by itself at the gdb prompt will repeat the last command that was entered.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>The first time you enter the list command, you get output that resembles the following:

⌨️ 快捷键说明

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