usdt: Use ProcMountNS
authorVicent Marti <tanoku@gmail.com>
Tue, 27 Jun 2017 17:06:46 +0000 (19:06 +0200)
committerBrenden Blanco <bblanco@gmail.com>
Fri, 30 Jun 2017 16:10:56 +0000 (09:10 -0700)
src/cc/usdt.cc
src/cc/usdt.h

index d390fb8..db9235b 100644 (file)
@@ -39,16 +39,19 @@ Location::Location(uint64_t addr, const char *arg_fmt) : address_(addr) {
 }
 
 Probe::Probe(const char *bin_path, const char *provider, const char *name,
-             uint64_t semaphore, const optional<int> &pid)
+             uint64_t semaphore, const optional<int> &pid, ProcMountNS *ns)
     : bin_path_(bin_path),
       provider_(provider),
       name_(name),
       semaphore_(semaphore),
-      pid_(pid) {}
+      pid_(pid),
+      mount_ns_(ns) {}
 
 bool Probe::in_shared_object() {
-  if (!in_shared_object_)
+  if (!in_shared_object_) {
+    ProcMountNSGuard g(mount_ns_);
     in_shared_object_ = bcc_elf_is_shared_obj(bin_path_.c_str());
+  }
   return in_shared_object_.value();
 }
 
@@ -205,6 +208,7 @@ int Context::_each_module(const char *modpath, uint64_t, uint64_t, bool, void *p
   // executable region. We are going to parse the ELF on disk anyway, so we
   // don't need these duplicates.
   if (ctx->modules_.insert(modpath).second /*inserted new?*/) {
+    ProcMountNSGuard g(ctx->mount_ns_instance_.get());
     bcc_elf_foreach_usdt(modpath, _each_probe, p);
   }
   return 0;
@@ -219,7 +223,8 @@ void Context::add_probe(const char *binpath, const struct bcc_elf_usdt *probe) {
   }
 
   probes_.emplace_back(
-      new Probe(binpath, probe->provider, probe->name, probe->semaphore, pid_));
+      new Probe(binpath, probe->provider, probe->name, probe->semaphore, pid_,
+       mount_ns_instance_.get()));
   probes_.back()->add_location(probe->pc, probe->arg_fmt);
 }
 
@@ -296,7 +301,8 @@ Context::Context(const std::string &bin_path) : loaded_(false) {
   }
 }
 
-Context::Context(int pid) : pid_(pid), pid_stat_(pid), loaded_(false) {
+Context::Context(int pid) : pid_(pid), pid_stat_(pid),
+  mount_ns_instance_(new ProcMountNS(pid)), loaded_(false) {
   if (bcc_procutils_each_module(pid, _each_module, this) == 0)
     loaded_ = true;
 }
index 05446a8..ab14f2b 100644 (file)
@@ -20,6 +20,7 @@
 #include <unordered_map>
 #include <vector>
 
+#include "ns_guard.h"
 #include "syms.h"
 #include "vendor/optional.hpp"
 
@@ -149,6 +150,7 @@ class Probe {
   std::vector<Location> locations_;
 
   optional<int> pid_;
+  ProcMountNS *mount_ns_;
   optional<bool> in_shared_object_;
 
   optional<std::string> attached_to_;
@@ -163,7 +165,7 @@ class Probe {
 
 public:
   Probe(const char *bin_path, const char *provider, const char *name,
-        uint64_t semaphore, const optional<int> &pid);
+        uint64_t semaphore, const optional<int> &pid, ProcMountNS *ns);
 
   size_t num_locations() const { return locations_.size(); }
   size_t num_arguments() const { return locations_.front().arguments_.size(); }
@@ -195,6 +197,7 @@ class Context {
 
   optional<int> pid_;
   optional<ProcStat> pid_stat_;
+  std::unique_ptr<ProcMountNS> mount_ns_instance_;
   bool loaded_;
 
   static void _each_probe(const char *binpath, const struct bcc_elf_usdt *probe,