2011-01-26 02:59:19 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2011-02-01 01:41:51 +01:00
|
|
|
use IPC::Open2;
|
2011-01-26 02:59:19 +01:00
|
|
|
|
2011-12-31 00:20:29 +01:00
|
|
|
my $stdin_input = join ' ', @ARGV;
|
|
|
|
|
2012-01-30 00:50:33 +01:00
|
|
|
my $debug = 0;
|
|
|
|
|
|
|
|
my $watching = 0;
|
|
|
|
my $got_output = 0;
|
|
|
|
my $local_vars = "";
|
|
|
|
|
2011-01-26 02:59:19 +01:00
|
|
|
sub execute {
|
2012-01-28 08:39:13 +01:00
|
|
|
my ($cmdline) = @_;
|
|
|
|
my ($ret, $result);
|
|
|
|
|
|
|
|
my ($out, $in);
|
|
|
|
open2($out, $in, "$cmdline 2>&1");
|
2012-01-30 00:50:33 +01:00
|
|
|
|
2012-01-28 08:39:13 +01:00
|
|
|
while(my $line = <$out>) {
|
|
|
|
chomp $line;
|
2012-01-30 00:50:33 +01:00
|
|
|
print "-- got: [$line]\n" if $debug >= 1;
|
|
|
|
|
|
|
|
my $ignore_response = 0;
|
|
|
|
|
|
|
|
next if not length $line;
|
|
|
|
next if $line =~ m/^\(gdb\) Continuing/;
|
|
|
|
next if $line =~ m/^\(gdb\) \$\d+ = "Ok\."/;
|
|
|
|
next if $line =~ m/^(\(gdb\) )?Breakpoint \d+ at 0x/;
|
|
|
|
next if $line =~ m/^(\(gdb\) )?Starting program/;
|
|
|
|
next if $line =~ m/^\d+: .*? =/;
|
|
|
|
|
2012-01-28 08:39:13 +01:00
|
|
|
if($line =~ m/^Reading symbols from.*done\.$/) {
|
|
|
|
print $in "break gdb\n";
|
2012-01-30 00:50:33 +01:00
|
|
|
#<$out>;
|
|
|
|
|
|
|
|
print $in "list main\n";
|
|
|
|
print $in "print \"Ok.\"\n";
|
|
|
|
while(my $line = <$out>) {
|
|
|
|
chomp $line;
|
|
|
|
print "list got: [$line]\n" if $debug >= 4;
|
|
|
|
if($line =~ m/^(\d+)\s+return 0;/) {
|
|
|
|
print $in "break $1\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
last if $line =~ m/^\(gdb\) \$\d+ = "Ok."/;
|
|
|
|
}
|
|
|
|
|
2012-01-28 08:39:13 +01:00
|
|
|
print $in "run\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2012-01-30 00:50:33 +01:00
|
|
|
if($line =~ m/^Breakpoint \d+, main/) {
|
|
|
|
my $line = <$out>;
|
|
|
|
print "== got: $line\n" if $debug >= 5;
|
|
|
|
if($line =~ m/^\d+\s+return 0;$/) {
|
|
|
|
if($got_output == 0) {
|
|
|
|
print "no output, checking locals\n" if $debug >= 5;
|
|
|
|
print $in "info locals\nprint \"Ok.\"\n";
|
|
|
|
my $result = "";
|
|
|
|
my $vars = "";
|
|
|
|
my $varsep = "";
|
|
|
|
|
|
|
|
while(my $line = <$out>) {
|
|
|
|
chomp $line;
|
|
|
|
print "got: [$line]\n" if $debug >= 5;
|
|
|
|
last if $line =~ m/\(gdb\) \$\d+ = "Ok."/;
|
|
|
|
if($line =~ m/([^=]+)=\s+(.*)/) {
|
|
|
|
$vars .= "$varsep$1= $2";
|
|
|
|
$varsep = "; ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result =~ s/^\s+//;
|
|
|
|
$result =~ s/\s+$//;
|
|
|
|
|
|
|
|
$vars =~ s/\(gdb\)\s*//g;
|
|
|
|
$local_vars = "<local variables: $vars>" if length $vars;
|
|
|
|
|
|
|
|
print $in "cont\n";
|
|
|
|
next;
|
|
|
|
} else {
|
|
|
|
print $in "cont\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print $in "cont\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-28 08:39:13 +01:00
|
|
|
if($line =~ m/Breakpoint \d+, gdb/) {
|
|
|
|
print $in "up\n";
|
2012-01-30 00:50:33 +01:00
|
|
|
$line = <$out>;
|
|
|
|
print "ignored $line\n" if $debug >= 2;
|
|
|
|
$line = <$out>;
|
|
|
|
print "ignored $line\n" if $debug >= 2;
|
2012-01-28 08:39:13 +01:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($line =~ m/^\d+\s+watch\((.*)\)/) {
|
|
|
|
$line = "1 gdb(\"watch $1\");";
|
|
|
|
}
|
|
|
|
|
|
|
|
if($line =~ m/^\d+\s+dump\((.*)\)/) {
|
|
|
|
$line = "1 gdb(\"print $1\");";
|
|
|
|
}
|
|
|
|
|
2012-01-30 00:50:33 +01:00
|
|
|
if($line =~ m/^\d+\s+print\((.*)\)/) {
|
|
|
|
$line = "1 gdb(\"print $1\");";
|
|
|
|
}
|
|
|
|
|
2012-01-28 08:39:13 +01:00
|
|
|
if($line =~ m/^\d+\s+ptype\((.*)\)/) {
|
|
|
|
$line = "1 gdb(\"ptype $1\");";
|
|
|
|
}
|
|
|
|
|
2012-01-30 00:50:33 +01:00
|
|
|
if($line =~ m/^\d+\s+.*gdb\("(.*)"\)/) {
|
2012-01-28 08:39:13 +01:00
|
|
|
my $command = $1;
|
|
|
|
my ($cmd, $args) = split / /, $command, 2;
|
|
|
|
$args = "" if not defined $args;
|
2012-01-30 00:50:33 +01:00
|
|
|
|
|
|
|
#print "got command [$command]\n";
|
|
|
|
|
|
|
|
if($cmd eq "watch") {
|
|
|
|
print $in "display $args\n";
|
|
|
|
#<$out>;
|
|
|
|
$watching++;
|
|
|
|
$ignore_response = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
print $in "$command\nprint \"Ok.\"\n";
|
2012-01-28 08:39:13 +01:00
|
|
|
my $next_line = <$out>;
|
|
|
|
chomp $next_line;
|
2012-01-30 00:50:33 +01:00
|
|
|
#print "nextline: $next_line\n";
|
|
|
|
|
|
|
|
$next_line =~ s/^\(gdb\)\s*\(gdb\)\s+\$\d+ = "Ok."//;
|
2012-01-28 08:39:13 +01:00
|
|
|
$next_line =~ s/^\(gdb\)\s+\$\d+//;
|
|
|
|
$next_line =~ s/^\(gdb\)\s+type//;
|
2012-01-30 00:50:33 +01:00
|
|
|
|
|
|
|
if(not $ignore_response) {
|
|
|
|
if($next_line =~ m/=/) {
|
|
|
|
$got_output = 1;
|
|
|
|
print "<$args$next_line>\n";
|
|
|
|
} else {
|
|
|
|
print "<$next_line>\n" if length $next_line;
|
|
|
|
$got_output = 1 if length $next_line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print $in "cont\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($line =~ m/^Watchpoint \d+: (.*)/) {
|
|
|
|
my $var = $1;
|
|
|
|
|
|
|
|
my $ignore = <$out>;
|
|
|
|
print "ignored $ignore\n" if $debug >= 5;
|
|
|
|
my $old = <$out>;
|
|
|
|
my $new = <$out>;
|
|
|
|
$ignore = <$out>;
|
|
|
|
print "ignored $ignore\n" if $debug >= 5;
|
|
|
|
$ignore = <$out>;
|
|
|
|
print "ignored $ignore\n" if $debug >= 5;
|
|
|
|
|
|
|
|
my ($val1) = $old =~ m/Old value = (.*)/;
|
|
|
|
my ($val2) = $new =~ m/New value = (.*)/;
|
|
|
|
|
|
|
|
$got_output = 1;
|
|
|
|
print "<$var = $val2>\n";
|
2012-01-28 08:39:13 +01:00
|
|
|
print $in "cont\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($line =~ m/^Hardware watchpoint \d+: (.*)/) {
|
|
|
|
my $var = $1;
|
|
|
|
|
|
|
|
my $ignore = <$out>;
|
|
|
|
my $old = <$out>;
|
|
|
|
my $new = <$out>;
|
|
|
|
$ignore = <$out>;
|
|
|
|
$ignore = <$out>;
|
|
|
|
|
|
|
|
my ($val1) = $old =~ m/Old value = (.*)/;
|
|
|
|
my ($val2) = $new =~ m/New value = (.*)/;
|
|
|
|
|
2012-01-30 00:50:33 +01:00
|
|
|
$got_output = 1;
|
2012-01-28 08:39:13 +01:00
|
|
|
print "<$var changed: $val1 => $val2>\n";
|
|
|
|
print $in "cont\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($line =~ m/^Watchpoint \d+ deleted/) {
|
|
|
|
my $ignore = <$out>;
|
2012-01-30 00:50:33 +01:00
|
|
|
print "ignored $ignore\n" if $debug >= 5;
|
2012-01-28 08:39:13 +01:00
|
|
|
$ignore = <$out>;
|
2012-01-30 00:50:33 +01:00
|
|
|
print "ignored $ignore\n" if $debug >= 5;
|
2012-01-28 08:39:13 +01:00
|
|
|
print $in "cont\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($line =~ m/^Program exited/) {
|
2012-01-30 00:50:33 +01:00
|
|
|
print " $local_vars\n" if length $local_vars and not $got_output;
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($line =~ s/\[Inferior .* exited with code (\d+)\]//) {
|
|
|
|
print "$line\n";
|
|
|
|
print "<Exit $1>\n";
|
|
|
|
print " $local_vars\n" if length $local_vars and not $got_output;
|
2012-01-28 08:39:13 +01:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2012-01-30 00:50:33 +01:00
|
|
|
if($line =~ s/\[Inferior .* exited normally\]//) {
|
|
|
|
print "$line\n" if length $line;
|
|
|
|
$got_output = 1 if length $line;
|
|
|
|
print " $local_vars\n" if length $local_vars and not $got_output;
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($line =~ m/Program received signal SIGTRAP/) {
|
|
|
|
my $line = <$out>;
|
|
|
|
print "ignored $line\n" if $debug >= 5;
|
|
|
|
$line = <$out>;
|
|
|
|
print "ignored $line\n" if $debug >= 5;
|
|
|
|
for(my $i = 0; $i < $watching; $i++) {
|
|
|
|
$line = <$out>;
|
|
|
|
chomp $line;
|
|
|
|
$line =~ s/^\d+:\s//;
|
|
|
|
$got_output = 1;
|
|
|
|
print "<$line>\n";
|
|
|
|
}
|
|
|
|
print $in "cont\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2012-01-28 08:39:13 +01:00
|
|
|
if($line =~ m/Program received signal/) {
|
|
|
|
my $result = "";
|
|
|
|
my $vars = "";
|
|
|
|
my $varsep = "";
|
|
|
|
|
|
|
|
$line =~ s/\.$//;
|
2012-01-30 00:50:33 +01:00
|
|
|
$got_output = 1;
|
2012-01-28 08:39:13 +01:00
|
|
|
print "$line ";
|
|
|
|
|
|
|
|
print $in "up\nup\nup\nup\nup\nup\nup\ninfo locals\nquit\ny\n";
|
|
|
|
|
|
|
|
while(my $line = <$out>) {
|
|
|
|
chomp $line;
|
|
|
|
#print "got: [$line]\n";
|
|
|
|
if($line =~ s/^0x[0-9A-Fa-f]+\s//) {
|
|
|
|
next if $line =~ /in main\s*\(/;
|
|
|
|
|
|
|
|
$line =~ s/\s+at .*:\d+//;
|
|
|
|
|
|
|
|
if($line !~ m/^\s*in\s+/) {
|
|
|
|
$result = "in $line from ";
|
|
|
|
} else {
|
|
|
|
$result .= "$line at ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif($line =~ s/^\d+\s+//) {
|
|
|
|
next if $line =~ /No such file/;
|
|
|
|
|
|
|
|
$result .= "at " if not length $result;
|
|
|
|
$result .= "statement: $line";
|
|
|
|
}
|
|
|
|
elsif($line =~ m/([^=]+)=\s+(.*)/) {
|
|
|
|
$vars .= "$varsep$1= $2";
|
|
|
|
$varsep = "; ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result =~ s/^\s+//;
|
|
|
|
$result =~ s/\s+$//;
|
|
|
|
|
|
|
|
$vars =~ s/\(gdb\)\s*//g;
|
2012-01-30 00:50:33 +01:00
|
|
|
$vars = " <local variables: $vars>" if length $vars;
|
2012-01-28 08:39:13 +01:00
|
|
|
|
|
|
|
print "$result$vars\n";
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2012-01-30 00:50:33 +01:00
|
|
|
if($line =~ s/^\(gdb\)\s*//) {
|
|
|
|
$got_output = 1;
|
|
|
|
print "<$line>\n";
|
2012-01-28 08:39:13 +01:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
next if $line =~ m/^\d+\s+void gdb\(\) {}/;
|
|
|
|
|
|
|
|
next if not length $line;
|
|
|
|
|
2012-01-30 00:50:33 +01:00
|
|
|
$got_output = 1;
|
2012-01-28 08:39:13 +01:00
|
|
|
print "$line\n";
|
2011-12-31 00:20:29 +01:00
|
|
|
}
|
2011-01-26 02:59:19 +01:00
|
|
|
}
|
|
|
|
|
2012-01-28 08:39:13 +01:00
|
|
|
execute("gdb -silent ./prog");
|