You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fnrec.pl 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as
  7. # published by the Free Software Foundation; either version 2 of the
  8. # License, or any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. # 02110-1301, USA.
  19. =head1 NAME
  20. fnrec.pl
  21. =head1 SYNOPSIS
  22. fnrec.pl [options] bin/image.xxx < logfile
  23. Decode a function trace produced by building with FNREC=1
  24. Options:
  25. -m,--max-depth=N Set maximum displayed function depth
  26. =cut
  27. use IPC::Open2;
  28. use Getopt::Long;
  29. use Pod::Usage;
  30. use strict;
  31. use warnings;
  32. use constant MAX_OPEN_BRACE => 10;
  33. use constant MAX_COMMON_BRACE => 3;
  34. use constant MAX_CLOSE_BRACE => 10;
  35. # Parse command-line options
  36. my $max_depth = 16;
  37. Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
  38. GetOptions (
  39. 'help|h' => sub { pod2usage ( 1 ); },
  40. 'max-depth|m=i' => sub { shift; $max_depth = shift; },
  41. ) or die "Could not parse command-line options\n";
  42. pod2usage ( 1 ) unless @ARGV == 1;
  43. my $image = shift;
  44. my $elf = $image.".tmp";
  45. die "ELF file ".$elf." not found\n" unless -e $elf;
  46. # Start up addr2line
  47. my $addr2line_pid = open2 ( my $addr2line_out, my $addr2line_in,
  48. "addr2line", "-f", "-e", $elf )
  49. or die "Could not start addr2line: $!\n";
  50. # Translate address using addr2line
  51. sub addr2line {
  52. my $address = shift;
  53. print $addr2line_in $address."\n";
  54. chomp ( my $name = <$addr2line_out> );
  55. chomp ( my $file_line = <$addr2line_out> );
  56. ( my $file, my $line ) = ( $file_line =~ /^(.*):(\d+)$/ );
  57. $file =~ s/^.*\/src\///;
  58. my $location = ( $line ? $file.":".$line." = ".$address : $address );
  59. return ( $name, $location );
  60. }
  61. # Parse logfile
  62. my $depth = 0;
  63. my $depths = [];
  64. while ( my $line = <> ) {
  65. chomp $line;
  66. $line =~ s/\r//g;
  67. ( my $called_fn, my $call_site, my $entry_count, my $exit_count ) =
  68. ( $line =~ /^(0x[0-9a-f]+)\s+(0x[0-9a-f]+)\s+([0-9]+)\s+([0-9]+)$/ )
  69. or print $line."\n" and next;
  70. ( my $called_fn_name, undef ) = addr2line ( $called_fn );
  71. ( undef, my $call_site_location ) = addr2line ( $call_site );
  72. $entry_count = ( $entry_count + 0 );
  73. $exit_count = ( $exit_count + 0 );
  74. if ( $entry_count >= $exit_count ) {
  75. #
  76. # Function entry
  77. #
  78. my $text = "";
  79. $text .= $called_fn_name." (from ".$call_site_location.")";
  80. if ( $exit_count <= MAX_COMMON_BRACE ) {
  81. $text .= " { }" x $exit_count;
  82. } else {
  83. $text .= " { } x ".$exit_count;
  84. }
  85. $entry_count -= $exit_count;
  86. if ( $entry_count <= MAX_OPEN_BRACE ) {
  87. $text .= " {" x $entry_count;
  88. } else {
  89. $text .= " { x ".$entry_count;
  90. }
  91. my $indent = " " x $depth;
  92. print $indent.$text."\n";
  93. $depth += $entry_count;
  94. $depth = $max_depth if ( $depth > $max_depth );
  95. push @$depths, ( { called_fn => $called_fn, call_site => $call_site } ) x
  96. ( $depth - @$depths );
  97. } else {
  98. #
  99. # Function exit
  100. #
  101. my $text = "";
  102. if ( $entry_count <= MAX_COMMON_BRACE ) {
  103. $text .= " { }" x $entry_count;
  104. } else {
  105. $text .= " { } x ".$entry_count;
  106. }
  107. $exit_count -= $entry_count;
  108. if ( $exit_count <= MAX_CLOSE_BRACE ) {
  109. $text .= " }" x $exit_count;
  110. } else {
  111. $text .= " } x ".$exit_count;
  112. }
  113. $depth -= $exit_count;
  114. $depth = 0 if ( $depth < 0 );
  115. if ( ( @$depths == 0 ) ||
  116. ( $depths->[$depth]->{called_fn} ne $called_fn ) ||
  117. ( $depths->[$depth]->{call_site} ne $call_site ) ) {
  118. $text .= " (from ".$called_fn_name." to ".$call_site_location.")";
  119. }
  120. splice ( @$depths, $depth );
  121. my $indent = " " x $depth;
  122. print substr ( $indent.$text, 1 )."\n";
  123. }
  124. }
  125. # Clean up addr2line
  126. close $addr2line_in;
  127. close $addr2line_out;
  128. waitpid ( $addr2line_pid, 0 );