1356ae770b4168b335440f32b61d433928af593e
[enjoyable.git] / NSProcessInfo+Debugging.m
1 //
2 // NSProcessInfo+Debugging.m
3 // Enjoyable
4 //
5 // Created by Joe Wreschnig on 3/17/13.
6 //
7 //
8
9 #import "NSProcessInfo+Debugging.h"
10
11 #include <assert.h>
12 #include <stdbool.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15 #include <sys/sysctl.h>
16
17 @implementation NSProcessInfo (Debugging)
18
19 - (BOOL)isBeingDebugged {
20 #ifdef DEBUG
21 int mib[4];
22 struct kinfo_proc info;
23 size_t size = sizeof(info);
24
25 info.kp_proc.p_flag = 0;
26
27 mib[0] = CTL_KERN;
28 mib[1] = KERN_PROC;
29 mib[2] = KERN_PROC_PID;
30 mib[3] = self.processIdentifier;
31
32 return sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0) == 0
33 && (info.kp_proc.p_flag & P_TRACED) != 0;
34 #else
35 return NO;
36 #endif
37 }
38
39 @end