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/StaticPtr.h"
14 #include "mozilla/Telemetry.h"
15 #include "mozilla/Unused.h"
16 #include "nsAppDirectoryServiceDefs.h"
17 #include "nsDirectoryServiceUtils.h"
18 #include "nsLocalFile.h"
19 #include "nsPrintfCString.h"
20 #include "mozilla/StackWalk.h"
24 # define NS_SLASH "\\"
29 # include <sys/stat.h>
35 #include "LateWriteChecks.h"
37 /*************************** Auxiliary Declarations ***************************/
39 static MOZ_THREAD_LOCAL(int) tlsSuspendLateWriteChecks
;
41 bool SuspendingLateWriteChecksForCurrentThread() {
42 if (!tlsSuspendLateWriteChecks
.init()) {
45 return tlsSuspendLateWriteChecks
.get() > 0;
48 // This a wrapper over a file descriptor that provides a Printf method and
49 // computes the sha1 of the data that passes through it.
52 explicit SHA1Stream(FILE* aStream
) : mFile(aStream
) {
53 MozillaRegisterDebugFILE(mFile
);
56 void Printf(const char* aFormat
, ...) MOZ_FORMAT_PRINTF(2, 3) {
59 va_start(list
, aFormat
);
61 str
.AppendVprintf(aFormat
, list
);
63 mSHA1
.update(str
.get(), str
.Length());
64 mozilla::Unused
<< fwrite(str
.get(), 1, str
.Length(), mFile
);
66 void Finish(mozilla::SHA1Sum::Hash
& aHash
) {
67 int fd
= fileno(mFile
);
69 MozillaUnRegisterDebugFD(fd
);
77 mozilla::SHA1Sum mSHA1
;
80 static void RecordStackWalker(uint32_t aFrameNumber
, void* aPC
, void* aSP
,
82 std::vector
<uintptr_t>* stack
=
83 static_cast<std::vector
<uintptr_t>*>(aClosure
);
84 stack
->push_back(reinterpret_cast<uintptr_t>(aPC
));
87 /**************************** Late-Write Observer ****************************/
90 * An implementation of IOInterposeObserver to be registered with IOInterposer.
91 * This observer logs all writes as late writes.
93 class LateWriteObserver final
: public mozilla::IOInterposeObserver
{
94 using char_type
= mozilla::filesystem::Path::value_type
;
97 explicit LateWriteObserver(const char_type
* aProfileDirectory
)
98 : mProfileDirectory(NS_xstrdup(aProfileDirectory
)) {}
99 ~LateWriteObserver() {
100 free(mProfileDirectory
);
101 mProfileDirectory
= nullptr;
105 mozilla::IOInterposeObserver::Observation
& aObservation
) override
;
108 char_type
* mProfileDirectory
;
111 void LateWriteObserver::Observe(
112 mozilla::IOInterposeObserver::Observation
& aOb
) {
113 if (SuspendingLateWriteChecksForCurrentThread()) {
121 // If we can't record then abort
122 if (!mozilla::Telemetry::CanRecordExtended()) {
126 // Write the stack and loaded libraries to a file. We can get here
127 // concurrently from many writes, so we use multiple temporary files.
128 std::vector
<uintptr_t> rawStack
;
130 MozStackWalk(RecordStackWalker
, nullptr, /* maxFrames */ 0, &rawStack
);
131 mozilla::Telemetry::ProcessedStack stack
=
132 mozilla::Telemetry::GetStackAndModules(rawStack
);
134 nsTAutoString
<char_type
> nameAux(mProfileDirectory
);
135 nameAux
.AppendLiteral(NS_SLASH
"Telemetry.LateWriteTmpXXXXXX");
136 char_type
* name
= nameAux
.BeginWriting();
138 // We want the sha1 of the entire file, so please don't write to fd
139 // directly; use sha1Stream.
144 // mkstemp isn't supported so keep trying until we get a file
145 _wmktemp_s(char16ptr_t(name
), NS_strlen(name
) + 1);
146 hFile
= CreateFileW(char16ptr_t(name
), GENERIC_WRITE
, 0, nullptr,
147 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, nullptr);
148 } while (GetLastError() == ERROR_FILE_EXISTS
);
150 if (hFile
== INVALID_HANDLE_VALUE
) {
151 MOZ_CRASH("Um, how did we get here?");
154 // http://support.microsoft.com/kb/139640
155 int fd
= _open_osfhandle((intptr_t)hFile
, _O_APPEND
);
157 MOZ_CRASH("Um, how did we get here?");
160 stream
= _fdopen(fd
, "w");
162 int fd
= mkstemp(name
);
164 MOZ_CRASH("mkstemp failed");
166 stream
= fdopen(fd
, "w");
169 SHA1Stream
sha1Stream(stream
);
171 size_t numModules
= stack
.GetNumModules();
172 sha1Stream
.Printf("%u\n", (unsigned)numModules
);
173 for (size_t i
= 0; i
< numModules
; ++i
) {
174 mozilla::Telemetry::ProcessedStack::Module module
= stack
.GetModule(i
);
175 sha1Stream
.Printf("%s %s\n", module
.mBreakpadId
.get(),
176 NS_ConvertUTF16toUTF8(module
.mName
).get());
179 size_t numFrames
= stack
.GetStackSize();
180 sha1Stream
.Printf("%u\n", (unsigned)numFrames
);
181 for (size_t i
= 0; i
< numFrames
; ++i
) {
182 const mozilla::Telemetry::ProcessedStack::Frame
& frame
= stack
.GetFrame(i
);
183 // NOTE: We write the offsets, while the atos tool expects a value with
184 // the virtual address added. For example, running otool -l on the the
185 // firefox binary shows
189 // vmaddr 0x0000000100000000
190 // so to print the line matching the offset 123 one has to run
191 // atos -o firefox 0x100000123.
192 sha1Stream
.Printf("%d %x\n", frame
.mModIndex
, (unsigned)frame
.mOffset
);
195 mozilla::SHA1Sum::Hash sha1
;
196 sha1Stream
.Finish(sha1
);
198 // Note: These files should be deleted by telemetry once it reads them. If
199 // there were no telemetry runs by the time we shut down, we just add files
200 // to the existing ones instead of replacing them. Given that each of these
201 // files is a bug to be fixed, that is probably the right thing to do.
203 // We append the sha1 of the contents to the file name. This provides a simple
204 // client side deduplication.
205 nsAutoString
finalName(u
"Telemetry.LateWriteFinal-"_ns
);
206 for (int i
= 0; i
< 20; ++i
) {
207 finalName
.AppendPrintf("%02x", sha1
[i
]);
209 RefPtr
<nsLocalFile
> file
= new nsLocalFile(nameAux
);
210 file
->RenameTo(nullptr, finalName
);
213 /******************************* Setup/Teardown *******************************/
215 static mozilla::StaticAutoPtr
<LateWriteObserver
> sLateWriteObserver
;
219 void InitLateWriteChecks() {
220 nsCOMPtr
<nsIFile
> mozFile
;
221 NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR
, getter_AddRefs(mozFile
));
223 PathString nativePath
= mozFile
->NativePath();
224 if (nativePath
.get()) {
225 sLateWriteObserver
= new LateWriteObserver(nativePath
.get());
230 void BeginLateWriteChecks() {
231 if (sLateWriteObserver
) {
232 IOInterposer::Register(IOInterposeObserver::OpWriteFSync
,
237 void StopLateWriteChecks() {
238 if (sLateWriteObserver
) {
239 IOInterposer::Unregister(IOInterposeObserver::OpAll
, sLateWriteObserver
);
240 // Deallocation would not be thread-safe, and StopLateWriteChecks() is
241 // called at shutdown and only in special cases.
242 // sLateWriteObserver = nullptr;
246 void PushSuspendLateWriteChecks() {
247 if (!tlsSuspendLateWriteChecks
.init()) {
250 tlsSuspendLateWriteChecks
.set(tlsSuspendLateWriteChecks
.get() + 1);
253 void PopSuspendLateWriteChecks() {
254 if (!tlsSuspendLateWriteChecks
.init()) {
257 int current
= tlsSuspendLateWriteChecks
.get();
258 MOZ_ASSERT(current
> 0);
259 tlsSuspendLateWriteChecks
.set(current
- 1);
262 } // namespace mozilla