]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/src/debugging.h
6a716d537c01e3c23068f9c200356eca368cdd7a
[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
28 /*
29 * CONFIGURING DEFAULT DEBUG SCOPES
30 *
31 * Default debug "scope" inclusion / exclusion is configured in com.apple.securityd.plist (iOS) and
32 * com.apple.secd.plist (OSX) in the Environmental Variable "DEBUGSCOPE". The current value for that
33 * variable begins with a dash ("-") indicating an "exclusion list". If you add a scope for a
34 * secnotice, etc that you don't want to always be "on" add the new string to the DEBUGSCOPE variable
35 * in both plists.
36 */
37
38 #ifndef _SECURITY_UTILITIES_DEBUGGING_H_
39 #define _SECURITY_UTILITIES_DEBUGGING_H_
40
41 #if TARGET_OS_OSX
42 #include <security_utilities/debugging_internal.h>
43 #endif
44
45 #ifdef KERNEL
46 #include <libkern/libkern.h>
47 #define secalert(format, ...) printf((format), ## __VA_ARGS__)
48 #define secemergency(format, ...) printf((format), ## __VA_ARGS__)
49 #define seccritical(format, ...) printf((format), ## __VA_ARGS__)
50 #define secerror(format, ...) printf((format), ## __VA_ARGS__)
51 #define secwarning(format, ...) printf((format), ## __VA_ARGS__)
52 #define secnotice(scope, format, ...) printf((format), ## __VA_ARGS__)
53 #define secnoticeq(scope, format, ...) printf((format), ## __VA_ARGS__)
54 #define secinfo(scope, format, ...) printf((format), ## __VA_ARGS__)
55 #undef secdebug
56 #if !defined(NDEBUG)
57 #define secdebug(scope, format, ...) printf((format), ## __VA_ARGS__)
58 #else // NDEBUG
59 #define secdebug(scope, format, ...) /* nothing */
60 #endif // NDEBUG
61 #else // !KERNEL
62
63 #include <TargetConditionals.h>
64 #include <CoreFoundation/CFString.h>
65 #include <asl.h>
66
67 __BEGIN_DECLS
68
69 #define SECLOG_LEVEL_EMERG 0
70 #define SECLOG_LEVEL_ALERT 1
71 #define SECLOG_LEVEL_CRIT 2
72 #define SECLOG_LEVEL_ERR 3
73 #define SECLOG_LEVEL_WARNING 4
74 #define SECLOG_LEVEL_NOTICE 5
75 #define SECLOG_LEVEL_INFO 6
76 #define SECLOG_LEVEL_DEBUG 7
77
78 #include <os/log_private.h>
79 extern os_log_t logObjForScope(const char *scope); /* XXX don't use me, remove */
80 extern os_log_t secLogObjForScope(const char *scope);
81 extern bool secLogEnabled(void);
82 extern void secLogDisable(void);
83 extern void secLogEnable(void);
84
85 #if TARGET_OS_OSX
86 #define NO_OS_LOG 1
87 #ifdef NO_OS_LOG
88
89 // There might be no os_log available. Weak link their internal functions.
90 void weak_os_log_impl(void *dso, os_log_t log, os_log_type_t type, const char *format, uint8_t *buf, unsigned int size);
91 #define _os_log_impl weak_os_log_impl
92
93 #undef os_log_create
94 os_log_t weak_os_log_create(const char *subsystem, const char *category);
95 #define os_log_create weak_os_log_create
96
97 bool weak_os_log_type_enabled(os_log_t oslog, os_log_type_t type);
98 #define os_log_type_enabled weak_os_log_type_enabled
99
100 #endif // NO_OS_LOG
101 #endif // TARGET_OS_OSX
102
103 CFStringRef SecLogAPICreate(bool apiIn, const char *api, CFStringRef format, ...);
104
105 extern const char *api_trace;
106
107 #define sec_trace_enter_api(format...) { \
108 CFStringRef info = SecLogAPICreate(true, __FUNCTION__, format, NULL); \
109 secinfo(api_trace, "%@", info); CFReleaseNull(info); \
110 }
111
112 #define sec_trace_return_api(rtype, body, format...) { \
113 rtype _r = body(); \
114 CFStringRef info = SecLogAPICreate(true, __FUNCTION__, format, _r); \
115 secinfo(api_trace, "%@", info); \
116 CFReleaseNull(info); return _r; \
117 }
118
119 #define sec_trace_return_bool_api(body, format...) { \
120 bool _r = body(); \
121 CFStringRef info = SecLogAPICreate(true, __FUNCTION__, format ? format : CFSTR("return=%d"), _r); \
122 secinfo(api_trace, "%@", info); \
123 CFReleaseNull(info); return _r; \
124 }
125
126 #define secemergency(format, ...) os_log_error(secLogObjForScope("SecEmergency"), format, ## __VA_ARGS__)
127 #define secalert(format, ...) os_log_error(secLogObjForScope("SecAlert"), format, ## __VA_ARGS__)
128 #define seccritical(format, ...) os_log(secLogObjForScope("SecCritical"), format, ## __VA_ARGS__)
129 #define secerror(format, ...) os_log(secLogObjForScope("SecError"), format, ## __VA_ARGS__)
130 #define secerrorq(format, ...) os_log(secLogObjForScope("SecError"), format, ## __VA_ARGS__)
131 #define secwarning(format, ...) os_log(secLogObjForScope("SecWarning"), format, ## __VA_ARGS__)
132 #define secnotice(scope, format, ...) os_log(secLogObjForScope(scope), format, ## __VA_ARGS__)
133 #define secnoticeq(scope, format, ...) os_log(secLogObjForScope(scope), format, ## __VA_ARGS__)
134 #define secinfo(scope, format, ...) os_log_debug(secLogObjForScope(scope), format, ## __VA_ARGS__)
135
136 #define secinfoenabled(scope) os_log_debug_enabled(secLogObjForScope(scope))
137
138 // secdebug is used for things that might not be privacy safe at all, so only debug builds can have these traces
139 #undef secdebug
140 #if !defined(NDEBUG)
141 #define secdebug(scope, format, ...) os_log_debug(secLogObjForScope(scope), format, ## __VA_ARGS__)
142 #else
143 # define secdebug(scope,...) /* nothing */
144 #endif
145
146 typedef void (^security_log_handler)(int level, CFStringRef scope, const char *function,
147 const char *file, int line, CFStringRef message);
148
149 /* To simulate a process crash in some conditions */
150 void __security_simulatecrash(CFStringRef reason, uint32_t code);
151 void __security_stackshotreport(CFStringRef reason, uint32_t code);
152
153 /* predefined simulate crash exception codes */
154 #define __sec_exception_code(x) (0x53c00000+x)
155 /* 1 was __sec_exception_code_CorruptDb */
156 #define __sec_exception_code_CorruptItem __sec_exception_code(2)
157 #define __sec_exception_code_OTRError __sec_exception_code(3)
158 #define __sec_exception_code_DbItemDescribe __sec_exception_code(4)
159 #define __sec_exception_code_TwiceCorruptDb(db) __sec_exception_code(5|((db)<<8))
160 #define __sec_exception_code_AuthLoop __sec_exception_code(6)
161 #define __sec_exception_code_MissingEntitlements __sec_exception_code(7)
162 #define __sec_exception_code_LostInMist __sec_exception_code(8)
163 #define __sec_exception_code_CKD_nil_pending_keys __sec_exception_code(9)
164 #define __sec_exception_code_SQLiteBusy __sec_exception_code(10)
165 #define __sec_exception_code_CorruptDb(rc) __sec_exception_code(11|((rc)<<8))
166
167 /* For testing only, turns off/on simulated crashes, when turning on, returns number of
168 simulated crashes which were not reported since last turned off. */
169 int __security_simulatecrash_enable(bool enable);
170
171 /* Logging control functions */
172
173 typedef enum {
174 kScopeIDEnvironment = 0,
175 kScopeIDDefaults = 1,
176 kScopeIDConfig = 2,
177 kScopeIDXPC = 3,
178 kScopeIDCircle = 4,
179 kScopeIDMax = 4,
180 } SecDebugScopeID;
181
182 void ApplyScopeListForID(CFStringRef scopeList, SecDebugScopeID whichID);
183 void ApplyScopeDictionaryForID(CFDictionaryRef scopeList, SecDebugScopeID whichID);
184 CFPropertyListRef CopyCurrentScopePlist(void);
185
186 __END_DECLS
187
188 #endif // !KERNEL
189
190 #endif /* _SECURITY_UTILITIES_DEBUGGING_H_ */