Bug 439354 - OS X toolbar background doesn't have a good gradient, part 1 / 3, r...
[mozilla-central.git] / xpcom / base / nsLeakDetector.cpp
blob66a6e07b170e4deea4dce15e7c7a9f1498ecdf3b
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
14 * License.
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.
23 * Contributor(s):
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"
44 #include "nsCOMPtr.h"
45 #include "nsIComponentManager.h"
46 #include "nsIServiceManager.h"
47 #include "nsIGenericFactory.h"
48 #include "nsILeakDetector.h"
49 #include "nsICollection.h"
51 #include <stdio.h>
52 #include <time.h>
54 #include "gc.h"
56 extern "C" {
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)
65 fclose(GC_stderr);
67 // generate a time stamped report name.
68 time_t timer;
69 time(&timer);
70 tm* now = localtime(&timer);
72 char reportName[256];
73 sprintf(reportName, "Leaks%02d%02d%02d",
74 now->tm_hour, now->tm_min, now->tm_sec);
75 GC_stderr = fopen(reportName, "w");
77 return NS_OK;
80 static FILE* openTraceFile()
82 // generate a time stamped report name.
83 time_t timer;
84 time(&timer);
85 tm* now = localtime(&timer);
87 char reportName[256];
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 {
94 public:
95 nsLeakDetector();
97 NS_DECL_ISUPPORTS
98 NS_DECL_NSILEAKDETECTOR
99 private:
100 ~nsLeakDetector() {}
103 NS_IMPL_ISUPPORTS1(nsLeakDetector, nsILeakDetector)
105 nsLeakDetector::nsLeakDetector() {
108 NS_METHOD nsLeakDetector::DumpLeaks()
110 GC_gcollect();
112 return nextLeakFile();
115 NS_METHOD nsLeakDetector::TraceObject(nsISupports* object, PRBool verbose)
117 FILE* trace = openTraceFile();
118 if (trace != NULL) {
119 FILE* old_stderr = GC_stderr;
120 GC_stderr = trace;
121 GC_trace_object(object, (verbose ? 1 : 0));
122 GC_stderr = old_stderr;
123 fclose(trace);
124 return NS_OK;
126 return NS_ERROR_FAILURE;
129 NS_METHOD nsLeakDetector::TraceCollection(nsICollection* objects, PRBool verbose)
131 PRUint32 count;
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();
144 if (trace != NULL) {
145 FILE* old_stderr = GC_stderr;
146 GC_stderr = trace;
147 GC_trace_object(elements, (verbose ? 1 : 0));
148 GC_stderr = old_stderr;
149 fclose(trace);
150 rv = NS_OK;
153 delete[] elements;
155 return rv;
158 NS_METHOD nsLeakDetector::MarkObject(nsISupports* object, PRBool marked)
160 GC_mark_object(object, (marked ? 1 : 0));
161 return NS_OK;
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()
179 nsresult rv;
181 // open the first leak file.
182 rv = nextLeakFile();
183 if (NS_FAILED(rv))
184 return rv;
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);
193 if (NS_FAILED(rv))
194 return rv;
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
208 public:
209 ~LeakDetectorFinalizer();
212 #ifdef SHUTDOWN_LEAKS_LATE
213 // do shutdown leaks when XPCOM library is unloaded
214 LeakDetectorFinalizer gLeakDetectorFinalizer;
215 #endif
217 LeakDetectorFinalizer::~LeakDetectorFinalizer()
219 GC_gcollect();
221 #if 0
222 nextLeakFile();
223 if (GC_stdout != NULL) {
224 fprintf(GC_stdout, "ShutDown Leaks\n");
225 GC_clear_roots();
226 GC_gcollect();
228 #endif
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;
240 #endif
241 return NS_OK;
244 #endif /* defined(GC_LEAK_DETECTOR) */