s3:smbd: add a scavenger process for disconnected durable handles
[obnox/samba/samba-obnox.git] / lib / util / debug.h
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 _DEBUG_H
24 #define _DEBUG_H
25
26 /* -------------------------------------------------------------------------- **
27  * Debugging code.  See also debug.c
28  */
29
30 /* the maximum debug level to compile into the code. This assumes a good
31    optimising compiler that can remove unused code
32    for embedded or low-memory systems set this to a value like 2 to get
33    only important messages. This gives *much* smaller binaries
34 */
35 #ifndef MAX_DEBUG_LEVEL
36 #define MAX_DEBUG_LEVEL 1000
37 #endif
38
39 int  Debug1( const char *, ... ) PRINTF_ATTRIBUTE(1,2);
40 bool dbgtext( const char *, ... ) PRINTF_ATTRIBUTE(1,2);
41 bool dbghdrclass( int level, int cls, const char *location, const char *func);
42 bool dbghdr( int level, const char *location, const char *func);
43
44 /*
45  * Redefine DEBUGLEVEL because so we don't have to change every source file
46  * that *unnecessarily* references it.
47  */
48 #define DEBUGLEVEL DEBUGLEVEL_CLASS[DBGC_ALL]
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
84 /* Always ensure this is updated when new fixed classes area added, to ensure the array in debug.c is the right size */
85 #define DBGC_MAX_FIXED          20
86
87 /* So you can define DBGC_CLASS before including debug.h */
88 #ifndef DBGC_CLASS
89 #define DBGC_CLASS            0     /* override as shown above */
90 #endif
91
92 extern int  *DEBUGLEVEL_CLASS;
93
94 /* Debugging macros
95  *
96  * DEBUGLVL()
97  *   If the 'file specific' debug class level >= level OR the system-wide
98  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
99  *   generate a header using the default macros for file, line, and
100  *   function name. Returns True if the debug level was <= DEBUGLEVEL.
101  *
102  *   Example: if( DEBUGLVL( 2 ) ) dbgtext( "Some text.\n" );
103  *
104  * DEBUG()
105  *   If the 'file specific' debug class level >= level OR the system-wide
106  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
107  *   generate a header using the default macros for file, line, and
108  *   function name. Each call to DEBUG() generates a new header *unless* the
109  *   previous debug output was unterminated (i.e. no '\n').
110  *   See debug.c:dbghdr() for more info.
111  *
112  *   Example: DEBUG( 2, ("Some text and a value %d.\n", value) );
113  *
114  * DEBUGC()
115  *   If the 'macro specified' debug class level >= level OR the system-wide
116  *   DEBUGLEVEL (synomym for DEBUGLEVEL_CLASS[ DBGC_ALL ]) >= level then
117  *   generate a header using the default macros for file, line, and
118  *   function name. Each call to DEBUG() generates a new header *unless* the
119  *   previous debug output was unterminated (i.e. no '\n').
120  *   See debug.c:dbghdr() for more info.
121  *
122  *   Example: DEBUGC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
123  *
124  *  DEBUGADD(), DEBUGADDC()
125  *    Same as DEBUG() and DEBUGC() except the text is appended to the previous
126  *    DEBUG(), DEBUGC(), DEBUGADD(), DEBUGADDC() with out another interviening
127  *    header.
128  *
129  *    Example: DEBUGADD( 2, ("Some text and a value %d.\n", value) );
130  *             DEBUGADDC( DBGC_TDB, 2, ("Some text and a value %d.\n", value) );
131  *
132  * Note: If the debug class has not be redeined (see above) then the optimizer
133  * will remove the extra conditional test.
134  */
135
136 /*
137  * From talloc.c:
138  */
139
140 /* these macros gain us a few percent of speed on gcc */
141 #if (__GNUC__ >= 3)
142 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
143    as its first argument */
144 #ifndef likely
145 #define likely(x)   __builtin_expect(!!(x), 1)
146 #endif
147 #ifndef unlikely
148 #define unlikely(x) __builtin_expect(!!(x), 0)
149 #endif
150 #else
151 #ifndef likely
152 #define likely(x) (x)
153 #endif
154 #ifndef unlikely
155 #define unlikely(x) (x)
156 #endif
157 #endif
158
159 #define CHECK_DEBUGLVL( level ) \
160   ( ((level) <= MAX_DEBUG_LEVEL) && \
161     unlikely(DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level)))
162
163 #define DEBUGLVL( level ) \
164   ( CHECK_DEBUGLVL(level) \
165    && dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ ) )
166
167
168 #define DEBUG( level, body ) \
169   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
170           unlikely(DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level))           \
171        && (dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ )) \
172        && (dbgtext body) )
173
174 #define DEBUGC( dbgc_class, level, body ) \
175   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
176           unlikely(DEBUGLEVEL_CLASS[ dbgc_class ] >= (level))           \
177        && (dbghdrclass( level, DBGC_CLASS, __location__, __FUNCTION__ )) \
178        && (dbgtext body) )
179
180 #define DEBUGADD( level, body ) \
181   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
182           unlikely(DEBUGLEVEL_CLASS[ DBGC_CLASS ] >= (level))   \
183        && (dbgtext body) )
184
185 #define DEBUGADDC( dbgc_class, level, body ) \
186   (void)( ((level) <= MAX_DEBUG_LEVEL) && \
187           unlikely((DEBUGLEVEL_CLASS[ dbgc_class ] >= (level))) \
188        && (dbgtext body) )
189
190 /* Print a separator to the debug log. */
191 #define DEBUGSEP(level)\
192         DEBUG((level),("===============================================================\n"))
193
194 /* The following definitions come from lib/debug.c  */
195
196 /** Possible destinations for the debug log (in order of precedence -
197  * once set to DEBUG_FILE, it is not possible to reset to DEBUG_STDOUT
198  * for example.  This makes it easy to override for debug to stderr on
199  * the command line, as the smb.conf cannot reset it back to
200  * file-based logging */
201 enum debug_logtype {DEBUG_DEFAULT_STDERR = 0, DEBUG_DEFAULT_STDOUT = 1, DEBUG_FILE = 2, DEBUG_STDOUT = 3, DEBUG_STDERR = 4, DEBUG_CALLBACK = 5};
202
203 struct debug_settings {
204         size_t max_log_size;
205         int syslog;
206         bool syslog_only;
207         bool timestamp_logs;
208         bool debug_prefix_timestamp;
209         bool debug_hires_timestamp;
210         bool debug_pid;
211         bool debug_uid;
212         bool debug_class;
213 };
214
215 void setup_logging(const char *prog_name, enum debug_logtype new_logtype);
216
217 void debug_close_dbf(void);
218 void gfree_debugsyms(void);
219 int debug_add_class(const char *classname);
220 int debug_lookup_classname(const char *classname);
221 bool debug_parse_levels(const char *params_str);
222 void debug_setup_talloc_log(void);
223 void debug_set_logfile(const char *name);
224 void debug_set_settings(struct debug_settings *settings);
225 bool reopen_logs_internal( void );
226 void force_check_log_size( void );
227 bool need_to_check_log_size( void );
228 void check_log_size( void );
229 void dbgflush( void );
230 bool dbghdrclass(int level, int cls, const char *location, const char *func);
231 bool dbghdr(int level, const char *location, const char *func);
232 bool debug_get_output_is_stderr(void);
233 bool debug_get_output_is_stdout(void);
234 void debug_schedule_reopen_logs(void);
235 char *debug_list_class_names_and_levels(void);
236
237 typedef void (*debug_callback_fn)(void *private_ptr, int level, const char *msg);
238
239 /**
240    Set a callback for all debug messages.  Use in dlz_bind9 to push output to the bind logs
241  */
242 void debug_set_callback(void *private_ptr, debug_callback_fn fn);
243
244 /**
245   log suspicious usage - print comments and backtrace
246 */      
247 _PUBLIC_ void log_suspicious_usage(const char *from, const char *info);
248
249 /**
250   print suspicious usage - print comments and backtrace
251 */      
252 _PUBLIC_ void print_suspicious_usage(const char* from, const char* info);
253 _PUBLIC_ uint32_t get_task_id(void);
254 _PUBLIC_ void log_task_id(void);
255
256 /* the debug operations structure - contains function pointers to
257    various debug implementations of each operation */
258 struct debug_ops {
259         /* function to log (using DEBUG) suspicious usage of data structure */
260         void (*log_suspicious_usage)(const char* from, const char* info);
261
262         /* function to log (using printf) suspicious usage of data structure.
263          * To be used in circumstances when using DEBUG would cause loop. */
264         void (*print_suspicious_usage)(const char* from, const char* info);
265
266         /* function to return process/thread id */
267         uint32_t (*get_task_id)(void);
268
269         /* function to log process/thread id */
270         void (*log_task_id)(int fd);
271 };
272
273 /**
274   register a set of debug handlers. 
275 */
276 _PUBLIC_ void register_debug_handlers(const char *name, struct debug_ops *ops);
277
278 #endif