Bug 529134: Initialize tracemalloc in NS_LogInit() to ensure that tracemalloc is always thread safe. r=bsmedberg r=dbaron
authorChris Jones <jones.chris.g@gmail.com>
Tue, 29 Dec 2009 16:45:59 -0600
changeset 36737 7c5e126880cfae388d5317821690539bc9cc9f47
parent 36736 a468805d1ae4f35c2153c059683c6671af9d9f5d
child 36738 83c9f9bfa127eea79a20672b931aed0bfdda1f49
push idunknown
push userunknown
push dateunknown
reviewersbsmedberg, dbaron
bugs529134
milestone1.9.3a1pre
Bug 529134: Initialize tracemalloc in NS_LogInit() to ensure that tracemalloc is always thread safe. r=bsmedberg r=dbaron
tools/trace-malloc/lib/nsTraceMalloc.c
tools/trace-malloc/lib/nsTraceMalloc.h
xpcom/base/nsTraceRefcntImpl.cpp
--- a/tools/trace-malloc/lib/nsTraceMalloc.c
+++ b/tools/trace-malloc/lib/nsTraceMalloc.c
@@ -1472,16 +1472,22 @@ NS_TraceMallocStartupArgs(int argc, char
         strncpy(sdlogname, sdlogname_local, sizeof(sdlogname));
         sdlogname[sizeof(sdlogname) - 1] = '\0';
     }
 
     NS_TraceMallocStartup(logfd);
     return argc;
 }
 
+PR_IMPLEMENT(PRBool)
+NS_TraceMallocHasStarted(void)
+{
+    return tmlock ? PR_TRUE : PR_FALSE;
+}
+
 PR_IMPLEMENT(void)
 NS_TraceMallocShutdown(void)
 {
     logfile *fp;
 
     if (sdlogname[0])
         NS_TraceMallocDumpAllocations(sdlogname);
 
--- a/tools/trace-malloc/lib/nsTraceMalloc.h
+++ b/tools/trace-malloc/lib/nsTraceMalloc.h
@@ -160,16 +160,21 @@ typedef struct nsTMStats {
 PR_EXTERN(void) NS_TraceMallocStartup(int logfd);
 
 /**
  * Initialize malloc tracing, using the ``standard'' startup arguments.
  */
 PR_EXTERN(int) NS_TraceMallocStartupArgs(int argc, char* argv[]);
 
 /**
+ * Return PR_TRUE iff |NS_TraceMallocStartup[Args]| has been successfully called.
+ */
+PR_EXTERN(PRBool) NS_TraceMallocHasStarted(void);
+
+/**
  * Stop all malloc tracing, flushing any buffered events to the logfile.
  */
 PR_EXTERN(void) NS_TraceMallocShutdown(void);
 
 /**
  * Disable malloc tracing.
  */
 PR_EXTERN(void) NS_TraceMallocDisable(void);
--- a/xpcom/base/nsTraceRefcntImpl.cpp
+++ b/xpcom/base/nsTraceRefcntImpl.cpp
@@ -56,16 +56,20 @@
 #ifdef XP_WIN
 #include <process.h>
 #define getpid _getpid
 #else
 #include <unistd.h>
 #endif
 #endif
 
+#ifdef NS_TRACE_MALLOC
+#include "nsTraceMalloc.h"
+#endif
+
 #ifdef HAVE_LIBDL
 #include <dlfcn.h>
 #endif
 
 ////////////////////////////////////////////////////////////////////////////////
 
 NS_COM void
 NS_MeanAndStdDev(double n, double sumOfValues, double sumOfSquaredValues,
@@ -895,16 +899,23 @@ nsTraceRefcntImpl::DemangleSymbol(const 
 
 EXPORT_XPCOM_API(void)
 NS_LogInit()
 {
 #ifdef NS_IMPL_REFCNT_LOGGING
   if (++gInitCount)
     nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE);
 #endif
+
+#ifdef NS_TRACE_MALLOC
+  // XXX we don't have to worry about shutting down trace-malloc; it
+  // handles this itself, through an atexit() callback.
+  if (!NS_TraceMallocHasStarted())
+    NS_TraceMallocStartup(-1);  // -1 == no logging
+#endif
 }
 
 EXPORT_XPCOM_API(void)
 NS_LogTerm()
 {
   NS_ASSERTION(gInitCount > 0,
                "NS_LogTerm without matching NS_LogInit");