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"
25 # define NS_SLASH "\\"
30 # include <sys/stat.h>
36 #include "LateWriteChecks.h"
38 /*************************** Auxiliary Declarations ***************************/
40 static MOZ_THREAD_LOCAL(int) tlsSuspendLateWriteChecks
;
42 bool SuspendingLateWriteChecksForCurrentThread() {
43 if (!tlsSuspendLateWriteChecks
.init()) {
46 return tlsSuspendLateWriteChecks
.get() > 0;
49 // This a wrapper over a file descriptor that provides a Printf method and
50 // computes the sha1 of the data that passes through it.
53 explicit SHA1Stream(FILE* aStream
) : mFile(aStream
) {
54 MozillaRegisterDebugFILE(mFile
);
57 void Printf(const char* aFormat
, ...) MOZ_FORMAT_PRINTF(2, 3) {
60 va_start(list
, aFormat
);
62 str
.AppendVprintf(aFormat
, list
);
64 mSHA1
.update(str
.get(), str
.Length());
65 mozilla::Unused
<< fwrite(str
.get(), 1, str
.Length(), mFile
);
67 void Finish(mozilla::SHA1Sum::Hash
& aHash
) {
68 int fd
= fileno(mFile
);
70 MozillaUnRegisterDebugFD(fd
);
78 mozilla::SHA1Sum mSHA1
;
81 static void RecordStackWalker(uint32_t aFrameNumber
, void* aPC
, void* aSP
,
83 std::vector
<uintptr_t>* stack
=
84 static_cast<std::vector
<uintptr_t>*>(aClosure
);
85 stack
->push_back(reinterpret_cast<uintptr_t>(aPC
));
88 /**************************** Late-Write Observer ****************************/
91 * An implementation of IOInterposeObserver to be registered with IOInterposer.
92 * This observer logs all writes as late writes.
94 class LateWriteObserver final
: public mozilla::IOInterposeObserver
{
95 using char_type
= mozilla::filesystem::Path::value_type
;
98 explicit LateWriteObserver(const char_type
* aProfileDirectory
)
99 : mProfileDirectory(NS_xstrdup(aProfileDirectory
)) {}
100 ~LateWriteObserver() {
101 free(mProfileDirectory
);
102 mProfileDirectory
= nullptr;
106 mozilla::IOInterposeObserver::Observation
& aObservation
) override
;
109 char_type
* mProfileDirectory
;
112 void LateWriteObserver::Observe(
113 mozilla::IOInterposeObserver::Observation
& aOb
) {
114 if (SuspendingLateWriteChecksForCurrentThread()) {
122 // If we can't record then abort
123 if (!mozilla::Telemetry::CanRecordExtended()) {
127 // Write the stack and loaded libraries to a file. We can get here
128 // concurrently from many writes, so we use multiple temporary files.
129 std::vector
<uintptr_t> rawStack
;
131 MozStackWalk(RecordStackWalker
, nullptr, /* maxFrames */ 0, &rawStack
);
132 mozilla::Telemetry::ProcessedStack stack
=
133 mozilla::Telemetry::GetStackAndModules(rawStack
);
135 nsTAutoString
<char_type
> nameAux(mProfileDirectory
);
136 nameAux
.AppendLiteral(NS_SLASH
"Telemetry.LateWriteTmpXXXXXX");
137 char_type
* name
= nameAux
.BeginWriting();
139 // We want the sha1 of the entire file, so please don't write to fd
140 // directly; use sha1Stream.
145 // mkstemp isn't supported so keep trying until we get a file
146 _wmktemp_s(char16ptr_t(name
), NS_strlen(name
) + 1);
147 hFile
= CreateFileW(char16ptr_t(name
), GENERIC_WRITE
, 0, nullptr,
148 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, nullptr);
149 } while (GetLastError() == ERROR_FILE_EXISTS
);
151 if (hFile
== INVALID_HANDLE_VALUE
) {
152 MOZ_CRASH("Um, how did we get here?");
155 // http://support.microsoft.com/kb/139640
156 int fd
= _open_osfhandle((intptr_t)hFile
, _O_APPEND
);
158 MOZ_CRASH("Um, how did we get here?");
161 stream
= _fdopen(fd
, "w");
163 int fd
= mkstemp(name
);
165 MOZ_CRASH("mkstemp failed");
167 stream
= fdopen(fd
, "w");
170 SHA1Stream
sha1Stream(stream
);
172 size_t numModules
= stack
.GetNumModules();
173 sha1Stream
.Printf("%u\n", (unsigned)numModules
);
174 for (size_t i
= 0; i
< numModules
; ++i
) {
175 mozilla::Telemetry::ProcessedStack::Module module
= stack
.GetModule(i
);
176 sha1Stream
.Printf("%s %s\n", module
.mBreakpadId
.get(),
177 NS_ConvertUTF16toUTF8(module
.mName
).get());
180 size_t numFrames
= stack
.GetStackSize();
181 sha1Stream
.Printf("%u\n", (unsigned)numFrames
);
182 for (size_t i
= 0; i
< numFrames
; ++i
) {
183 const mozilla::Telemetry::ProcessedStack::Frame
& frame
= stack
.GetFrame(i
);
184 // NOTE: We write the offsets, while the atos tool expects a value with
185 // the virtual address added. For example, running otool -l on the the
186 // firefox binary shows
190 // vmaddr 0x0000000100000000
191 // so to print the line matching the offset 123 one has to run
192 // atos -o firefox 0x100000123.
193 sha1Stream
.Printf("%d %x\n", frame
.mModIndex
, (unsigned)frame
.mOffset
);
196 mozilla::SHA1Sum::Hash sha1
;
197 sha1Stream
.Finish(sha1
);
199 // Note: These files should be deleted by telemetry once it reads them. If
200 // there were no telemetry runs by the time we shut down, we just add files
201 // to the existing ones instead of replacing them. Given that each of these
202 // files is a bug to be fixed, that is probably the right thing to do.
204 // We append the sha1 of the contents to the file name. This provides a simple
205 // client side deduplication.
206 nsAutoString
finalName(u
"Telemetry.LateWriteFinal-"_ns
);
207 for (int i
= 0; i
< 20; ++i
) {
208 finalName
.AppendPrintf("%02x", sha1
[i
]);
210 RefPtr
<nsLocalFile
> file
= new nsLocalFile(nameAux
);
211 file
->RenameTo(nullptr, finalName
);
214 /******************************* Setup/Teardown *******************************/
216 static mozilla::StaticAutoPtr
<LateWriteObserver
> sLateWriteObserver
;
220 void InitLateWriteChecks() {
221 nsCOMPtr
<nsIFile
> mozFile
;
222 NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR
, getter_AddRefs(mozFile
));
224 PathString nativePath
= mozFile
->NativePath();
225 if (nativePath
.get()) {
226 sLateWriteObserver
= new LateWriteObserver(nativePath
.get());
231 void BeginLateWriteChecks() {
232 if (sLateWriteObserver
) {
233 IOInterposer::Register(IOInterposeObserver::OpWriteFSync
,
238 void StopLateWriteChecks() {
239 if (sLateWriteObserver
) {
240 IOInterposer::Unregister(IOInterposeObserver::OpAll
, sLateWriteObserver
);
241 // Deallocation would not be thread-safe, and StopLateWriteChecks() is
242 // called at shutdown and only in special cases.
243 // sLateWriteObserver = nullptr;
247 void PushSuspendLateWriteChecks() {
248 if (!tlsSuspendLateWriteChecks
.init()) {
251 tlsSuspendLateWriteChecks
.set(tlsSuspendLateWriteChecks
.get() + 1);
254 void PopSuspendLateWriteChecks() {
255 if (!tlsSuspendLateWriteChecks
.init()) {
258 int current
= tlsSuspendLateWriteChecks
.get();
259 MOZ_ASSERT(current
> 0);
260 tlsSuspendLateWriteChecks
.set(current
- 1);
263 } // namespace mozilla