stackcollapse-elfutils.pl (2304B)
1 #!/usr/bin/perl -w 2 # 3 # stackcollapse-elfutils Collapse elfutils stack (eu-stack) backtraces 4 # 5 # Parse a list of elfutils backtraces as generated with the poor man's 6 # profiler [1]: 7 # 8 # for x in $(seq 1 "$nsamples"); do 9 # eu-stack -p "$pid" "$@" 10 # sleep "$sleeptime" 11 # done 12 # 13 # [1] http://poormansprofiler.org/ 14 # 15 # Copyright 2014 Gabriel Corona. All rights reserved. 16 # 17 # CDDL HEADER START 18 # 19 # The contents of this file are subject to the terms of the 20 # Common Development and Distribution License (the "License"). 21 # You may not use this file except in compliance with the License. 22 # 23 # You can obtain a copy of the license at docs/cddl1.txt or 24 # http://opensource.org/licenses/CDDL-1.0. 25 # See the License for the specific language governing permissions 26 # and limitations under the License. 27 # 28 # When distributing Covered Code, include this CDDL HEADER in each 29 # file and include the License file at docs/cddl1.txt. 30 # If applicable, add the following below this CDDL HEADER, with the 31 # fields enclosed by brackets "[]" replaced with your own identifying 32 # information: Portions Copyright [yyyy] [name of copyright owner] 33 # 34 # CDDL HEADER END 35 36 use strict; 37 use Getopt::Long; 38 39 my $with_pid = 0; 40 my $with_tid = 0; 41 42 GetOptions('pid' => \$with_pid, 43 'tid' => \$with_tid) 44 or die <<USAGE_END; 45 USAGE: $0 [options] infile > outfile\n 46 --pid # include PID 47 --tid # include TID 48 USAGE_END 49 50 my $pid = ""; 51 my $tid = ""; 52 my $current = ""; 53 my $previous_function = ""; 54 55 my %stacks; 56 57 sub add_current { 58 if(!($current eq "")) { 59 my $entry; 60 if ($with_tid) { 61 $current = "TID=$tid;$current"; 62 } 63 if ($with_pid) { 64 $current = "PID=$pid;$current"; 65 } 66 $stacks{$current} += 1; 67 $current = ""; 68 } 69 } 70 71 while(<>) { 72 chomp; 73 if (m/^PID ([0-9]*)/) { 74 add_current(); 75 $pid = $1; 76 } 77 elsif(m/^TID ([0-9]*)/) { 78 add_current(); 79 $tid = $1; 80 } elsif(m/^#[0-9]* *0x[0-9a-f]* (.*)/) { 81 if ($current eq "") { 82 $current = $1; 83 } else { 84 $current = "$1;$current"; 85 } 86 } elsif(m/^#[0-9]* *0x[0-9a-f]*/) { 87 if ($current eq "") { 88 $current = "[unknown]"; 89 } else { 90 $current = "[unknown];$current"; 91 } 92 } 93 } 94 add_current(); 95 96 foreach my $k (sort { $a cmp $b } keys %stacks) { 97 print "$k $stacks{$k}\n"; 98 }