FlameGraph icon indicating copy to clipboard operation
FlameGraph copied to clipboard

Adding line numbers to perf output breaks stackcollapse-perf.pl

Open Hi-Angel opened this issue 5 years ago • 3 comments

Sometimes it's hard to navigate code without having line numbers in source. This is especially relevant for Vala-based apps, as they tend to mangle function names in various ways. So I'm trying to add srcline column to perf script, so that I'd have in flamegraph line numbers, but this breaks flamegraph.

Steps to reproduce (in terms of terminal commands)

$ perf record -g --sample-cpu ls
$ perf script -F+srcline | stackcollapse-perf.pl

Expected

Script finishes with no warnings or errors

Actual

Lots of

Unrecognized line:   ld-2.29.so[11634] at /usr/bin/stackcollapse-perf.pl line 339, <> line 50.

Hi-Angel avatar May 04 '19 01:05 Hi-Angel

I recognize this is actually not a failing of FlameGraph, so maybe it belongs somewhere else... but I too would love to know if there is a way to get perf (or some other profiler) to record stack traces with line numbers in a way that FlameGraph can use them. The -F+srcline above doesn't really give me line numbers anyway, otherwise I would be able to massage the output to send to stackcollapse-perf.pl.

SyamGadde avatar Dec 03 '19 22:12 SyamGadde

I found a way to filter the output of perf script -F +srcline so it's compatible with "stackcollapse-perf.pl" - "filter-perf-script.pl":

#!/usr/bin/env perl

use strict;
use warnings;

my $prev_line = "";
my $line_info;

while(<>) {
    if(/^  \S+/ and $prev_line =~ /\+0x/) {
        $line_info = $_;
        $line_info =~ s/^\s+//;
        $line_info =~ s/\s+$//;
        $line_info =~ s/^(\S+)/|$1|/;

        $prev_line =~ s/\+0x/$line_info+0x/;
    }
    if(not $prev_line =~ /^  \S+/) {
        print($prev_line);
    }
    $prev_line = $_;
}
print($prev_line);

And I use it like this:

perf record --call-graph dwarf,16000 ...
perf script -F +srcline | ./filter-perf-script.pl > out.perf
./stackcollapse-perf.pl out.perf > perf_collapsed.txt
./flamegraph.pl perf_collapsed.txt > perf.svg

stefantalpalaru avatar Sep 23 '20 22:09 stefantalpalaru

I see this feature as very handy! The @stefantalpalaru's patch works fine, but I would prefer to display the location only on hover, it makes the report less readable.

marxin avatar Feb 21 '21 14:02 marxin