性能调用图中的源代码行号?

Source line numbers in perf call graph?

我正在使用 perf record -a --call-graph dwarf -p XXX sleep 1 记录一些函数调用,然后 perf report 查看该数据,但是如果我还可以看到源代码行号以准确了解每个函数的位置,那将非常有帮助打电话了。例如:

-   4.18%  testbinary  testbinary                [.] malloc 
   - malloc 
      - 99.57% operator new(unsigned long)
         + 7.28% MyFunction()

我想知道 MyFunction() 中的那些 new operators 到底在哪里被调用(不用我通过查看整个函数源代码来猜测)

P.S.: 二进制用-m64 -O2 -ggdb3

编译

源代码片段(整行)由 perf 在注释模式下打印 (man page; relevant part of The Perf Tutorial)。使用 perf annotate -s=MyFunction 或在 perf report 中向下滚动到您的 MyFunction 是树根的子树(报告自我时间的行;您可以使用 / 命令进行搜索为它)然后 select a 按钮(或 Enter 然后 Annotate "MyFunction")。

在注释模式下,源代码及其行应该在装配线附近可见。 http://man7.org/linux/man-pages/man1/perf-annotate.1.html

This command reads the input file and displays an annotated version of the code. If the object file has debug symbols then the source code will be displayed alongside assembly code.

   -l, --print-line
       Print matching source lines (may be slow).
  --source
      Interleave source code with assembly code. Enabled by default,
       disable with `--no-source`.
   -s, --symbol=<symbol>
       Symbol to annotate.

Perf 报告可能在排序中使用 srclines(--sort= 选项)但说明不清楚。它的手册页也记录了 --source 选项,但显然它仅用于注释 some_function 模式:http://man7.org/linux/man-pages/man1/perf-report.1.html

  --source
       Interleave source code with assembly code. Enabled by default,
       disable with --no-source.

我不小心发现它在 perf script 中有松散的记录,但它也适用于其他命令:-F 选项接受 srcline。因此,您可以 -F+srcline 将行号添加到现有列中。

示例:perf report -g fractal -F+period,srcline

Samples: 22K of event 'cycles:u', Event count (approx.): 13031011295
  Children      Self        Period  Source:Line                           Command  Shared Object        Symbol
+   99.98%    38.76%    5051224000  test.cpp:7                            a        a                    [.] fib
+   96.42%     0.00%             0  _start+94372992700461                 a        a                    [.] _start
+   96.42%     0.00%             0  __libc_start_main+140304673091826     a        libc-2.29.so         [.] __libc_start_main
+   96.42%     0.00%             0  test.cpp:13                           a        a                    [.] main
+   21.47%    21.47%    2797741850  test.cpp:8                            a        a                    [.] fib
+   16.69%    16.69%    2174469736  test.cpp:4                            a        a                    [.] fib
+   16.37%    16.36%    2132462705  test.cpp:6                            a        a                    [.] fib
+    6.69%     6.69%     871128215  test.cpp:5                            a        a                    [.] fib