Line data Source code
1 : /* debug.h - interface to debugging functions
2 : Copyright (C) 2002, 2004, 2005, 2007 g10 Code GmbH
3 :
4 : This file is part of GPGME.
5 :
6 : GPGME is free software; you can redistribute it and/or modify it
7 : under the terms of the GNU Lesser General Public License as
8 : published by the Free Software Foundation; either version 2.1 of
9 : the License, or (at your option) any later version.
10 :
11 : GPGME is distributed in the hope that it will be useful, but
12 : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public
17 : License along with this program; if not, write to the Free Software
18 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 : 02111-1307, USA. */
20 :
21 : #ifndef DEBUG_H
22 : #define DEBUG_H
23 :
24 : #include <string.h>
25 : #ifdef HAVE_STDINT_H
26 : #include <stdint.h>
27 : #endif
28 :
29 : #include "gpgme.h" /* Required for gpgme_error stuff. */
30 :
31 :
32 : /* Indirect stringification, requires __STDC__ to work. */
33 : #define STRINGIFY(v) #v
34 : #define XSTRINGIFY(v) STRINGIFY(v)
35 :
36 :
37 : /*
38 : * The debug levels.
39 : *
40 : * Note that TRACE_LOGBUFX uses the current debug level + 1.
41 : */
42 :
43 : #define DEBUG_INIT 1
44 : #define DEBUG_GLOBAL 2
45 : #define DEBUG_CTX 3
46 : #define DEBUG_ENGINE 4
47 : #define DEBUG_DATA 5
48 : #define DEBUG_ASSUAN 6
49 : #define DEBUG_SYSIO 7
50 :
51 :
52 : /* Remove path components from filenames (i.e. __FILE__) for cleaner
53 : logs. */
54 : static inline const char *_gpgme_debug_srcname (const char *file)
55 : GPGME_GCC_A_PURE;
56 :
57 : static inline const char *
58 19 : _gpgme_debug_srcname (const char *file)
59 : {
60 19 : const char *s = strrchr (file, '/');
61 19 : return s? s+1:file;
62 : }
63 :
64 : /* Initialization helper function; see debug.c. */
65 : int _gpgme_debug_set_debug_envvar (const char *value);
66 :
67 : /* Called early to initialize the logging. */
68 : void _gpgme_debug_subsystem_init (void);
69 :
70 : /* Log the formatted string FORMAT at debug level LEVEL or higher. */
71 : int _gpgme_debug (int level, const char *format, ...);
72 :
73 : /* Start a new debug line in *LINE, logged at level LEVEL or higher,
74 : and starting with the formatted string FORMAT. */
75 : void _gpgme_debug_begin (void **helper, int level, const char *format, ...);
76 :
77 : /* Add the formatted string FORMAT to the debug line *LINE. */
78 : void _gpgme_debug_add (void **helper, const char *format, ...);
79 :
80 : /* Finish construction of *LINE and send it to the debug output
81 : stream. */
82 : void _gpgme_debug_end (void **helper);
83 :
84 : void _gpgme_debug_buffer (int lvl, const char *const fmt,
85 : const char *const func, const char *const buffer,
86 : size_t len);
87 :
88 : void _gpgme_debug_frame_begin (void);
89 : int _gpgme_debug_frame_end (void);
90 :
91 : static inline gpgme_error_t
92 19 : _gpgme_trace_gpgme_error (gpgme_error_t err, const char *file, int line)
93 : {
94 19 : _gpgme_debug (DEBUG_ENGINE, "%s:%d: returning error: %s\n",
95 : _gpgme_debug_srcname (file), line, gpgme_strerror (err));
96 19 : return err;
97 : }
98 :
99 :
100 : /* Trace support. */
101 :
102 : /* FIXME: For now. */
103 : #define _gpgme_debug_trace() 1
104 :
105 : #define _TRACE(lvl, name, tag) \
106 : int _gpgme_trace_level = lvl; \
107 : const char *const _gpgme_trace_func = name; \
108 : const char *const _gpgme_trace_tagname = STRINGIFY (tag); \
109 : void *_gpgme_trace_tag = (void *) (uintptr_t) tag; \
110 : _gpgme_debug_frame_begin ()
111 :
112 : #define TRACE_BEG(lvl, name, tag) \
113 : _TRACE (lvl, name, tag); \
114 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p\n", \
115 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag)
116 : #define TRACE_BEG0(lvl, name, tag, fmt) \
117 : _TRACE (lvl, name, tag); \
118 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
119 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag)
120 : #define TRACE_BEG1(lvl, name, tag, fmt, arg1) \
121 : _TRACE (lvl, name, tag); \
122 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
123 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
124 : arg1)
125 : #define TRACE_BEG2(lvl, name, tag, fmt, arg1, arg2) \
126 : _TRACE (lvl, name, tag); \
127 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
128 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
129 : arg1, arg2)
130 : #define TRACE_BEG3(lvl, name, tag, fmt, arg1, arg2, arg3) \
131 : _TRACE (lvl, name, tag); \
132 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
133 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
134 : arg1, arg2, arg3)
135 : #define TRACE_BEG4(lvl, name, tag, fmt, arg1, arg2, arg3, arg4) \
136 : _TRACE (lvl, name, tag); \
137 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
138 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
139 : arg1, arg2, arg3, arg4)
140 : #define TRACE_BEG5(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, arg5) \
141 : _TRACE (lvl, name, tag); \
142 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
143 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
144 : arg1, arg2, arg3, arg4, arg5)
145 : #define TRACE_BEG7(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, \
146 : arg5, arg6, arg7) \
147 : _TRACE (lvl, name, tag); \
148 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
149 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
150 : arg1, arg2, arg3, arg4, arg5, arg6, arg7)
151 : #define TRACE_BEG8(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, \
152 : arg5, arg6, arg7, arg8) \
153 : _TRACE (lvl, name, tag); \
154 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
155 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
156 : arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
157 :
158 : #define TRACE(lvl, name, tag) \
159 : _gpgme_debug_frame_begin (), \
160 : _gpgme_debug (lvl, "%s: call: %s=%p\n", \
161 : name, STRINGIFY (tag), (void *) (uintptr_t) tag), \
162 : _gpgme_debug_frame_end ()
163 : #define TRACE0(lvl, name, tag, fmt) \
164 : _gpgme_debug_frame_begin (), \
165 : _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n", \
166 : name, STRINGIFY (tag), (void *) (uintptr_t) tag), \
167 : _gpgme_debug_frame_end ()
168 : #define TRACE1(lvl, name, tag, fmt, arg1) \
169 : _gpgme_debug_frame_begin (), \
170 : _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n", \
171 : name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1), \
172 : _gpgme_debug_frame_end ()
173 : #define TRACE2(lvl, name, tag, fmt, arg1, arg2) \
174 : _gpgme_debug_frame_begin (), \
175 : _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n", \
176 : name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
177 : arg2), _gpgme_debug_frame_end ()
178 : #define TRACE3(lvl, name, tag, fmt, arg1, arg2, arg3) \
179 : _gpgme_debug_frame_begin (), \
180 : _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n", \
181 : name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
182 : arg2, arg3), _gpgme_debug_frame_end ()
183 : #define TRACE6(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, arg5, arg6) \
184 : _gpgme_debug_frame_begin (), \
185 : _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n", \
186 : name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
187 : arg2, arg3, arg4, arg5, arg6), \
188 : _gpgme_debug_frame_end ()
189 :
190 : #define TRACE_ERR(err) \
191 : err == 0 ? (TRACE_SUC ()) : \
192 : (_gpgme_debug (_gpgme_trace_level, "%s:%d: error: %s <%s>\n", \
193 : _gpgme_trace_func, __LINE__, gpgme_strerror (err), \
194 : gpgme_strsource (err)), _gpgme_debug_frame_end (), (err))
195 : /* The cast to void suppresses GCC warnings. */
196 : #define TRACE_SYSRES(res) \
197 : res >= 0 ? ((void) (TRACE_SUC1 ("result=%i", res)), (res)) : \
198 : (_gpgme_debug (_gpgme_trace_level, "%s: error: %s\n", \
199 : _gpgme_trace_func, strerror (errno)), _gpgme_debug_frame_end (), (res))
200 : #define TRACE_SYSERR(res) \
201 : res == 0 ? ((void) (TRACE_SUC1 ("result=%i", res)), (res)) : \
202 : (_gpgme_debug (_gpgme_trace_level, "%s: error: %s\n", \
203 : _gpgme_trace_func, strerror (res)), \
204 : _gpgme_debug_frame_end (), (res))
205 : #define TRACE_SYSERR_NR(res) \
206 : do { res == 0 ? ((void) (TRACE_SUC1 ("result=%i", res)), (res)) : \
207 : (_gpgme_debug (_gpgme_trace_level, "%s: error: %s\n", \
208 : _gpgme_trace_func, strerror (res)), \
209 : _gpgme_debug_frame_end ()); } while (0)
210 :
211 : #define TRACE_SUC() \
212 : _gpgme_debug (_gpgme_trace_level, "%s: leave\n", \
213 : _gpgme_trace_func), _gpgme_debug_frame_end ()
214 : #define TRACE_SUC0(fmt) \
215 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
216 : _gpgme_trace_func), _gpgme_debug_frame_end ()
217 : #define TRACE_SUC1(fmt, arg1) \
218 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
219 : _gpgme_trace_func, arg1), _gpgme_debug_frame_end ()
220 : #define TRACE_SUC2(fmt, arg1, arg2) \
221 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
222 : _gpgme_trace_func, arg1, arg2), _gpgme_debug_frame_end ()
223 : #define TRACE_SUC3(fmt, arg1, arg2, arg3) \
224 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
225 : _gpgme_trace_func, arg1, arg2, arg3), _gpgme_debug_frame_end ()
226 : #define TRACE_SUC4(fmt, arg1, arg2, arg3, arg4) \
227 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
228 : _gpgme_trace_func, arg1, arg2, arg3, arg4), \
229 : _gpgme_debug_frame_end ()
230 : #define TRACE_SUC5(fmt, arg1, arg2, arg3, arg4, arg5) \
231 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
232 : _gpgme_trace_func, arg1, arg2, arg3, arg4, arg5), \
233 : _gpgme_debug_frame_end ()
234 : #define TRACE_SUC6(fmt, arg1, arg2, arg3, arg4, arg5, arg6) \
235 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
236 : _gpgme_trace_func, arg1, arg2, arg3, arg4, arg5, arg6), \
237 : _gpgme_debug_frame_end ()
238 :
239 : #define TRACE_LOG(fmt) \
240 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
241 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag)
242 : #define TRACE_LOG1(fmt, arg1) \
243 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
244 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
245 : arg1)
246 : #define TRACE_LOG2(fmt, arg1, arg2) \
247 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
248 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
249 : arg1, arg2)
250 : #define TRACE_LOG3(fmt, arg1, arg2, arg3) \
251 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
252 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
253 : arg1, arg2, arg3)
254 : #define TRACE_LOG4(fmt, arg1, arg2, arg3, arg4) \
255 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
256 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
257 : arg1, arg2, arg3, arg4)
258 : #define TRACE_LOG5(fmt, arg1, arg2, arg3, arg4, arg5) \
259 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
260 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
261 : arg1, arg2, arg3, arg4, arg5)
262 : #define TRACE_LOG6(fmt, arg1, arg2, arg3, arg4, arg5, arg6) \
263 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
264 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
265 : arg1, arg2, arg3, arg4, arg5, arg6)
266 :
267 : #define TRACE_LOGBUF(buf, len) \
268 : _gpgme_debug_buffer (_gpgme_trace_level, "%s: check: %s", \
269 : _gpgme_trace_func, buf, len)
270 :
271 : #define TRACE_LOGBUFX(buf, len) \
272 : _gpgme_debug_buffer (_gpgme_trace_level+1, "%s: check: %s", \
273 : _gpgme_trace_func, buf, len)
274 :
275 : #define TRACE_SEQ(hlp,fmt) \
276 : _gpgme_debug_begin (&(hlp), _gpgme_trace_level, \
277 : "%s: check: %s=%p, " fmt, _gpgme_trace_func, \
278 : _gpgme_trace_tagname, _gpgme_trace_tag)
279 : #define TRACE_ADD0(hlp,fmt) \
280 : _gpgme_debug_add (&(hlp), fmt)
281 : #define TRACE_ADD1(hlp,fmt,a) \
282 : _gpgme_debug_add (&(hlp), fmt, (a))
283 : #define TRACE_ADD2(hlp,fmt,a,b) \
284 : _gpgme_debug_add (&(hlp), fmt, (a), (b))
285 : #define TRACE_ADD3(hlp,fmt,a,b,c) \
286 : _gpgme_debug_add (&(hlp), fmt, (a), (b), (c))
287 : #define TRACE_END(hlp,fmt) \
288 : _gpgme_debug_add (&(hlp), fmt); \
289 : _gpgme_debug_end (&(hlp))
290 : #define TRACE_ENABLED(hlp) (!!(hlp))
291 :
292 : /* And finally a simple macro to trace the location of an error code.
293 : This macro is independent of the other trace macros and may be used
294 : without any preconditions. */
295 : #define trace_gpg_error(e) \
296 : _gpgme_trace_gpgme_error (gpg_error (e), __FILE__, __LINE__)
297 :
298 :
299 : #endif /* DEBUG_H */
|