[padb-devel] [padb] r295 committed - Changes to the gdb interface, use sequence numbers to identify...

padb at googlecode.com padb at googlecode.com
Tue Oct 20 16:50:54 BST 2009


Revision: 295
Author: apittman
Date: Tue Oct 20 08:50:32 2009
Log: Changes to the gdb interface, use sequence numbers to identify
commands and ensure that what we send is what we get back.
Add a debug mode to log all interaction to a file, this needs to be
enabled by switching a flag in the code but is useful for development.
Finally abort if we see an EOF from gdb unless we have already
detached, it's still quite possible to crash gdb and this detects
it and stops padb from reporting rubbish.

http://code.google.com/p/padb/source/detail?r=295

Modified:
  /trunk/src/padb

=======================================
--- /trunk/src/padb	Mon Oct 19 09:01:06 2009
+++ /trunk/src/padb	Tue Oct 20 08:50:32 2009
@@ -5000,6 +5000,8 @@
          tracepid => -1,
          attached => 0,
          pa       => 0,
+        debug    => 0,
+        seq      => 1,
      };

      my $cmd = 'gdb --interpreter=mi -q';
@@ -5010,6 +5012,11 @@
      $gdb->{gdbpid} = open3( $gdb->{wtr}, $gdb->{rdr}, $gdb->{err}, $cmd )
        or croak "Unable to popen() gdb: $!";

+    if ( $gdb->{debug} ) {
+        my ( $fh, $file ) = tempfile("/tmp/padb-gdb-debug-log-XXXXXX");
+        $gdb->{debugfd} = $fh;
+    }
+
      return $gdb;
  }

@@ -5021,6 +5028,10 @@
          next unless exists $gdb->{$fdname};
          close $gdb->{$fdname};
      }
+    if ( defined $gdb->{debugfd} ) {
+        close $gdb->{debugfd};
+    }
+
      return;
  }

@@ -5065,7 +5076,7 @@
          $gdb->{runtime}{mpich2} = 1;
      }

-    gdb_n_send( $gdb, 'set print address off' );
+    gdb_n_send( $gdb, '-gdb-set print address off' );

      return $pid;
  }
@@ -5086,6 +5097,10 @@
      my ($gdb) = shift;
      my $handle = $gdb->{rdr};
      while (<$handle>) {
+
+        if ( defined $gdb->{debugfd} ) {
+            print { $gdb->{debugfd} } $_;
+        }
          return if /^\(gdb\)/;
      }

@@ -5096,10 +5111,19 @@
      my ( $gdb, $cmd ) = @_;
      gdb_wait_for_prompt($gdb);
      my $handle = $gdb->{wtr};
-    print {$handle} "$cmd\n";
-    my %r = gdb_n_next_result($gdb);
+    my $seq    = $gdb->{seq}++;
+    print {$handle} "$seq$cmd\n";
+    if ( defined $gdb->{debugfd} ) {
+        print { $gdb->{debugfd} } "$seq$cmd\n";
+    }
+    my %r = gdb_n_next_result( $gdb, $seq );
+    if ( $gdb->{attached} and $r{seq} ne $seq ) {
+        croak(
+"Invalid sequence number from gdb, expecting $seq got $r{seq} cmd=\"$cmd\""
+        );
+    }
      $r{cmd} = $cmd;
-    if ( 0 and defined $r{status} and $r{status} ne 'done' ) {
+    if ( $gdb->{debugfd} and defined $r{status} and $r{status} ne 'done' )  
{
          print Dumper \%r;
      }
      return %r;
@@ -5115,9 +5139,9 @@
      $gdb->{pa} = $flag;

      if ($flag) {
-        _gdb_send_real( $gdb, 'set print address on' );
+        _gdb_send_real( $gdb, '-gdb-set print address on' );
      } else {
-        _gdb_send_real( $gdb, 'set print address off' );
+        _gdb_send_real( $gdb, '-gdb-set print address off' );
      }

  }
@@ -5398,6 +5422,10 @@

      while (<$handle>) {

+        if ( defined $gdb->{debugfd} ) {
+            print { $gdb->{debugfd} } $_;
+        }
+
          #printf("Line $_\n");
          return %res if /^\(gdb\)/;

@@ -5407,10 +5435,14 @@
          #if (/\&\"(.*)\"\n/) {    #"
          #    $res{debug} .= $1;
          #}
-        if (m{\A\^(done|error),?(.*)\Z}x) {
-            $res{status} = $1;
-            if ( defined $2 and $2 ne $EMPTY_STRING ) {
-                $res{reason} = $2;
+        if (m{\A(\d+)\^(done|error),?(.*)\Z}x) {
+            my $seq    = $1;
+            my $status = $2;
+            my $reason = $3;
+            $res{status} = $status;
+            $res{seq}    = $seq;
+            if ( defined $reason and $reason ne $EMPTY_STRING ) {
+                $res{reason} = $reason;
              }

              #if ( defined $res{raw} ) {
@@ -5433,6 +5465,10 @@
      #    $res{debug} =~ s/\\n/\n/g;
      #    chomp $res{debug};
      #}
+
+    if ( $gdb->{attached} ) {
+        croak("Unexpected EOF from gdb");
+    }

      return %res;
  }




More information about the padb-devel mailing list