From efd235a8892613b2b7afb690a19bbfd0353c3665 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Tue, 29 Dec 2009 16:45:59 -0600 Subject: [PATCH] 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 | 6 ++++++ tools/trace-malloc/lib/nsTraceMalloc.h | 5 +++++ xpcom/base/nsTraceRefcntImpl.cpp | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/tools/trace-malloc/lib/nsTraceMalloc.c b/tools/trace-malloc/lib/nsTraceMalloc.c index f2644442e85..56c46f060be 100644 --- a/tools/trace-malloc/lib/nsTraceMalloc.c +++ b/tools/trace-malloc/lib/nsTraceMalloc.c @@ -1477,6 +1477,12 @@ NS_TraceMallocStartupArgs(int argc, char **argv) return argc; } +PR_IMPLEMENT(PRBool) +NS_TraceMallocHasStarted(void) +{ + return tmlock ? PR_TRUE : PR_FALSE; +} + PR_IMPLEMENT(void) NS_TraceMallocShutdown(void) { diff --git a/tools/trace-malloc/lib/nsTraceMalloc.h b/tools/trace-malloc/lib/nsTraceMalloc.h index a2b4a7cec50..10713ed5057 100644 --- a/tools/trace-malloc/lib/nsTraceMalloc.h +++ b/tools/trace-malloc/lib/nsTraceMalloc.h @@ -164,6 +164,11 @@ PR_EXTERN(void) NS_TraceMallocStartup(int logfd); */ 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. */ diff --git a/xpcom/base/nsTraceRefcntImpl.cpp b/xpcom/base/nsTraceRefcntImpl.cpp index 37697393603..6c79f2f3b46 100644 --- a/xpcom/base/nsTraceRefcntImpl.cpp +++ b/xpcom/base/nsTraceRefcntImpl.cpp @@ -61,6 +61,10 @@ #endif #endif +#ifdef NS_TRACE_MALLOC +#include "nsTraceMalloc.h" +#endif + #ifdef HAVE_LIBDL #include #endif @@ -900,6 +904,13 @@ NS_LogInit() 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)