#include "src/libsampler/sampler.h"
#include "include/v8-isolate.h"
#include "include/v8-platform.h"
#include "include/v8-unwinder.h"
#ifdef USE_SIGNALS
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <sys/time.h>
#include <atomic>
#if !V8_OS_QNX && !V8_OS_AIX && !V8_OS_ZOS
#include <sys/syscall.h>
#endif
#if V8_OS_AIX || V8_TARGET_ARCH_S390X
#include "src/base/platform/time.h"
#elif V8_OS_DARWIN
#include <mach/mach.h>
#elif !V8_OS_OPENBSD
#include <ucontext.h>
#endif
#include <unistd.h>
#elif V8_OS_WIN || V8_OS_CYGWIN
#include <windows.h>
#elif V8_OS_FUCHSIA
#include <zircon/process.h>
#include <zircon/syscalls.h>
#include <zircon/syscalls/debug.h>
#include <zircon/types.h>
#if defined(ZX_THREAD_STATE_REGSET0)
#define ZX_THREAD_STATE_GENERAL_REGS …
zx_status_t zx_thread_read_state(zx_handle_t h, uint32_t k, void* b, size_t l) {
uint32_t dummy_out_len = 0;
return zx_thread_read_state(h, k, b, static_cast<uint32_t>(l),
&dummy_out_len);
}
#if defined(__x86_64__)
using zx_thread_state_general_regs_t = zx_x86_64_general_regs_t;
#else
using zx_thread_state_general_regs_t = zx_arm64_general_regs_t;
#endif
#endif
#endif
#include <algorithm>
#include <vector>
#include "src/base/atomic-utils.h"
#include "src/base/platform/platform.h"
#if V8_OS_ZOS
#include "edcwccwi.h"
#endif
#if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T)
#if defined(__arm__)
using mcontext_t = struct sigcontext;
struct ucontext_t {
uint32_t uc_flags;
struct ucontext* uc_link;
stack_t uc_stack;
mcontext_t uc_mcontext;
};
#elif defined(__aarch64__)
using mcontext_t = struct sigcontext;
struct ucontext_t {
uint64_t uc_flags;
struct ucontext* uc_link;
stack_t uc_stack;
mcontext_t uc_mcontext;
};
#elif defined(__mips__)
struct mcontext_t {
uint32_t regmask;
uint32_t status;
uint64_t pc;
uint64_t gregs[32];
uint64_t fpregs[32];
uint32_t acx;
uint32_t fpc_csr;
uint32_t fpc_eir;
uint32_t used_math;
uint32_t dsp;
uint64_t mdhi;
uint64_t mdlo;
uint32_t hi1;
uint32_t lo1;
uint32_t hi2;
uint32_t lo2;
uint32_t hi3;
uint32_t lo3;
};
struct ucontext_t {
uint32_t uc_flags;
struct ucontext* uc_link;
stack_t uc_stack;
mcontext_t uc_mcontext;
};
#elif defined(__i386__)
struct mcontext_t {
uint32_t gregs[19];
void* fpregs;
uint32_t oldmask;
uint32_t cr2;
};
using kernel_sigset_t = uint32_t[2];
struct ucontext_t {
uint32_t uc_flags;
struct ucontext* uc_link;
stack_t uc_stack;
mcontext_t uc_mcontext;
};
enum { REG_EBP = 6, REG_ESP = 7, REG_EIP = 14 };
#elif defined(__x86_64__)
struct mcontext_t {
uint64_t gregs[23];
void* fpregs;
uint64_t __reserved1[8];
};
struct ucontext_t {
uint64_t uc_flags;
struct ucontext* uc_link;
stack_t uc_stack;
mcontext_t uc_mcontext;
};
enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 };
#endif
#endif
namespace v8 {
namespace sampler {
#if defined(USE_SIGNALS)
AtomicGuard::AtomicGuard(AtomicMutex* atomic, bool is_blocking)
: … { … }
AtomicGuard::~AtomicGuard() { … }
bool AtomicGuard::is_success() const { … }
class Sampler::PlatformData { … };
void SamplerManager::AddSampler(Sampler* sampler) { … }
void SamplerManager::RemoveSampler(Sampler* sampler) { … }
void SamplerManager::DoSample(const v8::RegisterState& state) { … }
SamplerManager* SamplerManager::instance() { … }
#elif V8_OS_WIN || V8_OS_CYGWIN
class Sampler::PlatformData {
public:
PlatformData() {
HANDLE current_process = GetCurrentProcess();
BOOL result = DuplicateHandle(
current_process, GetCurrentThread(), current_process, &profiled_thread_,
THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION,
FALSE, 0);
DCHECK(result);
USE(result);
}
~PlatformData() {
if (profiled_thread_ != nullptr) {
CloseHandle(profiled_thread_);
profiled_thread_ = nullptr;
}
}
HANDLE profiled_thread() { return profiled_thread_; }
private:
HANDLE profiled_thread_;
};
#elif V8_OS_FUCHSIA
class Sampler::PlatformData {
public:
PlatformData() {
zx_handle_duplicate(zx_thread_self(), ZX_RIGHT_SAME_RIGHTS,
&profiled_thread_);
}
~PlatformData() {
if (profiled_thread_ != ZX_HANDLE_INVALID) {
zx_handle_close(profiled_thread_);
profiled_thread_ = ZX_HANDLE_INVALID;
}
}
zx_handle_t profiled_thread() { return profiled_thread_; }
private:
zx_handle_t profiled_thread_ = ZX_HANDLE_INVALID;
};
#endif
#if defined(USE_SIGNALS)
class SignalHandler { … };
base::LazyRecursiveMutex SignalHandler::mutex_ = …;
int SignalHandler::client_count_ = …;
struct sigaction SignalHandler::old_signal_handler_;
bool SignalHandler::signal_handler_installed_ = …;
void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info,
void* context) { … }
void SignalHandler::FillRegisterState(void* context, RegisterState* state) { … }
#endif
Sampler::Sampler(Isolate* isolate)
: … { … }
Sampler::~Sampler() { … }
void Sampler::Start() { … }
void Sampler::Stop() { … }
#if defined(USE_SIGNALS)
void Sampler::DoSample() { … }
#elif V8_OS_WIN || V8_OS_CYGWIN
void Sampler::DoSample() {
HANDLE profiled_thread = platform_data()->profiled_thread();
if (profiled_thread == nullptr) return;
const DWORD kSuspendFailed = static_cast<DWORD>(-1);
if (SuspendThread(profiled_thread) == kSuspendFailed) return;
CONTEXT context;
memset(&context, 0, sizeof(context));
context.ContextFlags = CONTEXT_FULL;
if (GetThreadContext(profiled_thread, &context) != 0) {
v8::RegisterState state;
#if V8_HOST_ARCH_X64
state.pc = reinterpret_cast<void*>(context.Rip);
state.sp = reinterpret_cast<void*>(context.Rsp);
state.fp = reinterpret_cast<void*>(context.Rbp);
#elif V8_HOST_ARCH_ARM64
state.pc = reinterpret_cast<void*>(context.Pc);
state.sp = reinterpret_cast<void*>(context.Sp);
state.fp = reinterpret_cast<void*>(context.Fp);
#else
state.pc = reinterpret_cast<void*>(context.Eip);
state.sp = reinterpret_cast<void*>(context.Esp);
state.fp = reinterpret_cast<void*>(context.Ebp);
#endif
SampleStack(state);
}
ResumeThread(profiled_thread);
}
#elif V8_OS_FUCHSIA
void Sampler::DoSample() {
zx_handle_t profiled_thread = platform_data()->profiled_thread();
if (profiled_thread == ZX_HANDLE_INVALID) return;
zx_handle_t suspend_token = ZX_HANDLE_INVALID;
if (zx_task_suspend_token(profiled_thread, &suspend_token) != ZX_OK) return;
zx_signals_t signals = 0;
zx_status_t suspended = zx_object_wait_one(
profiled_thread, ZX_THREAD_SUSPENDED | ZX_THREAD_TERMINATED,
zx_deadline_after(ZX_MSEC(100)), &signals);
if (suspended != ZX_OK || (signals & ZX_THREAD_SUSPENDED) == 0) {
zx_handle_close(suspend_token);
return;
}
zx_thread_state_general_regs_t thread_state = {};
if (zx_thread_read_state(profiled_thread, ZX_THREAD_STATE_GENERAL_REGS,
&thread_state, sizeof(thread_state)) == ZX_OK) {
v8::RegisterState state;
#if V8_HOST_ARCH_X64
state.pc = reinterpret_cast<void*>(thread_state.rip);
state.sp = reinterpret_cast<void*>(thread_state.rsp);
state.fp = reinterpret_cast<void*>(thread_state.rbp);
#elif V8_HOST_ARCH_ARM64
state.pc = reinterpret_cast<void*>(thread_state.pc);
state.sp = reinterpret_cast<void*>(thread_state.sp);
state.fp = reinterpret_cast<void*>(thread_state.r[29]);
#endif
SampleStack(state);
}
zx_handle_close(suspend_token);
}
#if defined(ZX_THREAD_STATE_REGSET0)
#undef ZX_THREAD_STATE_GENERAL_REGS
#endif
#endif
}
}