2003-08-08 12:51:56 +04:00
|
|
|
#!/usr/bin/perl
|
2006-03-24 00:17:48 +03:00
|
|
|
# vim:sw=4:ts=4:et:
|
2012-05-21 15:12:37 +04:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2003-08-08 12:51:56 +04:00
|
|
|
|
2008-05-06 01:51:11 +04:00
|
|
|
# $Id: fix-linux-stack.pl,v 1.16 2008/05/05 21:51:11 dbaron%dbaron.org Exp $
|
2007-06-21 02:01:17 +04:00
|
|
|
#
|
2003-08-08 12:51:56 +04:00
|
|
|
# This script uses addr2line (part of binutils) to process the output of
|
|
|
|
# nsTraceRefcnt's Linux stack walking code. This is useful for two
|
|
|
|
# things:
|
|
|
|
# (1) Getting line number information out of
|
|
|
|
# |nsTraceRefcntImpl::WalkTheStack|'s output in debug builds.
|
|
|
|
# (2) Getting function names out of |nsTraceRefcntImpl::WalkTheStack|'s
|
|
|
|
# output on optimized builds (where it mostly prints UNKNOWN
|
|
|
|
# because only a handful of symbols are exported from component
|
|
|
|
# libraries).
|
|
|
|
#
|
|
|
|
# Use the script by piping output containing stacks (such as raw stacks
|
|
|
|
# or make-tree.pl balance trees) through this script.
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use IPC::Open2;
|
2007-03-17 00:22:53 +03:00
|
|
|
use File::Basename;
|
|
|
|
|
|
|
|
# XXX Hard-coded to gdb defaults (works on Fedora).
|
|
|
|
my $global_debug_dir = '/usr/lib/debug';
|
2003-08-08 12:51:56 +04:00
|
|
|
|
2012-11-16 02:38:21 +04:00
|
|
|
# We record several things for each file encountered.
|
|
|
|
#
|
|
|
|
# - {pipe_read}, {pipe_write}: these constitute a bidirectional pipe to an
|
|
|
|
# addr2line process that gives symbol information for a file.
|
|
|
|
#
|
2012-11-16 02:38:24 +04:00
|
|
|
# - {cache}: this table holds the results of lookups that we've done
|
|
|
|
# previously for (pre-adjustment) addresses, which lets us avoid redundant
|
|
|
|
# calls to addr2line.
|
|
|
|
#
|
2012-11-16 02:38:21 +04:00
|
|
|
# - {address_adjustment}: addr2line wants offsets relative to the base address
|
|
|
|
# for shared libraries, but it wants addresses including the base address
|
|
|
|
# offset for executables. This holds the appropriate address adjustment to
|
|
|
|
# add to an offset within file. See bug 230336.
|
|
|
|
#
|
|
|
|
my %file_infos;
|
|
|
|
|
|
|
|
sub set_address_adjustment($$) {
|
|
|
|
my ($file, $file_info) = @_;
|
|
|
|
|
|
|
|
# find out if it's an executable (as opposed to a shared library)
|
|
|
|
my $elftype;
|
|
|
|
open(ELFHDR, '-|', 'readelf', '-h', $file);
|
|
|
|
while (<ELFHDR>) {
|
|
|
|
if (/^\s*Type:\s+(\S+)/) {
|
|
|
|
$elftype = $1;
|
|
|
|
last;
|
2006-03-24 00:17:48 +03:00
|
|
|
}
|
2012-11-16 02:38:21 +04:00
|
|
|
}
|
|
|
|
close(ELFHDR);
|
|
|
|
|
|
|
|
# If it's an executable, make adjustment the base address.
|
|
|
|
# Otherwise, leave it zero.
|
|
|
|
my $adjustment = 0;
|
|
|
|
if ($elftype eq 'EXEC') {
|
|
|
|
open(ELFSECS, '-|', 'readelf', '-S', $file);
|
|
|
|
while (<ELFSECS>) {
|
|
|
|
if (/^\s*\[\s*\d+\]\s+\.text\s+\w+\s+(\w+)\s+(\w+)\s+/) {
|
|
|
|
# Subtract the .text section's offset within the
|
|
|
|
# file from its base address.
|
|
|
|
$adjustment = hex($1) - hex($2);
|
|
|
|
last;
|
2006-03-24 00:17:48 +03:00
|
|
|
}
|
|
|
|
}
|
2012-11-16 02:38:21 +04:00
|
|
|
close(ELFSECS);
|
2006-03-24 00:17:48 +03:00
|
|
|
}
|
2012-11-16 02:38:21 +04:00
|
|
|
|
|
|
|
$file_info->{address_adjustment} = $adjustment;
|
2006-03-24 00:17:48 +03:00
|
|
|
}
|
|
|
|
|
2007-03-17 00:22:53 +03:00
|
|
|
# Files sometimes contain a link to a separate object file that contains
|
2007-03-23 09:49:37 +03:00
|
|
|
# the debug sections of the binary, removed so that a smaller file can
|
|
|
|
# be shipped, but kept separately so that it can be obtained by those
|
|
|
|
# who want it.
|
2007-03-17 00:22:53 +03:00
|
|
|
# See http://sources.redhat.com/gdb/current/onlinedocs/gdb_16.html#SEC154
|
2007-03-23 09:49:37 +03:00
|
|
|
# for documentation of debugging information in separate files.
|
2007-03-17 00:22:53 +03:00
|
|
|
# On Fedora distributions, these files can be obtained by installing
|
|
|
|
# *-debuginfo RPM packages.
|
2007-03-23 09:49:37 +03:00
|
|
|
sub separate_debug_file_for($) {
|
2006-03-24 05:19:16 +03:00
|
|
|
my ($file) = @_;
|
2007-03-17 00:22:53 +03:00
|
|
|
# We can read the .gnu_debuglink section using either of:
|
|
|
|
# objdump -s --section=.gnu_debuglink $file
|
|
|
|
# readelf -x .gnu_debuglink $file
|
2007-06-21 02:01:17 +04:00
|
|
|
# Since readelf prints things backwards on little-endian platforms
|
|
|
|
# for some versions only (backwards on Fedora Core 6, forwards on
|
|
|
|
# Fedora 7), use objdump.
|
2007-03-17 00:22:53 +03:00
|
|
|
|
|
|
|
# See if there's a .gnu_debuglink section
|
|
|
|
my $have_debuglink = 0;
|
|
|
|
open(ELFSECS, '-|', 'readelf', '-S', $file);
|
|
|
|
while (<ELFSECS>) {
|
|
|
|
if (/^\s*\[\s*\d+\]\s+\.gnu_debuglink\s+\w+\s+(\w+)\s+(\w+)\s+/) {
|
|
|
|
$have_debuglink = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(ELFSECS);
|
|
|
|
return '' unless ($have_debuglink);
|
|
|
|
|
|
|
|
# Determine the endianness of the shared library.
|
|
|
|
my $endian = '';
|
|
|
|
open(ELFHDR, '-|', 'readelf', '-h', $file);
|
|
|
|
while (<ELFHDR>) {
|
|
|
|
if (/^\s*Data:\s+.*(little|big) endian.*$/) {
|
|
|
|
$endian = $1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(ELFHDR);
|
|
|
|
if ($endian ne 'little' && $endian ne 'big') {
|
|
|
|
print STDERR "Warning: could not determine endianness of $file.\n";
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2007-06-21 02:01:17 +04:00
|
|
|
|
|
|
|
# Read the debuglink section as an array of words, in hexidecimal.
|
|
|
|
open(DEBUGLINK, '-|', 'objdump', '-s', '--section=.gnu_debuglink', $file);
|
2007-03-17 00:22:53 +03:00
|
|
|
my @words;
|
|
|
|
while (<DEBUGLINK>) {
|
2007-06-21 02:01:17 +04:00
|
|
|
if ($_ =~ /^ [0-9a-f]* ([0-9a-f ]{8}) ([0-9a-f ]{8}) ([0-9a-f ]{8}) ([0-9a-f ]{8}).*/) {
|
|
|
|
push @words, $1, $2, $3, $4;
|
2007-03-17 00:22:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(DEBUGLINK);
|
|
|
|
|
|
|
|
while (@words[$#words] eq ' ') {
|
|
|
|
pop @words;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($#words < 1) {
|
|
|
|
print STDERR "Warning: .gnu_debuglink section in $file too short.\n";
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
my @chars;
|
|
|
|
while ($#words >= 0) {
|
|
|
|
my $w = shift @words;
|
|
|
|
if ($w =~ /^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/) {
|
2007-06-21 02:01:17 +04:00
|
|
|
push @chars, $1, $2, $3, $4;
|
2007-03-17 00:22:53 +03:00
|
|
|
} else {
|
2007-06-21 02:01:17 +04:00
|
|
|
print STDERR "Warning: malformed objdump output for $file.\n";
|
2007-03-17 00:22:53 +03:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my @hash_bytes = map(hex, @chars[$#chars - 3 .. $#chars]);
|
|
|
|
$#chars -= 4;
|
|
|
|
|
|
|
|
my $hash;
|
|
|
|
if ($endian eq 'little') {
|
|
|
|
$hash = ($hash_bytes[3] << 24) | ($hash_bytes[2] << 16) | ($hash_bytes[1] << 8) | $hash_bytes[0];
|
|
|
|
} else {
|
|
|
|
$hash = ($hash_bytes[0] << 24) | ($hash_bytes[1] << 16) | ($hash_bytes[2] << 8) | $hash_bytes[3];
|
|
|
|
}
|
|
|
|
|
2008-05-06 01:51:11 +04:00
|
|
|
# The string ends with a null-terminator and then 0 to three bytes
|
|
|
|
# of padding to fill the current 32-bit unit. (This padding is
|
|
|
|
# usually null bytes, but I've seen null-null-H, on Ubuntu x86_64.)
|
|
|
|
my $terminator = 1;
|
|
|
|
while ($chars[$terminator] ne '00') {
|
|
|
|
if ($terminator == $#chars) {
|
|
|
|
print STDERR "Warning: missing null terminator in " .
|
|
|
|
".gnu_debuglink section of $file.\n";
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
++$terminator;
|
2007-03-17 00:22:53 +03:00
|
|
|
}
|
2008-05-06 01:51:11 +04:00
|
|
|
if ($#chars - $terminator > 3) {
|
|
|
|
print STDERR "Warning: Excess padding in .gnu_debuglink section " .
|
|
|
|
"of $file.\n";
|
2007-03-17 00:22:53 +03:00
|
|
|
return '';
|
|
|
|
}
|
2008-05-06 01:51:11 +04:00
|
|
|
$#chars = $terminator - 1;
|
2007-03-17 00:22:53 +03:00
|
|
|
|
|
|
|
my $basename = join('', map { chr(hex($_)) } @chars);
|
|
|
|
|
|
|
|
# Now $basename and $hash represent the information in the
|
|
|
|
# .gnu_debuglink section.
|
|
|
|
#printf STDERR "%x: %s\n", $hash, $basename;
|
|
|
|
|
|
|
|
my @possible_results = (
|
|
|
|
dirname($file) . $basename,
|
|
|
|
dirname($file) . '.debug/' . $basename,
|
|
|
|
$global_debug_dir . dirname($file) . '/' . $basename
|
|
|
|
);
|
|
|
|
foreach my $result (@possible_results) {
|
|
|
|
if (-f $result) {
|
|
|
|
# XXX We should check the hash.
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
2006-03-24 05:19:16 +03:00
|
|
|
}
|
|
|
|
|
2012-11-16 02:38:21 +04:00
|
|
|
sub get_file_info($) {
|
2006-03-24 05:19:16 +03:00
|
|
|
my ($file) = @_;
|
2012-11-16 02:38:21 +04:00
|
|
|
my $file_info = $file_infos{$file};
|
|
|
|
unless (defined $file_info) {
|
2007-03-23 09:49:37 +03:00
|
|
|
my $debug_file = separate_debug_file_for($file);
|
2007-03-23 09:24:27 +03:00
|
|
|
$debug_file = $file if ($debug_file eq '');
|
2006-03-24 05:19:16 +03:00
|
|
|
|
2012-11-16 02:38:21 +04:00
|
|
|
my $pid = open2($file_info->{pipe_read}, $file_info->{pipe_write},
|
2007-03-23 09:24:27 +03:00
|
|
|
'/usr/bin/addr2line', '-C', '-f', '-e', $debug_file);
|
2012-11-16 02:38:21 +04:00
|
|
|
|
|
|
|
set_address_adjustment($file, $file_info);
|
|
|
|
|
|
|
|
$file_infos{$file} = $file_info;
|
2006-03-24 05:19:16 +03:00
|
|
|
}
|
2012-11-16 02:38:21 +04:00
|
|
|
return $file_info;
|
2006-03-24 05:19:16 +03:00
|
|
|
}
|
|
|
|
|
2011-05-02 00:33:16 +04:00
|
|
|
# Ignore SIGPIPE as a workaround for addr2line crashes in some situations.
|
|
|
|
$SIG{PIPE} = 'IGNORE';
|
|
|
|
|
2009-08-13 02:20:52 +04:00
|
|
|
select STDOUT; $| = 1; # make STDOUT unbuffered
|
2003-08-08 12:51:56 +04:00
|
|
|
while (<>) {
|
|
|
|
my $line = $_;
|
2003-08-18 09:07:31 +04:00
|
|
|
if ($line =~ /^([ \|0-9-]*)(.*) ?\[([^ ]*) \+(0x[0-9A-F]{1,8})\](.*)$/) {
|
2003-08-09 02:30:37 +04:00
|
|
|
my $before = $1; # allow preservation of balance trees
|
2003-08-08 12:51:56 +04:00
|
|
|
my $badsymbol = $2;
|
2003-08-09 02:23:18 +04:00
|
|
|
my $file = $3;
|
2006-03-24 00:17:48 +03:00
|
|
|
my $address = hex($4);
|
2003-08-09 02:30:37 +04:00
|
|
|
my $after = $5; # allow preservation of counts
|
2003-08-08 12:51:56 +04:00
|
|
|
|
2007-03-23 09:29:25 +03:00
|
|
|
if (-f $file) {
|
2012-11-16 02:38:21 +04:00
|
|
|
my $file_info = get_file_info($file);
|
2012-11-16 02:38:24 +04:00
|
|
|
my $result = $file_info->{cache}->{$address};
|
|
|
|
if (not defined $result) {
|
|
|
|
my $address2 = $address + $file_info->{address_adjustment};
|
|
|
|
my $out = $file_info->{pipe_write};
|
|
|
|
my $in = $file_info->{pipe_read};
|
|
|
|
printf {$out} "0x%X\n", $address2;
|
|
|
|
chomp(my $symbol = <$in>);
|
|
|
|
chomp(my $fileandline = <$in>);
|
|
|
|
if (!$symbol || $symbol eq '??') { $symbol = $badsymbol; }
|
|
|
|
if (!$fileandline || $fileandline eq '??:0') {
|
|
|
|
$fileandline = $file;
|
|
|
|
}
|
|
|
|
$result = "$symbol ($fileandline)";
|
|
|
|
$file_info->{cache}->{$address} = $result;
|
2011-05-02 00:33:16 +04:00
|
|
|
}
|
2012-11-16 02:38:24 +04:00
|
|
|
print "$before$result$after\n";
|
2007-03-23 09:29:25 +03:00
|
|
|
} else {
|
|
|
|
print STDERR "Warning: File \"$file\" does not exist.\n";
|
|
|
|
print $line;
|
|
|
|
}
|
2006-03-24 00:17:48 +03:00
|
|
|
|
2003-08-08 12:51:56 +04:00
|
|
|
} else {
|
|
|
|
print $line;
|
|
|
|
}
|
|
|
|
}
|