The iOS framework that grows only as fast as its documentation
NIDebuggingTools.m
1 //
2 // Copyright 2011-2014 NimbusKit
3 // Copyright 2009-2011 Facebook
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 #import "NIDebuggingTools.h"
19 
20 #if !defined(__has_feature) || !__has_feature(objc_arc)
21 #error "Nimbus requires ARC support."
22 #endif
23 
24 NSInteger NIMaxLogLevel = NILOGLEVEL_WARNING;
26 
27 #if defined(DEBUG) || defined(NI_DEBUG)
28 
29 #import <unistd.h>
30 #import <sys/sysctl.h>
31 
32 // From: http://developer.apple.com/mac/library/qa/qa2004/qa1361.html
33 int NIIsInDebugger(void) {
34  int mib[4];
35  struct kinfo_proc info;
36  size_t size;
37 
38  // Initialize the flags so that, if sysctl fails for some bizarre
39  // reason, we get a predictable result.
40 
41  info.kp_proc.p_flag = 0;
42 
43  // Initialize mib, which tells sysctl the info we want, in this case
44  // we're looking for information about a specific process ID.
45 
46  mib[0] = CTL_KERN;
47  mib[1] = KERN_PROC;
48  mib[2] = KERN_PROC_PID;
49  mib[3] = getpid();
50 
51  // Call sysctl.
52 
53  size = sizeof(info);
54  sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
55 
56  // We're being debugged if the P_TRACED flag is set.
57 
58  return (info.kp_proc.p_flag & P_TRACED) != 0;
59 }
60 
61 #endif // #if defined(DEBUG) || defined(NI_DEBUG)
int NIIsInDebugger(void)
Assertions that only fire when DEBUG is defined.
NSInteger NIMaxLogLevel
The maximum log level to output for Nimbus debug logs.
BOOL NIDebugAssertionsShouldBreak
Whether or not debug assertions should halt program execution like a breakpoint when they fail...