]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - malloc/mtrace.pl
Update copyright dates not handled by scripts/update-copyrights
[thirdparty/glibc.git] / malloc / mtrace.pl
index a254c6e86047ee33657707a90a80fed6e738a2dc..075da0d9f11da15c32fb50dbe4a9d34b6e90e2cc 100644 (file)
@@ -1,9 +1,8 @@
 #! @PERL@
-eval "exec @PERL@ -S $0 $*"
+eval "exec @PERL@ -S $0 $@"
     if 0;
-# Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+# Copyright (C) 1997-2024 Free Software Foundation, Inc.
 # This file is part of the GNU C Library.
-# Contributed by Ulrich Drepper <drepper@gnu.org>, 1997.
 # Based on the mtrace.awk script.
 
 # The GNU C Library is free software; you can redistribute it and/or
@@ -17,12 +16,12 @@ eval "exec @PERL@ -S $0 $*"
 # Lesser General Public License for more details.
 
 # You should have received a copy of the GNU Lesser General Public
-# License along with the GNU C Library; if not, write to the Free
-# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-# 02111-1307 USA.
+# License along with the GNU C Library; if not, see
+# <https://www.gnu.org/licenses/>.
 
 $VERSION = "@VERSION@";
-$PACKAGE = "libc";
+$PKGVERSION = "@PKGVERSION@";
+$REPORT_BUGS_TO = '@REPORT_BUGS_TO@';
 $progname = $0;
 
 sub usage {
@@ -30,7 +29,8 @@ sub usage {
     print "  --help       print this help, then exit\n";
     print "  --version    print version number, then exit\n";
     print "\n";
-    print "Report bugs using the `glibcbug' script to <bugs\@gnu.org>.\n";
+    print "For bug reporting instructions, please see:\n";
+    print "$REPORT_BUGS_TO.\n";
     exit 0;
 }
 
@@ -43,8 +43,8 @@ arglist: while (@ARGV) {
     if ($ARGV[0] eq "--v" || $ARGV[0] eq "--ve" || $ARGV[0] eq "--ver" ||
        $ARGV[0] eq "--vers" || $ARGV[0] eq "--versi" ||
        $ARGV[0] eq "--versio" || $ARGV[0] eq "--version") {
-       print "mtrace (GNU $PACKAGE) $VERSION\n";
-       print "Copyright (C) 2001 Free Software Foundation, Inc.\n";
+       print "mtrace $PKGVERSION$VERSION\n";
+       print "Copyright (C) 2024 Free Software Foundation, Inc.\n";
        print "This is free software; see the source for copying conditions.  There is NO\n";
        print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
        print "Written by Ulrich Drepper <drepper\@gnu.org>\n";
@@ -68,10 +68,40 @@ if ($#ARGV == 0) {
 } elsif ($#ARGV == 1) {
     $binary=$ARGV[0];
     $data=$ARGV[1];
+
+    if ($binary =~ /^.*[\/].*$/) {
+       $prog = $binary;
+    } else {
+       $prog = "./$binary";
+    }
+    # Set the environment variable LD_TRACE_LOADED_OBJECTS to 2 so the
+    # executable is also printed.
+    if (open (locs, "env LD_TRACE_LOADED_OBJECTS=2 $prog |")) {
+       while (<locs>) {
+           chop;
+           if (/^.*=> (.*) .(0x[0123456789abcdef]*).$/) {
+               $locs{$1} = $2;
+               $rel{$1} = hex($2);
+           }
+       }
+       close (LOCS);
+    }
 } else {
     die "Wrong number of arguments, run $progname --help for help.";
 }
 
+sub addr2line {
+    my $addr = pop(@_);
+    my $prog = pop(@_);
+    if (open (ADDR, "addr2line -e $prog $addr|")) {
+       my $line = <ADDR>;
+       chomp $line;
+       close (ADDR);
+       if ($line ne '??:0') {
+           return $line
+       }
+    }
+}
 sub location {
     my $str = pop(@_);
     return $str if ($str eq "");
@@ -79,24 +109,36 @@ sub location {
        my $addr = $1;
        my $fct = $2;
        return $cache{$addr} if (exists $cache{$addr});
-       if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
-           my $line = <ADDR>;
-           chomp $line;
-           close (ADDR);
-           if ($line ne '??:0') {
+       if ($binary ne "") {
+           my $line = &addr2line($binary, $addr);
+           if ($line) {
                $cache{$addr} = $line;
                return $cache{$addr};
            }
        }
        $cache{$addr} = $str = "$fct @ $addr";
+    } elsif ($str =~ /^(.*):.*[[](0x[^]]*)]$/) {
+       my $prog = $1;
+       my $addr = $2;
+       my $searchaddr;
+       return $cache{$addr} if (exists $cache{$addr});
+       $searchaddr = sprintf "%#x", hex($addr) + $rel{$prog};
+       if ($binary ne "") {
+           for my $address ($searchaddr, $addr) {
+               my $line = &addr2line($prog, $address);
+               if ($line) {
+                   $cache{$addr} = $line;
+                   return $cache{$addr};
+               }
+           }
+       }
+       $cache{$addr} = $str = $addr;
     } elsif ($str =~ /^.*[[](0x[^]]*)]$/) {
        my $addr = $1;
        return $cache{$addr} if (exists $cache{$addr});
-       if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
-           my $line = <ADDR>;
-           chomp $line;
-           close (ADDR);
-           if ($line ne '??:0') {
+       if ($binary ne "") {
+           my $line = &addr2line($binary, $addr);
+           if ($line) {
                $cache{$addr} = $line;
                return $cache{$addr};
            }
@@ -128,17 +170,18 @@ while (<DATA>) {
        if ($cols[$n] eq "+") {
            if (defined $allocated{$allocaddr}) {
                printf ("+ %#0@XXX@x Alloc %d duplicate: %s %s\n",
-                       hex($allocaddr), $nr, $wherewas{$allocaddr}, $where);
-           } else {
+                       hex($allocaddr), $nr, &location($addrwas{$allocaddr}),
+                       $where);
+           } elsif ($allocaddr =~ /^0x/) {
                $allocated{$allocaddr}=$howmuch;
-               $wherewas{$allocaddr}=&location($where);
+               $addrwas{$allocaddr}=$where;
            }
            last SWITCH;
        }
        if ($cols[$n] eq "-") {
            if (defined $allocated{$allocaddr}) {
                undef $allocated{$allocaddr};
-               undef $wherewas{$allocaddr};
+               undef $addrwas{$allocaddr};
            } else {
                printf ("- %#0@XXX@x Free %d was never alloc'd %s\n",
                        hex($allocaddr), $nr, &location($where));
@@ -148,7 +191,7 @@ while (<DATA>) {
        if ($cols[$n] eq "<") {
            if (defined $allocated{$allocaddr}) {
                undef $allocated{$allocaddr};
-               undef $wherewas{$allocaddr};
+               undef $addrwas{$allocaddr};
            } else {
                printf ("- %#0@XXX@x Realloc %d was never alloc'd %s\n",
                        hex($allocaddr), $nr, &location($where));
@@ -159,10 +202,10 @@ while (<DATA>) {
            if (defined $allocated{$allocaddr}) {
                printf ("+ %#0@XXX@x Realloc %d duplicate: %#010x %s %s\n",
                        hex($allocaddr), $nr, $allocated{$allocaddr},
-                       $wherewas{$allocaddr}, &location($where));
+                       &location($addrwas{$allocaddr}), &location($where));
            } else {
                $allocated{$allocaddr}=$howmuch;
-               $wherewas{$allocaddr}=&location($where);
+               $addrwas{$allocaddr}=$where;
            }
            last SWITCH;
        }
@@ -190,7 +233,7 @@ if ($#addrs >= 0) {
                $anything=1;
            }
            printf ("%#0@XXX@x %#8x  at %s\n", hex($addr), $allocated{$addr},
-                   $wherewas{$addr});
+                   &location($addrwas{$addr}));
        }
     }
 }