1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "mozilla/IOInterposer.h"
10 #include "mozilla/PoisonIOInterposer.h"
11 #include "mozilla/ProcessedStack.h"
12 #include "mozilla/SHA1.h"
13 #include "mozilla/Scoped.h"
14 #include "mozilla/StaticPtr.h"
15 #include "mozilla/Telemetry.h"
16 #include "mozilla/Unused.h"
17 #include "nsAppDirectoryServiceDefs.h"
18 #include "nsDirectoryServiceUtils.h"
19 #include "nsLocalFile.h"
20 #include "nsPrintfCString.h"
21 #include "mozilla/StackWalk.h"
37 #include "LateWriteChecks.h"
39 using namespace mozilla
;
41 /*************************** Auxiliary Declarations ***************************/
43 // This a wrapper over a file descriptor that provides a Printf method and
44 // computes the sha1 of the data that passes through it.
47 explicit SHA1Stream(FILE* aStream
) : mFile(aStream
) {
48 MozillaRegisterDebugFILE(mFile
);
51 void Printf(const char* aFormat
, ...) MOZ_FORMAT_PRINTF(2, 3) {
54 va_start(list
, aFormat
);
56 str
.AppendPrintf(aFormat
, list
);
58 mSHA1
.update(str
.get(), str
.Length());
59 Unused
<< fwrite(str
.get(), 1, str
.Length(), mFile
);
61 void Finish(SHA1Sum::Hash
& aHash
) {
62 int fd
= fileno(mFile
);
64 MozillaUnRegisterDebugFD(fd
);
75 static void RecordStackWalker(uint32_t aFrameNumber
, void* aPC
, void* aSP
,
77 std::vector
<uintptr_t>* stack
=
78 static_cast<std::vector
<uintptr_t>*>(aClosure
);
79 stack
->push_back(reinterpret_cast<uintptr_t>(aPC
));
82 /**************************** Late-Write Observer ****************************/
85 * An implementation of IOInterposeObserver to be registered with IOInterposer.
86 * This observer logs all writes as late writes.
88 class LateWriteObserver final
: public IOInterposeObserver
{
89 using char_type
= filesystem::Path::value_type
;
92 explicit LateWriteObserver(const char_type
* aProfileDirectory
)
93 : mProfileDirectory(NS_xstrdup(aProfileDirectory
)) {}
94 ~LateWriteObserver() {
95 free(mProfileDirectory
);
96 mProfileDirectory
= nullptr;
99 void Observe(IOInterposeObserver::Observation
& aObservation
) override
;
102 char_type
* mProfileDirectory
;
105 void LateWriteObserver::Observe(IOInterposeObserver::Observation
& aOb
) {
106 // Crash if that is the shutdown check mode
107 if (gShutdownChecks
== SCM_CRASH
) {
111 // If we have shutdown mode SCM_NOTHING or we can't record then abort
112 if (gShutdownChecks
== SCM_NOTHING
|| !Telemetry::CanRecordExtended()) {
116 // Write the stack and loaded libraries to a file. We can get here
117 // concurrently from many writes, so we use multiple temporary files.
118 std::vector
<uintptr_t> rawStack
;
120 MozStackWalk(RecordStackWalker
, /* skipFrames */ 0, /* maxFrames */ 0,
122 Telemetry::ProcessedStack stack
= Telemetry::GetStackAndModules(rawStack
);
124 nsTAutoString
<char_type
> nameAux(mProfileDirectory
);
125 nameAux
.AppendLiteral(NS_SLASH
"Telemetry.LateWriteTmpXXXXXX");
126 char_type
* name
= nameAux
.BeginWriting();
128 // We want the sha1 of the entire file, so please don't write to fd
129 // directly; use sha1Stream.
134 // mkstemp isn't supported so keep trying until we get a file
135 _wmktemp_s(char16ptr_t(name
), NS_strlen(name
) + 1);
136 hFile
= CreateFileW(char16ptr_t(name
), GENERIC_WRITE
, 0, nullptr,
137 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, nullptr);
138 } while (GetLastError() == ERROR_FILE_EXISTS
);
140 if (hFile
== INVALID_HANDLE_VALUE
) {
141 MOZ_CRASH("Um, how did we get here?");
144 // http://support.microsoft.com/kb/139640
145 int fd
= _open_osfhandle((intptr_t)hFile
, _O_APPEND
);
147 MOZ_CRASH("Um, how did we get here?");
150 stream
= _fdopen(fd
, "w");
152 int fd
= mkstemp(name
);
154 MOZ_CRASH("mkstemp failed");
156 stream
= fdopen(fd
, "w");
159 SHA1Stream
sha1Stream(stream
);
161 size_t numModules
= stack
.GetNumModules();
162 sha1Stream
.Printf("%u\n", (unsigned)numModules
);
163 for (size_t i
= 0; i
< numModules
; ++i
) {
164 Telemetry::ProcessedStack::Module module
= stack
.GetModule(i
);
165 sha1Stream
.Printf("%s %s\n", module
.mBreakpadId
.get(),
166 NS_ConvertUTF16toUTF8(module
.mName
).get());
169 size_t numFrames
= stack
.GetStackSize();
170 sha1Stream
.Printf("%u\n", (unsigned)numFrames
);
171 for (size_t i
= 0; i
< numFrames
; ++i
) {
172 const Telemetry::ProcessedStack::Frame
& frame
= stack
.GetFrame(i
);
173 // NOTE: We write the offsets, while the atos tool expects a value with
174 // the virtual address added. For example, running otool -l on the the
175 // firefox binary shows
179 // vmaddr 0x0000000100000000
180 // so to print the line matching the offset 123 one has to run
181 // atos -o firefox 0x100000123.
182 sha1Stream
.Printf("%d %x\n", frame
.mModIndex
, (unsigned)frame
.mOffset
);
186 sha1Stream
.Finish(sha1
);
188 // Note: These files should be deleted by telemetry once it reads them. If
189 // there were no telemetry runs by the time we shut down, we just add files
190 // to the existing ones instead of replacing them. Given that each of these
191 // files is a bug to be fixed, that is probably the right thing to do.
193 // We append the sha1 of the contents to the file name. This provides a simple
194 // client side deduplication.
195 nsAutoString
finalName(NS_LITERAL_STRING("Telemetry.LateWriteFinal-"));
196 for (int i
= 0; i
< 20; ++i
) {
197 finalName
.AppendPrintf("%02x", sha1
[i
]);
199 RefPtr
<nsLocalFile
> file
= new nsLocalFile(nameAux
);
200 file
->RenameTo(nullptr, finalName
);
203 /******************************* Setup/Teardown *******************************/
205 static StaticAutoPtr
<LateWriteObserver
> sLateWriteObserver
;
209 void InitLateWriteChecks() {
210 nsCOMPtr
<nsIFile
> mozFile
;
211 NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR
, getter_AddRefs(mozFile
));
213 PathString nativePath
= mozFile
->NativePath();
214 if (nativePath
.get()) {
215 sLateWriteObserver
= new LateWriteObserver(nativePath
.get());
220 void BeginLateWriteChecks() {
221 if (sLateWriteObserver
) {
222 IOInterposer::Register(IOInterposeObserver::OpWriteFSync
,
227 void StopLateWriteChecks() {
228 if (sLateWriteObserver
) {
229 IOInterposer::Unregister(IOInterposeObserver::OpAll
, sLateWriteObserver
);
230 // Deallocation would not be thread-safe, and StopLateWriteChecks() is
231 // called at shutdown and only in special cases.
232 // sLateWriteObserver = nullptr;
236 } // namespace mozilla