mtrace 6.5 KB

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