Back to index

lightning-sunbird  0.9+nobinonly
nsLeakDetector.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Patrick C. Beard <beard@netscape.com>
00025  *   Scott Collins <scc@mozilla.org>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #if defined(GC_LEAK_DETECTOR)
00042 
00043 #include "nsLeakDetector.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsIComponentManager.h"
00046 #include "nsIServiceManager.h"
00047 #include "nsIGenericFactory.h"
00048 #include "nsILeakDetector.h"
00049 #include "nsICollection.h"
00050 
00051 #include <stdio.h>
00052 #include <time.h>
00053 
00054 #include "gc.h"
00055 
00056 extern "C" {
00057 extern FILE *GC_stdout, *GC_stderr;
00058 extern void GC_trace_object(GC_PTR object, int verbose);
00059 extern void GC_mark_object(GC_PTR object, GC_word mark);
00060 }
00061 
00062 static nsresult nextLeakFile()
00063 {
00064        if (GC_stderr != NULL)
00065               fclose(GC_stderr);
00066        
00067        // generate a time stamped report name.
00068        time_t timer;
00069        time(&timer);
00070        tm* now = localtime(&timer);
00071        
00072        char reportName[256];
00073        sprintf(reportName, "Leaks%02d%02d%02d",
00074                      now->tm_hour, now->tm_min, now->tm_sec);
00075        GC_stderr = fopen(reportName, "w");
00076        
00077        return NS_OK;
00078 }
00079 
00080 static FILE* openTraceFile()
00081 {
00082        // generate a time stamped report name.
00083        time_t timer;
00084        time(&timer);
00085        tm* now = localtime(&timer);
00086        
00087        char reportName[256];
00088        sprintf(reportName, "Trace%02d%02d%02d",
00089                      now->tm_hour, now->tm_min, now->tm_sec);
00090        return fopen(reportName, "w");
00091 }
00092 
00093 class nsLeakDetector : public nsILeakDetector {
00094 public:
00095        nsLeakDetector();
00096        
00097        NS_DECL_ISUPPORTS    
00098        NS_DECL_NSILEAKDETECTOR
00099 private:
00100        ~nsLeakDetector() {}
00101 };
00102 
00103 NS_IMPL_ISUPPORTS1(nsLeakDetector, nsILeakDetector)
00104 
00105 nsLeakDetector::nsLeakDetector() {
00106    }
00107 
00108 NS_METHOD nsLeakDetector::DumpLeaks()
00109 {
00110        GC_gcollect();
00111 
00112        return nextLeakFile();
00113 }
00114 
00115 NS_METHOD nsLeakDetector::TraceObject(nsISupports* object, PRBool verbose)
00116 {
00117     FILE* trace = openTraceFile();
00118     if (trace != NULL) {
00119         FILE* old_stderr = GC_stderr;
00120         GC_stderr = trace;
00121         GC_trace_object(object, (verbose ? 1 : 0));
00122         GC_stderr = old_stderr;
00123         fclose(trace);
00124         return NS_OK;
00125     }
00126     return NS_ERROR_FAILURE;
00127 }
00128 
00129 NS_METHOD nsLeakDetector::TraceCollection(nsICollection* objects, PRBool verbose)
00130 {
00131     PRUint32 count;
00132     if (NS_FAILED(objects->Count(&count)))
00133         return NS_ERROR_FAILURE;
00134 
00135     nsCOMPtr<nsISupports>* elements = new nsCOMPtr<nsISupports>[count];
00136     if (elements == nsnull)
00137         return NS_ERROR_OUT_OF_MEMORY;
00138 
00139     for (PRUint32 i = 0; i < count; ++i)
00140         objects->GetElementAt(i, getter_AddRefs(elements[i]));
00141 
00142     nsresult rv = NS_ERROR_FAILURE;
00143     FILE* trace = openTraceFile();
00144     if (trace != NULL) {
00145         FILE* old_stderr = GC_stderr;
00146         GC_stderr = trace;
00147         GC_trace_object(elements, (verbose ? 1 : 0));
00148         GC_stderr = old_stderr;
00149         fclose(trace);
00150         rv = NS_OK;
00151     }
00152     
00153     delete[] elements;
00154     
00155     return rv;
00156 }
00157 
00158 NS_METHOD nsLeakDetector::MarkObject(nsISupports* object, PRBool marked)
00159 {
00160     GC_mark_object(object, (marked ? 1 : 0));
00161     return NS_OK;
00162 }
00163 
00164 NS_METHOD nsLeakDetector::GetServices(nsISupports* *result)
00165 {
00166     return NS_GetServiceManager((nsIServiceManager**)result);
00167 }
00168 
00169 #define NS_CLEAKDETECTOR_CID_STR "bb1ba360-1dd1-11b2-b30e-aa2314429f54"
00170 #define NS_CLEAKDETECTOR_CID {0xbb1ba360, 0x1dd1, 0x11b2, {0xb3, 0x0e, 0xaa, 0x23, 0x14, 0x42, 0x9f, 0x54}}
00171 #define NS_CLEAKDETECTOR_CONTRACTID "@mozilla.org/xpcom/leakdetector;1"
00172 
00173 NS_GENERIC_FACTORY_CONSTRUCTOR(nsLeakDetector)
00174 
00175 static NS_DEFINE_CID(kCLeakDetectorCID, NS_CLEAKDETECTOR_CID);
00176 
00177 nsresult NS_InitLeakDetector()
00178 {
00179        nsresult rv;
00180 
00181        // open the first leak file.
00182        rv = nextLeakFile();
00183        if (NS_FAILED(rv))
00184               return rv;
00185 
00186     static const nsModuleComponentInfo info = {
00187         "Leak Detector", kCLeakDetectorCID, NS_CLEAKDETECTOR_CONTRACTID, nsLeakDetectorConstructor
00188     };
00189 
00190        // create a generic factory for the leak detector.
00191        nsCOMPtr<nsIGenericFactory> factory;
00192        rv = NS_NewGenericFactory(getter_AddRefs(factory), &info);
00193        if (NS_FAILED(rv))
00194               return rv;
00195 
00196        nsCOMPtr<nsIComponentRegistrar> registrar;
00197        NS_GetComponentRegistrar(getter_AddRefs(registrar));
00198 
00199        return registrar->RegisterFactory(info.mCID, info.mDescription, info.mContractID, factory);
00200 }
00201 
00202 #undef SHUTDOWN_LEAKS_EARLY
00203 #undef SHUTDOWN_LEAKS_MEDIUM
00204 #define SHUTDOWN_LEAKS_LATE
00205 
00206 class LeakDetectorFinalizer
00207 {
00208 public:
00209        ~LeakDetectorFinalizer();
00210 };
00211 
00212 #ifdef SHUTDOWN_LEAKS_LATE
00213 // do shutdown leaks when XPCOM library is unloaded
00214 LeakDetectorFinalizer gLeakDetectorFinalizer;
00215 #endif
00216 
00217 LeakDetectorFinalizer::~LeakDetectorFinalizer()
00218 {
00219        GC_gcollect();
00220 
00221 #if 0
00222        nextLeakFile();
00223        if (GC_stdout != NULL) {
00224               fprintf(GC_stdout, "ShutDown Leaks\n");
00225               GC_clear_roots();
00226               GC_gcollect();
00227        }
00228 #endif
00229 }
00230 
00231 nsresult NS_ShutdownLeakDetector()
00232 {
00233 #if defined(SHUTDOWN_LEAKS_MEDIUM)
00234     // Make this the first atexit() called so it's before the atexit() crashes
00235     // see http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=23552
00236     static LeakDetectorFinalizer trick;
00237 #elif defined(SHUTDOWN_LEAKS_EARLY)
00238     // do shutdown leaks now
00239     LeakDetectorFinalizer trick;
00240 #endif
00241        return NS_OK;
00242 }
00243 
00244 #endif /* defined(GC_LEAK_DETECTOR) */