]> git.saurik.com Git - apple/security.git/blob - utilities/src/debugging.h
Security-55471.14.8.tar.gz
[apple/security.git] / utilities / src / debugging.h
1 /*
2 * Copyright (c) 2006-2007,2009-2010 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 secinfo(scope, format, ...) printf((format), ## __VA_ARGS__)
39 #if !defined(NDEBUG)
40 #define secdebug(scope, format, ...) printf((format), ## __VA_ARGS__)
41 #else // NDEBUG
42 #define secdebug(scope, format, ...) /* nothing */
43 #endif // NDEBUG
44 #else // !KERNEL
45
46 #include <TargetConditionals.h>
47 #include <CoreFoundation/CFString.h>
48 #include <asl.h>
49
50 __BEGIN_DECLS
51
52 extern void __security_trace_enter_api(const char *api, CFStringRef format, ...) CF_FORMAT_FUNCTION(2, 3);
53 extern void __security_trace_return_api(const char *api, CFStringRef format, ...) CF_FORMAT_FUNCTION(2, 3);
54
55 extern void __security_debug(CFStringRef scope,
56 const char *function, const char *file, int line,
57 CFStringRef format, ...) CF_FORMAT_FUNCTION(5,6);
58
59 extern void __security_log(const char *level, CFStringRef scope,
60 const char *function, const char *file, int line,
61 CFStringRef format, ...) CF_FORMAT_FUNCTION(6,7);
62
63 #define sec_trace_enter_api(format...) __security_trace_enter_api(__FUNCTION__, format)
64 #define sec_trace_return_api(rtype, body, format...) { rtype _r = body(); __security_trace_return_api(__FUNCTION__, format, _r); return _r; }
65 #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; }
66
67 #define secemergency(format, ...) __security_log(ASL_STRING_EMERG, NULL, \
68 __FUNCTION__, __FILE__, __LINE__, \
69 CFSTR(format), ## __VA_ARGS__)
70
71 #define secalert(format, ...) __security_log(ASL_STRING_ALERT, NULL, \
72 __FUNCTION__, __FILE__, __LINE__, \
73 CFSTR(format), ## __VA_ARGS__)
74
75 #define seccritical(format, ...) __security_log(ASL_STRING_CRIT, NULL, \
76 __FUNCTION__, __FILE__, __LINE__, \
77 CFSTR(format), ## __VA_ARGS__)
78
79 #define secerror(format, ...) __security_log(ASL_STRING_ERR, NULL, \
80 __FUNCTION__, __FILE__, __LINE__, \
81 CFSTR(format), ## __VA_ARGS__)
82
83 #define secwarning(format, ...) __security_log(ASL_STRING_WARNING, NULL, \
84 __FUNCTION__, __FILE__, __LINE__, \
85 CFSTR(format), ## __VA_ARGS__)
86
87 #define secnotice(scope, format, ...) __security_log(ASL_STRING_NOTICE, CFSTR(scope), \
88 __FUNCTION__, __FILE__, __LINE__, \
89 CFSTR(format), ## __VA_ARGS__)
90
91 #define secinfo(scope, format, ...) __security_log(ASL_STRING_INFO, CFSTR(scope), \
92 __FUNCTION__, __FILE__, __LINE__, \
93 CFSTR(format), ## __VA_ARGS__)
94
95 #if !defined(NDEBUG)
96
97 # define secdebug(scope,format, ...) __security_debug(CFSTR(scope), \
98 __FUNCTION__, __FILE__, __LINE__, \
99 CFSTR(format), ## __VA_ARGS__)
100 #else
101 # define secdebug(scope,...) /* nothing */
102 #endif
103
104 typedef void (^security_log_handler)(const char *level, CFStringRef scope, const char *function,
105 const char *file, int line, CFStringRef message);
106
107 void add_security_log_hanlder(security_log_handler handler);
108
109 /* To simulate a process crash in some conditions */
110 void __security_simulatecrash(CFStringRef reason, uint32_t code);
111
112 /* predefined simulate crash exception codes */
113 #define __sec_exception_code(x) (0x53c00000+x)
114 #define __sec_exception_code_CorruptDb(db,rc) __sec_exception_code(1|((db)<<8)|((rc)<<16))
115 #define __sec_exception_code_CorruptItem __sec_exception_code(2)
116 #define __sec_exception_code_OTRError __sec_exception_code(3)
117 #define __sec_exception_code_DbItemDescribe __sec_exception_code(4)
118 #define __sec_exception_code_TwiceCorruptDb(db) __sec_exception_code(5|((db)<<8))
119
120 __END_DECLS
121
122 #endif // !KERNEL
123
124 #endif /* _SECURITY_UTILITIES_DEBUGGING_H_ */