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
53extern void __security_trace_enter_api(const char *api, CFStringRef format, ...) CF_FORMAT_FUNCTION(2, 3);
54extern void __security_trace_return_api(const char *api, CFStringRef format, ...) CF_FORMAT_FUNCTION(2, 3);
55
56extern void __security_debug(CFStringRef scope,
57                             const char *function, const char *file, int line,
58                             CFStringRef format, ...) CF_FORMAT_FUNCTION(5,6);
59
60extern 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#define secinfo(scope, format, ...)	__security_log(ASL_LEVEL_INFO, CFSTR(scope), \
101    __FUNCTION__, __FILE__, __LINE__, \
102    CFSTR(format), ## __VA_ARGS__)
103
104#if !defined(NDEBUG)
105
106# define secdebug(scope,format, ...)	__security_debug(CFSTR(scope), \
107    __FUNCTION__, __FILE__, __LINE__, \
108    CFSTR(format), ## __VA_ARGS__)
109#else
110# define secdebug(scope,...)	/* nothing */
111#endif
112
113typedef void (^security_log_handler)(int level, CFStringRef scope, const char *function,
114                                     const char *file, int line, CFStringRef message);
115
116void add_security_log_handler(security_log_handler handler);
117void remove_security_log_handler(security_log_handler handler);
118
119/* To simulate a process crash in some conditions */
120void __security_simulatecrash(CFStringRef reason, uint32_t code);
121
122/* predefined simulate crash exception codes */
123#define __sec_exception_code(x) (0x53c00000+x)
124#define __sec_exception_code_CorruptDb(db,rc)       __sec_exception_code(1|((db)<<8)|((rc)<<16))
125#define __sec_exception_code_CorruptItem            __sec_exception_code(2)
126#define __sec_exception_code_OTRError               __sec_exception_code(3)
127#define __sec_exception_code_DbItemDescribe         __sec_exception_code(4)
128#define __sec_exception_code_TwiceCorruptDb(db)     __sec_exception_code(5|((db)<<8))
129
130/* Logging control functions */
131
132typedef enum {
133    kScopeIDEnvironment = 0,
134    kScopeIDDefaults = 1,
135    kScopeIDConfig = 2,
136    kScopeIDXPC = 3,
137    kScopeIDMax = 3,
138} SecDebugScopeID;
139
140void ApplyScopeListForID(CFStringRef scopeList, SecDebugScopeID whichID);
141void ApplyScopeDictionaryForID(CFDictionaryRef scopeList, SecDebugScopeID whichID);
142CFPropertyListRef CopyCurrentScopePlist(void);
143
144__END_DECLS
145
146#endif // !KERNEL
147
148#endif /* _SECURITY_UTILITIES_DEBUGGING_H_ */
149