]> git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/debugging.h
Security-55163.44.tar.gz
[apple/security.git] / libsecurity_utilities / lib / debugging.h
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, 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 //
26 // debugging - non-trivial debug support
27 //
28 #ifndef _H_DEBUGGING
29 #define _H_DEBUGGING
30
31
32 //
33 // Include DTrace static probe definitions
34 //
35 typedef const void *DTException;
36
37 #include <security_utilities/utilities_dtrace.h>
38
39
40 #ifdef __cplusplus
41
42 #include <security_utilities/utilities.h>
43 #include <cstdarg>
44 #include <typeinfo>
45
46
47 namespace Security {
48 namespace Debug {
49
50
51 //
52 // Debug-dumping functions always exist. They may be stubs depending on build options.
53 //
54 bool dumping(const char *scope);
55 void dump(const char *format, ...) __attribute((format(printf,1,2)));
56 void dumpData(const void *data, size_t length);
57 void dumpData(const char *title, const void *data, size_t length);
58 template <class Data> inline void dumpData(const Data &obj)
59 { dumpData(obj.data(), obj.length()); }
60 template <class Data> inline void dumpData(const char *title, const Data &obj)
61 { dumpData(title, obj.data(), obj.length()); }
62
63
64 //
65 // The following functions perform runtime recovery of type names.
66 // This is meant for debugging ONLY. Don't even THINK of depending
67 // on this for program correctness. For all you know, we may replace
68 // all those names with "XXX" tomorrow.
69 //
70 string makeTypeName(const type_info &info);
71
72 template <class Object>
73 string typeName(const Object &obj)
74 {
75 return makeTypeName(typeid(obj));
76 }
77
78 template <class Object>
79 string typeName()
80 {
81 return makeTypeName(typeid(Object));
82 }
83
84
85 //
86 // We are still conditionally emitting debug-dumping code
87 //
88 #undef DEBUGGING
89 #if !defined(NDEBUG)
90 # define DEBUGGING 1
91 # define DEBUGDUMP 1
92 #else //NDEBUG
93 # define DEBUGGING 0
94 #endif //NDEBUG
95
96 #if defined(DEBUGDUMP)
97 # define IFDUMP(code) code
98 # define IFDUMPING(scope,code) if (Debug::dumping(scope)) code; else /* no */
99 #else
100 # define IFDUMP(code) /* no-op */
101 # define IFDUMPING(scope,code) /* no-op */
102 #endif
103
104
105 //
106 // We have some very, very old customers who call old debug facilities.
107 // Dummy them out for now.
108 //
109 inline bool debugging(const char *scope) DEPRECATED_ATTRIBUTE;
110 inline void debug(const char *scope, const char *format, ...) DEPRECATED_ATTRIBUTE;
111 inline void vdebug(const char *scope, const char *format, va_list args) DEPRECATED_ATTRIBUTE;
112
113 inline bool debugging(const char *scope) { return false; }
114 inline void debug(const char *scope, const char *format, ...) { }
115 inline void vdebug(const char *scope, const char *format, va_list args) { }
116
117
118
119
120
121 } // end namespace Debug
122 } // end namespace Security
123
124 // leak debug() into the global namespace because URLAccess et al rely on that
125 using Security::Debug::debug;
126
127
128 #else //__cplusplus
129
130 #include <stdio.h>
131
132 #endif //__cplusplus
133
134
135 //
136 // The debug-log macro is now unconditionally emitted as a DTrace static probe point.
137 //
138 #define secdebug(scope, format...) \
139 if (__builtin_expect(SECURITY_DEBUG_LOG_ENABLED(), 0)) { \
140 char __msg[500]; snprintf(__msg, sizeof(__msg), ## format); \
141 volatile char c __attribute__((unused)) = scope[0]; \
142 SECURITY_DEBUG_LOG((char *)(scope), (__msg)); \
143 } else /* nothing */
144 #define secdebugf(scope, __msg) SECURITY_DEBUG_LOG((char *)(scope), (__msg))
145
146
147 //
148 // The old secdelay() macro is also emitted as a DTrace probe (use destructive actions to handle this).
149 // Secdelay() should be considered a legacy feature; just put a secdebug at the intended delay point.
150 //
151 #define secdelay(file) SECURITY_DEBUG_DELAY((char *)(file))
152
153
154 #endif //_H_DEBUGGING