]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/simulate_crash.m
Security-59306.41.2.tar.gz
[apple/security.git] / OSX / utilities / simulate_crash.m
1 //
2 // simulate_crash
3 // utilities
4 //
5 // Copyright (c) 2014 Apple Inc. All Rights Reserved.
6 //
7
8 #include "debugging.h"
9
10 #import <mach/mach.h>
11 #import <SoftLinking/SoftLinking.h>
12 #import <Foundation/Foundation.h>
13
14 SOFT_LINK_FRAMEWORK_SAFE(PrivateFrameworks, CrashReporterSupport);
15
16 SOFT_LINK_FUNCTION(CrashReporterSupport, SimulateCrash, soft_SimulateCrash, \
17 BOOL, (pid_t pid, mach_exception_data_type_t exceptionCode, NSString *description),
18 (pid, exceptionCode, description));
19 SOFT_LINK_FUNCTION(CrashReporterSupport, WriteStackshotReport, soft_WriteStackshotReport, \
20 BOOL, (NSString *reason, mach_exception_data_type_t exceptionCode),
21 (reason, exceptionCode));
22
23 static int __simulate_crash_counter = -1;
24
25 void __security_simulatecrash(CFStringRef reason, uint32_t code)
26 {
27 secerror("Simulating crash, reason: %@, code=%08x", reason, code);
28 if (__security_simulatecrash_enabled() && isCrashReporterSupportAvailable()) {
29 soft_SimulateCrash(getpid(), code, (__bridge NSString *)reason);
30 } else {
31 __simulate_crash_counter++;
32 }
33 }
34
35 void __security_stackshotreport(CFStringRef reason, uint32_t code)
36 {
37 secerror("stackshot report, reason: %@, code=%08x", reason, code);
38 if (!__security_simulatecrash_enabled() && isCrashReporterSupportAvailable()) {
39 return;
40 }
41 if (isCrashReporterSupportAvailable()) {
42 soft_WriteStackshotReport((__bridge NSString *)reason, code);
43 }
44 }
45
46
47 int __security_simulatecrash_enable(bool enable)
48 {
49 int count = __simulate_crash_counter;
50 __simulate_crash_counter = enable ? -1 : 0;
51 return count;
52 }
53
54 bool __security_simulatecrash_enabled(void)
55 {
56 return __simulate_crash_counter == -1;
57 }
58