1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
---|
2 | *
|
---|
3 | * ***** BEGIN LICENSE BLOCK *****
|
---|
4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
5 | *
|
---|
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/
|
---|
10 | *
|
---|
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.
|
---|
15 | *
|
---|
16 | * The Original Code is the Mozilla browser.
|
---|
17 | *
|
---|
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.
|
---|
22 | *
|
---|
23 | * Contributor(s):
|
---|
24 | * Patrick C. Beard <[email protected]>
|
---|
25 | * Scott Collins <[email protected]>
|
---|
26 | *
|
---|
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.
|
---|
38 | *
|
---|
39 | * ***** END LICENSE BLOCK ***** */
|
---|
40 |
|
---|
41 | #if defined(GC_LEAK_DETECTOR)
|
---|
42 |
|
---|
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"
|
---|
50 |
|
---|
51 | #include <stdio.h>
|
---|
52 | #include <time.h>
|
---|
53 |
|
---|
54 | #include "gc.h"
|
---|
55 |
|
---|
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);
|
---|
60 | }
|
---|
61 |
|
---|
62 | static nsresult nextLeakFile()
|
---|
63 | {
|
---|
64 | if (GC_stderr != NULL)
|
---|
65 | fclose(GC_stderr);
|
---|
66 |
|
---|
67 | // generate a time stamped report name.
|
---|
68 | time_t timer;
|
---|
69 | time(&timer);
|
---|
70 | tm* now = localtime(&timer);
|
---|
71 |
|
---|
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");
|
---|
76 |
|
---|
77 | return NS_OK;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static FILE* openTraceFile()
|
---|
81 | {
|
---|
82 | // generate a time stamped report name.
|
---|
83 | time_t timer;
|
---|
84 | time(&timer);
|
---|
85 | tm* now = localtime(&timer);
|
---|
86 |
|
---|
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");
|
---|
91 | }
|
---|
92 |
|
---|
93 | class nsLeakDetector : public nsILeakDetector {
|
---|
94 | public:
|
---|
95 | nsLeakDetector();
|
---|
96 |
|
---|
97 | NS_DECL_ISUPPORTS
|
---|
98 | NS_DECL_NSILEAKDETECTOR
|
---|
99 | private:
|
---|
100 | ~nsLeakDetector() {}
|
---|
101 | };
|
---|
102 |
|
---|
103 | NS_IMPL_ISUPPORTS1(nsLeakDetector, nsILeakDetector)
|
---|
104 |
|
---|
105 | nsLeakDetector::nsLeakDetector() {
|
---|
106 | }
|
---|
107 |
|
---|
108 | NS_METHOD nsLeakDetector::DumpLeaks()
|
---|
109 | {
|
---|
110 | GC_gcollect();
|
---|
111 |
|
---|
112 | return nextLeakFile();
|
---|
113 | }
|
---|
114 |
|
---|
115 | NS_METHOD nsLeakDetector::TraceObject(nsISupports* object, PRBool verbose)
|
---|
116 | {
|
---|
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;
|
---|
125 | }
|
---|
126 | return NS_ERROR_FAILURE;
|
---|
127 | }
|
---|
128 |
|
---|
129 | NS_METHOD nsLeakDetector::TraceCollection(nsICollection* objects, PRBool verbose)
|
---|
130 | {
|
---|
131 | PRUint32 count;
|
---|
132 | if (NS_FAILED(objects->Count(&count)))
|
---|
133 | return NS_ERROR_FAILURE;
|
---|
134 |
|
---|
135 | nsCOMPtr<nsISupports>* elements = new nsCOMPtr<nsISupports>[count];
|
---|
136 | if (elements == nsnull)
|
---|
137 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
138 |
|
---|
139 | for (PRUint32 i = 0; i < count; ++i)
|
---|
140 | objects->GetElementAt(i, getter_AddRefs(elements[i]));
|
---|
141 |
|
---|
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;
|
---|
151 | }
|
---|
152 |
|
---|
153 | delete[] elements;
|
---|
154 |
|
---|
155 | return rv;
|
---|
156 | }
|
---|
157 |
|
---|
158 | NS_METHOD nsLeakDetector::MarkObject(nsISupports* object, PRBool marked)
|
---|
159 | {
|
---|
160 | GC_mark_object(object, (marked ? 1 : 0));
|
---|
161 | return NS_OK;
|
---|
162 | }
|
---|
163 |
|
---|
164 | NS_METHOD nsLeakDetector::GetServices(nsISupports* *result)
|
---|
165 | {
|
---|
166 | return NS_GetServiceManager((nsIServiceManager**)result);
|
---|
167 | }
|
---|
168 |
|
---|
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"
|
---|
172 |
|
---|
173 | NS_GENERIC_FACTORY_CONSTRUCTOR(nsLeakDetector)
|
---|
174 |
|
---|
175 | static NS_DEFINE_CID(kCLeakDetectorCID, NS_CLEAKDETECTOR_CID);
|
---|
176 |
|
---|
177 | nsresult NS_InitLeakDetector()
|
---|
178 | {
|
---|
179 | nsresult rv;
|
---|
180 |
|
---|
181 | // open the first leak file.
|
---|
182 | rv = nextLeakFile();
|
---|
183 | if (NS_FAILED(rv))
|
---|
184 | return rv;
|
---|
185 |
|
---|
186 | static const nsModuleComponentInfo info = {
|
---|
187 | "Leak Detector", kCLeakDetectorCID, NS_CLEAKDETECTOR_CONTRACTID, nsLeakDetectorConstructor
|
---|
188 | };
|
---|
189 |
|
---|
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;
|
---|
195 |
|
---|
196 | // register this factory with the component manager.
|
---|
197 | return nsComponentManager::RegisterFactory(info.mCID, info.mDescription, info.mContractID, factory, PR_TRUE);
|
---|
198 | }
|
---|
199 |
|
---|
200 | #ifdef XP_MAC
|
---|
201 | #define SHUTDOWN_LEAKS_EARLY
|
---|
202 | #undef SHUTDOWN_LEAKS_MEDIUM
|
---|
203 | #undef SHUTDOWN_LEAKS_LATE
|
---|
204 | #else
|
---|
205 | #undef SHUTDOWN_LEAKS_EARLY
|
---|
206 | #undef SHUTDOWN_LEAKS_MEDIUM
|
---|
207 | #define SHUTDOWN_LEAKS_LATE
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | class LeakDetectorFinalizer
|
---|
211 | {
|
---|
212 | public:
|
---|
213 | ~LeakDetectorFinalizer();
|
---|
214 | };
|
---|
215 |
|
---|
216 | #ifdef SHUTDOWN_LEAKS_LATE
|
---|
217 | // do shutdown leaks when XPCOM library is unloaded
|
---|
218 | LeakDetectorFinalizer gLeakDetectorFinalizer;
|
---|
219 | #endif
|
---|
220 |
|
---|
221 | LeakDetectorFinalizer::~LeakDetectorFinalizer()
|
---|
222 | {
|
---|
223 | GC_gcollect();
|
---|
224 |
|
---|
225 | #if 0
|
---|
226 | nextLeakFile();
|
---|
227 | if (GC_stdout != NULL) {
|
---|
228 | fprintf(GC_stdout, "ShutDown Leaks\n");
|
---|
229 | GC_clear_roots();
|
---|
230 | GC_gcollect();
|
---|
231 | }
|
---|
232 | #endif
|
---|
233 | }
|
---|
234 |
|
---|
235 | nsresult NS_ShutdownLeakDetector()
|
---|
236 | {
|
---|
237 | #if defined(SHUTDOWN_LEAKS_MEDIUM)
|
---|
238 | // Make this the first atexit() called so it's before the atexit() crashes
|
---|
239 | // see http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=23552
|
---|
240 | static LeakDetectorFinalizer trick;
|
---|
241 | #elif defined(SHUTDOWN_LEAKS_EARLY)
|
---|
242 | // do shutdown leaks now
|
---|
243 | LeakDetectorFinalizer trick;
|
---|
244 | #endif
|
---|
245 | return NS_OK;
|
---|
246 | }
|
---|
247 |
|
---|
248 | #endif /* defined(GC_LEAK_DETECTOR) */
|
---|