]> sourceware.org Git - systemtap.git/commitdiff
PR14707: Support stapdyn -x PID
authorJosh Stone <jistone@redhat.com>
Tue, 30 Oct 2012 22:28:41 +0000 (15:28 -0700)
committerJosh Stone <jistone@redhat.com>
Tue, 30 Oct 2012 22:28:41 +0000 (15:28 -0700)
* stapdyn/stapdyn.cxx (main): Parse -x and use attach_process for it.
* stapdyn/mutator.cxx (mutator::attach_process): Implement it.

stapdyn/mutator.cxx
stapdyn/stapdyn.cxx

index 5dfdabef8740376bc0838b82d45edb1fc06702ea..15dc908f66502db28aa773aca603eaa0f6bf9446 100644 (file)
@@ -164,6 +164,37 @@ mutator::create_process(const string& command)
   return true;
 }
 
+// Attach to a specific existing process.
+bool
+mutator::attach_process(pid_t pid)
+{
+  if (target_mutatee)
+    {
+      staperror() << "Already attached to a target process!" << endl;
+      return false;
+    }
+
+  BPatch_process* app = patch.processAttach(NULL, pid);
+  if (!app)
+    {
+      staperror() << "Couldn't attach to the target process" << endl;
+      return false;
+    }
+
+  boost::shared_ptr<mutatee> m(new mutatee(app));
+  mutatees.push_back(m);
+  target_mutatee = m;
+  p_target_created = false;
+
+  if (!m->load_stap_dso(module_name))
+    return false;
+
+  if (!targets.empty())
+    m->instrument_dynprobes(targets);
+
+  return true;
+}
+
 // Initialize the module session
 bool
 mutator::run_module_init()
index b7488fceb45701717736d08c4036395ed3a62e93..7edd7678a22ead6972dd75b7158d67fe4b7182a9 100644 (file)
@@ -43,7 +43,7 @@ static void __attribute__ ((noreturn))
 usage (int rc)
 {
   clog << "Usage: " << program_invocation_short_name
-       << " MODULE -c CMD" << endl;
+       << " MODULE [-c CMD | -x PID]" << endl;
   exit (rc);
 }
 
@@ -52,12 +52,13 @@ usage (int rc)
 int
 main(int argc, char * const argv[])
 {
+  pid_t pid = 0;
   const char* command = NULL;
   const char* module = NULL;
 
   // First, option parsing.
   int opt;
-  while ((opt = getopt (argc, argv, "c:vwV")) != -1)
+  while ((opt = getopt (argc, argv, "c:x:vwV")) != -1)
     {
       switch (opt)
         {
@@ -65,6 +66,10 @@ main(int argc, char * const argv[])
           command = optarg;
           break;
 
+        case 'x':
+          pid = atoi(optarg);
+          break;
+
         case 'v':
           ++stapdyn_log_level;
           break;
@@ -89,7 +94,7 @@ main(int argc, char * const argv[])
   if (optind == argc - 1)
     module = argv[optind];
 
-  if (!module)
+  if (!module || (command && pid))
     usage (1);
 
   // Make sure that environment variables and selinux are set ok.
@@ -108,6 +113,9 @@ main(int argc, char * const argv[])
   if (command && !session->create_process(command))
     return 1;
 
+  if (pid && !session->attach_process(pid))
+    return 1;
+
   return session->run() ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
This page took 0.032737 seconds and 5 git commands to generate.