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 : /* The debug levels. */
38 :
39 : #define DEBUG_INIT 1
40 : #define DEBUG_GLOBAL 2
41 : #define DEBUG_CTX 3
42 : #define DEBUG_ENGINE 4
43 : #define DEBUG_DATA 5
44 : #define DEBUG_ASSUAN 6
45 : #define DEBUG_SYSIO 7
46 :
47 :
48 : /* Remove path components from filenames (i.e. __FILE__) for cleaner
49 : logs. */
50 : static inline const char *_gpgme_debug_srcname (const char *file)
51 : GPGME_GCC_A_PURE;
52 :
53 : static inline const char *
54 2 : _gpgme_debug_srcname (const char *file)
55 : {
56 2 : const char *s = strrchr (file, '/');
57 2 : return s? s+1:file;
58 : }
59 :
60 : /* Initialization helper function; see debug.c. */
61 : int _gpgme_debug_set_debug_envvar (const char *value);
62 :
63 : /* Called early to initialize the logging. */
64 : void _gpgme_debug_subsystem_init (void);
65 :
66 : /* Log the formatted string FORMAT at debug level LEVEL or higher. */
67 : int _gpgme_debug (int level, const char *format, ...);
68 :
69 : /* Start a new debug line in *LINE, logged at level LEVEL or higher,
70 : and starting with the formatted string FORMAT. */
71 : void _gpgme_debug_begin (void **helper, int level, const char *format, ...);
72 :
73 : /* Add the formatted string FORMAT to the debug line *LINE. */
74 : void _gpgme_debug_add (void **helper, const char *format, ...);
75 :
76 : /* Finish construction of *LINE and send it to the debug output
77 : stream. */
78 : void _gpgme_debug_end (void **helper);
79 :
80 : void _gpgme_debug_buffer (int lvl, const char *const fmt,
81 : const char *const func, const char *const buffer,
82 : size_t len);
83 :
84 : void _gpgme_debug_frame_begin (void);
85 : int _gpgme_debug_frame_end (void);
86 :
87 : static inline gpgme_error_t
88 2 : _gpgme_trace_gpgme_error (gpgme_error_t err, const char *file, int line)
89 : {
90 2 : _gpgme_debug (DEBUG_ENGINE, "%s:%d: returning error: %s\n",
91 : _gpgme_debug_srcname (file), line, gpgme_strerror (err));
92 2 : return err;
93 : }
94 :
95 :
96 : /* Trace support. */
97 :
98 : /* FIXME: For now. */
99 : #define _gpgme_debug_trace() 1
100 :
101 : #define _TRACE(lvl, name, tag) \
102 : int _gpgme_trace_level = lvl; \
103 : const char *const _gpgme_trace_func = name; \
104 : const char *const _gpgme_trace_tagname = STRINGIFY (tag); \
105 : void *_gpgme_trace_tag = (void *) (uintptr_t) tag; \
106 : _gpgme_debug_frame_begin ()
107 :
108 : #define TRACE_BEG(lvl, name, tag) \
109 : _TRACE (lvl, name, tag); \
110 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p\n", \
111 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag)
112 : #define TRACE_BEG0(lvl, name, tag, fmt) \
113 : _TRACE (lvl, name, tag); \
114 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
115 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag)
116 : #define TRACE_BEG1(lvl, name, tag, fmt, arg1) \
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 : arg1)
121 : #define TRACE_BEG2(lvl, name, tag, fmt, arg1, arg2) \
122 : _TRACE (lvl, name, tag); \
123 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
124 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
125 : arg1, arg2)
126 : #define TRACE_BEG3(lvl, name, tag, fmt, arg1, arg2, arg3) \
127 : _TRACE (lvl, name, tag); \
128 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
129 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
130 : arg1, arg2, arg3)
131 : #define TRACE_BEG4(lvl, name, tag, fmt, arg1, arg2, arg3, arg4) \
132 : _TRACE (lvl, name, tag); \
133 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
134 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
135 : arg1, arg2, arg3, arg4)
136 : #define TRACE_BEG5(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, arg5) \
137 : _TRACE (lvl, name, tag); \
138 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
139 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
140 : arg1, arg2, arg3, arg4, arg5)
141 : #define TRACE_BEG7(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, \
142 : arg5, arg6, arg7) \
143 : _TRACE (lvl, name, tag); \
144 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
145 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
146 : arg1, arg2, arg3, arg4, arg5, arg6, arg7)
147 : #define TRACE_BEG8(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, \
148 : arg5, arg6, arg7, arg8) \
149 : _TRACE (lvl, name, tag); \
150 : _gpgme_debug (_gpgme_trace_level, "%s: enter: %s=%p, " fmt "\n", \
151 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
152 : arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
153 :
154 : #define TRACE(lvl, name, tag) \
155 : _gpgme_debug_frame_begin (), \
156 : _gpgme_debug (lvl, "%s: call: %s=%p\n", \
157 : name, STRINGIFY (tag), (void *) (uintptr_t) tag), \
158 : _gpgme_debug_frame_end ()
159 : #define TRACE0(lvl, name, tag, fmt) \
160 : _gpgme_debug_frame_begin (), \
161 : _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n", \
162 : name, STRINGIFY (tag), (void *) (uintptr_t) tag), \
163 : _gpgme_debug_frame_end ()
164 : #define TRACE1(lvl, name, tag, fmt, arg1) \
165 : _gpgme_debug_frame_begin (), \
166 : _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n", \
167 : name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1), \
168 : _gpgme_debug_frame_end ()
169 : #define TRACE2(lvl, name, tag, fmt, arg1, arg2) \
170 : _gpgme_debug_frame_begin (), \
171 : _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n", \
172 : name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
173 : arg2), _gpgme_debug_frame_end ()
174 : #define TRACE3(lvl, name, tag, fmt, arg1, arg2, arg3) \
175 : _gpgme_debug_frame_begin (), \
176 : _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n", \
177 : name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
178 : arg2, arg3), _gpgme_debug_frame_end ()
179 : #define TRACE6(lvl, name, tag, fmt, arg1, arg2, arg3, arg4, arg5, arg6) \
180 : _gpgme_debug_frame_begin (), \
181 : _gpgme_debug (lvl, "%s: call: %s=%p, " fmt "\n", \
182 : name, STRINGIFY (tag), (void *) (uintptr_t) tag, arg1, \
183 : arg2, arg3, arg4, arg5, arg6), \
184 : _gpgme_debug_frame_end ()
185 :
186 : #define TRACE_ERR(err) \
187 : err == 0 ? (TRACE_SUC ()) : \
188 : (_gpgme_debug (_gpgme_trace_level, "%s:%d: error: %s <%s>\n", \
189 : _gpgme_trace_func, __LINE__, gpgme_strerror (err), \
190 : gpgme_strsource (err)), _gpgme_debug_frame_end (), (err))
191 : /* The cast to void suppresses GCC warnings. */
192 : #define TRACE_SYSRES(res) \
193 : res >= 0 ? ((void) (TRACE_SUC1 ("result=%i", res)), (res)) : \
194 : (_gpgme_debug (_gpgme_trace_level, "%s: error: %s\n", \
195 : _gpgme_trace_func, strerror (errno)), _gpgme_debug_frame_end (), (res))
196 : #define TRACE_SYSERR(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 (res)), \
200 : _gpgme_debug_frame_end (), (res))
201 :
202 : #define TRACE_SUC() \
203 : _gpgme_debug (_gpgme_trace_level, "%s: leave\n", \
204 : _gpgme_trace_func), _gpgme_debug_frame_end ()
205 : #define TRACE_SUC0(fmt) \
206 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
207 : _gpgme_trace_func), _gpgme_debug_frame_end ()
208 : #define TRACE_SUC1(fmt, arg1) \
209 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
210 : _gpgme_trace_func, arg1), _gpgme_debug_frame_end ()
211 : #define TRACE_SUC2(fmt, arg1, arg2) \
212 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
213 : _gpgme_trace_func, arg1, arg2), _gpgme_debug_frame_end ()
214 : #define TRACE_SUC5(fmt, arg1, arg2, arg3, arg4, arg5) \
215 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
216 : _gpgme_trace_func, arg1, arg2, arg3, arg4, arg5), \
217 : _gpgme_debug_frame_end ()
218 : #define TRACE_SUC6(fmt, arg1, arg2, arg3, arg4, arg5, arg6) \
219 : _gpgme_debug (_gpgme_trace_level, "%s: leave: " fmt "\n", \
220 : _gpgme_trace_func, arg1, arg2, arg3, arg4, arg5, arg6), \
221 : _gpgme_debug_frame_end ()
222 :
223 : #define TRACE_LOG(fmt) \
224 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
225 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag)
226 : #define TRACE_LOG1(fmt, arg1) \
227 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
228 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
229 : arg1)
230 : #define TRACE_LOG2(fmt, arg1, arg2) \
231 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
232 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
233 : arg1, arg2)
234 : #define TRACE_LOG3(fmt, arg1, arg2, arg3) \
235 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
236 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
237 : arg1, arg2, arg3)
238 : #define TRACE_LOG4(fmt, arg1, arg2, arg3, arg4) \
239 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
240 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
241 : arg1, arg2, arg3, arg4)
242 : #define TRACE_LOG5(fmt, arg1, arg2, arg3, arg4, arg5) \
243 : _gpgme_debug (_gpgme_trace_level, "%s: check: %s=%p, " fmt "\n", \
244 : _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
245 : arg1, arg2, arg3, arg4, arg5)
246 : #define TRACE_LOG6(fmt, arg1, arg2, arg3, arg4, arg5, arg6) \
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, arg3, arg4, arg5, arg6)
250 :
251 : #define TRACE_LOGBUF(buf, len) \
252 : _gpgme_debug_buffer (_gpgme_trace_level, "%s: check: %s", \
253 : _gpgme_trace_func, buf, len)
254 :
255 : #define TRACE_SEQ(hlp,fmt) \
256 : _gpgme_debug_begin (&(hlp), _gpgme_trace_level, \
257 : "%s: check: %s=%p, " fmt, _gpgme_trace_func, \
258 : _gpgme_trace_tagname, _gpgme_trace_tag)
259 : #define TRACE_ADD0(hlp,fmt) \
260 : _gpgme_debug_add (&(hlp), fmt)
261 : #define TRACE_ADD1(hlp,fmt,a) \
262 : _gpgme_debug_add (&(hlp), fmt, (a))
263 : #define TRACE_ADD2(hlp,fmt,a,b) \
264 : _gpgme_debug_add (&(hlp), fmt, (a), (b))
265 : #define TRACE_ADD3(hlp,fmt,a,b,c) \
266 : _gpgme_debug_add (&(hlp), fmt, (a), (b), (c))
267 : #define TRACE_END(hlp,fmt) \
268 : _gpgme_debug_add (&(hlp), fmt); \
269 : _gpgme_debug_end (&(hlp))
270 : #define TRACE_ENABLED(hlp) (!!(hlp))
271 :
272 : /* And finally a simple macro to trace the location of an error code.
273 : This macro is independent of the other trace macros and may be used
274 : without any preconditions. */
275 : #define trace_gpg_error(e) \
276 : _gpgme_trace_gpgme_error (gpg_error (e), __FILE__, __LINE__)
277 :
278 :
279 : #endif /* DEBUG_H */
|