gprof (调用图配置文件数据)

舞夕之 发表于 2020-07-11 03:55
浏览次数:
在手机上阅读

在类Unix操作系统上,gprof命令是一个软件开发工具,用于显示编译的二进制文件的调用图配置文件数据。

查看英文版

目录

1 gprof 运行系统环境

2 gprof 描述

3 gprof 语法

4 gprof 选项

gprof 运行系统环境

Unix&Linux

gprof 描述

gprof生成C、Pascal或Fortran77程序的执行概要文件。被调用例程的效果包含在每个调用方的配置文件中。配置文件数据取自调用图配置文件(gmon.out公司,这是由使用cc、pc和f77的-pg选项编译的程序创建的。pg选项还链接到为评测而编译的库例程的版本。gprof读取给定的对象文件(默认为a.out),并从中建立其符号表和调用图配置文件之间的关系gmon.out公司. 如果指定了多个配置文件,gprof输出将显示给定配置文件中配置文件信息的总和。

如果使用GCC 2.95.x或3.0来编译二进制文件,则可能需要将-fprofile-arcs添加到编译命令行,以便调用图正确存储在gmon.out中。

gprof计算每个例程花费的时间。接下来,这些时间沿调用图的边缘传播。发现周期,并调用周期以共享周期的时间。

分析提供了几种形式的输出:

  • 平面配置文件显示程序在每个函数上花费了多少时间,以及该函数被调用了多少次。如果你想知道哪些函数消耗了大部分的周期,这里就简单地说明了。
  • 对于每个函数,调用图显示了哪些函数调用了它,它调用了哪些其他函数,以及调用了多少次。还估计了每个函数的子例程花费了多少时间。这可能会建议您在哪些地方尝试消除耗费大量时间的函数调用。
  • 带注释的源代码清单是程序源代码的副本,上面标有程序的每一行执行的次数。

gprof produces an execution profile of C, Pascal, or Fortran77 programs. The effect of called routines is incorporated in the profile of each caller. The profile data is taken from the call graph profile file (gmon.out, by default) which is created by programs that are compiled with the -pgoption of cc, pc, and f77. The -pg option also links in versions of the library routines that are compiled for profiling. gprof reads the given object file (the default is a.out) and establishes the relation between its symbol table and the call graph profile from gmon.out. If more than one profile file is specified, the gprof output shows the sum of the profile information in the given profile files.

If you use gcc 2.95.x or 3.0 to compile your binaries, you may need to add the -fprofile-arcs to the compile command line for the call graphs to be properly stored in gmon.out.

gprof calculates the amount of time spent in each routine. Next, these times are propagated along the edges of the call graph. Cycles are discovered, and calls into a cycle are made to share the time of the cycle.

Several forms of output are available from the analysis:

  • The flat profile shows how much time your program spent in each function, and how many times that function was called. If you want to know which functions burn most of the cycles, it is stated concisely here.
  • The call graph shows, for each function, which functions called it, which other functions it called, and how many times. There is also an estimate of how much time was spent in the subroutines of each function. This can suggest places where you might try to eliminate function calls that use a lot of time.
  • The annotated source listing is a copy of the program's source code, labeled with the number of times each line of the program was executed.

查看英文版

查看中文版

gprof 语法

gprof [ -[abcDhilLrsTvwxyz] ] [ -[ACeEfFJnNOpPqQZ][name] ] [ -I dirs ] 
      [ -d[num] ] [ -k from/to ] [ -m min-count ] [ -R map_file ] 
      [ -t table-length ] [ --[no-]annotated-source[=name] ] 
      [ --[no-]exec-counts[=name] ] [ --[no-]flat-profile[=name] ] 
      [ --[no-]graph[=name] ] [ --[no-]time=name] [ --all-lines ] [ --brief ] 
      [ --debug[=level] ] [ --function-ordering ] [ --file-ordering map_file ] 
      [ --directory-path=dirs ] [ --display-unused-functions ] 
      [ --file-format=name ] [ --file-info ] [ --help ] [ --line ] 
      [ --min-count=n ] [ --no-static ] [ --print-path ] [ --separate-files ] 
      [ --static-call-graph ] [ --sum ] [ --table-length=len ] 
      [ --traditional ] [ --version ] [ --width=n ] [ --ignore-non-functions ] 
      [ --demangle[=STYLE] ] [ --no-demangle ] [--external-symbol-table=name] 
      [ image-file ] [ profile-file ... ]
gprof [ -[abcDhilLrsTvwxyz] ] [ -[ACeEfFJnNOpPqQZ][name] ] [ -I dirs ] 
      [ -d[num] ] [ -k from/to ] [ -m min-count ] [ -R map_file ] 
      [ -t table-length ] [ --[no-]annotated-source[=name] ] 
      [ --[no-]exec-counts[=name] ] [ --[no-]flat-profile[=name] ] 
      [ --[no-]graph[=name] ] [ --[no-]time=name] [ --all-lines ] [ --brief ] 
      [ --debug[=level] ] [ --function-ordering ] [ --file-ordering map_file ] 
      [ --directory-path=dirs ] [ --display-unused-functions ] 
      [ --file-format=name ] [ --file-info ] [ --help ] [ --line ] 
      [ --min-count=n ] [ --no-static ] [ --print-path ] [ --separate-files ] 
      [ --static-call-graph ] [ --sum ] [ --table-length=len ] 
      [ --traditional ] [ --version ] [ --width=n ] [ --ignore-non-functions ] 
      [ --demangle[=STYLE] ] [ --no-demangle ] [--external-symbol-table=name] 
      [ image-file ] [ profile-file ... ]

查看英文版

查看中文版

gprof 选项

这些选项指定gprof应该产生几种输出格式中的哪一种。

这些选项中的许多选项都带有可选的符号规范,以指定要包括或排除的功能。可以使用不同的符号规格多次指定这些选项,以包括或排除符号集。

指定这些选项中的任何一个都会覆盖默认值(-p -q),该默认值将打印平面轮廓并为所有功能调用图分析。

-A [ symspec ],--annotated-source [= symspec ] 该-A选项原因GPROF打印注释的源代码。如果指定了symspec,则仅输出匹配符号的输出。
-b, --brief 如果给出了-b选项,则gprof不会打印试图解释表中所有字段含义的详细内容。如果您打算打印输出,或厌倦了看到模糊效果,这很有用。
-C [ symspec],-- exec -counts [= symspec ] 的-C选项原因GPROF打印的功能的帐簿和次,每次被调用的数目。如果指定了symspec,则仅对匹配的符号打印提示符。
如果概要文件数据文件包含基本块计数记录,则将-l选项与-C一起指定将导致对基本块执行计数进行计数和显示。
-i,-- file -info 的-i选项原因GPROF至约简档数据文件(一个或多个),然后退出显示摘要信息。显示直方图,调用图和基本块计数记录的数量。
-I dirs,-- directory-path=dirs 该-I选项指定搜索目录,在其中找到源文件的列表。环境变量GPROF_PATH也可以用于传达此信息。主要用于带注释的源输出。
-J [ symspec ],-- no-annotated-source [= symspec ] 在-J选项的原因GPROF不打印注释的源代码。如果指定了symspec,则gprof将打印带注释的源,但排除匹配的符号。
-L, --print-path 通常,打印源文件名时会压缩路径部分。的-L选项原因GPROF打印源文件名,这是从符号调试信息中确定的图像文件中,并且相对于在其中编译器被调用的目录的全路径名。
-p [ symspec ],--flat-profile [= symspec ] 该-p选项原因GPROF打印平面轮廓。如果指定了symspec,则仅打印匹配符号的平面轮廓。
-P [ symspec ],-- no-flat-profile [= symspec ] 该-P选项原因GPROF压制印刷平面轮廓。如果指定了symspec,则gprof将打印平面轮廓,但排除匹配的符号。
-q [ symspec ],-- graph [=symspec ] 该-q选项的原因GPROF打印调用图分析。如果指定了symspec,则仅打印匹配符号及其子代的调用图。
-Q [ symspec ],-- no -graph [=symspec ] 在-Q选项原因GPROF打压打印调用图。如果指定了symspec,则gprof将打印调用图,但不包括匹配的符号。
-t,-- table-length = num 的-t选项使NUM在每个源文件最活跃的源极线被使能源注释时被列出。默认值为10。
-y,-- separate-files 此选项仅影响带注释的源输出。通常,gprof将带注释的源文件打印到标准输出。如果指定了此选项,则在文件filename-ann中生成名为path / filename的文件的带注释的源。如果底层文件系统将截断文件名-ANN,使得它覆盖原来的文件名,gprof的生成在文件中注释的源filename.ann代替(如果原来的文件名具有扩展,该扩展名替换为.ANN)。
-Z [ symspec ],-- no-exec-counts [= symspec ] 该-Z选项的原因GPROF不打印功能理货和每次被呼叫的号码。如果指定了symspec,则打印提示符,但排除匹配的符号。
-r, --function-ordering 该--function排序选项的原因GPROF打印基于分析数据的程序的建议功能排序。此选项建议一种排序,可以改善在支持可执行文件中功能的任意排序的系统上程序的分页,tlb和缓存行为。
如何强制链接程序以特定顺序放置功能的确切详细信息取决于系统,并且不在本手册的范围之内。
-R map_file,-- file -orderingmap_file 在--file排序选项的原因GPROF打印建议的.o基于分析数据的程序链接网上订购。此选项建议一种排序方式,可以改善不支持可执行文件中功能的任意排序的系统上程序的页面调度,tlb和缓存行为。强烈建议在此选项中
使用-a参数。
所述MAP_FILE参数是一个路径到提供函数名对象文件的映射的文件。文件的格式类似于程序nm的输出。
c-parse.o:00000000 T yyparsec-parse.o:00000004 C yyerrflagc-lang.o:00000000 Tmay_objc_method_namec-lang.o:00000000 T print_lang_statisticsc-lang.o:00000000 Tognize_objc_keywordc-decl.o:00000000 T print_lang_identifierc- decl.o:00000000 T print_lang_type ...
要使用GNU nm创建map_file,请输入“ nm --extern-only --defined-only -v --print-file-name program-name ”之类的命令。
-T, --traditional 该-T选项的原因GPROF打印其“传统”输出BSD风格。
-w width,--width = width 将输出线的宽度设置为width。当前仅在调用图底部打印功能索引时使用。
-x, --all-lines 此选项仅影响带注释的源输出。默认情况下,仅注释基本块开头的行。如果指定了此选项,则通过重复第一行的注释来注释基本块中的每一行。
--demangle [= style ],-- no -demangle 这些选项控制在打印输出时是否应该对C ++符号名称进行解密。默认设置是使符号脱嵌。该--no-还原函数选项可以用来关闭demangling。不同的编译器具有不同的处理样式。可选的demangling样式参数可用于为编译器选择适当的demangling样式。
分析选项
-a --no-static 该-a选项的原因GPROF压制的静态声明(私人)功能的打印。(这些函数的名称未列出为全局函数,并且在定义它们的文件/函数/块之外不可见。)这些函数所花费的时间,对其的调用/从中进行的调用等等,都将归因于在可执行文件中直接加载的函数。此选项会影响平面轮廓和调用图。
-c --static-call-graph 的-c选项使程序的调用图以由被扩充启发式它检查在二进制机器代码的目标文件,并标识函数调用的文本空间。由于仅在输入函数时才生成常规调用图记录,因此此选项标识了可以调用但从未调用过的子级。仅在符号表项存在的情况下,也会标识对未启用概要分析的未编译函数的调用。此选项通常找不到对动态库例程的调用。通过此试探法确定的父母或孩子将在呼叫图中显示,呼叫计数为0。
-D,-- ignore-non-functions 所述-D选项原因GPROF到忽略未已知的功能的符号。此选项可在受支持的系统上提供更准确的配置文件数据(例如Solaris和HPUX)。
-k from/to 该-k选项允许你从调用图任何弧线从匹配symspec符号删除从那些匹配symspec 来。
-l --line 的-l选项允许代替功能行由线谱,这会导致直方图命中被充电至个别行源代码。此功能仅适用于由旧版gcc编译器编译的程序。较新版本的gcc旨在与gcov工具配合使用。

如果在启用基本块计数的情况下编译程序,则此选项还将标识每行代码执行了多少次。虽然逐行概要分析可以帮助隔离程序在大型函数中花费时间的位置,但它也显着增加了gprof的运行时间,并放大了统计上的不准确性。
-m num,-- min-count = num 此选项仅影响执行计数输出。执行少于num次的符号被抑制。
-n symspec,-- time = symspec 在-n选项使gprof的,在其调用图分析,仅传播时间匹配符号symspec。
-N symspec,-- no -time =symspec 在-n选项使gprof的,在其调用图分析,不传播时间匹配符号symspec。
-S filename,--external-symbol-table=filename 的-S选项原因GPROF读取外部符号表文件,如/ PROC / kallsyms,而不是从给定对象文件中读取的符号表(默认为“ a.out的”)。这对于分析内核模块很有用。
-z,-- display-unused-functions 如果提供-z选项,则gprof将在平面配置文件中提及所有函数,甚至包括那些从未调用过且没有时间花费的函数。与-c选项结合使用时,发现从未调用过的例程非常有用。
其他选项
-d [ num ],--debug [= num ] 该-d NUM选项指定调试选项。如果未指定num,则打开所有调试信息。
-h,--help 该-h选项打印命令行用法。
-Oname, --file-format=name 选择配置文件数据文件的格式。公认的格式是auto(默认),bsd,4.4bsd,magic和prof(尚不支持)。
-s,-- sum 该-s选项的原因GPROF总结在配置文件中的数据文件,它读取的信息,并写出称为配置数据文件gmon.sum,其中包含从配置文件数据文件,所有的信息GPROF读取该文件gmon。 sum可以是指定的输入文件之一;这样的效果是将其他输入文件中的数据合并到gmon.sum中。

最终,您可以再次运行不带-s的gprof来分析文件gmon.sum中的累积数据。
-v,-- version 该-v标志导致GPROF打印当前版本号,然后退出。
不推荐使用的选项

这些选项已被使用symspecs的较新版本替换。

-e function_name 该-e功能选项告诉gprof的有关函数的不打印信息函数名调用图(和它的孩子们......)。该函数仍将作为任何调用该函数的子函数列出,但其索引号将显示为[not print]。可以给出多个-e选项;每个-e选项只能指示一个function_name。
-E function_name 该-E 功能选项的作用类似于-e选项,但时间的函数花费(和孩子谁不是从别的地方调用),将不会被用于计算百分比-的时间用于调用图。可以给出多个-E选项;每个-E选项只能指示一个function_name。
-f function_name 该-f功能选项原因GPROF限制调用图的功能函数名和它的孩子(和他们的孩子......)。可以给出多个-f选项;每个-f选项只能指示一个function_name。
-F function_name 该-F功能选项的作用类似于-f选项,但只有在功能和它的孩子所花费的时间(和他们的孩子......)将被用于测定总时间和百分比-的时间用于调用图。可以给出多个-F选项;每个-F选项只能指示一个function_name。该-F选项将覆盖-E选项。

These options specify which of several output formats gprof should produce.

Many of these options take an optional symspec to specify functions to be included or excluded. These options can be specified multiple times, with different symspecs, to include or exclude sets of symbols.

Specifying any of these options overrides the default (-p -q), which prints a flat profile and call graph analysis for all functions.

-A[symspec], --annotated-source[=symspec] The -A option causes gprof to print annotated source code. If symspec is specified, print output only for matching symbols.
-b, --brief If the -b option is given, gprof doesn't print the verboseblurbs that try to explain the meaning of all of the fields in the tables. This is useful if you intend to print out the output, or are tired of seeing the blurbs.
-C[symspec], --exec-counts[=symspec] The -C option causes gprof to print a tally of functions and the number of times each was called. If symspec is specified, print tally only for matching symbols.

If the profile data file contains basic-block count records, specifying the -l option, along with -C, will cause basic-block execution counts to be tallied and displayed.
-i, --file-info The -i option causes gprof to display summary information about the profile data file(s) and then exit. The number of histogram, call graph, and basic-block count records is displayed.
-I dirs, --directory-path=dirs The -I option specifies a list of search directories in which to find source files. Environment variable GPROF_PATHcan also be used to convey this information. Used mostly for annotated source output.
-J[symspec], --no-annotated-source[=symspec] The -J option causes gprof not to print annotated source code. If symspec is specified, gprof prints annotated source, but excludes matching symbols.
-L, --print-path Normally, source filenames are printed with the path component suppressed. The -L option causes gprof to print the full pathname of source filenames, which is determined from symbolic debugging information in the image file and is relative to the directory in which the compiler was invoked.
-p[symspec], --flat-profile[=symspec] The -p option causes gprof to print a flat profile. If symspec is specified, print flat profile only for matching symbols.
-P[symspec], --no-flat-profile[=symspec] The -P option causes gprof to suppress printing a flat profile. If symspec is specified, gprof prints a flat profile, but excludes matching symbols.
-q[symspec], --graph[=symspec] The -q option causes gprof to print the call graph analysis. If symspec is specified, print call graph only for matching symbols and their children.
-Q[symspec], --no-graph[=symspec] The -Q option causes gprof to suppress printing the call graph. If symspec is specified, gprof prints a call graph, but excludes matching symbols.
-t, --table-length=num The -t option causes the num most active source lines in each source file to be listed when source annotation is enabled. The default is 10.
-y, --separate-files This option affects annotated source output only. Normally, gprof prints annotated source files to standard output. If this option is specified, annotated source for a file named path/filename is generated in the file filename-ann. If the underlying file system would truncate filename-ann so that it overwrites the original filename, gprof generates annotated source in the file filename.ann instead (if the original file name has an extension, that extension is replaced with .ann).
-Z[symspec], --no-exec-counts[=symspec] The -Z option causes gprof not to print a tally of functions and the number of times each was called. If symspec is specified, print tally, but exclude matching symbols.
-r, --function-ordering The --function-ordering option causes gprof to print a suggested function ordering for the program based on profiling data. This option suggests an ordering which may improve paging, tlb and cache behavior for the program on systems which support arbitrary ordering of functions in an executable.

The exact details of how to force the linker to place functions in a particular order is system dependent and out of the scope of this manual.
-R map_file, --file-orderingmap_file The --file-ordering option causes gprof to print a suggested .o link line ordering for the program based on profiling data. This option suggests an ordering which may improve paging, tlb and cache behavior for the program on systems which do not support arbitrary ordering of functions in an executable.

Use of the -a argument is highly recommended with this option.

The map_file argument is a pathname to a file which provides function name to object file mappings. The format of the file is similar to the output of the program nm.

c-parse.o:00000000 T yyparsec-parse.o:00000004 C yyerrflagc-lang.o:00000000 T maybe_objc_method_namec-lang.o:00000000 T print_lang_statisticsc-lang.o:00000000 T recognize_objc_keywordc-decl.o:00000000 T print_lang_identifierc-decl.o:00000000 T print_lang_type...
To create a map_file with GNU nm, type a command like "nm --extern-only --defined-only -v --print-file-name program-name".
-T, --traditional The -T option causes gprof to print its output in "traditional" BSD style.
-w width, --width=width Sets width of output lines to width. Currently only used when printing the function index at the bottom of the call graph.
-x, --all-lines This option affects annotated source output only. By default, only the lines at the beginning of a basic-block are annotated. If this option is specified, every line in a basic-block is annotated by repeating the annotation for the first line.
--demangle[=style], --no-demangle These options control whether C++ symbol names should be demangled when printing output. The default is to demangle symbols. The --no-demangle option may be used to turn off demangling. Different compilers have different mangling styles. The optional demangling style argument can be used to choose an appropriate demangling style for your compiler.
Analysis Options
-a --no-static The -a option causes gprof to suppress the printing of statically declared (private) functions. (These are functions whose names are not listed as global, and that are not visible outside the file/function/block where they were defined.) Time spent in these functions, calls to/from them, etc., will all be attributed to the function that was loaded directly before it in the executable file. This option affects both the flat profile and the call graph.
-c --static-call-graph The -c option causes the call graph of the program to be augmented by a heuristic which examines the text space of the object file and identifies function calls in the binary machine code. Since normal call graph records are only generated when functions are entered, this option identifies children that could have been called, but never were. Calls to functions that were not compiled with profiling enabled are also identified, but only if symbol table entries are present for them. Calls to dynamic library routines are typically not found by this option. Parents or children identified via this heuristic are indicated in the call graph with call counts of 0.
-D, --ignore-non-functions The -D option causes gprof to ignore symbols that are not known to be functions. This option gives more accurate profile data on systems where it is supported (Solaris and HPUX for example).
-k from/to The -k option allows you to delete from the call graph any arcs from symbols matching symspec from to those matching symspec to.
-l --line The -l option enables line-by-line profiling, which causes histogram hits to be charged to individual source code lines, instead of functions. This feature only works with programs compiled by older versions of the gcc compiler. Newer versions of gcc are designed to work with the gcov tool instead.

If the program was compiled with basic-block counting enabled, this option will also identify how many times each line of code was executed. While line-by-line profiling can help isolate where in a large function a program is spending its time, it also significantly increases the running time of gprof, and magnifies statistical inaccuracies.
-m num, --min-count=num This option affects execution count output only. Symbols that are executed less than num times are suppressed.
-nsymspec, --time=symspec The -n option causes gprof, in its call graph analysis, to only propagate times for symbols matching symspec.
-Nsymspec, --no-time=symspec The -n option causes gprof, in its call graph analysis, not to propagate times for symbols matching symspec.
-Sfilename, --external-symbol-table=filename The -S option causes gprof to read an external symbol table file, such as /proc/kallsyms, rather than read the symbol table from the given object file (the default is "a.out"). This is useful for profiling kernel modules.
-z, --display-unused-functions If you give the -z option, gprof will mention all functions in the flat profile, even those that were never called, and that had no time spent in them. This is useful in conjunction with the -c option for discovering which routines were never called.
Miscellaneous Options
-d[num], --debug[=num] The -d num option specifies debugging options. If num is not specified, enable all debugging.
-h, --help The -h option prints command line usage.
-Oname, --file-format=name Selects the format of the profile data files. Recognized formats are auto(the default), bsd, 4.4bsd, magic, and prof (not yet supported).
-s, --sum The -s option causes gprof to summarize the information in the profile data files it read in, and write out a profile data file called gmon.sum, which contains all the information from the profile data files that gprof read in. The file gmon.sum may be one of the specified input files; the effect of this is to merge the data in the other input files into gmon.sum.

Eventually you can run gprof again without -s to analyze the cumulative data in the file gmon.sum.
-v, --version The -v flag causes gprof to print the current version number, and then exit.
Deprecated Options

These options have been replaced with newer versions that use symspecs.

-e function_name The -e function option tells gprof to not print information about the function function_name (and its children...) in the call graph. The function will still be listed as a child of any functions that call it, but its index number will be shown as [not printed]. More than one -e option may be given; only one function_name may be indicated with each -e option.
-E function_name The -E function option works like the -e option, but time spent in the function (and children who were not called from anywhere else), will not be used to compute the percentages-of-time for the call graph. More than one -E option may be given; only one function_name may be indicated with each -E option.
-f function_name The -f function option causes gprof to limit the call graph to the function function_name and its children (and their children...). More than one -foption may be given; only one function_name may be indicated with each -f option.
-F function_name The -F function option works like the -f option, but only time spent in the function and its children (and their children...) will be used to determine total-time and percentages-of-time for the call graph. More than one -Foption may be given; only one function_name may be indicated with each -F option. The -F option overrides the -E option.

查看英文版

查看中文版

其他命令行

gzip,gunzip and zcat | gawk | getfacl | gpasswd | grep | groupadd | groupdel | groupmod |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条