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 #include <TargetConditionals.h> 42 43 #ifdef KERNEL 44 #include <libkern/libkern.h> 45 #define secalert(format, ...) printf((format), ## __VA_ARGS__) 46 #define secemergency(format, ...) printf((format), ## __VA_ARGS__) 47 #define seccritical(format, ...) printf((format), ## __VA_ARGS__) 48 #define secerror(format, ...) printf((format), ## __VA_ARGS__) 49 #define secwarning(format, ...) printf((format), ## __VA_ARGS__) 50 #define secnotice(scope, format, ...) printf((format), ## __VA_ARGS__) 51 #define secnoticeq(scope, format, ...) printf((format), ## __VA_ARGS__) 52 #define secinfo(scope, format, ...) printf((format), ## __VA_ARGS__) 53 #undef secdebug 54 #if !defined(NDEBUG) 55 #define secdebug(scope, format, ...) printf((format), ## __VA_ARGS__) 56 #else // NDEBUG 57 #define secdebug(scope, format, ...) /* nothing */ 58 #endif // NDEBUG 59 #else // !KERNEL 60 61 #include <CoreFoundation/CFString.h> 62 #include <asl.h> 63 64 __BEGIN_DECLS 65 66 #define SECLOG_LEVEL_EMERG 0 67 #define SECLOG_LEVEL_ALERT 1 68 #define SECLOG_LEVEL_CRIT 2 69 #define SECLOG_LEVEL_ERR 3 70 #define SECLOG_LEVEL_WARNING 4 71 #define SECLOG_LEVEL_NOTICE 5 72 #define SECLOG_LEVEL_INFO 6 73 #define SECLOG_LEVEL_DEBUG 7 74 75 #include <os/log_private.h> 76 extern os_log_t secLogObjForScope(const char *scope); 77 extern os_log_t secLogObjForCFScope(CFStringRef scope); 78 extern bool secLogEnabled(void); 79 extern void secLogDisable(void); 80 extern void secLogEnable(void); 81 82 CFStringRef SecLogAPICreate(bool apiIn, const char *api, CFStringRef format, ...) 83 CF_FORMAT_FUNCTION(3, 4); 84 85 extern const char *api_trace; 86 87 #define sec_trace_enter_api(format...) { \ 88 CFStringRef info = SecLogAPICreate(true, __FUNCTION__, format, NULL); \ 89 secinfo(api_trace, "%@", info); CFReleaseNull(info); \ 90 } 91 92 #define sec_trace_return_api(rtype, body, format...) { \ 93 rtype _r = body(); \ 94 CFStringRef info = SecLogAPICreate(true, __FUNCTION__, format, _r); \ 95 secinfo(api_trace, "%@", info); \ 96 CFReleaseNull(info); return _r; \ 97 } 98 99 #define sec_trace_return_bool_api(body, format...) { \ 100 bool _r = body(); \ 101 CFStringRef info = SecLogAPICreate(true, __FUNCTION__, format ? format : CFSTR("return=%d"), _r); \ 102 secinfo(api_trace, "%@", info); \ 103 CFReleaseNull(info); return _r; \ 104 } 105 106 #define secemergency(format, ...) os_log_error(secLogObjForScope("SecEmergency"), format, ## __VA_ARGS__) 107 #define secalert(format, ...) os_log_error(secLogObjForScope("SecAlert"), format, ## __VA_ARGS__) 108 #define seccritical(format, ...) os_log(secLogObjForScope("SecCritical"), format, ## __VA_ARGS__) 109 #define secerror(format, ...) os_log(secLogObjForScope("SecError"), format, ## __VA_ARGS__) 110 #define secerrorq(format, ...) os_log(secLogObjForScope("SecError"), format, ## __VA_ARGS__) 111 #define secwarning(format, ...) os_log(secLogObjForScope("SecWarning"), format, ## __VA_ARGS__) 112 #define secnotice(scope, format, ...) os_log(secLogObjForScope(scope), format, ## __VA_ARGS__) 113 #define secnoticeq(scope, format, ...) os_log(secLogObjForScope(scope), format, ## __VA_ARGS__) 114 #define secinfo(scope, format, ...) os_log_debug(secLogObjForScope(scope), format, ## __VA_ARGS__) 115 116 #define secinfoenabled(scope) os_log_debug_enabled(secLogObjForScope(scope)) 117 118 // secdebug is used for things that might not be privacy safe at all, so only debug builds can have these traces 119 #undef secdebug 120 #if !defined(NDEBUG) 121 #define secdebug(scope, format, ...) os_log_debug(secLogObjForScope(scope), format, ## __VA_ARGS__) 122 #else 123 # define secdebug(scope,...) /* nothing */ 124 #endif 125 126 typedef void (^security_log_handler)(int level, CFStringRef scope, const char *function, 127 const char *file, int line, CFStringRef message); 128 129 /* To simulate a process crash in some conditions */ 130 void __security_simulatecrash(CFStringRef reason, uint32_t code); 131 void __security_stackshotreport(CFStringRef reason, uint32_t code); 132 133 /* predefined simulate crash exception codes */ 134 #define __sec_exception_code(x) (0x53c00000+x) 135 /* 1 was __sec_exception_code_CorruptDb */ 136 #define __sec_exception_code_CorruptItem __sec_exception_code(2) 137 #define __sec_exception_code_OTRError __sec_exception_code(3) 138 #define __sec_exception_code_DbItemDescribe __sec_exception_code(4) 139 #define __sec_exception_code_TwiceCorruptDb(db) __sec_exception_code(5|((db)<<8)) 140 #define __sec_exception_code_AuthLoop __sec_exception_code(6) 141 #define __sec_exception_code_MissingEntitlements __sec_exception_code(7) 142 #define __sec_exception_code_LostInMist __sec_exception_code(8) 143 #define __sec_exception_code_CKD_nil_pending_keys __sec_exception_code(9) 144 #define __sec_exception_code_SQLiteBusy __sec_exception_code(10) 145 #define __sec_exception_code_CorruptDb(rc) __sec_exception_code(11|((rc)<<8)) 146 #define __sec_exception_code_Watchdog __sec_exception_code(12) 147 #define __sec_exception_code_BadStash __sec_exception_code(13) 148 #define __sec_exception_code_UnexpectedState __sec_exception_code(14) 149 #define __sec_exception_code_RateLimit __sec_exception_code(15) 150 151 /* For testing only, turns off/on simulated crashes, when turning on, returns number of 152 simulated crashes which were not reported since last turned off. */ 153 int __security_simulatecrash_enable(bool enable); 154 bool __security_simulatecrash_enabled(void); 155 156 /* Logging control functions */ 157 158 typedef enum { 159 kScopeIDEnvironment = 0, 160 kScopeIDDefaults = 1, 161 kScopeIDConfig = 2, 162 kScopeIDXPC = 3, 163 kScopeIDCircle = 4, 164 kScopeIDMax = 4, 165 } SecDebugScopeID; 166 167 void ApplyScopeListForID(CFStringRef scopeList, SecDebugScopeID whichID); 168 void ApplyScopeDictionaryForID(CFDictionaryRef scopeList, SecDebugScopeID whichID); 169 CFPropertyListRef CopyCurrentScopePlist(void); 170 171 __END_DECLS 172 173 #endif // !KERNEL 174 175 #endif /* _SECURITY_UTILITIES_DEBUGGING_H_ */