static FILE* OpenDumpFile(AudioStream* aStream) { if (!getenv("MOZ_DUMP_AUDIO")) return nullptr; char buf[100]; snprintf_literal(buf, "dumped-audio-%d.wav", gDumpedAudioCount); FILE* f = fopen(buf, "wb"); if (!f) return nullptr; ++gDumpedAudioCount; uint8_t header[] = { // RIFF header 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, // fmt chunk. We always write 16-bit samples. 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x10, 0x00, // data chunk 0x64, 0x61, 0x74, 0x61, 0xFE, 0xFF, 0xFF, 0x7F }; static const int CHANNEL_OFFSET = 22; static const int SAMPLE_RATE_OFFSET = 24; static const int BLOCK_ALIGN_OFFSET = 32; SetUint16LE(header + CHANNEL_OFFSET, aStream->GetChannels()); SetUint32LE(header + SAMPLE_RATE_OFFSET, aStream->GetRate()); SetUint16LE(header + BLOCK_ALIGN_OFFSET, aStream->GetChannels()*2); fwrite(header, sizeof(header), 1, f); return f; }
nsresult nsPosixLocale::GetXPLocale(const char* posixLocale, nsAString& locale) { char country_code[MAX_COUNTRY_CODE_LEN+1]; char lang_code[MAX_LANGUAGE_CODE_LEN+1]; char extra[MAX_EXTRA_LEN+1]; char posix_locale[MAX_LOCALE_LEN+1]; if (posixLocale!=nullptr) { if (strcmp(posixLocale,"C")==0 || strcmp(posixLocale,"POSIX")==0) { locale.AssignLiteral("en-US"); return NS_OK; } if (strcmp(posixLocale,"C.UTF-8")==0) { locale.AssignLiteral("en-US.UTF-8"); return NS_OK; } if (!ParseLocaleString(posixLocale,lang_code,country_code,extra,'_')) { // * locale = "x-user-defined"; // use posix if parse failed CopyASCIItoUTF16(nsDependentCString(posixLocale), locale); return NS_OK; } // Special case: substitute "nb" (Norwegian Bokmal) for macrolanguage // code "no" (Norwegian) if (nsDependentCString(lang_code).LowerCaseEqualsLiteral("no")) { lang_code[1] = 'b'; } if (*country_code) { snprintf_literal(posix_locale,"%s-%s",lang_code,country_code); } else { snprintf_literal(posix_locale,"%s",lang_code); } CopyASCIItoUTF16(nsDependentCString(posix_locale), locale); return NS_OK; } return NS_ERROR_FAILURE; }
void nsID::ToProvidedString(char (&aDest)[NSID_LENGTH]) const { snprintf_literal(aDest, gIDFormat, m0, (uint32_t)m1, (uint32_t)m2, (uint32_t)m3[0], (uint32_t)m3[1], (uint32_t)m3[2], (uint32_t)m3[3], (uint32_t)m3[4], (uint32_t)m3[5], (uint32_t)m3[6], (uint32_t)m3[7]); }
nsresult nsPosixLocale::GetPlatformLocale(const nsAString& locale, nsACString& posixLocale) { char country_code[MAX_COUNTRY_CODE_LEN+1]; char lang_code[MAX_LANGUAGE_CODE_LEN+1]; char extra[MAX_EXTRA_LEN+1]; char posix_locale[MAX_LOCALE_LEN+1]; NS_LossyConvertUTF16toASCII xp_locale(locale); if (!xp_locale.IsEmpty()) { if (!ParseLocaleString(xp_locale.get(),lang_code,country_code,extra,'-')) { // strncpy(posixLocale,"C",length); posixLocale = xp_locale; // use xp locale if parse failed return NS_OK; } if (*country_code) { if (*extra) { snprintf_literal(posix_locale,"%s_%s.%s",lang_code,country_code,extra); } else { snprintf_literal(posix_locale,"%s_%s",lang_code,country_code); } } else { if (*extra) { snprintf_literal(posix_locale,"%s.%s",lang_code,extra); } else { snprintf_literal(posix_locale,"%s",lang_code); } } posixLocale = posix_locale; return NS_OK; } return NS_ERROR_FAILURE; }
bool CrashReporterParent::GenerateChildData(const AnnotationTable* processNotes) { MOZ_ASSERT(mInitialized); if (mChildDumpID.IsEmpty()) { NS_WARNING("problem with GenerateChildData: no child dump id yet!"); return false; } nsAutoCString type; switch (mProcessType) { case GeckoProcessType_Content: type = NS_LITERAL_CSTRING("content"); break; case GeckoProcessType_Plugin: case GeckoProcessType_GMPlugin: type = NS_LITERAL_CSTRING("plugin"); break; default: NS_ERROR("unknown process type"); break; } mNotes.Put(NS_LITERAL_CSTRING("ProcessType"), type); char startTime[32]; snprintf_literal(startTime, "%lld", static_cast<long long>(mStartTime)); mNotes.Put(NS_LITERAL_CSTRING("StartupTime"), nsDependentCString(startTime)); if (!mAppNotes.IsEmpty()) { mNotes.Put(NS_LITERAL_CSTRING("Notes"), mAppNotes); } // Append these notes to the end of the extra file based on the current // dump id we obtained from CreatePairedMinidumps. bool ret = CrashReporter::AppendExtraData(mChildDumpID, mNotes); if (ret && processNotes) { ret = CrashReporter::AppendExtraData(mChildDumpID, *processNotes); } if (!ret) { NS_WARNING("problem appending child data to .extra"); } FinalizeChildData(); return ret; }
/** * Loads config from env vars if present. */ void Init() { bool shouldAppend = false; bool addTimestamp = false; bool isSync = false; const char* modules = PR_GetEnv("NSPR_LOG_MODULES"); NSPRLogModulesParser(modules, [&shouldAppend, &addTimestamp, &isSync] (const char* aName, LogLevel aLevel) mutable { if (strcmp(aName, "append") == 0) { shouldAppend = true; } else if (strcmp(aName, "timestamp") == 0) { addTimestamp = true; } else if (strcmp(aName, "sync") == 0) { isSync = true; } else { LogModule::Get(aName)->SetLevel(aLevel); } }); mAddTimestamp = addTimestamp; mIsSync = isSync; const char* logFile = PR_GetEnv("NSPR_LOG_FILE"); if (logFile && logFile[0]) { static const char kPIDToken[] = "%PID"; const char* pidTokenPtr = strstr(logFile, kPIDToken); char buf[2048]; if (pidTokenPtr && snprintf_literal(buf, "%.*s%d%s", static_cast<int>(pidTokenPtr - logFile), logFile, detail::log_pid(), pidTokenPtr + strlen(kPIDToken)) > 0) { logFile = buf; } mOutFile = fopen(logFile, shouldAppend ? "a" : "w"); } }
static FILE* OpenDumpFile(uint32_t aChannels, uint32_t aRate) { /** * When MOZ_DUMP_AUDIO is set in the environment (to anything), * we'll drop a series of files in the current working directory named * dumped-audio-<nnn>.wav, one per AudioStream created, containing * the audio for the stream including any skips due to underruns. */ static Atomic<int> gDumpedAudioCount(0); if (!getenv("MOZ_DUMP_AUDIO")) return nullptr; char buf[100]; snprintf_literal(buf, "dumped-audio-%d.wav", ++gDumpedAudioCount); FILE* f = fopen(buf, "wb"); if (!f) return nullptr; uint8_t header[] = { // RIFF header 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, // fmt chunk. We always write 16-bit samples. 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x10, 0x00, // data chunk 0x64, 0x61, 0x74, 0x61, 0xFE, 0xFF, 0xFF, 0x7F }; static const int CHANNEL_OFFSET = 22; static const int SAMPLE_RATE_OFFSET = 24; static const int BLOCK_ALIGN_OFFSET = 32; SetUint16LE(header + CHANNEL_OFFSET, aChannels); SetUint32LE(header + SAMPLE_RATE_OFFSET, aRate); SetUint16LE(header + BLOCK_ALIGN_OFFSET, aChannels * 2); fwrite(header, sizeof(header), 1, f); return f; }
const char* ThreadStackHelper::AppendJSEntry(const volatile StackEntry* aEntry, intptr_t& aAvailableBufferSize, const char* aPrevLabel) { // May be called from another thread or inside a signal handler. // We assume querying the script is safe but we must not manupulate it. // Also we must not allocate any memory from heap. MOZ_ASSERT(aEntry->isJs()); MOZ_ASSERT(aEntry->script()); const char* label; if (IsChromeJSScript(aEntry->script())) { const char* filename = JS_GetScriptFilename(aEntry->script()); const unsigned lineno = JS_PCToLineNumber(aEntry->script(), aEntry->pc()); MOZ_ASSERT(filename); char buffer[128]; // Enough to fit longest js file name from the tree // Some script names are in the form "foo -> bar -> baz". // Here we find the origin of these redirected scripts. const char* basename = GetPathAfterComponent(filename, " -> "); if (basename) { filename = basename; } basename = GetFullPathForScheme(filename, "chrome://"); if (!basename) { basename = GetFullPathForScheme(filename, "resource://"); } if (!basename) { // If the (add-on) script is located under the {profile}/extensions // directory, extract the path after the /extensions/ part. basename = GetPathAfterComponent(filename, "/extensions/"); } if (!basename) { // Only keep the file base name for paths outside the above formats. basename = strrchr(filename, '/'); basename = basename ? basename + 1 : filename; // Look for Windows path separator as well. filename = strrchr(basename, '\\'); if (filename) { basename = filename + 1; } } size_t len = snprintf_literal(buffer, "%s:%u", basename, lineno); if (len < sizeof(buffer)) { if (mStackToFill->IsSameAsEntry(aPrevLabel, buffer)) { return aPrevLabel; } // Keep track of the required buffer size aAvailableBufferSize -= (len + 1); if (aAvailableBufferSize >= 0) { // Buffer is big enough. return mStackToFill->InfallibleAppendViaBuffer(buffer, len); } // Buffer is not big enough; fall through to using static label below. } // snprintf failed or buffer is not big enough. label = "(chrome script)"; } else { label = "(content script)"; } if (mStackToFill->IsSameAsEntry(aPrevLabel, label)) { return aPrevLabel; } mStackToFill->infallibleAppend(label); return label; }
nsresult writeoutto(const char* i_pURL, char** o_Result, int32_t urlFactory = URL_FACTORY_DEFAULT) { if (!o_Result || !i_pURL) return NS_ERROR_FAILURE; *o_Result = 0; nsCOMPtr<nsIURI> pURL; nsresult result = NS_OK; switch (urlFactory) { case URL_FACTORY_STDURL: { nsIURI* url; result = CallCreateInstance(kStdURLCID, &url); if (NS_FAILED(result)) { printf("CreateInstance failed\n"); return NS_ERROR_FAILURE; } pURL = url; pURL->SetSpec(nsDependentCString(i_pURL)); break; } case URL_FACTORY_DEFAULT: { nsCOMPtr<nsIIOService> pService = do_GetService(kIOServiceCID, &result); if (NS_FAILED(result)) { printf("Service failed!\n"); return NS_ERROR_FAILURE; } result = pService->NewURI(nsDependentCString(i_pURL), nullptr, nullptr, getter_AddRefs(pURL)); } } nsCString output; if (NS_SUCCEEDED(result)) { nsCOMPtr<nsIURL> tURL = do_QueryInterface(pURL); nsAutoCString temp; int32_t port; nsresult rv; #define RESULT() NS_SUCCEEDED(rv) ? temp.get() : "" rv = tURL->GetScheme(temp); output += RESULT(); output += ','; rv = tURL->GetUsername(temp); output += RESULT(); output += ','; rv = tURL->GetPassword(temp); output += RESULT(); output += ','; rv = tURL->GetHost(temp); output += RESULT(); output += ','; rv = tURL->GetPort(&port); char portbuffer[40]; snprintf_literal(portbuffer, "%d", port); output.Append(portbuffer); output += ','; rv = tURL->GetDirectory(temp); output += RESULT(); output += ','; rv = tURL->GetFileBaseName(temp); output += RESULT(); output += ','; rv = tURL->GetFileExtension(temp); output += RESULT(); output += ','; // removed with https://bugzilla.mozilla.org/show_bug.cgi?id=665706 // rv = tURL->GetParam(temp); // output += RESULT(); output += ','; rv = tURL->GetQuery(temp); output += RESULT(); output += ','; rv = tURL->GetRef(temp); output += RESULT(); output += ','; rv = tURL->GetSpec(temp); output += RESULT(); *o_Result = ToNewCString(output); } else { output = "Can not create URL"; *o_Result = ToNewCString(output); } return NS_OK; }
void Print(const char* aName, LogLevel aLevel, const char* aFmt, va_list aArgs) { const size_t kBuffSize = 1024; char buff[kBuffSize]; char* buffToWrite = buff; // For backwards compat we need to use the NSPR format string versions // of sprintf and friends and then hand off to printf. va_list argsCopy; va_copy(argsCopy, aArgs); size_t charsWritten = PR_vsnprintf(buff, kBuffSize, aFmt, argsCopy); va_end(argsCopy); if (charsWritten == kBuffSize - 1) { // We may have maxed out, allocate a buffer instead. buffToWrite = PR_vsmprintf(aFmt, aArgs); charsWritten = strlen(buffToWrite); } // Determine if a newline needs to be appended to the message. const char* newline = ""; if (charsWritten == 0 || buffToWrite[charsWritten - 1] != '\n') { newline = "\n"; } FILE* out = mOutFile ? mOutFile : stderr; // This differs from the NSPR format in that we do not output the // opaque system specific thread pointer (ie pthread_t) cast // to a long. The address of the current PR_Thread continues to be // prefixed. // // Additionally we prefix the output with the abbreviated log level // and the module name. PRThread *currentThread = PR_GetCurrentThread(); const char *currentThreadName = (mMainThread == currentThread) ? "Main Thread" : PR_GetThreadName(currentThread); char noNameThread[40]; if (!currentThreadName) { snprintf_literal(noNameThread, "Unnamed thread %p", currentThread); currentThreadName = noNameThread; } if (!mAddTimestamp) { fprintf_stderr(out, "[%s]: %s/%s %s%s", currentThreadName, ToLogStr(aLevel), aName, buffToWrite, newline); } else { PRExplodedTime now; PR_ExplodeTime(PR_Now(), PR_GMTParameters, &now); fprintf_stderr( out, "%04d-%02d-%02d %02d:%02d:%02d.%06d UTC - [%s]: %s/%s %s%s", now.tm_year, now.tm_month + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, now.tm_usec, currentThreadName, ToLogStr(aLevel), aName, buffToWrite, newline); } if (mIsSync) { fflush(out); } if (buffToWrite != buff) { PR_smprintf_free(buffToWrite); } }