]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/src/debugging.h
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / utilities / src / debugging.h
1 /*
2 * Copyright (c) 2006-2007,2009-2010,2012-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * debugging.h - non-trivial debug support
26 */
27 #ifndef _SECURITY_UTILITIES_DEBUGGING_H_
28 #define _SECURITY_UTILITIES_DEBUGGING_H_
29
30 #ifdef KERNEL
31 #include <libkern/libkern.h>
32 #define secalert(format, ...) printf((format), ## __VA_ARGS__)
33 #define secemergency(format, ...) printf((format), ## __VA_ARGS__)
34 #define seccritical(format, ...) printf((format), ## __VA_ARGS__)
35 #define secerror(format, ...) printf((format), ## __VA_ARGS__)
36 #define secwarning(format, ...) printf((format), ## __VA_ARGS__)
37 #define secnotice(scope, format, ...) printf((format), ## __VA_ARGS__)
38 #define secnoticeq(scope, format, ...) printf((format), ## __VA_ARGS__)
39 #define secinfo(scope, format, ...) printf((format), ## __VA_ARGS__)
40 #if !defined(NDEBUG)
41 #define secdebug(scope, format, ...) printf((format), ## __VA_ARGS__)
42 #else // NDEBUG
43 #define secdebug(scope, format, ...) /* nothing */
44 #endif // NDEBUG
45 #else // !KERNEL
46
47 #include <TargetConditionals.h>
48 #include <CoreFoundation/CFString.h>
49 #include <asl.h>
50
51 __BEGIN_DECLS
52
53 extern void __security_trace_enter_api(const char *api, CFStringRef format, ...) CF_FORMAT_FUNCTION(2, 3);
54 extern void __security_trace_return_api(const char *api, CFStringRef format, ...) CF_FORMAT_FUNCTION(2, 3);
55
56 extern void __security_debug(CFStringRef scope,
57 const char *function, const char *file, int line,
58 CFStringRef format, ...) CF_FORMAT_FUNCTION(5,6);
59
60 extern void __security_log(int level, CFStringRef scope,
61 const char *function, const char *file, int line,
62 CFStringRef format, ...) CF_FORMAT_FUNCTION(6,7);
63
64 #define sec_trace_enter_api(format...) __security_trace_enter_api(__FUNCTION__, format)
65 #define sec_trace_return_api(rtype, body, format...) { rtype _r = body(); __security_trace_return_api(__FUNCTION__, format, _r); return _r; }
66 #define sec_trace_return_bool_api(body, format...) { bool _r = body(); typeof(format) _fmt = format; __security_trace_return_api(__FUNCTION__, _fmt ? _fmt : CFSTR("return=%d"), (int)_r); return _r; }
67
68 #define secemergency(format, ...) __security_log(ASL_LEVEL_EMERG, NULL, \
69 __FUNCTION__, __FILE__, __LINE__, \
70 CFSTR(format), ## __VA_ARGS__)
71
72 #define secalert(format, ...) __security_log(ASL_LEVEL_ALERT, NULL, \
73 __FUNCTION__, __FILE__, __LINE__, \
74 CFSTR(format), ## __VA_ARGS__)
75
76 #define seccritical(format, ...) __security_log(ASL_LEVEL_CRIT, NULL, \
77 __FUNCTION__, __FILE__, __LINE__, \
78 CFSTR(format), ## __VA_ARGS__)
79
80 #define secerror(format, ...) __security_log(ASL_LEVEL_ERR, NULL, \
81 __FUNCTION__, __FILE__, __LINE__, \
82 CFSTR(format), ## __VA_ARGS__)
83
84 #define secerrorq(format, ...) __security_log(ASL_LEVEL_ERR, NULL, \
85 "", "", 0, \
86 CFSTR(format), ## __VA_ARGS__)
87
88 #define secwarning(format, ...) __security_log(ASL_LEVEL_WARNING, NULL, \
89 __FUNCTION__, __FILE__, __LINE__, \
90 CFSTR(format), ## __VA_ARGS__)
91
92 #define secnotice(scope, format, ...) __security_log(ASL_LEVEL_NOTICE, CFSTR(scope), \
93 __FUNCTION__, __FILE__, __LINE__, \
94 CFSTR(format), ## __VA_ARGS__)
95
96 #define secnoticeq(scope, format, ...) __security_log(ASL_LEVEL_NOTICE, CFSTR(scope), \
97 "", "", 0, \
98 CFSTR(format), ## __VA_ARGS__)
99
100
101 #if !defined(NDEBUG)
102
103 #define secinfo(scope, format, ...) __security_log(ASL_LEVEL_INFO, CFSTR(scope), \
104 __FUNCTION__, __FILE__, __LINE__, \
105 CFSTR(format), ## __VA_ARGS__)
106
107 # define secdebug(scope,format, ...) __security_debug(CFSTR(scope), \
108 __FUNCTION__, __FILE__, __LINE__, \
109 CFSTR(format), ## __VA_ARGS__)
110
111 #else
112 # define secinfo(scope,...) /* nothing */
113 # define secdebug(scope,...) /* nothing */
114 #endif
115
116 typedef void (^security_log_handler)(int level, CFStringRef scope, const char *function,
117 const char *file, int line, CFStringRef message);
118
119 void add_security_log_handler(security_log_handler handler);
120 void remove_security_log_handler(security_log_handler handler);
121
122 /* To simulate a process crash in some conditions */
123 void __security_simulatecrash(CFStringRef reason, uint32_t code);
124
125 /* predefined simulate crash exception codes */
126 #define __sec_exception_code(x) (0x53c00000+x)
127 #define __sec_exception_code_CorruptDb(db,rc) __sec_exception_code(1|((db)<<8)|((rc)<<16))
128 #define __sec_exception_code_CorruptItem __sec_exception_code(2)
129 #define __sec_exception_code_OTRError __sec_exception_code(3)
130 #define __sec_exception_code_DbItemDescribe __sec_exception_code(4)
131 #define __sec_exception_code_TwiceCorruptDb(db) __sec_exception_code(5|((db)<<8))
132 #define __sec_exception_code_AuthLoop __sec_exception_code(6)
133 #define __sec_exception_code_MissingEntitlements __sec_exception_code(7)
134
135 /* For testing only, turns off/on simulated crashes, when turning on, returns number of
136 simulated crashes which were not reported since last turned off. */
137 int __security_simulatecrash_enable(bool enable);
138
139 /* Logging control functions */
140
141 typedef enum {
142 kScopeIDEnvironment = 0,
143 kScopeIDDefaults = 1,
144 kScopeIDConfig = 2,
145 kScopeIDXPC = 3,
146 kScopeIDCircle = 4,
147 kScopeIDMax = 4,
148 } SecDebugScopeID;
149
150 void ApplyScopeListForID(CFStringRef scopeList, SecDebugScopeID whichID);
151 void ApplyScopeDictionaryForID(CFDictionaryRef scopeList, SecDebugScopeID whichID);
152 CFPropertyListRef CopyCurrentScopePlist(void);
153
154 __END_DECLS
155
156 #endif // !KERNEL
157
158 #endif /* _SECURITY_UTILITIES_DEBUGGING_H_ */