]> 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 f183340b21ffc9c1472e0d785a9a900ff1a43c12..075da0d9f11da15c32fb50dbe4a9d34b6e90e2cc 100644 (file)
@@ -1,9 +1,8 @@
 #! @PERL@
 eval "exec @PERL@ -S $0 $@"
     if 0;
-# Copyright (C) 1997-2015 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
@@ -18,7 +17,7 @@ eval "exec @PERL@ -S $0 $@"
 
 # You should have received a copy of the GNU Lesser General Public
 # License along with the GNU C Library; if not, see
-# <http://www.gnu.org/licenses/>.
+# <https://www.gnu.org/licenses/>.
 
 $VERSION = "@VERSION@";
 $PKGVERSION = "@PKGVERSION@";
@@ -45,7 +44,7 @@ arglist: while (@ARGV) {
        $ARGV[0] eq "--vers" || $ARGV[0] eq "--versi" ||
        $ARGV[0] eq "--versio" || $ARGV[0] eq "--version") {
        print "mtrace $PKGVERSION$VERSION\n";
-       print "Copyright (C) 2014 Free Software Foundation, Inc.\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";
@@ -75,11 +74,14 @@ if ($#ARGV == 0) {
     } else {
        $prog = "./$binary";
     }
-    if (open (LOCS, "env LD_TRACE_LOADED_OBJECTS=1 $prog |")) {
-       while (<LOCS>) {
+    # 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);
@@ -88,6 +90,18 @@ if ($#ARGV == 0) {
     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 "");
@@ -95,11 +109,9 @@ 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};
            }
@@ -110,30 +122,23 @@ sub location {
        my $addr = $2;
        my $searchaddr;
        return $cache{$addr} if (exists $cache{$addr});
-       if ($locs{$prog} ne "") {
-           $searchaddr = sprintf "%#x", $addr - $locs{$prog};
-       } else {
-           $searchaddr = $addr;
-           $prog = $binary;
-       }
-       if ($binary ne "" && open (ADDR, "addr2line -e $prog $searchaddr|")) {
-           my $line = <ADDR>;
-           chomp $line;
-           close (ADDR);
-           if ($line ne '??:0') {
-               $cache{$addr} = $line;
-               return $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};
            }
@@ -167,7 +172,7 @@ while (<DATA>) {
                printf ("+ %#0@XXX@x Alloc %d duplicate: %s %s\n",
                        hex($allocaddr), $nr, &location($addrwas{$allocaddr}),
                        $where);
-           } else {
+           } elsif ($allocaddr =~ /^0x/) {
                $allocated{$allocaddr}=$howmuch;
                $addrwas{$allocaddr}=$where;
            }