[padb] r350 committed - Add global_attach and global_detach functions for looking after gdb...

padb at googlecode.com padb at googlecode.com
Mon Dec 7 12:58:20 GMT 2009


Revision: 350
Author: apittman
Date: Mon Dec  7 04:57:39 2009
Log: Add global_attach and global_detach functions for looking after gdb
handles, if the mode can inform padb of if it needs to attach to
the target processes or not then padb can perform this operation for
it in a optimal way, keeping all the code common.  Allows persistent
attachment over multiple mode calls as well.

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

Modified:
  /trunk/src/padb

=======================================
--- /trunk/src/padb	Mon Dec  7 04:41:09 2009
+++ /trunk/src/padb	Mon Dec  7 04:57:39 2009
@@ -4065,14 +4065,15 @@
      }

      my $cmd;
+    my $req;

      if ($watch) {
          $cmd = $commands[0];
+        $req->{detach_after_callback} = 1;
      } else {
          $cmd = shift @commands;
      }

-    my $req;
      $req->{mode} = $cmd->{mode};

      if ( defined $cmd->{args} ) {
@@ -7648,6 +7649,67 @@
      }
      return;
  }
+
+# Attach to all local processes in preperation for calling the mode
+# callback.  This function, along with the corresponding one below it
+# handles persistent attachment between modes: modes specify if they
+# want gdb handles or not, if they do then this function attaches for
+# it, if they don't and gdb is attached this function will detach.
+sub global_attach {
+    my ( $mode, $procs ) = @_;
+
+    if ( not $allfns{$mode}{needs_gdb} ) {
+        global_detach($procs);
+        return;
+    }
+
+    foreach my $proc ( @{$procs} ) {
+        my $vp  = $proc->{vp};
+        my $pid = $proc->{pid};
+
+        next if defined $proc->{gdb_handle};
+
+        $proc->{gdb_tmp} = gdb_start();
+        gdb_attach_async_start( $proc->{gdb_tmp}, $pid );
+    }
+
+    foreach my $proc ( @{$procs} ) {
+
+        next if defined $proc->{gdb_handle};
+
+        my $vp  = $proc->{vp};
+        my $pid = $proc->{pid};
+        my $gdb = $proc->{gdb_tmp};
+
+        delete $proc->{gdb_tmp};
+
+        if ( gdb_attach_async_end( $gdb, $pid ) ) {
+            $proc->{gdb_handle} = $gdb;
+        } else {
+            if ( defined $gdb->{error} ) {
+                target_error( $vp, $gdb->{error} );
+            } else {
+                target_error( $vp, 'Failed to attach to process' );
+            }
+            gdb_quit($gdb);
+        }
+    }
+
+    return;
+}
+
+# Detach from all local processes, this function is called from both
+# global_attach and also when padb is exiting.
+sub global_detach {
+    my ($procs) = @_;
+
+    foreach my $proc ( @{$procs} ) {
+        if ( defined $proc->{gdb_handle} ) {
+            gdb_detach( $proc->{gdb_handle} );
+            delete $proc->{gdb_handle};
+        }
+    }
+}

  # Try and be clever here, attach to each and every process on this node
  # first, then go back and query them each in turn, should mean that some
@@ -8742,6 +8804,7 @@
      }

      if ( $cmd->{mode} eq 'exit' ) {
+        global_detach( $inner_conf{all_pids} );
          $netdata->{shutdown} = 1;
          return;
      }
@@ -8801,6 +8864,10 @@
      } else {
          $cargs->{out_format} = 'raw';
      }
+
+    # Ensure that we are attached to the target processes if required
+    # and that we are not if not required.
+    global_attach( $cmd->{mode}, $pid_list );

      if ( defined $allfns{ $cmd->{mode} }{handler_all} ) {
          eval {
@@ -8849,6 +8916,11 @@
              $netdata->{target_response} = \%gres;
          }
      }
+
+    # Detach from all processes if the outer requested us to.
+    if ( defined $cmd->{detach_after_callback} ) {
+        global_detach( $cmd->{mode}, $pid_list );
+    }

      return;
  }




More information about the padb-devel mailing list