190792Sgshapiro/*
2261363Sgshapiro * Copyright (c) 2000, 2001, 2003 Proofpoint, Inc. and its suppliers.
390792Sgshapiro *	All rights reserved.
490792Sgshapiro *
590792Sgshapiro * By using this file, you agree to the terms and conditions set
690792Sgshapiro * forth in the LICENSE file which can be found at the top level of
790792Sgshapiro * the sendmail distribution.
890792Sgshapiro *
9266692Sgshapiro *	$Id: debug.h,v 1.17 2013-11-22 20:51:31 ca Exp $
1090792Sgshapiro */
1190792Sgshapiro
1290792Sgshapiro/*
1390792Sgshapiro**  libsm debugging and tracing
1490792Sgshapiro**  See libsm/debug.html for documentation.
1590792Sgshapiro*/
1690792Sgshapiro
1790792Sgshapiro#ifndef SM_DEBUG_H
1890792Sgshapiro# define SM_DEBUG_H
1990792Sgshapiro
2090792Sgshapiro# include <sm/gen.h>
2190792Sgshapiro# include <sm/io.h>
2290792Sgshapiro
2390792Sgshapiro/*
2490792Sgshapiro**  abstractions for printing trace messages
2590792Sgshapiro*/
2690792Sgshapiro
2790792Sgshapiroextern SM_FILE_T *
2890792Sgshapirosm_debug_file __P((void));
2990792Sgshapiro
3090792Sgshapiroextern void
31132943Sgshapirosm_debug_setfile __P(( SM_FILE_T *));
3290792Sgshapiro
3390792Sgshapiroextern void PRINTFLIKE(1, 2)
34132943Sgshapirosm_dprintf __P((char *_fmt, ...));
3590792Sgshapiro
3690792Sgshapiroextern void
3790792Sgshapirosm_dflush __P((void));
3890792Sgshapiro
39132943Sgshapiroextern void
40132943Sgshapirosm_debug_close __P((void));
41132943Sgshapiro
4290792Sgshapiro/*
4390792Sgshapiro**  abstractions for setting and testing debug activation levels
4490792Sgshapiro*/
4590792Sgshapiro
4690792Sgshapiroextern void
47132943Sgshapirosm_debug_addsettings_x __P((const char *));
4890792Sgshapiro
4990792Sgshapiroextern void
50132943Sgshapirosm_debug_addsetting_x __P((const char *, int));
5190792Sgshapiro
5290792Sgshapiro# define SM_DEBUG_UNKNOWN	((SM_ATOMIC_UINT_T)(-1))
5390792Sgshapiro
5490792Sgshapiroextern const char SmDebugMagic[];
5590792Sgshapiro
5690792Sgshapirotypedef struct sm_debug SM_DEBUG_T;
5790792Sgshapirostruct sm_debug
5890792Sgshapiro{
5990792Sgshapiro	const char *sm_magic;	/* points to SmDebugMagic */
6090792Sgshapiro
6190792Sgshapiro	/*
6290792Sgshapiro	**  debug_level is the activation level of this debug
6390792Sgshapiro	**  object.  Level 0 means no debug activity.
6490792Sgshapiro	**  It is initialized to SM_DEBUG_UNKNOWN, which indicates
6590792Sgshapiro	**  that the true value is unknown.  If debug_level ==
6690792Sgshapiro	**  SM_DEBUG_UNKNOWN, then the access functions will look up
6790792Sgshapiro	**  its true value in the internal table of debug settings.
6890792Sgshapiro	*/
6990792Sgshapiro
7090792Sgshapiro	SM_ATOMIC_UINT_T debug_level;
7190792Sgshapiro
7290792Sgshapiro	/*
7390792Sgshapiro	**  debug_name is the name used to reference this SM_DEBUG
7490792Sgshapiro	**  structure via the sendmail -d option.
7590792Sgshapiro	*/
7690792Sgshapiro
7790792Sgshapiro	char *debug_name;
7890792Sgshapiro
7990792Sgshapiro	/*
8090792Sgshapiro	**  debug_desc is a literal character string of the form
8190792Sgshapiro	**  "@(#)$Debug: <name> - <short description> $"
8290792Sgshapiro	*/
8390792Sgshapiro
8490792Sgshapiro	char *debug_desc;
8590792Sgshapiro
8690792Sgshapiro	/*
8790792Sgshapiro	**  We keep a linked list of initialized SM_DEBUG structures
8890792Sgshapiro	**  so that when sm_debug_addsetting is called, we can reset
8990792Sgshapiro	**  them all back to the uninitialized state.
9090792Sgshapiro	*/
9190792Sgshapiro
9290792Sgshapiro	SM_DEBUG_T *debug_next;
9390792Sgshapiro};
9490792Sgshapiro
9590792Sgshapiro# ifndef SM_DEBUG_CHECK
9690792Sgshapiro#  define SM_DEBUG_CHECK 1
9790792Sgshapiro# endif /* ! SM_DEBUG_CHECK */
9890792Sgshapiro
9990792Sgshapiro# if SM_DEBUG_CHECK
10090792Sgshapiro/*
10190792Sgshapiro**  This macro is cleverly designed so that if the debug object is below
10290792Sgshapiro**  the specified level, then the only overhead is a single comparison
10390792Sgshapiro**  (except for the first time this macro is invoked).
10490792Sgshapiro*/
10590792Sgshapiro
10690792Sgshapiro#  define sm_debug_active(debug, level) \
10790792Sgshapiro	    ((debug)->debug_level >= (level) && \
10890792Sgshapiro	     ((debug)->debug_level != SM_DEBUG_UNKNOWN || \
10990792Sgshapiro	      sm_debug_loadactive(debug, level)))
11090792Sgshapiro
11190792Sgshapiro#  define sm_debug_level(debug) \
11290792Sgshapiro	    ((debug)->debug_level == SM_DEBUG_UNKNOWN \
11390792Sgshapiro	     ? sm_debug_loadlevel(debug) : (debug)->debug_level)
11490792Sgshapiro
11590792Sgshapiro#  define sm_debug_unknown(debug) ((debug)->debug_level == SM_DEBUG_UNKNOWN)
11690792Sgshapiro# else /* SM_DEBUG_CHECK */
11790792Sgshapiro#  define sm_debug_active(debug, level)	0
11890792Sgshapiro#  define sm_debug_level(debug)		0
11990792Sgshapiro#  define sm_debug_unknown(debug)	0
12090792Sgshapiro# endif /* SM_DEBUG_CHECK */
12190792Sgshapiro
12290792Sgshapiroextern bool
123132943Sgshapirosm_debug_loadactive __P((SM_DEBUG_T *, int));
12490792Sgshapiro
12590792Sgshapiroextern int
126132943Sgshapirosm_debug_loadlevel __P((SM_DEBUG_T *));
12790792Sgshapiro
12890792Sgshapiro# define SM_DEBUG_INITIALIZER(name, desc) { \
12990792Sgshapiro		SmDebugMagic, \
13090792Sgshapiro		SM_DEBUG_UNKNOWN, \
13190792Sgshapiro		name, \
13290792Sgshapiro		desc, \
13390792Sgshapiro		NULL}
13490792Sgshapiro
13590792Sgshapiro#endif /* ! SM_DEBUG_H */
136