19
19
20
20
#if SWIFT_STDLIB_TRACING
21
21
22
+ #include " swift/Basic/Lazy.h"
22
23
#include " swift/Runtime/Config.h"
23
24
#include < os/signpost.h>
24
25
@@ -35,24 +36,51 @@ namespace trace {
35
36
static inline bool shouldEnableTracing () {
36
37
if (!SWIFT_RUNTIME_WEAK_CHECK (os_signpost_enabled))
37
38
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 ;
44
39
return true ;
45
40
}
46
41
47
- static inline bool tracingReady () {
48
42
#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.
49
72
if (!_os_trace_lazy_init_completed_4swift ())
50
73
return false ;
51
- #endif
52
74
53
75
return true ;
54
76
}
55
77
78
+ #else
79
+
80
+ static inline bool tracingReady () { return true ; }
81
+
82
+ #endif
83
+
56
84
} // namespace trace
57
85
} // namespace runtime
58
86
} // namespace swift
0 commit comments