Skip to content

Commit d62b7db

Browse files
committed
[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
1 parent 9b876fd commit d62b7db

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

include/swift/Runtime/TracingCommon.h

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#if SWIFT_STDLIB_TRACING
2121

22+
#include "swift/Basic/Lazy.h"
2223
#include "swift/Runtime/Config.h"
2324
#include <os/signpost.h>
2425

@@ -35,24 +36,53 @@ namespace trace {
3536
static inline bool shouldEnableTracing() {
3637
if (!SWIFT_RUNTIME_WEAK_CHECK(os_signpost_enabled))
3738
return false;
38-
if (__progname && (strcmp(__progname, "logd") == 0 ||
39-
strcmp(__progname, "diagnosticd") == 0 ||
40-
strcmp(__progname, "notifyd") == 0 ||
41-
strcmp(__progname, "xpcproxy") == 0 ||
42-
strcmp(__progname, "logd_helper") == 0))
43-
return false;
4439
return true;
4540
}
4641

47-
static inline bool tracingReady() {
4842
#if SWIFT_USE_OS_TRACE_LAZY_INIT
43+
# if __has_include(<sys/codesign.h>)
44+
# include <sys/codesign.h>
45+
# else
46+
// SPI
47+
# define CS_OPS_STATUS 0
48+
# define CS_PLATFORM_BINARY 0x04000000
49+
extern "C" int csops(pid_t, unsigned int, void *, size_t);
50+
# endif
51+
52+
#include <unistd.h>
53+
54+
static inline bool isPlatformBinary() {
55+
unsigned int flags = 0;
56+
int error = csops(getpid(), CS_OPS_STATUS, &flags, sizeof(flags));
57+
if (error)
58+
return true; // Fail safe if the call fails, assume it's a platform binary.
59+
return (flags & CS_PLATFORM_BINARY) != 0;
60+
}
61+
62+
static inline bool tracingReady() {
63+
// For non-platform binaries, consider tracing to always be ready. We can
64+
// safely initiate setup if it isn't.
65+
bool platformBinary = SWIFT_LAZY_CONSTANT(isPlatformBinary());
66+
if (!platformBinary)
67+
return true;
68+
69+
// For platform binaries, we may be on the path that sets up tracing, and
70+
// making tracing calls may deadlock in that case. Wait until something else
71+
// set up tracing before using it.
4972
if (!_os_trace_lazy_init_completed_4swift())
5073
return false;
51-
#endif
5274

5375
return true;
5476
}
5577

78+
#else
79+
80+
static inline bool tracingReady() {
81+
return true;
82+
}
83+
84+
#endif
85+
5686
} // namespace trace
5787
} // namespace runtime
5888
} // namespace swift

0 commit comments

Comments
 (0)