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
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsTraceRefcntImpl.h"
39 :
40 : // if NS_BUILD_REFCNT_LOGGING isn't defined, don't try to build
41 : #ifdef NS_BUILD_REFCNT_LOGGING
42 :
43 : #include "nsAboutBloat.h"
44 : #include "nsIIOService.h"
45 : #include "nsIServiceManager.h"
46 : #include "nsStringStream.h"
47 : #include "nsXPIDLString.h"
48 : #include "nsIURI.h"
49 : #include "prtime.h"
50 : #include "nsCOMPtr.h"
51 : #include "nsIFileStreams.h"
52 : #include "nsNetUtil.h"
53 : #include "nsDirectoryServiceDefs.h"
54 : #include "nsILocalFile.h"
55 :
56 0 : static void GC_gcollect() {}
57 :
58 0 : NS_IMPL_ISUPPORTS1(nsAboutBloat, nsIAboutModule)
59 :
60 : NS_IMETHODIMP
61 0 : nsAboutBloat::NewChannel(nsIURI *aURI, nsIChannel **result)
62 : {
63 0 : NS_ENSURE_ARG_POINTER(aURI);
64 : nsresult rv;
65 0 : nsCAutoString path;
66 0 : rv = aURI->GetPath(path);
67 0 : if (NS_FAILED(rv)) return rv;
68 :
69 0 : nsTraceRefcntImpl::StatisticsType statType = nsTraceRefcntImpl::ALL_STATS;
70 0 : bool clear = false;
71 0 : bool leaks = false;
72 :
73 0 : PRInt32 pos = path.Find("?");
74 0 : if (pos > 0) {
75 0 : nsCAutoString param;
76 0 : (void)path.Right(param, path.Length() - (pos+1));
77 0 : if (param.EqualsLiteral("new"))
78 0 : statType = nsTraceRefcntImpl::NEW_STATS;
79 0 : else if (param.EqualsLiteral("clear"))
80 0 : clear = true;
81 0 : else if (param.EqualsLiteral("leaks"))
82 0 : leaks = true;
83 : }
84 :
85 0 : nsCOMPtr<nsIInputStream> inStr;
86 0 : if (clear) {
87 0 : nsTraceRefcntImpl::ResetStatistics();
88 :
89 0 : rv = NS_NewCStringInputStream(getter_AddRefs(inStr),
90 0 : NS_LITERAL_CSTRING("Bloat statistics cleared."));
91 0 : if (NS_FAILED(rv)) return rv;
92 : }
93 0 : else if (leaks) {
94 : // dump the current set of leaks.
95 0 : GC_gcollect();
96 :
97 0 : rv = NS_NewCStringInputStream(getter_AddRefs(inStr),
98 0 : NS_LITERAL_CSTRING("Memory leaks dumped."));
99 0 : if (NS_FAILED(rv)) return rv;
100 : }
101 : else {
102 0 : nsCOMPtr<nsIFile> file;
103 : rv = NS_GetSpecialDirectory(NS_OS_CURRENT_PROCESS_DIR,
104 0 : getter_AddRefs(file));
105 0 : if (NS_FAILED(rv)) return rv;
106 :
107 0 : rv = file->AppendNative(NS_LITERAL_CSTRING("bloatlogs"));
108 0 : if (NS_FAILED(rv)) return rv;
109 :
110 : bool exists;
111 0 : rv = file->Exists(&exists);
112 0 : if (NS_FAILED(rv)) return rv;
113 :
114 0 : if (!exists) {
115 : // On all the platforms that I know use permissions,
116 : // directories need to have the executable flag set
117 : // if you want to do anything inside the directory.
118 0 : rv = file->Create(nsIFile::DIRECTORY_TYPE, 0755);
119 0 : if (NS_FAILED(rv)) return rv;
120 : }
121 :
122 0 : nsCAutoString dumpFileName;
123 0 : if (statType == nsTraceRefcntImpl::ALL_STATS)
124 0 : dumpFileName.AssignLiteral("all-");
125 : else
126 0 : dumpFileName.AssignLiteral("new-");
127 : PRExplodedTime expTime;
128 0 : PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &expTime);
129 : char time[128];
130 0 : PR_FormatTimeUSEnglish(time, 128, "%Y-%m-%d-%H%M%S.txt", &expTime);
131 0 : dumpFileName += time;
132 0 : rv = file->AppendNative(dumpFileName);
133 0 : if (NS_FAILED(rv)) return rv;
134 :
135 : FILE* out;
136 0 : nsCOMPtr<nsILocalFile> lfile = do_QueryInterface(file);
137 0 : if (lfile == nsnull)
138 0 : return NS_ERROR_FAILURE;
139 0 : rv = lfile->OpenANSIFileDesc("w", &out);
140 0 : if (NS_FAILED(rv)) return rv;
141 :
142 0 : rv = nsTraceRefcntImpl::DumpStatistics(statType, out);
143 0 : ::fclose(out);
144 0 : if (NS_FAILED(rv)) return rv;
145 :
146 0 : rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), file);
147 0 : if (NS_FAILED(rv)) return rv;
148 : }
149 :
150 : nsIChannel* channel;
151 : rv = NS_NewInputStreamChannel(&channel, aURI, inStr,
152 0 : NS_LITERAL_CSTRING("text/plain"),
153 0 : NS_LITERAL_CSTRING("utf-8"));
154 0 : if (NS_FAILED(rv)) return rv;
155 :
156 0 : *result = channel;
157 0 : return rv;
158 : }
159 :
160 : NS_IMETHODIMP
161 0 : nsAboutBloat::GetURIFlags(nsIURI *aURI, PRUint32 *result)
162 : {
163 0 : *result = 0;
164 0 : return NS_OK;
165 : }
166 :
167 : nsresult
168 0 : nsAboutBloat::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
169 : {
170 0 : nsAboutBloat* about = new nsAboutBloat();
171 0 : if (about == nsnull)
172 0 : return NS_ERROR_OUT_OF_MEMORY;
173 0 : NS_ADDREF(about);
174 0 : nsresult rv = about->QueryInterface(aIID, aResult);
175 0 : NS_RELEASE(about);
176 0 : return rv;
177 : }
178 :
179 : ////////////////////////////////////////////////////////////////////////////////
180 : #endif /* NS_BUILD_REFCNT_LOGGING */
|