5 // Copyright (c) 2014 Apple Inc. All Rights Reserved.
10 #include <dispatch/dispatch.h>
12 #include <mach/mach.h>
14 /// Type to represent a boolean value.
15 #if TARGET_OS_IPHONE && __LP64__
18 typedef signed char BOOL
;
19 // BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C"
20 // even if -funsigned-char is used.
24 __security_get_CrashReporterSupport(void)
26 static void *image
= NULL
;
27 static dispatch_once_t onceToken
;
28 dispatch_once(&onceToken
, ^{
29 image
= dlopen("/System/Library/PrivateFrameworks/CrashReporterSupport.framework/CrashReporterSupport", RTLD_NOW
);
34 static void __security_simulatecrash_link(CFStringRef reason
, uint32_t code
)
36 #if !TARGET_OS_SIMULATOR
37 // Prototype defined in <CrashReporterSupport/CrashReporterSupport.h>, but objC only.
38 // Soft linking here so we don't link unless we hit this.
39 static BOOL (*__SimulateCrash
)(pid_t pid
, mach_exception_data_type_t exceptionCode
, CFStringRef description
) = NULL
;
41 static dispatch_once_t once
= 0;
42 dispatch_once(&once
, ^{
43 void *image
= __security_get_CrashReporterSupport();
45 __SimulateCrash
= dlsym(image
, "SimulateCrash");
49 __SimulateCrash(getpid(), code
, reason
);
51 secerror("SimulateCrash not available");
53 secerror("SimulateCrash not available in iOS simulator");
57 static int __simulate_crash_counter
= -1;
59 void __security_simulatecrash(CFStringRef reason
, uint32_t code
)
61 secerror("Simulating crash, reason: %@, code=%08x", reason
, code
);
62 if (__simulate_crash_counter
< 0)
63 __security_simulatecrash_link(reason
, code
);
65 __simulate_crash_counter
++;
68 void __security_stackshotreport(CFStringRef reason
, uint32_t code
)
70 secerror("stackshot report, reason: %@, code=%08x", reason
, code
);
71 #if !TARGET_OS_SIMULATOR
72 // Prototype defined in <CrashReporterSupport/CrashReporterSupport.h>, but objC only.
73 // Soft linking here so we don't link unless we hit this.
74 static BOOL (*__WriteStackshotReport
)(void *, mach_exception_data_type_t
) = NULL
;
76 static dispatch_once_t once
= 0;
77 dispatch_once(&once
, ^{
78 void *image
= __security_get_CrashReporterSupport();
80 __WriteStackshotReport
= dlsym(image
, "WriteStackshotReport");
83 if (__WriteStackshotReport
)
84 __WriteStackshotReport((void *)reason
, code
);
86 secerror("WriteStackshotReport not available");
88 secerror("WriteStackshotReport not available in iOS simulator");
93 int __security_simulatecrash_enable(bool enable
)
95 int count
= __simulate_crash_counter
;
96 __simulate_crash_counter
= enable
? -1 : 0;