stackcollapse-elfutils.pl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. use strict;
  36. use Getopt::Long;
  37. my $with_pid = 0;
  38. my $with_tid = 0;
  39. GetOptions('pid' => \$with_pid,
  40. 'tid' => \$with_tid)
  41. or die <<USAGE_END;
  42. USAGE: $0 [options] infile > outfile\n
  43. --pid # include PID
  44. --tid # include TID
  45. USAGE_END
  46. my $pid = "";
  47. my $tid = "";
  48. my $current = "";
  49. my $previous_function = "";
  50. my %stacks;
  51. sub add_current {
  52. if(!($current eq "")) {
  53. my $entry;
  54. if ($with_tid) {
  55. $current = "TID=$tid;$current";
  56. }
  57. if ($with_pid) {
  58. $current = "PID=$pid;$current";
  59. }
  60. $stacks{$current} += 1;
  61. $current = "";
  62. }
  63. }
  64. while(<>) {
  65. chomp;
  66. if (m/^PID ([0-9]*)/) {
  67. add_current();
  68. $pid = $1;
  69. }
  70. elsif(m/^TID ([0-9]*)/) {
  71. add_current();
  72. $tid = $1;
  73. } elsif(m/^#[0-9]* *0x[0-9a-f]* (.*)/) {
  74. if ($current eq "") {
  75. $current = $1;
  76. } else {
  77. $current = "$1;$current";
  78. }
  79. } elsif(m/^#[0-9]* *0x[0-9a-f]*/) {
  80. if ($current eq "") {
  81. $current = "[unknown]";
  82. } else {
  83. $current = "[unknown];$current";
  84. }
  85. }
  86. }
  87. add_current();
  88. foreach my $k (sort { $a cmp $b } keys %stacks) {
  89. print "$k $stacks{$k}\n";
  90. }