]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/debugging.h
Security-164.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / debugging.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // debugging - non-trivial debug support
21 //
22 #ifndef _H_DEBUGGING
23 #define _H_DEBUGGING
24
25 #ifdef __cplusplus
26
27 #include <Security/utilities.h>
28 #include <cstdarg>
29 #include <typeinfo>
30
31
32 namespace Security {
33 namespace Debug {
34
35 //
36 // Debug logging functions always exist.
37 // They may be stubs depending on build options.
38 //
39 bool debugging(const char *scope);
40 void debug(const char *scope, const char *format, ...) __attribute__((format(printf,2,3)));
41 void vdebug(const char *scope, const char *format, va_list args);
42
43 //
44 // Ditto with debug dumping functions.
45 //
46 bool dumping(const char *scope);
47 void dump(const char *format, ...) __attribute((format(printf,1,2)));
48 void dumpData(const void *data, size_t length);
49 void dumpData(const char *title, const void *data, size_t length);
50 template <class Data> inline void dumpData(const Data &obj)
51 { dumpData(obj.data(), obj.length()); }
52 template <class Data> inline void dumpData(const char *title, const Data &obj)
53 { dumpData(title, obj.data(), obj.length()); }
54
55
56 //
57 // The following functions perform runtime recovery of type names.
58 // This is meant for debugging ONLY. Don't even THINK of depending
59 // on this for program correctness. For all you know, we may replace
60 // all those names with "XXX" tomorrow.
61 //
62 string makeTypeName(const type_info &info);
63
64 template <class Object>
65 string typeName(const Object &obj)
66 {
67 return makeTypeName(typeid(obj));
68 }
69
70 template <class Object>
71 string typeName()
72 {
73 return makeTypeName(typeid(Object));
74 }
75
76
77 //
78 // Now for the conditional inline code
79 //
80 #if !defined(NDEBUG)
81 # define secdebug(scope, format...) Security::Debug::debug(scope, ## format)
82 #else //NDEBUG
83 # define secdebug(scope, format...) /* nothing */
84 #endif //NDEBUG
85
86
87 //
88 // Conditional dump code
89 //
90 #if defined(DEBUGDUMP)
91 # define IFDUMP(code) code
92 # define IFDUMPING(scope,code) if (Debug::dumping(scope)) code; else /* no */
93 #else
94 # define IFDUMP(code) /* no-op */
95 # define IFDUMPING(scope,code) /* no-op */
96 #endif
97
98
99 //
100 // Kernel trace support
101 //
102 inline void trace(int code, int arg1 = 0, int arg2 = 0, int arg3 = 0, int arg4 = 0)
103 {
104 #if defined(ENABLE_SECTRACE)
105 syscall(180, code, arg1, arg2, arg3, arg4);
106 #endif
107 }
108
109
110 } // end namespace Debug
111 } // end namespace Security
112
113 // We intentionally leak a few functions into the global namespace
114 // @@@ (not much longer: after the switch to secdebug(), this will go)
115 using Security::Debug::debug;
116
117
118 #else //!__cplusplus, C code
119
120
121 extern void __security_debug(const char *scope, const char *format, ...);
122 extern int __security_debugging(const char *scope);
123
124 #if !defined(NDEBUG)
125 # define secdebug(scope, format...) __security_debug(scope, ## format)
126 #else
127 # define secdebug(scope, format...) /* nothing */
128 #endif
129
130
131 // ktrace support (C style)
132 extern void security_ktrace(int code);
133
134 #endif //__cplusplus
135
136 #endif //_H_DEBUGGING