1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is the Mozilla browser.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications, Inc.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
24 * Patrick C. Beard <beard@netscape.com>
25 * Scott Collins <scc@mozilla.org>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #if defined(GC_LEAK_DETECTOR)
43 #include "nsLeakDetector.h"
45 #include "nsIComponentManager.h"
46 #include "nsIServiceManager.h"
47 #include "nsIGenericFactory.h"
48 #include "nsILeakDetector.h"
49 #include "nsICollection.h"
57 extern FILE *GC_stdout
, *GC_stderr
;
58 extern void GC_trace_object(GC_PTR object
, int verbose
);
59 extern void GC_mark_object(GC_PTR object
, GC_word mark
);
62 static nsresult
nextLeakFile()
64 if (GC_stderr
!= NULL
)
67 // generate a time stamped report name.
70 tm
* now
= localtime(&timer
);
73 sprintf(reportName
, "Leaks%02d%02d%02d",
74 now
->tm_hour
, now
->tm_min
, now
->tm_sec
);
75 GC_stderr
= fopen(reportName
, "w");
80 static FILE* openTraceFile()
82 // generate a time stamped report name.
85 tm
* now
= localtime(&timer
);
88 sprintf(reportName
, "Trace%02d%02d%02d",
89 now
->tm_hour
, now
->tm_min
, now
->tm_sec
);
90 return fopen(reportName
, "w");
93 class nsLeakDetector
: public nsILeakDetector
{
98 NS_DECL_NSILEAKDETECTOR
103 NS_IMPL_ISUPPORTS1(nsLeakDetector
, nsILeakDetector
)
105 nsLeakDetector::nsLeakDetector() {
108 NS_METHOD
nsLeakDetector::DumpLeaks()
112 return nextLeakFile();
115 NS_METHOD
nsLeakDetector::TraceObject(nsISupports
* object
, PRBool verbose
)
117 FILE* trace
= openTraceFile();
119 FILE* old_stderr
= GC_stderr
;
121 GC_trace_object(object
, (verbose
? 1 : 0));
122 GC_stderr
= old_stderr
;
126 return NS_ERROR_FAILURE
;
129 NS_METHOD
nsLeakDetector::TraceCollection(nsICollection
* objects
, PRBool verbose
)
132 if (NS_FAILED(objects
->Count(&count
)))
133 return NS_ERROR_FAILURE
;
135 nsCOMPtr
<nsISupports
>* elements
= new nsCOMPtr
<nsISupports
>[count
];
136 if (elements
== nsnull
)
137 return NS_ERROR_OUT_OF_MEMORY
;
139 for (PRUint32 i
= 0; i
< count
; ++i
)
140 objects
->GetElementAt(i
, getter_AddRefs(elements
[i
]));
142 nsresult rv
= NS_ERROR_FAILURE
;
143 FILE* trace
= openTraceFile();
145 FILE* old_stderr
= GC_stderr
;
147 GC_trace_object(elements
, (verbose
? 1 : 0));
148 GC_stderr
= old_stderr
;
158 NS_METHOD
nsLeakDetector::MarkObject(nsISupports
* object
, PRBool marked
)
160 GC_mark_object(object
, (marked
? 1 : 0));
164 NS_METHOD
nsLeakDetector::GetServices(nsISupports
* *result
)
166 return NS_GetServiceManager((nsIServiceManager
**)result
);
169 #define NS_CLEAKDETECTOR_CID_STR "bb1ba360-1dd1-11b2-b30e-aa2314429f54"
170 #define NS_CLEAKDETECTOR_CID {0xbb1ba360, 0x1dd1, 0x11b2, {0xb3, 0x0e, 0xaa, 0x23, 0x14, 0x42, 0x9f, 0x54}}
171 #define NS_CLEAKDETECTOR_CONTRACTID "@mozilla.org/xpcom/leakdetector;1"
173 NS_GENERIC_FACTORY_CONSTRUCTOR(nsLeakDetector
)
175 static NS_DEFINE_CID(kCLeakDetectorCID
, NS_CLEAKDETECTOR_CID
);
177 nsresult
NS_InitLeakDetector()
181 // open the first leak file.
186 static const nsModuleComponentInfo info
= {
187 "Leak Detector", kCLeakDetectorCID
, NS_CLEAKDETECTOR_CONTRACTID
, nsLeakDetectorConstructor
190 // create a generic factory for the leak detector.
191 nsCOMPtr
<nsIGenericFactory
> factory
;
192 rv
= NS_NewGenericFactory(getter_AddRefs(factory
), &info
);
196 nsCOMPtr
<nsIComponentRegistrar
> registrar
;
197 NS_GetComponentRegistrar(getter_AddRefs(registrar
));
199 return registrar
->RegisterFactory(info
.mCID
, info
.mDescription
, info
.mContractID
, factory
);
202 #undef SHUTDOWN_LEAKS_EARLY
203 #undef SHUTDOWN_LEAKS_MEDIUM
204 #define SHUTDOWN_LEAKS_LATE
206 class LeakDetectorFinalizer
209 ~LeakDetectorFinalizer();
212 #ifdef SHUTDOWN_LEAKS_LATE
213 // do shutdown leaks when XPCOM library is unloaded
214 LeakDetectorFinalizer gLeakDetectorFinalizer
;
217 LeakDetectorFinalizer::~LeakDetectorFinalizer()
223 if (GC_stdout
!= NULL
) {
224 fprintf(GC_stdout
, "ShutDown Leaks\n");
231 nsresult
NS_ShutdownLeakDetector()
233 #if defined(SHUTDOWN_LEAKS_MEDIUM)
234 // Make this the first atexit() called so it's before the atexit() crashes
235 // see http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=23552
236 static LeakDetectorFinalizer trick
;
237 #elif defined(SHUTDOWN_LEAKS_EARLY)
238 // do shutdown leaks now
239 LeakDetectorFinalizer trick
;
244 #endif /* defined(GC_LEAK_DETECTOR) */