Skip to content

Commit 6793952

Browse files
authored
Merge pull request #80018 from mikeash/eager-tracing-non-platform-binaries
[Runtime] Only use lazy os_signpost initialization for platform binaries.
2 parents 39f58de + b2fe7b4 commit 6793952

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

include/swift/Runtime/TracingCommon.h

Lines changed: 36 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,51 @@ 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() { return true; }
81+
82+
#endif
83+
5684
} // namespace trace
5785
} // namespace runtime
5886
} // namespace swift

0 commit comments

Comments
 (0)