xref: /illumos-gate/usr/src/contrib/mDNSResponder/mDNSCore/mDNSDebug.h (revision c94be9439c4f0773ef60e2cec21d548359cfea20)
1 /* -*- Mode: C; tab-width: 4 -*-
2  *
3  * Copyright (c) 2002-2018 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            1
91     #define MDNS_GNU_VA_ARGS            0
92     #define MDNS_HAS_VA_ARG_MACROS      1
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         #define LogDebug(... )         do { if (mDNS_LoggingEnabled) LogMsgWithLevel(MDNS_LOG_DEBUG,     __VA_ARGS__);} while (0)
103     #elif (MDNS_GNU_VA_ARGS)
104         #define debug_noop( ARGS... ) ((void)0)
105         #define LogMsg( ARGS... )       LogMsgWithLevel(MDNS_LOG_MSG, ARGS)
106         #define LogOperation( ARGS... ) do { if (mDNS_LoggingEnabled) LogMsgWithLevel(MDNS_LOG_OPERATION, ARGS);} while (0)
107         #define LogSPS( ARGS... )       do { if (mDNS_LoggingEnabled) LogMsgWithLevel(MDNS_LOG_SPS,       ARGS);} while (0)
108         #define LogInfo( ARGS... )      do { if (mDNS_LoggingEnabled) LogMsgWithLevel(MDNS_LOG_INFO,      ARGS);} while (0)
109         #define LogDebug( ARGS... )     do { if (mDNS_LoggingEnabled) LogMsgWithLevel(MDNS_LOG_DEBUG,     ARGS);} while (0)
110     #else
111         #error Unknown variadic macros
112     #endif
113 #else
114 // If your platform does not support variadic macros, you need to define the following variadic functions.
115 // See mDNSShared/mDNSDebug.c for sample implementation
116     #define debug_noop 1 ? (void)0 : (void)
117     #define LogMsg LogMsg_
118     #define LogOperation (mDNS_LoggingEnabled == 0) ? ((void)0) : LogOperation_
119     #define LogSPS       (mDNS_LoggingEnabled == 0) ? ((void)0) : LogSPS_
120     #define LogInfo      (mDNS_LoggingEnabled == 0) ? ((void)0) : LogInfo_
121     #define LogDebug     (mDNS_LoggingEnabled == 0) ? ((void)0) : LogDebug_
122 extern void LogMsg_(const char *format, ...)       IS_A_PRINTF_STYLE_FUNCTION(1,2);
123 extern void LogOperation_(const char *format, ...) IS_A_PRINTF_STYLE_FUNCTION(1,2);
124 extern void LogSPS_(const char *format, ...)       IS_A_PRINTF_STYLE_FUNCTION(1,2);
125 extern void LogInfo_(const char *format, ...)      IS_A_PRINTF_STYLE_FUNCTION(1,2);
126 extern void LogDebug_(const char *format, ...)     IS_A_PRINTF_STYLE_FUNCTION(1,2);
127 #endif
128 
129 #if MDNS_DEBUGMSGS
130 #define debugf debugf_
131 extern void debugf_(const char *format, ...) IS_A_PRINTF_STYLE_FUNCTION(1,2);
132 #else
133 #define debugf debug_noop
134 #endif
135 
136 #if MDNS_DEBUGMSGS > 1
137 #define verbosedebugf verbosedebugf_
138 extern void verbosedebugf_(const char *format, ...) IS_A_PRINTF_STYLE_FUNCTION(1,2);
139 #else
140 #define verbosedebugf debug_noop
141 #endif
142 
143 extern int mDNS_LoggingEnabled;
144 extern int mDNS_PacketLoggingEnabled;
145 extern int mDNS_McastLoggingEnabled;
146 extern int mDNS_McastTracingEnabled;
147 extern int mDNS_DebugMode;          // If non-zero, LogMsg() writes to stderr instead of syslog
148 extern const char ProgramName[];
149 
150 extern void LogMsgWithLevel(mDNSLogLevel_t logLevel, const char *format, ...) IS_A_PRINTF_STYLE_FUNCTION(2,3);
151 // LogMsgNoIdent needs to be fixed so that it logs without the ident prefix like it used to
152 // (or completely overhauled to use the new "log to a separate file" facility)
153 #define LogMsgNoIdent LogMsg
154 
155 #if APPLE_OSX_mDNSResponder
156 extern void LogFatalError(const char *format, ...);
157 #else
158 #define LogFatalError LogMsg
159 #endif
160 
161 #if APPLE_OSX_mDNSResponder && MACOSX_MDNS_MALLOC_DEBUGGING >= 1
162 extern void *mallocL(char *msg, unsigned int size);
163 extern void freeL(char *msg, void *x);
164 extern void uds_validatelists(void);
165 extern void udns_validatelists(void *const v);
166 extern void LogMemCorruption(const char *format, ...);
167 #else
168 #define mallocL(X,Y) malloc(Y)
169 #define freeL(X,Y) free(Y)
170 #endif
171 
172 #ifdef __cplusplus
173 }
174 #endif
175 
176 #endif
177