X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Futils%2Fbacktrace;h=487e224791388de739f57fdd512e75b69a579fd8;hb=eb5ba83c314e0074e2b280516e3e30b8626c638f;hp=300df642d4927f733f22795e6411c5d2aef08808;hpb=6dca07ec0e8cde38f64f0c9f543b8e52ba169e94;p=pintos-anon diff --git a/src/utils/backtrace b/src/utils/backtrace index 300df64..487e224 100755 --- a/src/utils/backtrace +++ b/src/utils/backtrace @@ -1,19 +1,56 @@ -#! /usr/bin/perl -$a2l = search_path ("i386-elf-addr2line") || search_path ("addr2line"); -$bin = shift @ARGV; -open (A2L, "$a2l -fe $bin " . join (' ', @ARGV) . "|"); -while ($function = ) { - $line = ; - chomp $function; - chomp $line; - print shift (@ARGV), ": $function ($line)\n"; +#! /usr/bin/perl -w + +use strict; + +# Check command line. +if (grep ($_ eq '-h' || $_ eq '--help', @ARGV)) { + print "backtrace, for converting raw addresses into symbolic backtraces\n"; + print "\n"; + print "usage: backtrace BINARY ADDRESS...\n"; + print "where BINARY is the binary file from which to obtain symbols\n"; + print " and each ADDRESS is a raw address to convert to a symbol name.\n"; + print "\n"; + print "In use with Pintos, BINARY is usually kernel.o and the ADDRESS\n"; + print "list is taken from the \"Call stack:\" printed by the kernel.\n"; + print "Read \"Backtraces\" in the \"Debugging Tools\" chapter\n"; + print "of the Pintos documentation for more information.\n"; + exit 0; +} +die "backtrace: binary file argument required (use --help for help)\n" + if @ARGV == 0; +die "backtrace: at least one address argument required (use --help for help)\n" + if @ARGV == 1; + +# Find binary file. +my ($bin) = shift @ARGV; +die "backtrace: $bin: not found (use --help for help)\n" + if ! -e $bin; + +# Find addr2line. +my ($a2l) = search_path ("i386-elf-addr2line") || search_path ("addr2line"); +if (!$a2l) { + die "backtrace: neither `i386-elf-addr2line' nor `addr2line' in PATH\n"; } - sub search_path { my ($target) = @_; - for $dir (split (':', $ENV{PATH})) { + for my $dir (split (':', $ENV{PATH})) { my ($file) = "$dir/$target"; return $file if -e $file; } - return 0; + return undef; } + +# Drop leading and trailing garbage inserted by kernel. +shift while grep (/call|stack/i, $ARGV[0]); +s/\.$// foreach @ARGV; + +# Do backtrace. +open (A2L, "$a2l -fe $bin " . join (' ', @ARGV) . "|"); +while () { + my ($function, $line); + chomp ($function = $_); + chomp ($line = ); + print shift (@ARGV), ": $function ($line)\n"; +} +close (A2L); +