Introduction to gcc gdb xxgdb callcalltree cprot in C development tools in Linux

Source: Internet
Author: User
Tags ftp site
The Linux release contains many software development tools. many of them are used for C and C ++ application development. this article describes the tools that can be used for C application development and debugging in Linux. the purpose of this article is to introduce how to use the C compiler and other C programming tools in Linux, rather than the C Programming Tutorial.

Gnu c Compiler
The gnu c compiler (GCC) is a fully functional ansi c-compatible compiler. if you are familiar with a C compiler on other operating systems or hardware platforms, you will be able to quickly master GCC. this section describes how to use GCC and some of the most common options for GCC compilers.

Use gcc
The basic usage of the GCC compiler. GCC command is usually followed by some options and file names:

GCC [Options] [filenames]
The operation specified by the command line option will be executed on each given file on the command line. The next section describes the options you will most commonly use.

GCC options
GCC has more than 100 compilation options available. many of these options may never be used, but some of the main options will be frequently used. many GCC options include more than one character. therefore, you must specify the respective characters for each option, and, just like most Linux commands, you cannot separate the characters with a group of options. for example, the following two commands are different:

Gcc-p-g test. c

Gcc-PG test. c
The First Command tells GCC to compile test. in C, the profile information is created for the prof command and the debugging information is added to the executable file. the second command only tells GCC to create profiling information for the GPROF command.

When you do not need any options to compile a program, GCC will create (assuming the compilation is successful) a program named. out executable file. for example, the following command will generate. out file:

GCC test. c
You can use the-O compilation option to specify a file name for the generated executable file instead of. out. for example. the C program of C is compiled into an executable file named count. Enter the following command:

Gcc-O count. c

--------------------------------------------------------------------------------

Note: When you use the-O option,-o must be followed by a file name.

--------------------------------------------------------------------------------

GCC also has the compilation option to specify how much the compiler processes. -C indicates that GCC only compiles the source code as the target code and skips the Assembly and connection steps. this option is frequently used because it makes compilation of multiple C Programs faster and easier to manage. the target code file created by GCC is missing. O Extension.

The-s compilation option tells GCC to stop compiling after an assembly language file is generated for C code. the default extension of assembly language files generated by GCC is. s. -E indicates that the compiler only processes the input file. when this option is used, the pre-processor output is sent to the standard output instead of stored in the file.

Optimized items
When you use GCC to compile C code, it will try to complete the compilation at least and make the compiled code easy to debug. easy debugging means that the compiled code has the same execution order as the source code, and the compiled code has not been optimized. there are many options that can be used to tell GCC to generate smaller and faster executable files at the expense of more Compilation Time and easier debugging. the most typical options are-O and-O2.

The-O option tells GCC to optimize the source code. in most cases, these optimizations make program execution faster. -The O2 option tells GCC to generate code as small as possible and as fast as possible. the-O2 option will make compilation faster than-o. however, code execution is usually faster.

In addition to the-O and-O2 optimization options, there are also some low-level options for faster code generation. these options are very special, and it is best to use them only when you fully understand the effects these options will have on the compiled code. for a detailed description of these options, refer to the GCC guide page and type man GCC on the command line.

Debugging and profiling options
GCC supports several debugging and profiling options. Among these options,-G and-PG options are most commonly used.
The-G option tells GCC to generate debugging information that can be used by the GNU Debugger to debug your program. GCC provides a feature that is not available in many other C compilers. in GCC, you can combine-G with-O (to produce optimization code. this is useful because you can debug your code as close as possible to the final product. when you use these two options at the same time, you must be aware that some of your code has been modified by GCC during optimization. for more information about debugging C Programs, see the next section "Debug C Programs with GDB ".
-The PG option tells GCC to add additional code to your program. During execution, GPROF profiling information is generated to show the time consumption of your program. for more information about GPROF, see "GPROF.

Debug the GCC program with GDB
Linux contains a GNU debugging program called GDB. GDB is a powerful debugger used to debug C and C ++ programs. it enables you to observe the internal structure and memory usage of the program while the program is running. the following are some functions provided by GDB:

It enables you to monitor the value of variables in your program.
It enables you to set breakpoints so that the program stops running on the specified code line.
It enables you to execute your code in one row.

Type GDB on the command line and press the Enter key to run GDB. If everything works properly, GDB will be started and you will see similar content on the screen:

Gnu gdb 5.0
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
Welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux ".
(GDB)
After you start GDB, you can specify many options on the command line. You can also run GDB in the following ways:

GDB
When you run GDB in this way, you can directly specify the program to be debugged. this tells GDB to mount the executable file named fname. you can also use GDB to check a core file generated due to program termination exceptions, or connect it to a running program. you can refer to the gdb guide or Type GDB-H on the command line to get a simple list of instructions on these options.

Compile the code for debugging (compiling code for debugging)
To make GDB work normally, you must make your program contain debugging information during compilation. the debugging information includes the type of each variable in your program, the address ing in the executable file, and the source code line number. GDB uses this information to associate the source code with the machine code.

Enable the debugging option with the-G option during compilation.

GDB basic commands
GDB supports many commands to enable different functions. these commands are loaded from simple files to complex commands that allow you to check the stack content you are calling. Table 27.1 lists some of the commands you will use when debugging with GDB. for details about how to use GDB, refer to the Guide page of GDB.

Basic gdb commands.

Description
File to load the executable file to be debugged.
Kill to terminate the program being debugged.
List lists part of the source code that generates the execution file.
Next, execute a line of source code but do not enter the function.
Step: execute a line of source code and enter the function.
Run to execute the program currently being debugged
Quit terminate GDB
Watch enables you to monitor the value of a variable, regardless of when it is changed.
Print display expression value
Break sets breakpoints in the code, which will cause the program to be suspended when it is executed here.
Make enables you to re-generate executable files without exiting GDB.
Shell enables you to run Unix shell commands without leaving GDB.

GDB supports many command editing features similar to those of UNIX Shell programs. you can press the tab key like in bash or tcsh to allow GDB to help you complete a unique command. If it is not unique, GDB will list all matching commands. you can also use the mouse key to flip up and down History commands.

GDB application example
This section uses an example to teach you how to debug a program with GDB step by step. The program to be debugged is quite simple, but it shows typical applications of GDB.

The program to be debugged is listed below. This program is called "hello", which displays a simple greeting and is listed in reverse order.

# Include

Static void my_print (char *);
Static void my_print2 (char *);

Main ()
{
Char my_string [] = "Hello world! ";
My_print (my_string );
My_print2 (my_string );
}

Void my_print (char * string)
{
Printf ("the string is % s", 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] =;

Printf ("the string printed backward is % s", string2 );
}
Compile it with the following command:

Gcc-g-o hello. c
The following results are displayed when the program is executed:
./Hello
The string is Hello world!

The string printed backward is
The first line of output is correct, but the output in the second line is not what we expect. The output we imagine should be:

The string printed backward is! Dlrow olleh
For some reason, the my_print2 function does not work properly. Let's use GDB to see where the problem is. First, enter the following command:

GDB hello

--------------------------------------------------------------------------------

Note: Remember to open the debugging option when compiling the hello program.

--------------------------------------------------------------------------------

If you forget to pass the program to be debugged as a parameter when entering the command to GDB, you can use the file command at the gdb prompt to load it:

(GDB) file hello
This command will load the hello executable file just as you load it in the gdb command line.

You can use the gdb Run Command to run Hello. When it is run in GDB, the result will be like this:

(GDB) Run

Starting program:/root/Hello

The string is Hello world!

The string printed backward is

Program exited with code 040
The output is the same as the running result outside GDB. The question is, why does reverse printing not work? To find out the crux of the problem, we can set a breakpoint after the for statement of the my_print2 function. The specific method is to enter the LIST Command three times at the gdb prompt to list the source code:

(GDB) List

(GDB) List

(GDB) List

--------------------------------------------------------------------------------

Tip: Press enter at the gdb prompt to repeat the previous command.

--------------------------------------------------------------------------------

The output of the first LIST command is as follows:

1 # include
2
3 static void my_print (char *);
4 static void my_print2 (char *);
5
6 main ()
7 {
8 char my_string [] = "Hello world! ";
9 my_print (my_string );
10 my_print2 (my_string );

If you press enter, GDB will execute the LIST command again and give the following output:

11}
12
13 void my_print (char * string)
14 {
15 printf ("the string is % s", string );
16}
17
18 void my_print2 (char * string)
19 {
20 char * string2;
Press enter again to list the remaining parts of the hello program:

21 int size, I;
22
23 size = strlen (string );
24 string2 = (char *) malloc (size + 1 );
25 For (I = 0; I <size; I ++)
26 string2 [size-I] = string [I];
27 string2 [size + 1] =;
28
29 printf ("the string printed backward is % s", string2 );
30}
Based on the listed source program, you can see that the place where the breakpoint is to be set is in line 26th. At the gdb command line prompt, type the following command to set the breakpoint:

(GDB) Break 26
GDB will make the following response:

Breakpoint 1 at 0x804857c: file hello. C, line 26.

(GDB)

Enter the Run Command to generate the following output:

Starting program:/root/Hello

The string is Hello world!

Breakpoint 1, my_print2 (string = 0xbffffab0 "Hello world! ") At hello. C: 26
26 string2 [size-I] = string [I];
You can set an observation point to observe the value of the string2 [size-I] variable to see how an error is generated by typing:

(GDB) Watch string2 [size-I]
GDB will respond as follows:

Hardware watchpoint 2: string2 [size-I]
Now we can use the next command to execute the for loop step by step:

(GDB) Next
After the first loop, GDB tells us that the value of string2 [size-I] Is 'H'. GDB uses the following display to tell you this information:

Hardware watchpoint 2: string2 [size-I]

Old value = 0 0
New value = 104 H
My_print2 (string = 0xbffffab0 "Hello world! ") At hello. C: 25
25 For (I = 0; I <size; I ++)
This value is expected. The results of subsequent cycles are correct. When I = 11, the value of the expression string2 [size-I] is equal '! ', The value of size-I is equal to 1, and the last character has been copied to the new string.

If you run the loop again, you will see that no value is allocated to string2 [0], and it is the first character of the new string, because the malloc function initializes them as null characters when allocating memory. therefore, the first character of string2 is a null character. this explains why there is no output when string2 is printed.

It is very easy to correct the problem after finding out the problem. you have to change the offset of the first character written to string2 in the code to size-1 instead of size. this is because the size of string2 is 12, but the starting offset is 0. The characters in the string are offset 0 to offset 10, and the offset 11 is reserved as null characters.

The correction method is very simple. This is the code for this solution:

# Include

Static void my_print (char *);
Static void my_print2 (char *);

Main ()
{
Char my_string [] = "Hello world! ";
My_print (my_string );
My_print2 (my_string );
}

Void my_print (char * string)
{
Printf ("the string is % s", 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-1-I] = string [I];
String2 [size] =;

Printf ("the string printed backward is % s", string2 );
}
If the program generates a core file, you can use the gdb Hello Core Command to view the program's errors. For example, in the my_print2 () function, if you forget to allocate the memory string2 = (char *) malloc (size + 1) to string2, it is likely that the core dump will be performed.

Other C programming tools

Xxgdb
Xxgdb is a graphical interface of GDB Based on the X Window System. xxgdb includes all the features of GDB in the command line version. xxgdb allows you to execute Common commands by pressing the button. the place where the breakpoint is set is also displayed in graphs.

You can run the following command in an xterm window:

Xxgdb
You can use any valid command line options in GDB to initialize xxgdb. In addition, xxgdb also has some special command line options. Table 27.2 lists these options.

Table 27.2. xxgdb command line options.

Description
Db_name specifies the name of the debugger used. The default value is GDB.
Db_prompt specifies the debugger prompt. The default value is GDB.
Gdbinit specifies the name of the command file that initializes GDB. The default value is. gdbinit.

NX tells xxgdb not to execute the. gdbinit file.
Bigicon uses a large icon.

CILS
You can use the following path on the sunsite.unc.edu FTP site:
/Pub/Linux/devel/lang/C/calls.tar. Z
To obtain CILS, some earlier versions of the Linux CD-ROM release is also included. because it is a useful tool, we will introduce it here. if you find it useful, get a copy from BBS, FTP, or another CD-ROM. CILS calls the pre-processor of GCC to process the given source program files, and then outputs the function call tree in these files.

Note: After you log on to your system as a Super User, perform the following steps: 1. decompress and untar files. 2. the sub-directory created after the CD enters calluntar. 3. move the file named CILS to the/usr/bin directory. 4. move the file named calls.1 to the/usr/man/Man1 directory. 5. delete the/tmp/CILS directory. these steps will install the CILS program and its Guide page on your system.

--------------------------------------------------------------------------------

When the call trace result is printed by CILS, the file name of the file where the function is located is given in brackets after the function:

Main [Hello. C]
If the function is not in the file provided to CILS, and does not know where the called function comes from, only the function name is displayed:

Printf
CILS does not output recursive and static functions. recursive functions are shown as follows:

Fact <recursive in factorial. c>
Static functions are shown as follows:

Total [static in calculate. C]
As an example, we assume that the following program is processed with cballs:

# Include

Static void my_print (char *);
Static void my_print2 (char *);

Main ()
{
Char my_string [] = "Hello world! ";
My_print (my_string );
My_print2 (my_string );
My_print (my_string );
}

Void count_sum ()
{
Int I, sum = 0;
For (I = 0; I <1000000; I ++)
Sum + = I;
}

Void my_print (char * string)
{
Count_sum ();
Printf ("the string is % s", string );
}

Void my_print2 (char * string)
{
Char * string2;
Int size, I, sum = 0;

Count_sum ();
Size = strlen (string );
String2 = (char *) malloc (size + 1 );
For (I = 0; I <size; I ++) string2 [size-1-I] = string [I];
String2 [size] =;
For (I = 0; I <5000000; I ++)
Sum + = I;

Printf ("the string printed backward is % s", string2 );
}
The following output is generated:

1 _ underflow [Hello. C]
2 main
3 my_print [Hello. C]
4 count_sum [Hello. C]
5 printf
6 my_print2 [Hello. C]
7 count_sum
8 strlen
9 malloc
10 printf
CILS has many command line options to set different output formats. For more information about these options, refer to the CILS guide page. The method is to type CILS-H on the command line.

Calltree
The calltree is similar to CILS. Besides the output function call tree, there are other detailed information.
You can use the following path to obtain the calltree from the sunsite.unc.edu FTP site:/pub/Linux/devel/lang/C/calltree.tar.gz.

Cproto
Cproto reads the C source program file and automatically generates prototype declarations for each function. Using cproto can save you a lot of time to define the function prototype when writing a program.
If you want cproto process the following code (cproto hello. C ):

# Include

Static void my_print (char *);
Static void my_print2 (char *);

Main ()
{
Char my_string [] = "Hello world! ";
My_print (my_string );
My_print2 (my_string );
}

Void my_print (char * string)
{
Printf ("the string is % s", 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-1-I] = string [I];
String2 [size] =;

Printf ("the string printed backward is % s", string2 );
}
You will get the following output:

/* Hello. C */

Int main (void );

Int my_print (char * string );

Int my_print2 (char * string );
This output can be redirected to an inclusion file that defines the function prototype.

Indent
The indent utility is another programming utility included in Linux. in short, this tool produces beautiful indent formats for your code. indent also has many options to specify how to format your source code. for more information about these options, see the indent guide. On the command line, type indent-H.

The following example shows the default output of indent:

Run the C code before indent:

# Include

Static void my_print (char *);
Static void my_print2 (char *);

Main ()
{
Char my_string [] = "Hello world! ";
My_print (my_string );
My_print2 (my_string );
}

Void my_print (char * string)
{
Printf ("the string is % s", 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-1-I] = string [I];
String2 [size] =;

Printf ("the string printed backward is % s", string2 );
}
C code after running indent:

# Include
Static void my_print (char *);
Static void my_print2 (char *);
Main ()
{
Char my_string [] = "Hello world! ";
My_print (my_string );
My_print2 (my_string );
}
Void
My_print (char * string)
{
Printf ("the string is % s", 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-1-I] = string [I];
String2 [size] =;
Printf ("the string printed backward is % s", string2 );
}
Indent does not change the essence of the code, but just changes the appearance of the Code to make it more readable. This is always a good thing.

GPROF
GPROF is a program installed in the/usr/bin directory of your Linux system. It allows you to parse your program to know which part of the program is most time-consuming during execution.

GPROF will tell you the number of calls to each function in the program and the percentage of time each function is executed. This information is useful if you want to improve the performance of your program.

To use GPROF in your program, you must add the-PG option when compiling the program. this will make the program generate a gmon every execution. out file. GPROF uses this file to generate profiling information.

After you run your program and generate the gmon. Out file, you can use the following command to obtain the profiling information:

GPROF
The program_name parameter is the name of the program that generates the gmon. Out file.

To illustrate the problem, the count_sum () function is added to the program to consume CPU time. The program is as follows:
# Include

Static void my_print (char *);
Static void my_print2 (char *);

Main ()
{
Char my_string [] = "Hello world! ";
My_print (my_string );
My_print2 (my_string );
My_print (my_string );
}

Void count_sum ()
{
Int I, sum = 0;
For (I = 0; I <1000000; I ++)
Sum + = I;
}

Void my_print (char * string)
{
Count_sum ();
Printf ("the string is % s", string );
}

Void my_print2 (char * string)
{
Char * string2;
Int size, I, sum = 0;

Count_sum ();
Size = strlen (string );
String2 = (char *) malloc (size + 1 );
For (I = 0; I <size; I ++) string2 [size-1-I] = string [I];
String2 [size] =;
For (I = 0; I <5000000; I ++)
Sum + = I;

Printf ("the string printed backward is % s", string2 );
}
$ Gcc-PG-O hello. c
$./Hello
$ GPROF Hello thank more
The following output will be generated:
Flat profile:

Each sample counts as 0.01 seconds.
% Cumulative Self total
Time seconds callus/call us/call name
69.23 0.09 0.09 1 90000.00 103333.33 my_print2
30.77 0.13 0.04 3 13333.33 13333.33 count_sum
0.00 0.13 0.00 2 0.00 13333.33 my_print

% Total time occupied by executing this function
Percentage of time execution time

Cumulative cumulative seconds elapsed time to execute this function
Seconds (including the time it takes for this function to call other functions)

Time consumed by self to execute this function
Seconds (the time spent calling other functions is not counted)

Number of CILS calls

SELF: The microseconds that this function takes to execute.
US/call

Total execute this function and add it to call other functions.
US/call time in microseconds

Name function name

From the data above, we can see that it takes no time to execute the my_print () function, but it calls the count_sum () function again, so the cumulative number of seconds is 0.13.

Tip: GPROF generates a large amount of profiling data. If you want to check the data, you 'd better redirect the output to a file.

Source: linuxaid

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.