Change two-pass behavior for loading mappings. Allow lazy binding of mappings by...
[enjoyable.git] / Categories / NSProcessInfo+Debugging.m
diff --git a/Categories/NSProcessInfo+Debugging.m b/Categories/NSProcessInfo+Debugging.m
new file mode 100644 (file)
index 0000000..649de3b
--- /dev/null
@@ -0,0 +1,35 @@
+//
+//  NSProcessInfo+Debugging.m
+//  Enjoyable
+//
+//  Created by Joe Wreschnig on 3/17/13.
+//
+//
+
+#import "NSProcessInfo+Debugging.h"
+
+#include <assert.h>
+#include <stdbool.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/sysctl.h>
+
+@implementation NSProcessInfo (Debugging)
+
+- (BOOL)isBeingDebugged {
+    int mib[4];
+    struct kinfo_proc info;
+    size_t size = sizeof(info);
+    
+    info.kp_proc.p_flag = 0;
+    
+    mib[0] = CTL_KERN;
+    mib[1] = KERN_PROC;
+    mib[2] = KERN_PROC_PID;
+    mib[3] = self.processIdentifier;
+    
+    return sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0) == 0
+        && (info.kp_proc.p_flag & P_TRACED) != 0;
+}
+
+@end