1 /*
2    Unix SMB/CIFS implementation.
3    SMB debug stuff
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) John H Terpstra 1996-1998
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1998
7    Copyright (C) Paul Ashton 1998
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #ifndef _SAMBA_DEBUG_H
24 #define _SAMBA_DEBUG_H
25 
26 #include <stdbool.h>
27 #include <stddef.h>
28 #include <stdarg.h>
29 #include "attr.h"
30 
31 
32 /* -------------------------------------------------------------------------- **
33  * Debugging code.  See also debug.c
34  */
35 
36 /* the maximum debug level to compile into the code. This assumes a good
37    optimising compiler that can remove unused code
38    for embedded or low-memory systems set this to a value like 2 to get
39    only important messages. This gives *much* smaller binaries
40 */
41 #ifndef MAX_DEBUG_LEVEL
42 #define MAX_DEBUG_LEVEL 1000
43 #endif
44 
45 bool dbgtext_va(const char *, va_list ap) PRINTF_ATTRIBUTE(1,0);
46 bool dbgtext( const char *, ... ) PRINTF_ATTRIBUTE(1,2);
47 bool dbghdrclass( int level, int cls, const char *location, const char *func);
48 bool dbgsetclass(int level, int cls);
49 
50 /*
51  * Define all new debug classes here. A class is represented by an entry in
52  * the DEBUGLEVEL_CLASS array. Index zero of this arrray is equivalent to the
53  * old DEBUGLEVEL. Any source file that does NOT add the following lines:
54  *
55  *   #undef  DBGC_CLASS
56  *   #define DBGC_CLASS DBGC_<your class name here>
57  *
58  * at the start of the file (after #include "includes.h") will default to
59  * using index zero, so it will behaive just like it always has.
60  */
61 #define DBGC_ALL		0 /* index equivalent to DEBUGLEVEL */
62 
63 #define DBGC_TDB		1
64 #define DBGC_PRINTDRIVERS	2
65 #define DBGC_LANMAN		3
66 #define DBGC_SMB		4
67 #define DBGC_RPC_PARSE		5
68 #define DBGC_RPC_SRV		6
69 #define DBGC_RPC_CLI		7
70 #define DBGC_PASSDB		8
71 #define DBGC_SAM		9
72 #define DBGC_AUTH		10
73 #define DBGC_WINBIND		11
74 #define DBGC_VFS		12
75 #define DBGC_IDMAP		13
76 #define DBGC_QUOTA		14
77 #define DBGC_ACLS		15
78 #define DBGC_LOCKING		16
79 #define DBGC_MSDFS		17
80 #define DBGC_DMAPI		18
81 #define DBGC_REGISTRY		19
82 #define DBGC_SCAVENGER		20
83 #define DBGC_DNS		21
84 #define DBGC_LDB		22
85 #define DBGC_TEVENT		23
86 #define DBGC_AUTH_AUDIT		24
87 #define DBGC_AUTH_AUDIT_JSON	25
88 #define DBGC_KERBEROS           26
89 #define DBGC_DRS_REPL           27
90 #define DBGC_SMB2               28
91 #define DBGC_SMB2_CREDITS       29
92 #define DBGC_DSDB_AUDIT	30
93 #define DBGC_DSDB_AUDIT_JSON	31
94 #define DBGC_DSDB_PWD_AUDIT		32
95 #define DBGC_DSDB_PWD_AUDIT_JSON	33
96 #define DBGC_DSDB_TXN_AUDIT		34
97 #define DBGC_DSDB_TXN_AUDIT_JSON	35
98 #define DBGC_DSDB_GROUP_AUDIT	36
99 #define DBGC_DSDB_GROUP_AUDIT_JSON	37
100 
101 /* So you can define DBGC_CLASS before including debug.h */
102 #ifndef DBGC_CLASS
103 #define DBGC_CLASS            0     /* override as shown above */
104 #endif
105 
106 #define DEBUGLEVEL debuglevel_get()
107 
108 #define debuglevel_get() debuglevel_get_class(DBGC_ALL)
109 #define debuglevel_set(lvl) debuglevel_set_class(DBGC_ALL, (lvl))
110 
111 /* Debugging macros
112  *
113  * DEBUGLVL()
114  *   If the 'file specific' debug class level >= level OR the system-wide
115  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
116  *   generate a header using the default macros for file, line, and
117  *   function name. Returns True if the debug level was <= DEBUGLEVEL.
118  *
119  *   Example: if( DEBUGLVL( 2 ) ) dbgtext( "Some text.\n" );
120  *
121  * DEBUG()
122  *   If the 'file specific' debug class level >= level OR the system-wide
123  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
124  *   generate a header using the default macros for file, line, and
125  *   function name. Each call to DEBUG() generates a new header *unless* the
126  *   previous debug output was unterminated (i.e. no '\n').
127  *   See debug.c:dbghdr() for more info.
128  *
129  *   Example: DEBUG( 2, ("Some text and a value %d.\n", value) );
130  *
131  * DEBUGC()
132  *   If the 'macro specified' debug class level >= level OR the system-wide
133  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
134  *   generate a header using the default macros for file, line, and
135  *   function name. Each call to DEBUG() generates a new header *unless* the
136  *   previous debug output was unterminated (i.e. no '\n').
137  *   See debug.c:dbghdr() for more info.
138  *
139  *   Example: DEBUGC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
140  *
141  *  DEBUGADD(), DEBUGADDC()
142  *    Same as DEBUG() and DEBUGC() except the text is appended to the previous
143  *    DEBUG(), DEBUGC(), DEBUGADD(), DEBUGADDC() with out another interviening
144  *    header.
145  *
146  *    Example: DEBUGADD( 2, ("Some text and a value %d.\n", value) );
147  *             DEBUGADDC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
148  *
149  * Note: If the debug class has not be redeined (see above) then the optimizer
150  * will remove the extra conditional test.
151  */
152 
153 /*
154  * From talloc.c:
155  */
156 
157 /* these macros gain us a few percent of speed on gcc */
158 #if (__GNUC__ >= 3)
159 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
160    as its first argument */
161 #ifndef likely
162 #define likely(x)   __builtin_expect(!!(x), 1)
163 #endif
164 #ifndef unlikely
165 #define unlikely(x) __builtin_expect(!!(x), 0)
166 #endif
167 #else
168 #ifndef likely
169 #define likely(x) (x)
170 #endif
171 #ifndef unlikely
172 #define unlikely(x) (x)
173 #endif
174 #endif
175 
176 int debuglevel_get_class(size_t idx);
177 void debuglevel_set_class(size_t idx, int level);
178 
179 #define CHECK_DEBUGLVL( level ) \
180   ( ((level) <= MAX_DEBUG_LEVEL) && \
181     unlikely(debuglevel_get_class(DBGC_CLASS) >= (level)))
182 
183 #define CHECK_DEBUGLVLC( dbgc_class, level ) \
184   ( ((level) <= MAX_DEBUG_LEVEL) && \
185     unlikely(debuglevel_get_class(dbgc_class) >= (level)))
186 
187 #define DEBUGLVL( level ) \
188   ( CHECK_DEBUGLVL(level) \
189    && dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ ) )
190 
191 #define DEBUGLVLC( dbgc_class, level ) \
192   ( CHECK_DEBUGLVLC( dbgc_class, level ) \
193    && dbghdrclass( level, dbgc_class, __location__, __FUNCTION__ ) )
194 
195 #define DEBUG( level, body ) \
196   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
197        unlikely(debuglevel_get_class(DBGC_CLASS) >= (level))             \
198        && (dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ )) \
199        && (dbgtext body) )
200 
201 #define DEBUGC( dbgc_class, level, body ) \
202   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
203        unlikely(debuglevel_get_class(dbgc_class) >= (level))             \
204        && (dbghdrclass( level, dbgc_class, __location__, __FUNCTION__ )) \
205        && (dbgtext body) )
206 
207 #define DEBUGADD( level, body ) \
208   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
209        unlikely(debuglevel_get_class(DBGC_CLASS) >= (level)) \
210        && (dbgsetclass(level, DBGC_CLASS))                   \
211        && (dbgtext body) )
212 
213 #define DEBUGADDC( dbgc_class, level, body ) \
214   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
215        unlikely((debuglevel_get_class(dbgc_class) >= (level))) \
216        && (dbgsetclass(level, dbgc_class))                     \
217        && (dbgtext body) )
218 
219 /* Print a separator to the debug log. */
220 #define DEBUGSEP(level)\
221 	DEBUG((level),("===============================================================\n"))
222 
223 /* Prefix messages with the function name */
224 #define DBG_PREFIX(level, body ) \
225 	(void)( ((level) <= MAX_DEBUG_LEVEL) &&			\
226 		unlikely(debuglevel_get_class(DBGC_CLASS) >= (level))	\
227 		&& (dbghdrclass(level, DBGC_CLASS, __location__, __func__ )) \
228 		&& (dbgtext("%s: ", __func__))				\
229 		&& (dbgtext body) )
230 
231 /* Prefix messages with the function name - class specific */
232 #define DBGC_PREFIX(dbgc_class, level, body ) \
233 	(void)( ((level) <= MAX_DEBUG_LEVEL) &&			\
234 		unlikely(debuglevel_get_class(dbgc_class) >= (level))	\
235 		&& (dbghdrclass(level, dbgc_class, __location__, __func__ )) \
236 		&& (dbgtext("%s: ", __func__))				\
237 		&& (dbgtext body) )
238 
239 /*
240  * Debug levels matching RFC 3164
241  */
242 #define DBGLVL_ERR	 0	/* error conditions */
243 #define DBGLVL_WARNING	 1	/* warning conditions */
244 #define DBGLVL_NOTICE	 3	/* normal, but significant, condition */
245 #define DBGLVL_INFO	 5	/* informational message */
246 #define DBGLVL_DEBUG	10	/* debug-level message */
247 
248 #define DBG_ERR(...)		DBG_PREFIX(DBGLVL_ERR,		(__VA_ARGS__))
249 #define DBG_WARNING(...)	DBG_PREFIX(DBGLVL_WARNING,	(__VA_ARGS__))
250 #define DBG_NOTICE(...)		DBG_PREFIX(DBGLVL_NOTICE,	(__VA_ARGS__))
251 #define DBG_INFO(...)		DBG_PREFIX(DBGLVL_INFO,		(__VA_ARGS__))
252 #define DBG_DEBUG(...)		DBG_PREFIX(DBGLVL_DEBUG,	(__VA_ARGS__))
253 
254 #define DBGC_ERR(dbgc_class, ...)	DBGC_PREFIX(dbgc_class, \
255 						DBGLVL_ERR, (__VA_ARGS__))
256 #define DBGC_WARNING(dbgc_class, ...)	DBGC_PREFIX(dbgc_class, \
257 						DBGLVL_WARNING,	(__VA_ARGS__))
258 #define DBGC_NOTICE(dbgc_class, ...)	DBGC_PREFIX(dbgc_class, \
259 						DBGLVL_NOTICE,	(__VA_ARGS__))
260 #define DBGC_INFO(dbgc_class, ...)	DBGC_PREFIX(dbgc_class, \
261 						DBGLVL_INFO,	(__VA_ARGS__))
262 #define DBGC_DEBUG(dbgc_class, ...)	DBGC_PREFIX(dbgc_class, \
263 						DBGLVL_DEBUG,	(__VA_ARGS__))
264 
265 #define D_ERR(...)		DEBUG(DBGLVL_ERR,	(__VA_ARGS__))
266 #define D_WARNING(...)		DEBUG(DBGLVL_WARNING,	(__VA_ARGS__))
267 #define D_NOTICE(...)		DEBUG(DBGLVL_NOTICE,	(__VA_ARGS__))
268 #define D_INFO(...)		DEBUG(DBGLVL_INFO,	(__VA_ARGS__))
269 #define D_DEBUG(...)		DEBUG(DBGLVL_DEBUG,	(__VA_ARGS__))
270 
271 #define DC_ERR(...)		DEBUGC(dbgc_class, \
272 					DBGLVL_ERR,	(__VA_ARGS__))
273 #define DC_WARNING(...)		DEBUGC(dbgc_class, \
274 					DBGLVL_WARNING,	(__VA_ARGS__))
275 #define DC_NOTICE(...)		DEBUGC(dbgc_class, \
276 					DBGLVL_NOTICE,	(__VA_ARGS__))
277 #define DC_INFO(...)		DEBUGC(dbgc_class, \
278 					DBGLVL_INFO,	(__VA_ARGS__))
279 #define DC_DEBUG(...)		DEBUGC(dbgc_class, \
280 					DBGLVL_DEBUG,	(__VA_ARGS__))
281 
282 /* The following definitions come from lib/debug.c  */
283 
284 /** Possible destinations for the debug log (in order of precedence -
285  * once set to DEBUG_FILE, it is not possible to reset to DEBUG_STDOUT
286  * for example.  This makes it easy to override for debug to stderr on
287  * the command line, as the smb.conf cannot reset it back to
288  * file-based logging */
289 enum debug_logtype {
290 	DEBUG_DEFAULT_STDERR = 0,
291 	DEBUG_DEFAULT_STDOUT = 1,
292 	DEBUG_FILE = 2,
293 	DEBUG_STDOUT = 3,
294 	DEBUG_STDERR = 4,
295 	DEBUG_CALLBACK = 5
296 };
297 
298 struct debug_settings {
299 	size_t max_log_size;
300 	bool timestamp_logs;
301 	bool debug_prefix_timestamp;
302 	bool debug_hires_timestamp;
303 	bool debug_pid;
304 	bool debug_uid;
305 	bool debug_class;
306 };
307 
308 void setup_logging(const char *prog_name, enum debug_logtype new_logtype);
309 
310 void gfree_debugsyms(void);
311 int debug_add_class(const char *classname);
312 bool debug_parse_levels(const char *params_str);
313 void debug_setup_talloc_log(void);
314 void debug_set_logfile(const char *name);
315 void debug_set_settings(struct debug_settings *settings,
316 			const char *logging_param,
317 			int syslog_level, bool syslog_only);
318 bool reopen_logs_internal( void );
319 void force_check_log_size( void );
320 bool need_to_check_log_size( void );
321 void check_log_size( void );
322 void dbgflush( void );
323 bool debug_get_output_is_stderr(void);
324 bool debug_get_output_is_stdout(void);
325 void debug_schedule_reopen_logs(void);
326 char *debug_list_class_names_and_levels(void);
327 
328 typedef void (*debug_callback_fn)(void *private_ptr, int level, const char *msg);
329 
330 /**
331    Set a callback for all debug messages.  Use in dlz_bind9 to push output to the bind logs
332  */
333 void debug_set_callback(void *private_ptr, debug_callback_fn fn);
334 
335 char *debug_get_ringbuf(void);
336 size_t debug_get_ringbuf_size(void);
337 
338 #endif /* _SAMBA_DEBUG_H */
339