core: Protect the trace macros for fun and profit.

* src/debug.h: Protect macros using.
(_trace_err, _trace_sysres, _trace_syserr): New helper inline
functions.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2018-11-16 18:17:22 +01:00
parent 7eda50a673
commit 7a1e7006d0
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
11 changed files with 107 additions and 70 deletions

View File

@ -110,7 +110,8 @@ gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname,
(*r_dh)->data.mem.size = length; (*r_dh)->data.mem.size = length;
(*r_dh)->data.mem.length = length; (*r_dh)->data.mem.length = length;
return TRACE_SUC ("r_dh=%p", *r_dh); TRACE_SUC ("r_dh=%p", *r_dh);
return 0;
} }

View File

@ -95,5 +95,6 @@ gpgme_data_new_from_estream (gpgme_data_t *r_dh, gpgrt_stream_t stream)
return TRACE_ERR (err); return TRACE_ERR (err);
(*r_dh)->data.e_stream = stream; (*r_dh)->data.e_stream = stream;
return TRACE_SUC ("dh=%p", *r_dh); TRACE_SUC ("dh=%p", *r_dh);
return 0;
} }

View File

@ -82,5 +82,6 @@ gpgme_data_new_from_fd (gpgme_data_t *r_dh, int fd)
return TRACE_ERR (err); return TRACE_ERR (err);
(*r_dh)->data.fd = fd; (*r_dh)->data.fd = fd;
return TRACE_SUC ("dh=%p", *r_dh); TRACE_SUC ("dh=%p", *r_dh);
return 0;
} }

View File

@ -177,7 +177,8 @@ gpgme_data_new (gpgme_data_t *r_dh)
if (err) if (err)
return TRACE_ERR (err); return TRACE_ERR (err);
return TRACE_SUC ("dh=%p", *r_dh); TRACE_SUC ("dh=%p", *r_dh);
return 0;
} }
@ -214,7 +215,8 @@ gpgme_data_new_from_mem (gpgme_data_t *r_dh, const char *buffer,
(*r_dh)->data.mem.size = size; (*r_dh)->data.mem.size = size;
(*r_dh)->data.mem.length = size; (*r_dh)->data.mem.length = size;
return TRACE_SUC ("dh=%p", *r_dh); TRACE_SUC ("dh=%p", *r_dh);
return 0;
} }
@ -282,13 +284,9 @@ gpgme_data_release_and_get_mem (gpgme_data_t dh, size_t *r_len)
gpgme_data_release (dh); gpgme_data_release (dh);
if (r_len) if (r_len)
{
TRACE_SUC ("buffer=%p, len=%zu", str, *r_len); TRACE_SUC ("buffer=%p, len=%zu", str, *r_len);
}
else else
{
TRACE_SUC ("buffer=%p", str); TRACE_SUC ("buffer=%p", str);
}
return str; return str;
} }

View File

@ -104,5 +104,6 @@ gpgme_data_new_from_stream (gpgme_data_t *r_dh, FILE *stream)
return TRACE_ERR (err); return TRACE_ERR (err);
(*r_dh)->data.stream = stream; (*r_dh)->data.stream = stream;
return TRACE_SUC ("dh=%p", *r_dh); TRACE_SUC ("dh=%p", *r_dh);
return 0;
} }

View File

@ -100,5 +100,6 @@ gpgme_data_new_from_cbs (gpgme_data_t *r_dh, gpgme_data_cbs_t cbs, void *handle)
(*r_dh)->data.user.cbs = cbs; (*r_dh)->data.user.cbs = cbs;
(*r_dh)->data.user.handle = handle; (*r_dh)->data.user.handle = handle;
return TRACE_SUC ("dh=%p", *r_dh); TRACE_SUC ("dh=%p", *r_dh);
return 0;
} }

View File

@ -113,68 +113,97 @@ _gpgme_trace_gpgme_error (gpgme_error_t err, const char *file, int line)
void *_gpgme_trace_tag = (void *) (uintptr_t) tag; \ void *_gpgme_trace_tag = (void *) (uintptr_t) tag; \
_gpgme_debug_frame_begin () _gpgme_debug_frame_begin ()
/* Note: We can't protect this with a do-while block. */
#define TRACE_BEG(lvl, name, tag, ...) \ #define TRACE_BEG(lvl, name, tag, ...) \
_TRACE (lvl, name, tag); \ _TRACE (lvl, name, tag); \
_gpgme_debug (_gpgme_trace_level, 1, \ _gpgme_debug (_gpgme_trace_level, 1, \
_gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \ _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
__VA_ARGS__) __VA_ARGS__)
#define TRACE(lvl, name, tag, ...) \ #define TRACE(lvl, name, tag, ...) do { \
_gpgme_debug_frame_begin (), \ _gpgme_debug_frame_begin (); \
_gpgme_debug (lvl, 0, \ _gpgme_debug (lvl, 0, name, STRINGIFY (tag), (void *)(uintptr_t)tag, \
name, STRINGIFY (tag), (void *) (uintptr_t) tag, \ __VA_ARGS__); \
__VA_ARGS__), \ _gpgme_debug_frame_end (); \
_gpgme_debug_frame_end () } while (0)
/* Trace a gpg-error and return it. */
#define TRACE_ERR(err) \ #define TRACE_ERR(err) \
err == 0 ? (TRACE_SUC ("")) : \ _trace_err ((err), _gpgme_trace_level, _gpgme_trace_func, __LINE__)
(_gpgme_debug (_gpgme_trace_level, -1, NULL, NULL, NULL, \ static inline gpg_error_t
"%s:%d: error: %s <%s>\n", \ _trace_err (gpg_error_t err, int lvl, const char *func, int line)
_gpgme_trace_func, __LINE__, gpgme_strerror (err), \ {
gpgme_strsource (err)), _gpgme_debug_frame_end (), (err)) if (!err)
_gpgme_debug (lvl, 3, func, NULL, NULL, "");
else
_gpgme_debug (lvl, -1, NULL, NULL, NULL,
"%s:%d: error: %s <%s>\n",
func, line, gpgme_strerror (err), gpgme_strsource (err));
_gpgme_debug_frame_end ();
return err;
}
/* Trace a system call result and return it. */
/* The cast to void suppresses GCC warnings. */
#define TRACE_SYSRES(res) \ #define TRACE_SYSRES(res) \
res >= 0 ? ((void) (TRACE_SUC ("result=%i", res)), (res)) : \ _trace_sysres ((res), _gpgme_trace_level, _gpgme_trace_func, __LINE__)
(_gpgme_debug (_gpgme_trace_level, -1, NULL, NULL, NULL, \ static inline int
"%s: error: %s\n", \ _trace_sysres (int res, int lvl, const char *func, int line)
_gpgme_trace_func, strerror (errno)), \ {
_gpgme_debug_frame_end (), (res)) if (res >= 0)
#define TRACE_SYSERR(res) \ _gpgme_debug (lvl, 3, func, NULL, NULL, "result=%d", res);
res == 0 ? ((void) (TRACE_SUC ("result=%i", res)), (res)) : \ else
(_gpgme_debug (_gpgme_trace_level, -1, NULL, NULL, NULL, \ _gpgme_debug (lvl, -1, NULL, NULL, NULL,
"%s: error: %s\n", \ "%s:%d: error: %s (%d)\n",
_gpgme_trace_func, strerror (res)), \ func, line, strerror (res), res);
_gpgme_debug_frame_end (), (res)) _gpgme_debug_frame_end ();
#define TRACE_SYSERR_NR(res) \ return res;
do { res == 0 ? ((void) (TRACE_SUC ("result=%i", res)), (res)) : \ }
(_gpgme_debug (_gpgme_trace_level, -1, NULL, NULL, NULL, \
"%s: error: %s\n", \
_gpgme_trace_func, strerror (res)), \
_gpgme_debug_frame_end ()); } while (0)
#define TRACE_SUC(...) \ /* Trace a system call error and return it. */
#define TRACE_SYSERR(rc) \
_trace_syserr ((rc), _gpgme_trace_level, _gpgme_trace_func, __LINE__)
static inline int
_trace_syserr (int rc, int lvl, const char *func, int line)
{
if (!rc)
_gpgme_debug (lvl, 3, func, NULL, NULL, "result=0");
else
_gpgme_debug (lvl, -1, NULL, NULL, NULL,
"%s:%d: error: %s (%d)\n",
func, line, strerror (rc), rc);
_gpgme_debug_frame_end ();
return rc;
}
#define TRACE_SUC(...) do { \
_gpgme_debug (_gpgme_trace_level, 3, _gpgme_trace_func, NULL, NULL, \ _gpgme_debug (_gpgme_trace_level, 3, _gpgme_trace_func, NULL, NULL, \
__VA_ARGS__), _gpgme_debug_frame_end () __VA_ARGS__); \
_gpgme_debug_frame_end (); \
} while (0)
#define TRACE_LOG(...) \ #define TRACE_LOG(...) do { \
_gpgme_debug (_gpgme_trace_level, 2, \ _gpgme_debug (_gpgme_trace_level, 2, \
_gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \ _gpgme_trace_func, _gpgme_trace_tagname, _gpgme_trace_tag, \
__VA_ARGS__) __VA_ARGS__); \
} while (0)
#define TRACE_LOGBUF(buf, len) \ #define TRACE_LOGBUF(buf, len) do { \
_gpgme_debug_buffer (_gpgme_trace_level, "%s: check: %s", \ _gpgme_debug_buffer (_gpgme_trace_level, "%s: check: %s", \
_gpgme_trace_func, buf, len) _gpgme_trace_func, buf, len); \
} while (0)
#define TRACE_LOGBUFX(buf, len) \ #define TRACE_LOGBUFX(buf, len) do { \
_gpgme_debug_buffer (_gpgme_trace_level+1, "%s: check: %s", \ _gpgme_debug_buffer (_gpgme_trace_level+1, "%s: check: %s", \
_gpgme_trace_func, buf, len) _gpgme_trace_func, buf, len); \
} while (0)
#define TRACE_SEQ(hlp,fmt) \ #define TRACE_SEQ(hlp,fmt) do { \
_gpgme_debug_begin (&(hlp), _gpgme_trace_level, \ _gpgme_debug_begin (&(hlp), _gpgme_trace_level, \
"%s: check: %s=%p, " fmt, _gpgme_trace_func, \ "%s: check: %s=%p, " fmt, _gpgme_trace_func, \
_gpgme_trace_tagname, _gpgme_trace_tag) _gpgme_trace_tagname, _gpgme_trace_tag); \
} while (0)
#define TRACE_ADD0(hlp,fmt) \ #define TRACE_ADD0(hlp,fmt) \
_gpgme_debug_add (&(hlp), fmt) _gpgme_debug_add (&(hlp), fmt)
#define TRACE_ADD1(hlp,fmt,a) \ #define TRACE_ADD1(hlp,fmt,a) \
@ -186,6 +215,7 @@ _gpgme_trace_gpgme_error (gpgme_error_t err, const char *file, int line)
#define TRACE_END(hlp,fmt) \ #define TRACE_END(hlp,fmt) \
_gpgme_debug_add (&(hlp), fmt); \ _gpgme_debug_add (&(hlp), fmt); \
_gpgme_debug_end (&(hlp)) _gpgme_debug_end (&(hlp))
#define TRACE_ENABLED(hlp) (!!(hlp)) #define TRACE_ENABLED(hlp) (!!(hlp))
/* And finally a simple macro to trace the location of an error code. /* And finally a simple macro to trace the location of an error code.

View File

@ -159,7 +159,8 @@ gpgme_new (gpgme_ctx_t *r_ctx)
*r_ctx = ctx; *r_ctx = ctx;
return TRACE_SUC ("ctx=%p", ctx); TRACE_SUC ("ctx=%p", ctx);
return 0;
} }

View File

@ -1235,9 +1235,10 @@ gpgme_op_keylist_next (gpgme_ctx_t ctx, gpgme_key_t *r_key)
*r_key = queue_item->key; *r_key = queue_item->key;
free (queue_item); free (queue_item);
return TRACE_SUC ("key=%p (%s)", *r_key, TRACE_SUC ("key=%p (%s)", *r_key,
((*r_key)->subkeys && (*r_key)->subkeys->fpr) ? ((*r_key)->subkeys && (*r_key)->subkeys->fpr) ?
(*r_key)->subkeys->fpr : "invalid"); (*r_key)->subkeys->fpr : "invalid");
return 0;
} }

View File

@ -166,7 +166,8 @@ _gpgme_io_pipe (int filedes[2], int inherit_idx)
if (err) if (err)
return TRACE_SYSRES (err); return TRACE_SYSRES (err);
return TRACE_SUC ("read=0x%x, write=0x%x", filedes[0], filedes[1]); TRACE_SUC ("read=0x%x, write=0x%x", filedes[0], filedes[1]);
return 0;
} }

View File

@ -89,7 +89,8 @@ gpgme_signers_add (gpgme_ctx_t ctx, const gpgme_key_t key)
gpgme_key_ref (key); gpgme_key_ref (key);
ctx->signers[ctx->signers_len++] = key; ctx->signers[ctx->signers_len++] = key;
return TRACE_SUC (""); TRACE_SUC ("");
return 0;
} }