mtrace 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #! /usr/bin/perl
  2. eval "exec /usr/bin/perl -S $0 $@"
  3. if 0;
  4. # Copyright (C) 1997-2021 Free Software Foundation, Inc.
  5. # This file is part of the GNU C Library.
  6. # Contributed by Ulrich Drepper <drepper@gnu.org>, 1997.
  7. # Based on the mtrace.awk script.
  8. # The GNU C Library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. # The GNU C Library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # Lesser General Public License for more details.
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with the GNU C Library; if not, see
  18. # <https://www.gnu.org/licenses/>.
  19. $VERSION = "2.33";
  20. $PKGVERSION = "(GNU) ";
  21. $REPORT_BUGS_TO = '<https://bugs.linaro.org/>';
  22. $progname = $0;
  23. sub usage {
  24. print "Usage: mtrace [OPTION]... [Binary] MtraceData\n";
  25. print " --help print this help, then exit\n";
  26. print " --version print version number, then exit\n";
  27. print "\n";
  28. print "For bug reporting instructions, please see:\n";
  29. print "$REPORT_BUGS_TO.\n";
  30. exit 0;
  31. }
  32. # We expect two arguments:
  33. # #1: the complete path to the binary
  34. # #2: the mtrace data filename
  35. # The usual options are also recognized.
  36. arglist: while (@ARGV) {
  37. if ($ARGV[0] eq "--v" || $ARGV[0] eq "--ve" || $ARGV[0] eq "--ver" ||
  38. $ARGV[0] eq "--vers" || $ARGV[0] eq "--versi" ||
  39. $ARGV[0] eq "--versio" || $ARGV[0] eq "--version") {
  40. print "mtrace $PKGVERSION$VERSION\n";
  41. print "Copyright (C) 2021 Free Software Foundation, Inc.\n";
  42. print "This is free software; see the source for copying conditions. There is NO\n";
  43. print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
  44. print "Written by Ulrich Drepper <drepper\@gnu.org>\n";
  45. exit 0;
  46. } elsif ($ARGV[0] eq "--h" || $ARGV[0] eq "--he" || $ARGV[0] eq "--hel" ||
  47. $ARGV[0] eq "--help") {
  48. &usage;
  49. } elsif ($ARGV[0] =~ /^-/) {
  50. print "$progname: unrecognized option `$ARGV[0]'\n";
  51. print "Try `$progname --help' for more information.\n";
  52. exit 1;
  53. } else {
  54. last arglist;
  55. }
  56. }
  57. if ($#ARGV == 0) {
  58. $binary="";
  59. $data=$ARGV[0];
  60. } elsif ($#ARGV == 1) {
  61. $binary=$ARGV[0];
  62. $data=$ARGV[1];
  63. if ($binary =~ /^.*[\/].*$/) {
  64. $prog = $binary;
  65. } else {
  66. $prog = "./$binary";
  67. }
  68. if (open (LOCS, "env LD_TRACE_LOADED_OBJECTS=1 $prog |")) {
  69. while (<LOCS>) {
  70. chop;
  71. if (/^.*=> (.*) .(0x[0123456789abcdef]*).$/) {
  72. $locs{$1} = $2;
  73. }
  74. }
  75. close (LOCS);
  76. }
  77. } else {
  78. die "Wrong number of arguments, run $progname --help for help.";
  79. }
  80. sub location {
  81. my $str = pop(@_);
  82. return $str if ($str eq "");
  83. if ($str =~ /.*[[](0x[^]]*)]:(.)*/) {
  84. my $addr = $1;
  85. my $fct = $2;
  86. return $cache{$addr} if (exists $cache{$addr});
  87. if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
  88. my $line = <ADDR>;
  89. chomp $line;
  90. close (ADDR);
  91. if ($line ne '??:0') {
  92. $cache{$addr} = $line;
  93. return $cache{$addr};
  94. }
  95. }
  96. $cache{$addr} = $str = "$fct @ $addr";
  97. } elsif ($str =~ /^(.*):.*[[](0x[^]]*)]$/) {
  98. my $prog = $1;
  99. my $addr = $2;
  100. my $searchaddr;
  101. return $cache{$addr} if (exists $cache{$addr});
  102. if ($locs{$prog} ne "") {
  103. $searchaddr = sprintf "%#x", $addr - $locs{$prog};
  104. } else {
  105. $searchaddr = $addr;
  106. $prog = $binary;
  107. }
  108. if ($binary ne "" && open (ADDR, "addr2line -e $prog $searchaddr|")) {
  109. my $line = <ADDR>;
  110. chomp $line;
  111. close (ADDR);
  112. if ($line ne '??:0') {
  113. $cache{$addr} = $line;
  114. return $cache{$addr};
  115. }
  116. }
  117. $cache{$addr} = $str = $addr;
  118. } elsif ($str =~ /^.*[[](0x[^]]*)]$/) {
  119. my $addr = $1;
  120. return $cache{$addr} if (exists $cache{$addr});
  121. if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
  122. my $line = <ADDR>;
  123. chomp $line;
  124. close (ADDR);
  125. if ($line ne '??:0') {
  126. $cache{$addr} = $line;
  127. return $cache{$addr};
  128. }
  129. }
  130. $cache{$addr} = $str = $addr;
  131. }
  132. return $str;
  133. }
  134. $nr=0;
  135. open(DATA, "<$data") || die "Cannot open mtrace data file";
  136. while (<DATA>) {
  137. my @cols = split (' ');
  138. my $n, $where;
  139. if ($cols[0] eq "@") {
  140. # We have address and/or function name.
  141. $where=$cols[1];
  142. $n=2;
  143. } else {
  144. $where="";
  145. $n=0;
  146. }
  147. $allocaddr=$cols[$n + 1];
  148. $howmuch=hex($cols[$n + 2]);
  149. ++$nr;
  150. SWITCH: {
  151. if ($cols[$n] eq "+") {
  152. if (defined $allocated{$allocaddr}) {
  153. printf ("+ %#018x Alloc %d duplicate: %s %s\n",
  154. hex($allocaddr), $nr, &location($addrwas{$allocaddr}),
  155. $where);
  156. } elsif ($allocaddr =~ /^0x/) {
  157. $allocated{$allocaddr}=$howmuch;
  158. $addrwas{$allocaddr}=$where;
  159. }
  160. last SWITCH;
  161. }
  162. if ($cols[$n] eq "-") {
  163. if (defined $allocated{$allocaddr}) {
  164. undef $allocated{$allocaddr};
  165. undef $addrwas{$allocaddr};
  166. } else {
  167. printf ("- %#018x Free %d was never alloc'd %s\n",
  168. hex($allocaddr), $nr, &location($where));
  169. }
  170. last SWITCH;
  171. }
  172. if ($cols[$n] eq "<") {
  173. if (defined $allocated{$allocaddr}) {
  174. undef $allocated{$allocaddr};
  175. undef $addrwas{$allocaddr};
  176. } else {
  177. printf ("- %#018x Realloc %d was never alloc'd %s\n",
  178. hex($allocaddr), $nr, &location($where));
  179. }
  180. last SWITCH;
  181. }
  182. if ($cols[$n] eq ">") {
  183. if (defined $allocated{$allocaddr}) {
  184. printf ("+ %#018x Realloc %d duplicate: %#010x %s %s\n",
  185. hex($allocaddr), $nr, $allocated{$allocaddr},
  186. &location($addrwas{$allocaddr}), &location($where));
  187. } else {
  188. $allocated{$allocaddr}=$howmuch;
  189. $addrwas{$allocaddr}=$where;
  190. }
  191. last SWITCH;
  192. }
  193. if ($cols[$n] eq "=") {
  194. # Ignore "= Start".
  195. last SWITCH;
  196. }
  197. if ($cols[$n] eq "!") {
  198. # Ignore failed realloc for now.
  199. last SWITCH;
  200. }
  201. }
  202. }
  203. close (DATA);
  204. # Now print all remaining entries.
  205. @addrs= keys %allocated;
  206. $anything=0;
  207. if ($#addrs >= 0) {
  208. foreach $addr (sort @addrs) {
  209. if (defined $allocated{$addr}) {
  210. if ($anything == 0) {
  211. print "\nMemory not freed:\n-----------------\n";
  212. print ' ' x (18 - 7), "Address Size Caller\n";
  213. $anything=1;
  214. }
  215. printf ("%#018x %#8x at %s\n", hex($addr), $allocated{$addr},
  216. &location($addrwas{$addr}));
  217. }
  218. }
  219. }
  220. print "No memory leaks.\n" if ($anything == 0);
  221. exit $anything != 0;