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>
|
2017-11-10 14:50:14 +03:00
|
|
|
#ifndef XP_WIN
|
|
|
|
#include <signal.h>
|
2017-06-14 11:50:44 +03:00
|
|
|
#include <unistd.h>
|
2017-11-10 14:50:14 +03:00
|
|
|
#endif
|
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"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
// The __gcov_dump function writes the coverage counters to gcda files.
|
|
|
|
// The __gcov_reset function resets the coverage counters to zero.
|
2017-09-07 01:31:03 +03:00
|
|
|
// They are defined at https://github.com/gcc-mirror/gcc/blob/aad93da1a579b9ae23ede6b9cf8523360f0a08b4/libgcc/libgcov-interface.c.
|
2017-06-14 11:50:44 +03:00
|
|
|
// __gcov_flush is protected by a mutex, __gcov_dump and __gcov_reset aren't.
|
|
|
|
// So we are using a CrossProcessMutex to protect them.
|
|
|
|
|
2017-09-07 01:31:03 +03:00
|
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
2017-06-14 11:50:44 +03:00
|
|
|
extern "C" void __gcov_dump();
|
|
|
|
extern "C" void __gcov_reset();
|
|
|
|
|
2017-09-07 01:31:03 +03:00
|
|
|
void counters_dump() {
|
|
|
|
__gcov_dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
void counters_reset() {
|
|
|
|
__gcov_reset();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void counters_dump() {
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
void counters_reset() {
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-06-14 11:50:44 +03:00
|
|
|
StaticAutoPtr<CodeCoverageHandler> CodeCoverageHandler::instance;
|
|
|
|
|
2018-06-22 16:31:59 +03:00
|
|
|
void CodeCoverageHandler::DumpCounters()
|
2017-06-14 11:50:44 +03:00
|
|
|
{
|
2018-07-17 11:05:45 +03:00
|
|
|
printf_stderr("[CodeCoverage] Requested dump for %d.\n", getpid());
|
|
|
|
|
2017-06-14 11:50:44 +03:00
|
|
|
CrossProcessMutexAutoLock lock(*CodeCoverageHandler::Get()->GetMutex());
|
|
|
|
|
2017-09-07 01:31:03 +03:00
|
|
|
counters_dump();
|
2017-06-14 11:50:44 +03:00
|
|
|
printf_stderr("[CodeCoverage] Dump completed.\n");
|
|
|
|
}
|
|
|
|
|
2018-06-22 16:31:59 +03:00
|
|
|
void CodeCoverageHandler::DumpCountersSignalHandler(int)
|
|
|
|
{
|
|
|
|
DumpCounters();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCoverageHandler::ResetCounters()
|
2017-06-14 11:50:44 +03:00
|
|
|
{
|
2018-07-17 11:05:45 +03:00
|
|
|
printf_stderr("[CodeCoverage] Requested reset for %d.\n", getpid());
|
|
|
|
|
2017-06-14 11:50:44 +03:00
|
|
|
CrossProcessMutexAutoLock lock(*CodeCoverageHandler::Get()->GetMutex());
|
|
|
|
|
2017-09-07 01:31:03 +03:00
|
|
|
counters_reset();
|
2017-06-14 11:50:44 +03:00
|
|
|
printf_stderr("[CodeCoverage] Reset completed.\n");
|
|
|
|
}
|
|
|
|
|
2018-06-22 16:31:59 +03:00
|
|
|
void CodeCoverageHandler::ResetCountersSignalHandler(int)
|
|
|
|
{
|
|
|
|
ResetCounters();
|
|
|
|
}
|
|
|
|
|
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-06-22 16:31:59 +03:00
|
|
|
dump_sa.sa_handler = CodeCoverageHandler::DumpCountersSignalHandler;
|
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-06-14 11:50:44 +03:00
|
|
|
|
|
|
|
struct sigaction reset_sa;
|
2018-06-22 16:31:59 +03:00
|
|
|
reset_sa.sa_handler = CodeCoverageHandler::ResetCountersSignalHandler;
|
2017-06-14 11:50:44 +03:00
|
|
|
reset_sa.sa_flags = SA_RESTART;
|
|
|
|
sigemptyset(&reset_sa.sa_mask);
|
2017-08-16 21:39:26 +03:00
|
|
|
DebugOnly<int> r2 = sigaction(SIGUSR2, &reset_sa, nullptr);
|
|
|
|
MOZ_ASSERT(r2 == 0, "Failed to install GCOV SIGUSR2 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);
|
|
|
|
}
|