Bug 1932613 - temporarily disable browser_ml_end_to_end.js for permanent failures...
[gecko.git] / xpcom / build / LateWriteChecks.cpp
blob515b484aef3b27beae86be346518be313c0092b3
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/. */
7 #include <algorithm>
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"
21 #include "prio.h"
23 #ifdef XP_WIN
24 # define NS_SLASH "\\"
25 # include <fcntl.h>
26 # include <io.h>
27 # include <stdio.h>
28 # include <stdlib.h>
29 # include <sys/stat.h>
30 # include <windows.h>
31 #else
32 # define NS_SLASH "/"
33 #endif
35 #include "LateWriteChecks.h"
37 /*************************** Auxiliary Declarations ***************************/
39 static MOZ_THREAD_LOCAL(int) tlsSuspendLateWriteChecks;
41 bool SuspendingLateWriteChecksForCurrentThread() {
42 if (!tlsSuspendLateWriteChecks.init()) {
43 return true;
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.
50 class SHA1Stream {
51 public:
52 explicit SHA1Stream(FILE* aStream) : mFile(aStream) {
53 MozillaRegisterDebugFILE(mFile);
56 void Printf(const char* aFormat, ...) MOZ_FORMAT_PRINTF(2, 3) {
57 MOZ_ASSERT(mFile);
58 va_list list;
59 va_start(list, aFormat);
60 nsAutoCString str;
61 str.AppendVprintf(aFormat, list);
62 va_end(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);
68 fflush(mFile);
69 MozillaUnRegisterDebugFD(fd);
70 fclose(mFile);
71 mSHA1.finish(aHash);
72 mFile = nullptr;
75 private:
76 FILE* mFile;
77 mozilla::SHA1Sum mSHA1;
80 static void RecordStackWalker(uint32_t aFrameNumber, void* aPC, void* aSP,
81 void* aClosure) {
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 ****************************/
89 /**
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;
96 public:
97 explicit LateWriteObserver(const char_type* aProfileDirectory)
98 : mProfileDirectory(NS_xstrdup(aProfileDirectory)) {}
99 ~LateWriteObserver() {
100 free(mProfileDirectory);
101 mProfileDirectory = nullptr;
104 void Observe(
105 mozilla::IOInterposeObserver::Observation& aObservation) override;
107 private:
108 char_type* mProfileDirectory;
111 void LateWriteObserver::Observe(
112 mozilla::IOInterposeObserver::Observation& aOb) {
113 if (SuspendingLateWriteChecksForCurrentThread()) {
114 return;
117 #ifdef DEBUG
118 MOZ_CRASH();
119 #endif
121 // If we can't record then abort
122 if (!mozilla::Telemetry::CanRecordExtended()) {
123 return;
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.
140 FILE* stream;
141 #ifdef XP_WIN
142 HANDLE hFile;
143 do {
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);
156 if (fd == -1) {
157 MOZ_CRASH("Um, how did we get here?");
160 stream = _fdopen(fd, "w");
161 #else
162 int fd = mkstemp(name);
163 if (fd == -1) {
164 MOZ_CRASH("mkstemp failed");
166 stream = fdopen(fd, "w");
167 #endif
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
186 // cmd LC_SEGMENT_64
187 // cmdsize 632
188 // segname __TEXT
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;
217 namespace mozilla {
219 void InitLateWriteChecks() {
220 nsCOMPtr<nsIFile> mozFile;
221 NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(mozFile));
222 if (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,
233 sLateWriteObserver);
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()) {
248 return;
250 tlsSuspendLateWriteChecks.set(tlsSuspendLateWriteChecks.get() + 1);
253 void PopSuspendLateWriteChecks() {
254 if (!tlsSuspendLateWriteChecks.init()) {
255 return;
257 int current = tlsSuspendLateWriteChecks.get();
258 MOZ_ASSERT(current > 0);
259 tlsSuspendLateWriteChecks.set(current - 1);
262 } // namespace mozilla