Skip to content

[Runtime] Only use lazy os_signpost initialization for platform binaries. #80018

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions include/swift/Runtime/TracingCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#if SWIFT_STDLIB_TRACING

#include "swift/Basic/Lazy.h"
#include "swift/Runtime/Config.h"
#include <os/signpost.h>

Expand All @@ -35,24 +36,51 @@ namespace trace {
static inline bool shouldEnableTracing() {
if (!SWIFT_RUNTIME_WEAK_CHECK(os_signpost_enabled))
return false;
if (__progname && (strcmp(__progname, "logd") == 0 ||
strcmp(__progname, "diagnosticd") == 0 ||
strcmp(__progname, "notifyd") == 0 ||
strcmp(__progname, "xpcproxy") == 0 ||
strcmp(__progname, "logd_helper") == 0))
return false;
return true;
}

static inline bool tracingReady() {
#if SWIFT_USE_OS_TRACE_LAZY_INIT
#if __has_include(<sys/codesign.h>)
#include <sys/codesign.h>
#else
// SPI
#define CS_OPS_STATUS 0
#define CS_PLATFORM_BINARY 0x04000000
extern "C" int csops(pid_t, unsigned int, void *, size_t);
#endif

#include <unistd.h>

static inline bool isPlatformBinary() {
unsigned int flags = 0;
int error = csops(getpid(), CS_OPS_STATUS, &flags, sizeof(flags));
if (error)
return true; // Fail safe if the call fails, assume it's a platform binary.
return (flags & CS_PLATFORM_BINARY) != 0;
}

static inline bool tracingReady() {
// For non-platform binaries, consider tracing to always be ready. We can
// safely initiate setup if it isn't.
bool platformBinary = SWIFT_LAZY_CONSTANT(isPlatformBinary());
if (!platformBinary)
return true;

// For platform binaries, we may be on the path that sets up tracing, and
// making tracing calls may deadlock in that case. Wait until something else
// set up tracing before using it.
if (!_os_trace_lazy_init_completed_4swift())
return false;
#endif

return true;
}

#else

static inline bool tracingReady() { return true; }

#endif

} // namespace trace
} // namespace runtime
} // namespace swift
Expand Down