Fix copyright lines
[folly.git] / folly / experimental / symbolizer / ElfCache.cpp
index 472c0b93a9fb37654943c0c74a181d4ca38160b8..9f95ebd926609c6dd3437755b781c8c418240815 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2014-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 #include <folly/experimental/symbolizer/ElfCache.h>
 
-namespace folly { namespace symbolizer {
+#include <link.h>
+
+/*
+ * This is declared in `link.h' on Linux platforms, but apparently not on the
+ * Mac version of the file.  It's harmless to declare again, in any case.
+ *
+ * Note that declaring it with `extern "C"` results in linkage conflicts.
+ */
+extern struct r_debug _r_debug;
+
+namespace folly {
+namespace symbolizer {
+
+size_t countLoadedElfFiles() {
+  // _r_debug synchronization is... lacking to say the least. It's
+  // meant as an aid for debuggers and synchronization is done by
+  // calling dl_debug_state() which debuggers are supposed to
+  // intercept by setting a breakpoint on.
+
+  // Can't really do that here, so we apply the hope-and-pray strategy.
+  if (_r_debug.r_version != 1 || _r_debug.r_state != r_debug::RT_CONSISTENT) {
+    // computo ergo sum
+    return 1;
+  }
+
+  //     r_map       -> head of a linked list of 'link_map_t' entries,
+  //                    one per ELF 'binary' in the process address space.
+  size_t count = 0;
+  for (auto lmap = _r_debug.r_map; lmap != nullptr; lmap = lmap->l_next) {
+    ++count;
+  }
+  return count;
+}
 
 SignalSafeElfCache::SignalSafeElfCache(size_t capacity) {
   map_.reserve(capacity);
@@ -33,8 +65,8 @@ std::shared_ptr<ElfFile> SignalSafeElfCache::getFile(StringPiece p) {
     return nullptr;
   }
 
-  Path path(p);
-  auto pos = map_.find(path);
+  scratchpad_.assign(p);
+  auto pos = map_.find(scratchpad_);
   if (pos != map_.end()) {
     return slots_[pos->second];
   }
@@ -48,16 +80,16 @@ std::shared_ptr<ElfFile> SignalSafeElfCache::getFile(StringPiece p) {
   auto& f = slots_[n];
 
   const char* msg = "";
-  int r = f->openAndFollow(path.data(), true, &msg);
+  int r = f->openAndFollow(scratchpad_.data(), true, &msg);
   if (r != ElfFile::kSuccess) {
     return nullptr;
   }
 
-  map_[path] = n;
+  map_[scratchpad_] = n;
   return f;
 }
 
-ElfCache::ElfCache(size_t capacity) : capacity_(capacity) { }
+ElfCache::ElfCache(size_t capacity) : capacity_(capacity) {}
 
 std::shared_ptr<ElfFile> ElfCache::getFile(StringPiece p) {
   std::lock_guard<std::mutex> lock(mutex_);
@@ -98,5 +130,5 @@ std::shared_ptr<ElfFile> ElfCache::filePtr(const std::shared_ptr<Entry>& e) {
   // share ownership
   return std::shared_ptr<ElfFile>(e, &e->file);
 }
-
-}}  // namespaces
+} // namespace symbolizer
+} // namespace folly