Introduction to program analysis tool GPROF

Source: Internet
Author: User

Program Analysis is a program written in a certain language and analyzes its internal operation process. The main purpose of program analysis is to grasp the program running process through the calling relationship between various modules in the program, so as to better understand the program, learn valuable content from it. Second, with the purpose of system optimization, the system performance bottleneck can be found through tracking the key functions in the program or statistics of runtime information, so as to take further actions to optimize the program. Finally, program analysis may also be used in system testing and program debugging. When the system tracing is complex and a bug is hard to find, you can use some special data to construct a test case, then, compare the analyzed function call relationships with the actual function call relationships during the operation to find out the location of the error code.

The program analysis tool is different from the debugger. It only generates macro information such as the number of calls and execution time of some functions when the program is running, rather than the details of each statement. GPROF is the next powerful program analysis tool in Linux. For programs in the C, Pascal, or FORTRAN77 language, it can record the statistical information of the program running in the form of "logs": the time consumed by each function in the program running and the function call relationship, and the number of times each function is called. This can help programmers find the most time-consuming functions among the many functions, and also help programmers analyze the running process of the program. I believe that these functions are quite attractive to programmers who analyze open-source code.

GPROF analysis program

To analyze a program using GPROF, perform the following three steps:

L use the compiler to compile the program and add the-PG parameter.

L run the compiled program.

L use the GPROF command to view the running information of the program.

Let's take a simple example. Find the source code of a program that can run, such as the following file main. C:

 

#include <stdio.h>int IsEven(int x) {         return 0 == x & 1; }  int main() {int i = 0;while(++i < 1000) IsEven(i);return 0;  }

 

 

First, compile with the following command:

Snail il @ Ubuntu :~ /Test/GPROF $ CC-O main-PG main. c

Then, run the executable file main.

Snail il @ Ubuntu :~ /Test/GPROF $./main

After running, a file gmon. out will be generated in the current directory. This is the file generated by GPROF, which stores information such as function calling during program running.

Finally, run the GPROF command to view the information saved by gmon. out:

Snail il @ Ubuntu :~ /Test/GPROF $ GPROF main gmon. Out-B

In this way, there is a lot of information output to the screen, there is a single function execution room, function call relationship diagram, and so on, as follows:

snail@ubuntu:~/test/gprof$ gprof main gmon.out -bFlat profile:Each sample counts as 0.01 seconds. no time accumulated  %   cumulative   self              self     total            time   seconds   seconds    calls  Ts/call  Ts/call  name      0.00      0.00     0.00      999     0.00     0.00  IsEvenCall graphgranularity: each sample hit covers 4 byte(s) no time propagatedindex % time    self  children    called     name                0.00    0.00     999/999         main [5][1]      0.0    0.00    0.00     999         IsEven [1]-----------------------------------------------Index by function name   [1] IsEvensnail@ubuntu:~/test/gprof$ 

 

The following describes the simplest use of GPROF.

Compilation and link

In the above example, the program is relatively simple and there is only one file. If the source code has multiple files or the code structure is complex, several target files are generated during compilation and then linked together by the linker, how can GPROF be used?

For a program composed of multiple source files, you must add the-PG parameter when generating each. o file during compilation, and add the-PG parameter when linking. The linker is not GCC, such as LD, and has special requirements.

At the same time, the-PG parameter can only record the call relationship of each function in the source code, but cannot record the call situation of the library function. To record the call status of each library function, you must specify the dynamic (or static) Connection Library libc_p.a of the library function when linking, that is, add-lc_p instead of-LC.

It should also be noted that if the-PG parameter is specified for a part of the code during compilation, and the other part of the code is not specified, the generated gmon. some functions are missing in the out file, and there is no call relationship between those functions. However, this does not affect GPROF's record of other functions.

Run

The compiled program is no different from the running program, but a file gmon. Out is generated more than the normal program. Note that the file name is fixed and cannot be changed through parameter settings. If there is already a gmon. Out in the program directory, it will be overwritten by the new gmon. Out.

The Directory of the generated gmon. Out file also includes the following conventions: the directory of the file running when the program exits is the directory of the generated gmon. Out file. If one program calls another program during execution and terminates it during the execution of another program, gmon. Out is generated in the directory where the other program is located.

Note that when the program ends abnormally, The gmon. Out file is not generated, and the information about the program running cannot be viewed. The gmon. Out file is generated only when the program Exits normally from the main function or exits by calling the exit () function. However, gmon. out is not generated when you exit through underlying calls such as _ exit.

View

The command for viewing the program running information is GPROF, which uses the gmon. Out file as the input, that is, the gmon. Out file is translated into a readable form and displayed to the user. The command format is as follows:

GPROF [executable file] [gmon. Out file] [other parameters]

The content in square brackets can be omitted. If the "executable file" is omitted, GPROF searches for. the out file is an executable file. If gmon is omitted. out file, GPROF will also find gmon in the current directory. out. Other parameters can control the format and other information of GPROF output content. The most common parameters are as follows:

L-B no longer outputs detailed descriptions of each field in the Statistical Chart.

L-P only outputs the call graph of the function (the part of the Call Graph Information ).

L-Q only outputs the time consumption list of the function.

L-e name no longer outputs the call diagrams of the function name and its subfunctions (unless they have other parent functions that are not restricted ). Multiple-e flags can be specified. One-e flag can only specify one function.

L-e name no longer outputs the call diagram of the function name and its subfunctions. This flag is similar to the-e flag, however, in the calculation of the total time and percentage time, it excludes the time used by the function name and its subfunctions.

L-f name: The call diagram of the output function name and its subfunctions. Multiple-F flag can be specified. One-F flag can only specify one function.

L-f name: The call graph of the output function name and its subfunctions. It is similar to the-F sign, but it only uses the Time of the printed routine in the total time and percentage time calculation. Multiple-F flag can be specified. One-F flag can only specify one function. -F flag overwrites the-e flag.

L-Z shows a routine with zero usage (calculated based on the call count and accumulation time ).

However, GPROF cannot display the inheritance relationships between objects, which is also its weakness.

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.