1 /**
2  *
3  * \file debug.h
4  * /brief Macro's for debugging
5  *
6  */
7 
8 /*
9  * Copyright (c) 2015, NLnet Labs, Verisign, Inc.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  * * Redistributions of source code must retain the above copyright
15  *   notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above copyright
17  *   notice, this list of conditions and the following disclaimer in the
18  *   documentation and/or other materials provided with the distribution.
19  * * Neither the names of the copyright holders nor the
20  *   names of its contributors may be used to endorse or promote products
21  *   derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef DEBUG_H
36 #define DEBUG_H
37 
38 #include "config.h"
39 #define STUB_DEBUG_ENTRY     "=> ENTRY:       "
40 #define STUB_DEBUG_SETUP     "--- SETUP:      "
41 #define STUB_DEBUG_SETUP_TLS "--- SETUP(TLS): "
42 #define STUB_DEBUG_TSIG      "--- TSIG:       "
43 #define STUB_DEBUG_SCHEDULE  "----- SCHEDULE: "
44 #define STUB_DEBUG_READ      "------- READ:   "
45 #define STUB_DEBUG_WRITE     "------- WRITE:  "
46 #define STUB_DEBUG_CLEANUP   "--- CLEANUP:    "
47 
48 #ifdef GETDNS_ON_WINDOWS
49 #define DEBUG_ON(...) do { \
50 		struct timeval tv_dEbUgSyM; \
51 		struct tm tm_dEbUgSyM; \
52 		char buf_dEbUgSyM[10]; \
53 		time_t tsec_dEbUgSyM; \
54 		\
55 		gettimeofday(&tv_dEbUgSyM, NULL); \
56 		tsec_dEbUgSyM = (time_t) tv_dEbUgSyM.tv_sec; \
57 		gmtime_s(&tm_dEbUgSyM, (const time_t *) &tsec_dEbUgSyM); \
58 		strftime(buf_dEbUgSyM, 10, "%H:%M:%S", &tm_dEbUgSyM); \
59 		fprintf(stderr, "[%s.%.6d] ", buf_dEbUgSyM, (int)tv_dEbUgSyM.tv_usec); \
60 		fprintf(stderr, __VA_ARGS__); \
61 	} while (0)
62 
63 #define DEBUG_NL(...) do {		    \
64 		struct timeval tv_dEbUgSyM; \
65 		struct tm tm_dEbUgSyM; \
66 		char buf_dEbUgSyM[10]; \
67 		time_t tsec_dEbUgSyM; \
68 		\
69 		gettimeofday(&tv_dEbUgSyM, NULL); \
70 		tsec_dEbUgSyM = (time_t) tv_dEbUgSyM.tv_sec; \
71 		gmtime_s(&tm_dEbUgSyM, (const time_t *) &tsec_dEbUgSyM); \
72 		strftime(buf_dEbUgSyM, 10, "%H:%M:%S", &tm_dEbUgSyM); \
73 		fprintf(stderr, "[%s.%.6d] ", buf_dEbUgSyM, (int)tv_dEbUgSyM.tv_usec); \
74 		fprintf(stderr, __VA_ARGS__); \
75 	} while (0)
76 #else
77 #define DEBUG_ON(...) do { \
78 		struct timeval tv_dEbUgSyM; \
79 		struct tm tm_dEbUgSyM; \
80 		char buf_dEbUgSyM[10]; \
81 		\
82 		gettimeofday(&tv_dEbUgSyM, NULL); \
83 		gmtime_r(&tv_dEbUgSyM.tv_sec, &tm_dEbUgSyM); \
84 		strftime(buf_dEbUgSyM, 10, "%H:%M:%S", &tm_dEbUgSyM); \
85 		fprintf(stderr, "[%s.%.6d] ", buf_dEbUgSyM, (int)tv_dEbUgSyM.tv_usec); \
86 		fprintf(stderr, __VA_ARGS__); \
87 	} while (0)
88 
89 #define DEBUG_NL(...) do {		    \
90 		struct timeval tv_dEbUgSyM; \
91 		struct tm tm_dEbUgSyM; \
92 		char buf_dEbUgSyM[10]; \
93 		\
94 		gettimeofday(&tv_dEbUgSyM, NULL); \
95 		gmtime_r(&tv_dEbUgSyM.tv_sec, &tm_dEbUgSyM); \
96 		strftime(buf_dEbUgSyM, 10, "%H:%M:%S", &tm_dEbUgSyM); \
97 		fprintf(stderr, "[%s.%.6d] ", buf_dEbUgSyM, (int)tv_dEbUgSyM.tv_usec); \
98 		fprintf(stderr, __VA_ARGS__); \
99 		fprintf(stderr, "\n"); \
100 	} while (0)
101 #endif
102 
103 #define DEBUG_OFF(...) do {} while (0)
104 
105 #if defined(REQ_DEBUG) && REQ_DEBUG
106 #include <time.h>
107 #define DEBUG_REQ(...) DEBUG_ON(__VA_ARGS__)
108 #include "gldns/wire2str.h"
109 #include "rr-dict.h"
110 #include "types-internal.h"
debug_req(const char * msg,getdns_network_req * netreq)111 static inline void debug_req(const char *msg, getdns_network_req *netreq)
112 {
113 	char str[1024];
114 	struct timeval tv;
115 	uint64_t t;
116 
117 	(void) gettimeofday(&tv, NULL);
118 	t = tv.tv_sec * 1000 + tv.tv_usec / 1000;
119 	t = t >= netreq->owner->expires ? 0 : netreq->owner->expires - t;
120 	(void) gldns_wire2str_dname_buf(netreq->owner->name,
121 	    netreq->owner->name_len, str, sizeof(str));
122 	DEBUG_REQ("NETREQ %s %4"PRIu64" %s %s\n", msg, t,
123 	    str, _getdns_rr_type_name(netreq->request_type));
124 }
125 #else
126 #define DEBUG_REQ(...) DEBUG_OFF(__VA_ARGS__)
127 #define debug_req(...) DEBUG_OFF(__VA_ARGS__)
128 #endif
129 
130 #if defined(SCHED_DEBUG) && SCHED_DEBUG
131 #include <time.h>
132 #define DEBUG_SCHED(...) DEBUG_ON(__VA_ARGS__)
133 #else
134 #define DEBUG_SCHED(...) DEBUG_OFF(__VA_ARGS__)
135 #endif
136 
137 #if defined(STUB_DEBUG) && STUB_DEBUG
138 #include <time.h>
139 #define DEBUG_STUB(...) DEBUG_ON(__VA_ARGS__)
140 #else
141 #define DEBUG_STUB(...) DEBUG_OFF(__VA_ARGS__)
142 #endif
143 
144 #if defined(DAEMON_DEBUG) && DAEMON_DEBUG
145 #include <time.h>
146 #define DEBUG_DAEMON(...) DEBUG_ON(__VA_ARGS__)
147 #else
148 #define DEBUG_DAEMON(...) DEBUG_OFF(__VA_ARGS__)
149 #endif
150 
151 #if defined(SEC_DEBUG) && SEC_DEBUG
152 #include <time.h>
153 #define DEBUG_SEC(...) DEBUG_ON(__VA_ARGS__)
154 #else
155 #define DEBUG_SEC(...) DEBUG_OFF(__VA_ARGS__)
156 #endif
157 
158 #if defined(SERVER_DEBUG) && SERVER_DEBUG
159 #include <time.h>
160 #define DEBUG_SERVER(...) DEBUG_ON(__VA_ARGS__)
161 #else
162 #define DEBUG_SERVER(...) DEBUG_OFF(__VA_ARGS__)
163 #endif
164 
165 #define MDNS_DEBUG_ENTRY     "-> MDNS ENTRY:  "
166 #define MDNS_DEBUG_READ      "-- MDNS READ:   "
167 #define MDNS_DEBUG_MREAD      "-- MDNS MREAD:  "
168 #define MDNS_DEBUG_WRITE     "-- MDNS WRITE:  "
169 #define MDNS_DEBUG_CLEANUP   "-- MDNS CLEANUP:"
170 
171 #if defined(MDNS_DEBUG) && MDNS_DEBUG
172 #include <time.h>
173 #define DEBUG_MDNS(...) DEBUG_ON(__VA_ARGS__)
174 #else
175 #define DEBUG_MDNS(...) DEBUG_OFF(__VA_ARGS__)
176 #endif
177 
178 #if defined(ANCHOR_DEBUG) && ANCHOR_DEBUG
179 #include <time.h>
180 #define DEBUG_ANCHOR(...) DEBUG_ON(__VA_ARGS__)
181 #else
182 #define DEBUG_ANCHOR(...) DEBUG_OFF(__VA_ARGS__)
183 #endif
184 
185 #if (defined(REQ_DEBUG)    && REQ_DEBUG)    || \
186     (defined(SCHED_DEBUG)  && SCHED_DEBUG)  || \
187     (defined(STUB_DEBUG)   && STUB_DEBUG)   || \
188     (defined(DAEMON_DEBUG) && DAEMON_DEBUG) || \
189     (defined(SEC_DEBUG)    && SEC_DEBUG)    || \
190     (defined(SERVER_DEBUG) && SERVER_DEBUG) || \
191     (defined(MDNS_DEBUG)   && MDNS_DEBUG)   || \
192     (defined(ANCHOR_DEBUG) && ANCHOR_DEBUG)
193 #define DEBUGGING 1
194 static inline int
_getdns_ERR_print_errors_cb_f(const char * str,size_t len,void * u)195 _getdns_ERR_print_errors_cb_f(const char *str, size_t len, void *u)
196 { DEBUG_ON("%.*s (u: %p)\n", (int)len, str, u); return 1; }
197 #endif
198 
199 #endif
200 /* debug.h */
201