stackcollapse-gdb.pl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/perl -ws
  2. #
  3. # stackcollapse-gdb Collapse GDB backtraces
  4. #
  5. # Parse a list of GDB backtraces as generated with the poor man's
  6. # profiler [1]:
  7. #
  8. # for x in $(seq 1 500); do
  9. # gdb -ex "set pagination 0" -ex "thread apply all bt" -batch -p $pid 2> /dev/null
  10. # sleep 0.01
  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. my $current = "";
  37. my $previous_function = "";
  38. my %stacks;
  39. while(<>) {
  40. chomp;
  41. if (m/^Thread/) {
  42. $current=""
  43. }
  44. elsif(m/^#[0-9]* *([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*)/) {
  45. my $function = $3;
  46. my $alt = $1;
  47. if(not($1 =~ /0x[a-zA-Z0-9]*/)) {
  48. $function = $alt;
  49. }
  50. if ($current eq "") {
  51. $current = $function;
  52. } else {
  53. $current = $function . ";" . $current;
  54. }
  55. } elsif(!($current eq "")) {
  56. $stacks{$current} += 1;
  57. $current = "";
  58. }
  59. }
  60. if(!($current eq "")) {
  61. $stacks{$current} += 1;
  62. $current = "";
  63. }
  64. foreach my $k (sort { $a cmp $b } keys %stacks) {
  65. print "$k $stacks{$k}\n";
  66. }