xref: /illumos-gate/usr/src/contrib/mDNSResponder/mDNSCore/mDNSDebug.h (revision f52943a93040563107b95bccb9db87d9971ef47d)
1 /* -*- Mode: C; tab-width: 4 -*-
2  *
3  * Copyright (c) 2002-2015 Apple Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef __mDNSDebug_h
19 #define __mDNSDebug_h
20 
21 // Set MDNS_DEBUGMSGS to 0 to optimize debugf() calls out of the compiled code
22 // Set MDNS_DEBUGMSGS to 1 to generate normal debugging messages
23 // Set MDNS_DEBUGMSGS to 2 to generate verbose debugging messages
24 // MDNS_DEBUGMSGS is normally set in the project options (or makefile) but can also be set here if desired
25 // (If you edit the file here to turn on MDNS_DEBUGMSGS while you're debugging some code, be careful
26 // not to accidentally check-in that change by mistake when you check in your other changes.)
27 
28 //#undef MDNS_DEBUGMSGS
29 //#define MDNS_DEBUGMSGS 2
30 
31 // Set MDNS_CHECK_PRINTF_STYLE_FUNCTIONS to 1 to enable extra GCC compiler warnings
32 // Note: You don't normally want to do this, because it generates a bunch of
33 // spurious warnings for the following custom extensions implemented by mDNS_vsnprintf:
34 //    warning: `#' flag used with `%s' printf format    (for %#s              -- pascal string format)
35 //    warning: repeated `#' flag in format              (for %##s             -- DNS name string format)
36 //    warning: double format, pointer arg (arg 2)       (for %.4a, %.16a, %#a -- IP address formats)
37 #define MDNS_CHECK_PRINTF_STYLE_FUNCTIONS 0
38 
39 typedef enum
40 {
41     MDNS_LOG_MSG,
42     MDNS_LOG_OPERATION,
43     MDNS_LOG_SPS,
44     MDNS_LOG_INFO,
45     MDNS_LOG_DEBUG,
46 } mDNSLogLevel_t;
47 
48 // Set this symbol to 1 to answer remote queries for our Address, reverse mapping PTR, and HINFO records
49 #define ANSWER_REMOTE_HOSTNAME_QUERIES 0
50 
51 // Set this symbol to 1 to do extra debug checks on malloc() and free()
52 // Set this symbol to 2 to write a log message for every malloc() and free()
53 //#define MACOSX_MDNS_MALLOC_DEBUGGING 1
54 
55 //#define ForceAlerts 1
56 //#define LogTimeStamps 1
57 
58 // Developer-settings section ends here
59 
60 #if MDNS_CHECK_PRINTF_STYLE_FUNCTIONS
61 #define IS_A_PRINTF_STYLE_FUNCTION(F,A) __attribute__ ((format(printf,F,A)))
62 #else
63 #define IS_A_PRINTF_STYLE_FUNCTION(F,A)
64 #endif
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
70 // Variable argument macro support. Use ANSI C99 __VA_ARGS__ where possible. Otherwise, use the next best thing.
71 
72 #if (defined(__GNUC__))
73     #if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 2)))
74         #define MDNS_C99_VA_ARGS        1
75         #define MDNS_GNU_VA_ARGS        0
76     #else
77         #define MDNS_C99_VA_ARGS        0
78         #define MDNS_GNU_VA_ARGS        1
79     #endif
80     #define MDNS_HAS_VA_ARG_MACROS      1
81 #elif (_MSC_VER >= 1400) // Visual Studio 2005 and later
82     #define MDNS_C99_VA_ARGS            1
83     #define MDNS_GNU_VA_ARGS            0
84     #define MDNS_HAS_VA_ARG_MACROS      1
85 #elif (defined(__MWERKS__))
86     #define MDNS_C99_VA_ARGS            1
87     #define MDNS_GNU_VA_ARGS            0
88     #define MDNS_HAS_VA_ARG_MACROS      1
89 #else
90     #define MDNS_C99_VA_ARGS            0
91     #define MDNS_GNU_VA_ARGS            0
92     #define MDNS_HAS_VA_ARG_MACROS      0
93 #endif
94 
95 #if (MDNS_HAS_VA_ARG_MACROS)
96     #if (MDNS_C99_VA_ARGS)
97         #define debug_noop(... ) ((void)0)
98         #define LogMsg(... )           LogMsgWithLevel(MDNS_LOG_MSG, __VA_ARGS__)
99         #define LogOperation(... )     do { if (mDNS_LoggingEnabled) LogMsgWithLevel(MDNS_LOG_OPERATION, __VA_ARGS__);} while (0)
100         #define LogSPS(... )           do { if (mDNS_LoggingEnabled) LogMsgWithLevel(MDNS_LOG_SPS,       __VA_ARGS__);} while (0)
101         #define LogInfo(... )          do { if (mDNS_LoggingEnabled) LogMsgWithLevel(MDNS_LOG_INFO,      __VA_ARGS__);} while (0)
102     #elif (MDNS_GNU_VA_ARGS)
103         #define debug_noop( ARGS... ) ((void)0)
104         #define LogMsg( ARGS... )       LogMsgWithLevel(MDNS_LOG_MSG, ARGS)
105         #define LogOperation( ARGS... ) do { if (mDNS_LoggingEnabled) LogMsgWithLevel(MDNS_LOG_OPERATION, ARGS);} while (0)
106         #define LogSPS( ARGS... )       do { if (mDNS_LoggingEnabled) LogMsgWithLevel(MDNS_LOG_SPS,       ARGS);} while (0)
107         #define LogInfo( ARGS... )      do { if (mDNS_LoggingEnabled) LogMsgWithLevel(MDNS_LOG_INFO,      ARGS);} while (0)
108     #else
109         #error Unknown variadic macros
110     #endif
111 #else
112 // If your platform does not support variadic macros, you need to define the following variadic functions.
113 // See mDNSShared/mDNSDebug.c for sample implementation
114     #define debug_noop 1 ? (void)0 : (void)
115     #define LogMsg LogMsg_
116     #define LogOperation (mDNS_LoggingEnabled == 0) ? ((void)0) : LogOperation_
117     #define LogSPS       (mDNS_LoggingEnabled == 0) ? ((void)0) : LogSPS_
118     #define LogInfo      (mDNS_LoggingEnabled == 0) ? ((void)0) : LogInfo_
119 extern void LogMsg_(const char *format, ...)       IS_A_PRINTF_STYLE_FUNCTION(1,2);
120 extern void LogOperation_(const char *format, ...) IS_A_PRINTF_STYLE_FUNCTION(1,2);
121 extern void LogSPS_(const char *format, ...)       IS_A_PRINTF_STYLE_FUNCTION(1,2);
122 extern void LogInfo_(const char *format, ...)      IS_A_PRINTF_STYLE_FUNCTION(1,2);
123 #endif
124 
125 #if MDNS_DEBUGMSGS
126 #define debugf debugf_
127 extern void debugf_(const char *format, ...) IS_A_PRINTF_STYLE_FUNCTION(1,2);
128 #else
129 #define debugf debug_noop
130 #endif
131 
132 #if MDNS_DEBUGMSGS > 1
133 #define verbosedebugf verbosedebugf_
134 extern void verbosedebugf_(const char *format, ...) IS_A_PRINTF_STYLE_FUNCTION(1,2);
135 #else
136 #define verbosedebugf debug_noop
137 #endif
138 
139 extern int mDNS_LoggingEnabled;
140 extern int mDNS_PacketLoggingEnabled;
141 extern int mDNS_McastLoggingEnabled;
142 extern int mDNS_McastTracingEnabled;
143 extern int mDNS_DebugMode;          // If non-zero, LogMsg() writes to stderr instead of syslog
144 extern const char ProgramName[];
145 
146 extern void LogMsgWithLevel(mDNSLogLevel_t logLevel, const char *format, ...) IS_A_PRINTF_STYLE_FUNCTION(2,3);
147 // LogMsgNoIdent needs to be fixed so that it logs without the ident prefix like it used to
148 // (or completely overhauled to use the new "log to a separate file" facility)
149 #define LogMsgNoIdent LogMsg
150 
151 #if APPLE_OSX_mDNSResponder
152 extern void LogFatalError(const char *format, ...);
153 #else
154 #define LogFatalError LogMsg
155 #endif
156 
157 #if APPLE_OSX_mDNSResponder && MACOSX_MDNS_MALLOC_DEBUGGING >= 1
158 extern void *mallocL(char *msg, unsigned int size);
159 extern void freeL(char *msg, void *x);
160 extern void uds_validatelists(void);
161 extern void udns_validatelists(void *const v);
162 extern void LogMemCorruption(const char *format, ...);
163 #else
164 #define mallocL(X,Y) malloc(Y)
165 #define freeL(X,Y) free(Y)
166 #endif
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif
173