From: Josh Stone Date: Tue, 30 Oct 2012 22:28:41 +0000 (-0700) Subject: PR14707: Support stapdyn -x PID X-Git-Tag: release-2.1~319 X-Git-Url: https://sourceware.org/git/?a=commitdiff_plain;h=18e3d52a;p=systemtap.git PR14707: Support stapdyn -x PID * stapdyn/stapdyn.cxx (main): Parse -x and use attach_process for it. * stapdyn/mutator.cxx (mutator::attach_process): Implement it. --- diff --git a/stapdyn/mutator.cxx b/stapdyn/mutator.cxx index 5dfdabef8..15dc908f6 100644 --- a/stapdyn/mutator.cxx +++ b/stapdyn/mutator.cxx @@ -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 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() diff --git a/stapdyn/stapdyn.cxx b/stapdyn/stapdyn.cxx index b7488fceb..7edd7678a 100644 --- a/stapdyn/stapdyn.cxx +++ b/stapdyn/stapdyn.cxx @@ -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; }