2017-06-14 11:50:44 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 <stdio.h>
|
2018-07-17 13:09:16 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
# include <process.h>
|
|
|
|
# define getpid _getpid
|
|
|
|
#else
|
2017-11-10 14:50:14 +03:00
|
|
|
# include <signal.h>
|
2017-06-14 11:50:44 +03:00
|
|
|
# include <unistd.h>
|
2017-11-10 14:50:14 +03:00
|
|
|
#endif
|
2018-10-24 22:32:41 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h" // for AutoJSAPI
|
2017-06-14 11:50:44 +03:00
|
|
|
#include "mozilla/CodeCoverageHandler.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2017-08-16 21:39:26 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2017-06-14 11:50:44 +03:00
|
|
|
#include "nsAppRunner.h"
|
2018-10-24 22:32:41 +03:00
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "prtime.h"
|
2017-06-14 11:50:44 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2018-10-15 11:36:45 +03:00
|
|
|
// The __gcov_flush function writes the coverage counters to gcda files and then
|
|
|
|
// resets them to zero. It is defined at
|
|
|
|
// https://github.com/gcc-mirror/gcc/blob/aad93da1a579b9ae23ede6b9cf8523360f0a08b4/libgcc/libgcov-interface.c.
|
|
|
|
// __gcov_flush is protected by a mutex in GCC, but not in LLVM, so we are using
|
|
|
|
// a CrossProcessMutex to protect it.
|
2017-09-07 01:31:03 +03:00
|
|
|
|
2019-12-10 14:47:12 +03:00
|
|
|
// We rename __gcov_flush to __custom_llvm_gcov_flush in our build of LLVM for
|
|
|
|
// Linux, to avoid naming clashes in builds which mix GCC and LLVM. So, when we
|
|
|
|
// are building with LLVM exclusively, we need to use __custom_llvm_gcov_flush
|
|
|
|
// instead.
|
|
|
|
#if !defined(XP_WIN) && defined(__clang__)
|
|
|
|
# define __gcov_flush __custom_llvm_gcov_flush
|
|
|
|
#endif
|
|
|
|
|
2018-10-15 11:36:45 +03:00
|
|
|
extern "C" void __gcov_flush();
|
2017-09-07 01:31:03 +03:00
|
|
|
|
2017-06-14 11:50:44 +03:00
|
|
|
StaticAutoPtr<CodeCoverageHandler> CodeCoverageHandler::instance;
|
|
|
|
|
2018-10-15 11:36:45 +03:00
|
|
|
void CodeCoverageHandler::FlushCounters() {
|
|
|
|
printf_stderr("[CodeCoverage] Requested flush for %d.\n", getpid());
|
2018-07-17 11:05:45 +03:00
|
|
|
|
2017-06-14 11:50:44 +03:00
|
|
|
CrossProcessMutexAutoLock lock(*CodeCoverageHandler::Get()->GetMutex());
|
|
|
|
|
2018-10-15 11:36:45 +03:00
|
|
|
__gcov_flush();
|
2018-10-24 22:32:41 +03:00
|
|
|
|
2018-10-15 11:36:45 +03:00
|
|
|
printf_stderr("[CodeCoverage] flush completed.\n");
|
2018-10-24 22:32:41 +03:00
|
|
|
|
|
|
|
const char* outDir = getenv("JS_CODE_COVERAGE_OUTPUT_DIR");
|
|
|
|
if (!outDir || *outDir == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
size_t length;
|
2019-09-19 03:08:39 +03:00
|
|
|
JS::UniqueChars result = js::GetCodeCoverageSummaryAll(jsapi.cx(), &length);
|
2018-10-24 22:32:41 +03:00
|
|
|
if (!result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
|
|
|
|
nsresult rv = NS_NewNativeLocalFile(nsDependentCString(outDir), false,
|
|
|
|
getter_AddRefs(file));
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
rv = file->AppendNative(
|
|
|
|
nsPrintfCString("%lu-%d.info", PR_Now() / PR_USEC_PER_MSEC, getpid()));
|
|
|
|
|
|
|
|
rv = file->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0666);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> outputStream;
|
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), file);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
2019-09-19 03:08:39 +03:00
|
|
|
char* data = result.get();
|
2018-10-24 22:32:41 +03:00
|
|
|
while (length) {
|
|
|
|
uint32_t n = 0;
|
|
|
|
rv = outputStream->Write(data, length, &n);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
data += n;
|
|
|
|
length -= n;
|
|
|
|
}
|
|
|
|
|
2018-11-02 03:03:11 +03:00
|
|
|
rv = outputStream->Close();
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
2018-10-24 22:32:41 +03:00
|
|
|
printf_stderr("[CodeCoverage] JS flush completed.\n");
|
2017-06-14 11:50:44 +03:00
|
|
|
}
|
|
|
|
|
2018-10-15 11:36:45 +03:00
|
|
|
void CodeCoverageHandler::FlushCountersSignalHandler(int) { FlushCounters(); }
|
2018-06-22 16:31:59 +03:00
|
|
|
|
2017-06-14 11:50:44 +03:00
|
|
|
void CodeCoverageHandler::SetSignalHandlers() {
|
2017-11-10 14:50:14 +03:00
|
|
|
#ifndef XP_WIN
|
2017-06-14 11:50:44 +03:00
|
|
|
printf_stderr("[CodeCoverage] Setting handlers for process %d.\n", getpid());
|
|
|
|
|
|
|
|
struct sigaction dump_sa;
|
2018-10-15 11:36:45 +03:00
|
|
|
dump_sa.sa_handler = CodeCoverageHandler::FlushCountersSignalHandler;
|
2017-06-14 11:50:44 +03:00
|
|
|
dump_sa.sa_flags = SA_RESTART;
|
|
|
|
sigemptyset(&dump_sa.sa_mask);
|
2017-08-16 21:39:26 +03:00
|
|
|
DebugOnly<int> r1 = sigaction(SIGUSR1, &dump_sa, nullptr);
|
|
|
|
MOZ_ASSERT(r1 == 0, "Failed to install GCOV SIGUSR1 handler");
|
2017-11-10 14:50:14 +03:00
|
|
|
#endif
|
2017-06-14 11:50:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeCoverageHandler::CodeCoverageHandler() : mGcovLock("GcovLock") {
|
|
|
|
SetSignalHandlers();
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeCoverageHandler::CodeCoverageHandler(const CrossProcessMutexHandle& aHandle)
|
|
|
|
: mGcovLock(aHandle) {
|
|
|
|
SetSignalHandlers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCoverageHandler::Init() {
|
|
|
|
MOZ_ASSERT(!instance);
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
instance = new CodeCoverageHandler();
|
|
|
|
ClearOnShutdown(&instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCoverageHandler::Init(const CrossProcessMutexHandle& aHandle) {
|
|
|
|
MOZ_ASSERT(!instance);
|
|
|
|
MOZ_ASSERT(!XRE_IsParentProcess());
|
|
|
|
instance = new CodeCoverageHandler(aHandle);
|
|
|
|
ClearOnShutdown(&instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeCoverageHandler* CodeCoverageHandler::Get() {
|
|
|
|
MOZ_ASSERT(instance);
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
CrossProcessMutex* CodeCoverageHandler::GetMutex() { return &mGcovLock; }
|
|
|
|
|
|
|
|
CrossProcessMutexHandle CodeCoverageHandler::GetMutexHandle(int aProcId) {
|
|
|
|
return mGcovLock.ShareToProcess(aProcId);
|
|
|
|
}
|