Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable FEATURE_PERFMAP on OSX, and update perfjitdump.cpp to work on OSX #99986

Merged
merged 15 commits into from
Apr 3, 2024
Merged
3 changes: 3 additions & 0 deletions src/coreclr/clrdefinitions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ endif(CLR_CMAKE_TARGET_LINUX AND CLR_CMAKE_HOST_LINUX)
if(CLR_CMAKE_TARGET_FREEBSD)
add_compile_definitions(FEATURE_PERFMAP)
endif(CLR_CMAKE_TARGET_FREEBSD)
if(CLR_CMAKE_TARGET_APPLE)
add_compile_definitions(FEATURE_PERFMAP)
endif(CLR_CMAKE_TARGET_APPLE)

if(FEATURE_COMWRAPPERS)
add_compile_definitions(FEATURE_COMWRAPPERS)
Expand Down
37 changes: 31 additions & 6 deletions src/coreclr/pal/src/misc/perfjitdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// ===========================================================================

#if defined(__linux__)
#if defined(__linux__) || defined(__APPLE__)
jkotas marked this conversation as resolved.
Show resolved Hide resolved
#define JITDUMP_SUPPORTED
#endif

Expand Down Expand Up @@ -61,6 +61,22 @@ namespace
JIT_CODE_LOAD = 0,
};

#if defined(__linux__)
static uint32_t sys_gettid()
{
return syscall(SYS_gettid);
}
#else
static uint32_t sys_gettid()
vvuk marked this conversation as resolved.
Show resolved Hide resolved
{
// macOS SYS_gettid is completely unrelated to linux gettid (it returns
// effective uid/gid a thread is operating under)
// There is pthread_threadid_np(), but it returns a uint64_t.
// Given that we need a uint32_t, just return 0 here for now.
return 0;
}
#endif

uint64_t GetTimeStampNS()
{
#if HAVE_CLOCK_MONOTONIC
Expand Down Expand Up @@ -115,7 +131,7 @@ namespace
{
JitCodeLoadRecord() :
pid(getpid()),
tid(syscall(SYS_gettid))
tid(sys_gettid())
{
header.id = JIT_CODE_LOAD;
header.timestamp = GetTimeStampNS();
Expand Down Expand Up @@ -203,12 +219,18 @@ struct PerfJitDumpState
if (result == -1)
return FatalError();

#if !defined(__APPLE__)
// mmap jitdump file
// this is a marker for perf inject to find the jitdumpfile
// this is a marker for perf inject to find the jitdumpfile on linux.
// On OSX, samply and others hook open and mmap is not needed. It also fails on OSX,
// likely because of PROT_EXEC and hardened runtime
mmapAddr = mmap(nullptr, sizeof(FileHeader), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);

if (mmapAddr == MAP_FAILED)
return FatalError();
#else
mmapAddr = NULL;
#endif

enabled = true;

Expand Down Expand Up @@ -314,10 +336,13 @@ struct PerfJitDumpState
if (!enabled)
goto exit;

result = munmap(mmapAddr, sizeof(FileHeader));
if (mmapAddr != NULL)
jkotas marked this conversation as resolved.
Show resolved Hide resolved
{
result = munmap(mmapAddr, sizeof(FileHeader));

if (result == -1)
return FatalError();
if (result == -1)
return FatalError();
}

mmapAddr = MAP_FAILED;

Expand Down
Loading