Bug 891266 - Allow to enable faulty.lib debug log at runtime. r=nfroyd
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 10 Jul 2013 14:12:35 +0900
changeset 138676 00d92898ea01ce45e308785df7adcdec0e7a4b6e
parent 138675 db8e3a4bf1006212c208d9bad2c6842c1394b5ad
child 138677 6063eaf3633fec74f5289571c7dd2ef36ab07074
push id2859
push userakeybl@mozilla.com
push dateMon, 16 Sep 2013 19:14:59 +0000
treeherdermozilla-esr52@87d3c51cd2bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnfroyd
bugs891266
milestone25.0a1
Bug 891266 - Allow to enable faulty.lib debug log at runtime. r=nfroyd
mozglue/linker/ElfLoader.cpp
mozglue/linker/Logging.h
mozglue/linker/szip.cpp
--- a/mozglue/linker/ElfLoader.cpp
+++ b/mozglue/linker/ElfLoader.cpp
@@ -284,16 +284,19 @@ SystemElf::GetMappable() const
  */
 
 /* Unique ElfLoader instance */
 ElfLoader ElfLoader::Singleton;
 
 TemporaryRef<LibHandle>
 ElfLoader::Load(const char *path, int flags, LibHandle *parent)
 {
+  /* Ensure logging is initialized or refresh if environment changed. */
+  Logging::Init();
+
   RefPtr<LibHandle> handle;
 
   /* Handle dlopen(NULL) directly. */
   if (!path) {
     handle = SystemElf::Load(NULL, flags);
     return handle;
   }
 
@@ -308,19 +311,17 @@ ElfLoader::Load(const char *path, int fl
         return *it;
   } else {
     for (LibHandleList::iterator it = handles.begin(); it < handles.end(); ++it)
       if ((*it)->GetPath() && (strcmp((*it)->GetPath(), path) == 0))
         return *it;
   }
 
   char *abs_path = NULL;
-#ifdef MOZ_DEBUG_LINKER
   const char *requested_path = path;
-#endif
 
   /* When the path is not absolute and the library is being loaded for
    * another, first try to load the library from the directory containing
    * that parent library. */
   if ((name == path) && parent) {
     const char *parentPath = parent->GetPath();
     abs_path = new char[strlen(parentPath) + strlen(path)];
     strcpy(abs_path, parentPath);
@@ -405,16 +406,19 @@ ElfLoader::Register(LibHandle *handle)
   handles.push_back(handle);
   if (dbg && !handle->IsSystemElf())
     dbg.Add(static_cast<CustomElf *>(handle));
 }
 
 void
 ElfLoader::Forget(LibHandle *handle)
 {
+  /* Ensure logging is initialized or refresh if environment changed. */
+  Logging::Init();
+
   LibHandleList::iterator it = std::find(handles.begin(), handles.end(), handle);
   if (it != handles.end()) {
     DEBUG_LOG("ElfLoader::Forget(%p [\"%s\"])", reinterpret_cast<void *>(handle),
                                                 handle->GetPath());
     if (dbg && !handle->IsSystemElf())
       dbg.Remove(static_cast<CustomElf *>(handle));
     handles.erase(it);
   } else {
@@ -976,8 +980,10 @@ SEGVHandler::__wrap_sigaction(int signum
 
   SEGVHandler &that = ElfLoader::Singleton;
   if (oldact)
     *oldact = that.action;
   if (act)
     that.action = *act;
   return 0;
 }
+
+Logging Logging::Singleton;
--- a/mozglue/linker/Logging.h
+++ b/mozglue/linker/Logging.h
@@ -1,15 +1,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef Logging_h
 #define Logging_h
 
+#include "mozilla/Likely.h"
+
 #ifdef ANDROID
 #include <android/log.h>
 #define LOG(...) __android_log_print(ANDROID_LOG_ERROR, "GeckoLinker", __VA_ARGS__)
 #else
 #include <cstdio>
 
 /* Expand to 1 or m depending on whether there is one argument or more
  * given. */
@@ -31,21 +33,45 @@
                  (__VA_ARGS__))
 
 #define LOG1(format) fprintf(stderr, format "\n")
 #define LOGm(format, ...) fprintf(stderr, format "\n", __VA_ARGS__)
 #define LOG(...) MOZ_CHOOSE_LOG(__VA_ARGS__)
 
 #endif
 
-#ifdef MOZ_DEBUG_LINKER
-#define DEBUG_LOG LOG
-#else
-#define DEBUG_LOG(...)
-#endif
+class Logging
+{
+public:
+  static bool isVerbose()
+  {
+    return Singleton.verbose;
+  }
+
+private:
+  bool verbose;
+
+public:
+  static void Init()
+  {
+    const char *env = getenv("MOZ_DEBUG_LINKER");
+    if (env && *env == '1')
+      Singleton.verbose = true;
+  }
+
+private:
+  static Logging Singleton;
+};
+
+#define DEBUG_LOG(...)   \
+  do {                   \
+    if (MOZ_UNLIKELY(Logging::isVerbose())) {  \
+      LOG(__VA_ARGS__);  \
+    }                    \
+  } while(0)
 
 /* HAVE_64BIT_OS is not defined when building host tools, so use
  * __SIZEOF_POINTER__ */
 #if defined(HAVE_64BIT_OS) || __SIZEOF_POINTER__ == 8
 #  define PRIxAddr "lx"
 #  define PRIxSize "lx"
 #  define PRIdSize "ld"
 #  define PRIuSize "lu"
--- a/mozglue/linker/szip.cpp
+++ b/mozglue/linker/szip.cpp
@@ -13,16 +13,18 @@
 #include <fcntl.h>
 #include <errno.h>
 #include "mozilla/Assertions.h"
 #include "mozilla/Scoped.h"
 #include "SeekableZStream.h"
 #include "Utils.h"
 #include "Logging.h"
 
+Logging Logging::Singleton;
+
 const char *filterName[] = {
   "none",
   "thumb",
   "arm",
   "x86",
   "auto"
 };
 
@@ -465,16 +467,18 @@ int main(int argc, char* argv[])
 {
   mozilla::ScopedDeletePtr<SzipAction> action;
   char **firstArg;
   bool compress = true;
   size_t chunkSize = 0;
   SeekableZStream::FilterId filter = SzipCompress::DEFAULT_FILTER;
   size_t dictSize = (size_t) 0;
 
+  Logging::Init();
+
   for (firstArg = &argv[1]; argc > 2; argc--, firstArg++) {
     if (!firstArg[0] || firstArg[0][0] != '-')
       break;
     if (strcmp(firstArg[0], "-d") == 0) {
       compress = false;
     } else if (strcmp(firstArg[0], "-c") == 0) {
       firstArg++;
       argc--;