|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsTraceRefcnt.h" |
|
7 |
|
8 // if NS_BUILD_REFCNT_LOGGING isn't defined, don't try to build |
|
9 #ifdef NS_BUILD_REFCNT_LOGGING |
|
10 |
|
11 #include "nsAboutBloat.h" |
|
12 #include "nsStringStream.h" |
|
13 #include "nsIURI.h" |
|
14 #include "nsCOMPtr.h" |
|
15 #include "nsNetUtil.h" |
|
16 #include "nsDirectoryServiceDefs.h" |
|
17 #include "nsIFile.h" |
|
18 |
|
19 static void GC_gcollect() {} |
|
20 |
|
21 NS_IMPL_ISUPPORTS(nsAboutBloat, nsIAboutModule) |
|
22 |
|
23 NS_IMETHODIMP |
|
24 nsAboutBloat::NewChannel(nsIURI *aURI, nsIChannel **result) |
|
25 { |
|
26 NS_ENSURE_ARG_POINTER(aURI); |
|
27 nsresult rv; |
|
28 nsAutoCString path; |
|
29 rv = aURI->GetPath(path); |
|
30 if (NS_FAILED(rv)) return rv; |
|
31 |
|
32 nsTraceRefcnt::StatisticsType statType = nsTraceRefcnt::ALL_STATS; |
|
33 bool clear = false; |
|
34 bool leaks = false; |
|
35 |
|
36 int32_t pos = path.Find("?"); |
|
37 if (pos > 0) { |
|
38 nsAutoCString param; |
|
39 (void)path.Right(param, path.Length() - (pos+1)); |
|
40 if (param.EqualsLiteral("new")) |
|
41 statType = nsTraceRefcnt::NEW_STATS; |
|
42 else if (param.EqualsLiteral("clear")) |
|
43 clear = true; |
|
44 else if (param.EqualsLiteral("leaks")) |
|
45 leaks = true; |
|
46 } |
|
47 |
|
48 nsCOMPtr<nsIInputStream> inStr; |
|
49 if (clear) { |
|
50 nsTraceRefcnt::ResetStatistics(); |
|
51 |
|
52 rv = NS_NewCStringInputStream(getter_AddRefs(inStr), |
|
53 NS_LITERAL_CSTRING("Bloat statistics cleared.")); |
|
54 if (NS_FAILED(rv)) return rv; |
|
55 } |
|
56 else if (leaks) { |
|
57 // dump the current set of leaks. |
|
58 GC_gcollect(); |
|
59 |
|
60 rv = NS_NewCStringInputStream(getter_AddRefs(inStr), |
|
61 NS_LITERAL_CSTRING("Memory leaks dumped.")); |
|
62 if (NS_FAILED(rv)) return rv; |
|
63 } |
|
64 else { |
|
65 nsCOMPtr<nsIFile> file; |
|
66 rv = NS_GetSpecialDirectory(NS_OS_CURRENT_PROCESS_DIR, |
|
67 getter_AddRefs(file)); |
|
68 if (NS_FAILED(rv)) return rv; |
|
69 |
|
70 rv = file->AppendNative(NS_LITERAL_CSTRING("bloatlogs")); |
|
71 if (NS_FAILED(rv)) return rv; |
|
72 |
|
73 bool exists; |
|
74 rv = file->Exists(&exists); |
|
75 if (NS_FAILED(rv)) return rv; |
|
76 |
|
77 if (!exists) { |
|
78 // On all the platforms that I know use permissions, |
|
79 // directories need to have the executable flag set |
|
80 // if you want to do anything inside the directory. |
|
81 rv = file->Create(nsIFile::DIRECTORY_TYPE, 0755); |
|
82 if (NS_FAILED(rv)) return rv; |
|
83 } |
|
84 |
|
85 nsAutoCString dumpFileName; |
|
86 if (statType == nsTraceRefcnt::ALL_STATS) |
|
87 dumpFileName.AssignLiteral("all-"); |
|
88 else |
|
89 dumpFileName.AssignLiteral("new-"); |
|
90 PRExplodedTime expTime; |
|
91 PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &expTime); |
|
92 char time[128]; |
|
93 PR_FormatTimeUSEnglish(time, 128, "%Y-%m-%d-%H%M%S.txt", &expTime); |
|
94 dumpFileName += time; |
|
95 rv = file->AppendNative(dumpFileName); |
|
96 if (NS_FAILED(rv)) return rv; |
|
97 |
|
98 FILE* out; |
|
99 rv = file->OpenANSIFileDesc("w", &out); |
|
100 if (NS_FAILED(rv)) return rv; |
|
101 |
|
102 rv = nsTraceRefcnt::DumpStatistics(statType, out); |
|
103 ::fclose(out); |
|
104 if (NS_FAILED(rv)) return rv; |
|
105 |
|
106 rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), file); |
|
107 if (NS_FAILED(rv)) return rv; |
|
108 } |
|
109 |
|
110 nsIChannel* channel = nullptr; |
|
111 rv = NS_NewInputStreamChannel(&channel, aURI, inStr, |
|
112 NS_LITERAL_CSTRING("text/plain"), |
|
113 NS_LITERAL_CSTRING("utf-8")); |
|
114 if (NS_FAILED(rv)) return rv; |
|
115 |
|
116 *result = channel; |
|
117 return rv; |
|
118 } |
|
119 |
|
120 NS_IMETHODIMP |
|
121 nsAboutBloat::GetURIFlags(nsIURI *aURI, uint32_t *result) |
|
122 { |
|
123 *result = 0; |
|
124 return NS_OK; |
|
125 } |
|
126 |
|
127 nsresult |
|
128 nsAboutBloat::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) |
|
129 { |
|
130 nsAboutBloat* about = new nsAboutBloat(); |
|
131 if (about == nullptr) |
|
132 return NS_ERROR_OUT_OF_MEMORY; |
|
133 NS_ADDREF(about); |
|
134 nsresult rv = about->QueryInterface(aIID, aResult); |
|
135 NS_RELEASE(about); |
|
136 return rv; |
|
137 } |
|
138 |
|
139 //////////////////////////////////////////////////////////////////////////////// |
|
140 #endif /* NS_BUILD_REFCNT_LOGGING */ |