Files
palemoon27/xpcom/build/LateWriteChecks.cpp
roytam1 83aaad87df import changes from `dev' branch of rmottola/Arctic-Fox:
- bug 1191326 - always initialize ProxyAccessible::mOuterDoc r=lsocks (74ed8d596)
- Bug 1156062 part 1a - New nsEditor::SplitNodeDeep variant; r=ehsan (a80d26ece)
- minor fix (c1e5c74e3)
- Bug 1172216 - Move nsStackwalk to mozglue. r=glandium (971014ffb)
- Bug 1156903: Add quirk flag that causes NPN_GetValue(NPNVdocumentOrigin) to return an empty string even when it fails; r=jimm (9508b57b1)
- Bug 554178 - Remove unused member variable PluginModuleChild::mUserAgent. r=jimm (a6fda391a)
- Bug 1203428 - E10S for device storage API r=cyu (da575f819)
- Bug 1150642 - Make mozilla_sampler_save_profile_to_file callable from lldb in Nightly builds. r=jrmuizel (bb98fafd6)
- Bug 1136834 - Stop leaking markers in ProfileBuffer. (r=mstange) (b2f5f813a)
- Bug 1148069 - Ensure synchronous sampling does not set JitcodeGlobalEntry's generation. (r=djvj) (f5a4dd6a4)
- Bug 1181348 - Fix ARM64 toggledCall() under debug mode. r=djvj (4bbbe51a4)
- Bug 1181354 - Account for initaliasedlexical in this one weird const cutout in jit::SetProperty. (r=jandem) (472179ea2)
- Bug 1181558 part 0 - Remove unused SnapshotIterator constructor. r=jandem (cae21907a)
- Bug 1181558 part 1 - Share the machine state between all SnapshotIterators of the same InlineFrameIterator. r=jandem (49e53a014)
- Bug 1177922 - Fix a bogus assert on OOM in markSafepointAt. r=nbp (cf26143e7)
- Bug 1182060 - IsObjectEscaped: Handle UnboxedPlainObject in guard shape. r=bhackett (35b6c285a)
- Bug 1183051: Fix register allocations of Atomics callouts on arm vfp; r=h4writer (42d708374)
- Bug 1138693 - Add comments and test. r=jandem (9619e8053)
- Bug 1138693 - Add an early quit to the test if TypedObject isn't enabled. r=nbp (f6b04026e)
- Bug 1180184 - Support JSOP_TOSTRING used by template strings in baseline JIT. r=jandem (8215c953b)
2021-09-28 21:57:21 +08:00

257 lines
6.9 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <algorithm>
#include "mozilla/IOInterposer.h"
#include "mozilla/PoisonIOInterposer.h"
#include "mozilla/ProcessedStack.h"
#include "mozilla/SHA1.h"
#include "mozilla/Scoped.h"
#include "mozilla/StaticPtr.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "nsPrintfCString.h"
#include "mozilla/StackWalk.h"
#include "plstr.h"
#include "prio.h"
#ifdef XP_WIN
#define NS_T(str) L ## str
#define NS_SLASH "\\"
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <windows.h>
#else
#define NS_SLASH "/"
#endif
#include "LateWriteChecks.h"
#if defined(MOZ_STACKWALKING)
#define OBSERVE_LATE_WRITES
#endif
using namespace mozilla;
/*************************** Auxiliary Declarations ***************************/
// This a wrapper over a file descriptor that provides a Printf method and
// computes the sha1 of the data that passes through it.
class SHA1Stream
{
public:
explicit SHA1Stream(FILE* aStream)
: mFile(aStream)
{
MozillaRegisterDebugFILE(mFile);
}
void Printf(const char* aFormat, ...)
{
MOZ_ASSERT(mFile);
va_list list;
va_start(list, aFormat);
nsAutoCString str;
str.AppendPrintf(aFormat, list);
va_end(list);
mSHA1.update(str.get(), str.Length());
fwrite(str.get(), 1, str.Length(), mFile);
}
void Finish(SHA1Sum::Hash& aHash)
{
int fd = fileno(mFile);
fflush(mFile);
MozillaUnRegisterDebugFD(fd);
fclose(mFile);
mSHA1.finish(aHash);
mFile = nullptr;
}
private:
FILE* mFile;
SHA1Sum mSHA1;
};
static void
RecordStackWalker(uint32_t aFrameNumber, void* aPC, void* aSP, void* aClosure)
{
std::vector<uintptr_t>* stack =
static_cast<std::vector<uintptr_t>*>(aClosure);
stack->push_back(reinterpret_cast<uintptr_t>(aPC));
}
/**************************** Late-Write Observer ****************************/
/**
* An implementation of IOInterposeObserver to be registered with IOInterposer.
* This observer logs all writes as late writes.
*/
class LateWriteObserver final : public IOInterposeObserver
{
public:
explicit LateWriteObserver(const char* aProfileDirectory)
: mProfileDirectory(PL_strdup(aProfileDirectory))
{
}
~LateWriteObserver()
{
PL_strfree(mProfileDirectory);
mProfileDirectory = nullptr;
}
void Observe(IOInterposeObserver::Observation& aObservation);
private:
char* mProfileDirectory;
};
void
LateWriteObserver::Observe(IOInterposeObserver::Observation& aOb)
{
#ifdef OBSERVE_LATE_WRITES
// Crash if that is the shutdown check mode
if (gShutdownChecks == SCM_CRASH) {
MOZ_CRASH();
}
// If we have shutdown mode SCM_NOTHING or we can't record then abort
if (gShutdownChecks == SCM_NOTHING) {
return;
}
// Write the stack and loaded libraries to a file. We can get here
// concurrently from many writes, so we use multiple temporary files.
std::vector<uintptr_t> rawStack;
MozStackWalk(RecordStackWalker, /* skipFrames */ 0, /* maxFrames */ 0,
reinterpret_cast<void*>(&rawStack), 0, nullptr);
nsPrintfCString nameAux("%s%s%s", mProfileDirectory,
NS_SLASH, "Telemetry.LateWriteTmpXXXXXX");
char* name;
nameAux.GetMutableData(&name);
// We want the sha1 of the entire file, so please don't write to fd
// directly; use sha1Stream.
FILE* stream;
#ifdef XP_WIN
HANDLE hFile;
do {
// mkstemp isn't supported so keep trying until we get a file
int result = _mktemp_s(name, strlen(name) + 1);
hFile = CreateFileA(name, GENERIC_WRITE, 0, nullptr, CREATE_NEW,
FILE_ATTRIBUTE_NORMAL, nullptr);
} while (GetLastError() == ERROR_FILE_EXISTS);
if (hFile == INVALID_HANDLE_VALUE) {
NS_RUNTIMEABORT("Um, how did we get here?");
}
// http://support.microsoft.com/kb/139640
int fd = _open_osfhandle((intptr_t)hFile, _O_APPEND);
if (fd == -1) {
NS_RUNTIMEABORT("Um, how did we get here?");
}
stream = _fdopen(fd, "w");
#else
int fd = mkstemp(name);
stream = fdopen(fd, "w");
#endif
SHA1Stream sha1Stream(stream);
size_t numModules = stack.GetNumModules();
sha1Stream.Printf("%u\n", (unsigned)numModules);
for (size_t i = 0; i < numModules; ++i) {
sha1Stream.Printf("%s %s\n", module.mBreakpadId.c_str(),
module.mName.c_str());
}
size_t numFrames = stack.GetStackSize();
sha1Stream.Printf("%u\n", (unsigned)numFrames);
for (size_t i = 0; i < numFrames; ++i) {
// NOTE: We write the offsets, while the atos tool expects a value with
// the virtual address added. For example, running otool -l on the the firefox
// binary shows
// cmd LC_SEGMENT_64
// cmdsize 632
// segname __TEXT
// vmaddr 0x0000000100000000
// so to print the line matching the offset 123 one has to run
// atos -o firefox 0x100000123.
sha1Stream.Printf("%d %x\n", frame.mModIndex, (unsigned)frame.mOffset);
}
SHA1Sum::Hash sha1;
sha1Stream.Finish(sha1);
// Note: These files should be deleted by telemetry once it reads them. If
// there were no telemetry runs by the time we shut down, we just add files
// to the existing ones instead of replacing them. Given that each of these
// files is a bug to be fixed, that is probably the right thing to do.
// We append the sha1 of the contents to the file name. This provides a simple
// client side deduplication.
nsPrintfCString finalName("%s%s", mProfileDirectory,
"/Telemetry.LateWriteFinal-");
for (int i = 0; i < 20; ++i) {
finalName.AppendPrintf("%02x", sha1[i]);
}
PR_Delete(finalName.get());
PR_Rename(name, finalName.get());
#endif
}
/******************************* Setup/Teardown *******************************/
static StaticAutoPtr<LateWriteObserver> sLateWriteObserver;
namespace mozilla {
void
InitLateWriteChecks()
{
nsCOMPtr<nsIFile> mozFile;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(mozFile));
if (mozFile) {
nsAutoCString nativePath;
nsresult rv = mozFile->GetNativePath(nativePath);
if (NS_SUCCEEDED(rv) && nativePath.get()) {
sLateWriteObserver = new LateWriteObserver(nativePath.get());
}
}
}
void
BeginLateWriteChecks()
{
if (sLateWriteObserver) {
IOInterposer::Register(
IOInterposeObserver::OpWriteFSync,
sLateWriteObserver
);
}
}
void
StopLateWriteChecks()
{
if (sLateWriteObserver) {
IOInterposer::Unregister(
IOInterposeObserver::OpAll,
sLateWriteObserver
);
// Deallocation would not be thread-safe, and StopLateWriteChecks() is
// called at shutdown and only in special cases.
// sLateWriteObserver = nullptr;
}
}
} // namespace mozilla