mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[Runtime] Only use lazy os_signpost initialization for platform binaries.
We don't emit signposts until something else has set them up, to avoid deadlocks when we're running in code that's involved in setting them up. But this means that Instruments will miss Concurrency events in a simple program that doesn't otherwise trigger setup of the logging system. Since we must be in a platform binary if we're running in code that's setting up logging, we can check for that and only be lazy in platform binaries. Non-platform binaries can safely emit signposts eagerly. rdar://142483658
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
|
||||
#if SWIFT_STDLIB_TRACING
|
||||
|
||||
#include "swift/Basic/Lazy.h"
|
||||
#include "swift/Runtime/Config.h"
|
||||
#include <os/signpost.h>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
#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() {
|
||||
#if SWIFT_USE_OS_TRACE_LAZY_INIT
|
||||
// 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
|
||||
|
||||
Reference in New Issue
Block a user