Skip to content

Commit

Permalink
stackcollapse-perf.pl: add switch --srcline to support source context…
Browse files Browse the repository at this point in the history
… from 'perf script -F+srcline'

When given an argument '-F+srcline', perf scripts produces an extra line after
every stack frame with source context (unless the frame is unknown).
With --srcline that source context is included in output if it points to a lib
or a source file.
  • Loading branch information
jan-konczak-cs-put committed Mar 27, 2021
1 parent 449e931 commit 7b0ca27
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions stackcollapse-perf.pl
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ sub remember_stack {

my $show_inline = 0;
my $show_context = 0;

my $srcline_in_input = 0; # if there are extra lines with source location (perf script -F+srcline)
GetOptions('inline' => \$show_inline,
'context' => \$show_context,
'srcline' => \$srcline_in_input,
'pid' => \$include_pid,
'kernel' => \$annotate_kernel,
'jit' => \$annotate_jit,
Expand All @@ -106,6 +109,7 @@ sub remember_stack {
--kernel # annotate kernel functions with a _[k]
--jit # annotate jit functions with a _[j]
--context # adds source context to --inline
--srcline # parses output of 'perf script -F+srcline' and adds source context
--addrs # include raw addresses where symbols can't be found
--event-filter=EVENT # event name filter\n
[1] perf script must emit both PID and TIDs for these to work; eg, Linux < 4.1:
Expand Down Expand Up @@ -286,6 +290,7 @@ sub inline {

next if $rawfunc =~ /^\(/; # skip process names

my $is_unknown=0;
my @inline;
for (split /\->/, $rawfunc) {
my $func = $_;
Expand All @@ -296,6 +301,7 @@ sub inline {
$func =~ s/.*\///;
} else {
$func = "unknown";
$is_unknown=1;
}

if ($include_addrs) {
Expand Down Expand Up @@ -349,6 +355,42 @@ sub inline {
} elsif ($annotate_jit == 1 && $mod =~ m:/tmp/perf-\d+\.map:) {
$func .= "_[j]"; # jitted
}

#
# Source lines
#
#
# Sample outputs:
# | a.out 35081 252436.005167: 667783 cycles:
# | 408ebb some_method_name+0x8b (/full/path/to/a.out)
# | uniform_int_dist.h:300
# | 4069f5 main+0x935 (/full/path/to/a.out)
# | file.cpp:137
# | 7f6d2148eb25 __libc_start_main+0xd5 (/lib64/libc-2.33.so)
# | libc-2.33.so[27b25]
#
# | a.out 35081 252435.738165: 306459 cycles:
# | 7f6d213c2750 [unknown] (/usr/lib64/libkmod.so.2.3.6)
# | libkmod.so.2.3.6[6750]
#
# | a.out 35081 252435.738373: 315813 cycles:
# | 7f6d215ca51b __strlen_avx2+0x4b (/lib64/libc-2.33.so)
# | libc-2.33.so[16351b]
# | 7ffc71ee9580 [unknown] ([unknown])
# |
#
# | a.out 35081 252435.718940: 247984 cycles:
# | ffffffff814f9302 up_write+0x32 ([kernel.kallsyms])
# | [kernel.kallsyms][ffffffff814f9302]
if($srcline_in_input and not $is_unknown){
$_ = <>;
chomp;
s/\[.*?\]//g;
s/^\s*//g;
s/\s*$//g;
$func.=':'.$_ unless $_ eq "";
}

push @inline, $func;
}

Expand Down

0 comments on commit 7b0ca27

Please sign in to comment.