void InitWritePoisoning() { // Stdout and Stderr are OK. MozillaRegisterDebugFD(1); MozillaRegisterDebugFD(2); 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)) { sProfileDirectory = PL_strdup(nativePath.get()); } } }
void InitPoisonIOInterposer() { // Enable reporting from poisoned write methods sIsEnabled = true; // Make sure we only poison writes once! static bool WritesArePoisoned = false; if (WritesArePoisoned) { return; } WritesArePoisoned = true; // stdout and stderr are OK. MozillaRegisterDebugFD(1); MozillaRegisterDebugFD(2); #ifdef MOZ_REPLACE_MALLOC // The contract with InitDebugFd is that the given registry can be used // at any moment, so the instance needs to persist longer than the scope // of this functions. static DebugFdRegistry registry; ReplaceMalloc::InitDebugFd(registry); #endif for (int i = 0; i < NumFunctions; ++i) { FuncData* d = Functions[i]; if (!d->Function) { d->Function = dlsym(RTLD_DEFAULT, d->Name); } if (!d->Function) { continue; } DebugOnly<mach_error_t> t = mach_override_ptr(d->Function, d->Wrapper, &d->Buffer); MOZ_ASSERT(t == err_none); } }
static bool InitLog(const char* aEnvVar, const char* aMsg, FILE** aResult) { const char* value = getenv(aEnvVar); if (value) { if (nsCRT::strcmp(value, "1") == 0) { *aResult = stdout; fprintf(stdout, "### %s defined -- logging %s to stdout\n", aEnvVar, aMsg); return true; } else if (nsCRT::strcmp(value, "2") == 0) { *aResult = stderr; fprintf(stdout, "### %s defined -- logging %s to stderr\n", aEnvVar, aMsg); return true; } else { FILE* stream; nsAutoCString fname(value); if (!XRE_IsParentProcess()) { bool hasLogExtension = fname.RFind(".log", true, -1, 4) == kNotFound ? false : true; if (hasLogExtension) { fname.Cut(fname.Length() - 4, 4); } fname.Append('_'); fname.Append((char*)XRE_ChildProcessTypeToString(XRE_GetProcessType())); fname.AppendLiteral("_pid"); fname.AppendInt((uint32_t)getpid()); if (hasLogExtension) { fname.AppendLiteral(".log"); } } stream = ::fopen(fname.get(), "w" FOPEN_NO_INHERIT); if (stream) { MozillaRegisterDebugFD(fileno(stream)); *aResult = stream; fprintf(stdout, "### %s defined -- logging %s to %s\n", aEnvVar, aMsg, fname.get()); } else { fprintf(stdout, "### %s defined -- unable to log %s to %s\n", aEnvVar, aMsg, fname.get()); MOZ_ASSERT(false, "Tried and failed to create an XPCOM log"); } return stream != nullptr; } } return false; }
static bool InitLog(const char* envVar, const char* msg, FILE* *result) { const char* value = getenv(envVar); if (value) { if (nsCRT::strcmp(value, "1") == 0) { *result = stdout; fprintf(stdout, "### %s defined -- logging %s to stdout\n", envVar, msg); return true; } else if (nsCRT::strcmp(value, "2") == 0) { *result = stderr; fprintf(stdout, "### %s defined -- logging %s to stderr\n", envVar, msg); return true; } else { FILE *stream; nsAutoCString fname(value); if (XRE_GetProcessType() != GeckoProcessType_Default) { bool hasLogExtension = fname.RFind(".log", true, -1, 4) == kNotFound ? false : true; if (hasLogExtension) fname.Cut(fname.Length() - 4, 4); fname.AppendLiteral("_"); fname.Append((char*)XRE_ChildProcessTypeToString(XRE_GetProcessType())); fname.AppendLiteral("_pid"); fname.AppendInt((uint32_t)getpid()); if (hasLogExtension) fname.AppendLiteral(".log"); } stream = ::fopen(fname.get(), "w" FOPEN_NO_INHERIT); if (stream != NULL) { MozillaRegisterDebugFD(fileno(stream)); *result = stream; fprintf(stdout, "### %s defined -- logging %s to %s\n", envVar, msg, fname.get()); } else { fprintf(stdout, "### %s defined -- unable to log %s to %s\n", envVar, msg, fname.get()); } return stream != NULL; } } return false; }