6 if (grep ($_ eq '-h' || $_ eq '--help', @ARGV)) {
8 backtrace, for converting raw addresses into symbolic backtraces
9 usage: backtrace [BINARY]... ADDRESS...
10 where BINARY is the binary file or files from which to obtain symbols
11 and ADDRESS is a raw address to convert to a symbol name.
13 If no BINARY is unspecified, the default is the first of kernel.o or
14 build/kernel.o that exists. If multiple binaries are specified, each
15 symbol printed is from the first binary that contains a match.
17 The ADDRESS list should be taken from the "Call stack:" printed by the
18 kernel. Read "Backtraces" in the "Debugging Tools" chapter of the
19 Pintos documentation for more information.
23 die "backtrace: at least one argument required (use --help for help)\n"
26 # Drop garbage inserted by kernel.
27 @ARGV = grep (!/^(call|stack:?|[-+])$/i, @ARGV);
28 s/\.$// foreach @ARGV;
32 while ($ARGV[0] !~ /^0x/) {
33 my ($bin) = shift @ARGV;
34 die "backtrace: $bin: not found (use --help for help)\n" if ! -e $bin;
35 push (@binaries, $bin);
41 } elsif (-e 'build/kernel.o') {
42 $bin = 'build/kernel.o';
44 die "backtrace: no binary specified and neither \"kernel.o\" nor \"build/kernel.o\" exists (use --help for help)\n";
46 push (@binaries, $bin);
50 my ($a2l) = search_path ("i386-elf-addr2line") || search_path ("addr2line");
52 die "backtrace: neither `i386-elf-addr2line' nor `addr2line' in PATH\n";
56 for my $dir (split (':', $ENV{PATH})) {
57 my ($file) = "$dir/$target";
58 return $file if -e $file;
63 # Figure out backtrace.
64 my (@locs) = map ({ADDR => $_}, @ARGV);
65 for my $bin (@binaries) {
66 open (A2L, "$a2l -fe $bin " . join (' ', map ($_->{ADDR}, @locs)) . "|");
67 for (my ($i) = 0; <A2L>; $i++) {
68 my ($function, $line);
69 chomp ($function = $_);
70 chomp ($line = <A2L>);
71 next if defined $locs[$i]{BINARY};
73 if ($function ne '??' || $line ne '??:0') {
74 $locs[$i]{FUNCTION} = $function;
75 $locs[$i]{LINE} = $line;
76 $locs[$i]{BINARY} = $bin;
85 if (defined ($loc->{BINARY})
87 && (!defined ($cur_binary) || $loc->{BINARY} ne $cur_binary)) {
88 $cur_binary = $loc->{BINARY};
89 print "In $cur_binary:\n";
92 my ($addr) = $loc->{ADDR};
93 $addr = sprintf ("0x%08x", hex ($addr)) if $addr =~ /^0x[0-9a-f]+$/i;
96 if (defined ($loc->{BINARY})) {
97 my ($function) = $loc->{FUNCTION};
98 my ($line) = $loc->{LINE};
99 $line =~ s/^(\.\.\/)*//;
100 $line = "..." . substr ($line, -25) if length ($line) > 28;
101 print "$function ($line)";