1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsTraceRefcntImpl.h"
40 // if NS_BUILD_REFCNT_LOGGING isn't defined, don't try to build
41 #ifdef NS_BUILD_REFCNT_LOGGING
43 #include "nsAboutBloat.h"
44 #include "nsIIOService.h"
45 #include "nsIServiceManager.h"
46 #include "nsStringStream.h"
47 #include "nsXPIDLString.h"
51 #include "nsIFileStreams.h"
52 #include "nsNetUtil.h"
53 #include "nsDirectoryServiceDefs.h"
54 #include "nsILocalFile.h"
57 extern "C" void GC_gcollect(void);
59 static void GC_gcollect() {}
62 NS_IMPL_ISUPPORTS1(nsAboutBloat
, nsIAboutModule
)
65 nsAboutBloat::NewChannel(nsIURI
*aURI
, nsIChannel
**result
)
67 NS_ENSURE_ARG_POINTER(aURI
);
70 rv
= aURI
->GetPath(path
);
71 if (NS_FAILED(rv
)) return rv
;
73 nsTraceRefcntImpl::StatisticsType statType
= nsTraceRefcntImpl::ALL_STATS
;
74 PRBool clear
= PR_FALSE
;
75 PRBool leaks
= PR_FALSE
;
77 PRInt32 pos
= path
.Find("?");
80 (void)path
.Right(param
, path
.Length() - (pos
+1));
81 if (param
.EqualsLiteral("new"))
82 statType
= nsTraceRefcntImpl::NEW_STATS
;
83 else if (param
.EqualsLiteral("clear"))
85 else if (param
.EqualsLiteral("leaks"))
89 nsCOMPtr
<nsIInputStream
> inStr
;
91 nsTraceRefcntImpl::ResetStatistics();
93 const char* msg
= "Bloat statistics cleared.";
94 rv
= NS_NewCStringInputStream(getter_AddRefs(inStr
), nsDependentCString(msg
));
95 if (NS_FAILED(rv
)) return rv
;
98 // dump the current set of leaks.
101 const char* msg
= "Memory leaks dumped.";
102 rv
= NS_NewCStringInputStream(getter_AddRefs(inStr
), nsDependentCString(msg
));
103 if (NS_FAILED(rv
)) return rv
;
106 nsCOMPtr
<nsIFile
> file
;
107 rv
= NS_GetSpecialDirectory(NS_OS_CURRENT_PROCESS_DIR
,
108 getter_AddRefs(file
));
109 if (NS_FAILED(rv
)) return rv
;
111 rv
= file
->AppendNative(NS_LITERAL_CSTRING("bloatlogs"));
112 if (NS_FAILED(rv
)) return rv
;
115 rv
= file
->Exists(&exists
);
116 if (NS_FAILED(rv
)) return rv
;
119 // On all the platforms that I know use permissions,
120 // directories need to have the executable flag set
121 // if you want to do anything inside the directory.
122 rv
= file
->Create(nsIFile::DIRECTORY_TYPE
, 0755);
123 if (NS_FAILED(rv
)) return rv
;
126 nsCAutoString dumpFileName
;
127 if (statType
== nsTraceRefcntImpl::ALL_STATS
)
128 dumpFileName
.AssignLiteral("all-");
130 dumpFileName
.AssignLiteral("new-");
131 PRExplodedTime expTime
;
132 PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters
, &expTime
);
134 PR_FormatTimeUSEnglish(time
, 128, "%Y-%m-%d-%H%M%S.txt", &expTime
);
135 dumpFileName
+= time
;
136 rv
= file
->AppendNative(dumpFileName
);
137 if (NS_FAILED(rv
)) return rv
;
140 nsCOMPtr
<nsILocalFile
> lfile
= do_QueryInterface(file
);
142 return NS_ERROR_FAILURE
;
143 rv
= lfile
->OpenANSIFileDesc("w", &out
);
144 if (NS_FAILED(rv
)) return rv
;
146 rv
= nsTraceRefcntImpl::DumpStatistics(statType
, out
);
148 if (NS_FAILED(rv
)) return rv
;
150 rv
= NS_NewLocalFileInputStream(getter_AddRefs(inStr
), file
);
151 if (NS_FAILED(rv
)) return rv
;
155 rv
= NS_NewInputStreamChannel(&channel
, aURI
, inStr
,
156 NS_LITERAL_CSTRING("text/plain"),
157 NS_LITERAL_CSTRING("utf-8"));
158 if (NS_FAILED(rv
)) return rv
;
165 nsAboutBloat::GetURIFlags(nsIURI
*aURI
, PRUint32
*result
)
172 nsAboutBloat::Create(nsISupports
*aOuter
, REFNSIID aIID
, void **aResult
)
174 nsAboutBloat
* about
= new nsAboutBloat();
176 return NS_ERROR_OUT_OF_MEMORY
;
178 nsresult rv
= about
->QueryInterface(aIID
, aResult
);
183 ////////////////////////////////////////////////////////////////////////////////
184 #endif /* NS_BUILD_REFCNT_LOGGING */