[HB] Allow enabling different debug facilities individually

This commit is contained in:
Behdad Esfahbod 2009-08-28 16:31:20 -04:00
parent 37006bd1bd
commit 95e202403f
2 changed files with 34 additions and 26 deletions

View File

@ -35,6 +35,10 @@
#include <sys/mman.h> #include <sys/mman.h>
#endif /* HAVE_SYS_MMAN_H */ #endif /* HAVE_SYS_MMAN_H */
#ifndef HB_DEBUG_BLOB
#define HB_DEBUG_BLOB HB_DEBUG
#endif
struct _hb_blob_t { struct _hb_blob_t {
hb_reference_count_t ref_count; hb_reference_count_t ref_count;
@ -189,7 +193,7 @@ hb_blob_lock (hb_blob_t *blob)
hb_mutex_lock (blob->lock); hb_mutex_lock (blob->lock);
blob->lock_count++; blob->lock_count++;
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__, fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
blob->lock_count, blob->data); blob->lock_count, blob->data);
#endif #endif
@ -209,7 +213,7 @@ hb_blob_unlock (hb_blob_t *blob)
assert (blob->lock_count > 0); assert (blob->lock_count > 0);
blob->lock_count--; blob->lock_count--;
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__, fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
hb_atomic_int_get (blob->lock_count), blob->data); hb_atomic_int_get (blob->lock_count), blob->data);
#endif #endif
@ -251,31 +255,31 @@ _try_make_writable_inplace_unix_locked (hb_blob_t *blob)
#endif #endif
if ((unsigned int) -1 == pagesize) { if ((unsigned int) -1 == pagesize) {
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s: failed to get pagesize: %s\n", blob, __FUNCTION__, strerror (errno)); fprintf (stderr, "%p %s: failed to get pagesize: %s\n", blob, __FUNCTION__, strerror (errno));
#endif #endif
return FALSE; return FALSE;
} }
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s: pagesize is %u\n", blob, __FUNCTION__, pagesize); fprintf (stderr, "%p %s: pagesize is %u\n", blob, __FUNCTION__, pagesize);
#endif #endif
mask = ~(pagesize-1); mask = ~(pagesize-1);
addr = (const char *) (((size_t) blob->data) & mask); addr = (const char *) (((size_t) blob->data) & mask);
length = (const char *) (((size_t) blob->data + blob->length + pagesize-1) & mask) - addr; length = (const char *) (((size_t) blob->data + blob->length + pagesize-1) & mask) - addr;
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%d bytes)\n", fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%d bytes)\n",
blob, __FUNCTION__, blob, __FUNCTION__,
addr, addr+length, length); addr, addr+length, length);
#endif #endif
if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) { if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno)); fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno));
#endif #endif
return FALSE; return FALSE;
} }
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s: successfully made [%p..%p] (%d bytes) writable\n", fprintf (stderr, "%p %s: successfully made [%p..%p] (%d bytes) writable\n",
blob, __FUNCTION__, blob, __FUNCTION__,
addr, addr+length, length); addr, addr+length, length);
@ -289,17 +293,17 @@ _try_make_writable_inplace_unix_locked (hb_blob_t *blob)
static void static void
_try_writable_inplace_locked (hb_blob_t *blob) _try_writable_inplace_locked (hb_blob_t *blob)
{ {
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s: making writable\n", blob, __FUNCTION__); fprintf (stderr, "%p %s: making writable\n", blob, __FUNCTION__);
#endif #endif
if (_try_make_writable_inplace_unix_locked (blob)) { if (_try_make_writable_inplace_unix_locked (blob)) {
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s: making writable -> succeeded\n", blob, __FUNCTION__); fprintf (stderr, "%p %s: making writable -> succeeded\n", blob, __FUNCTION__);
#endif #endif
blob->mode = HB_MEMORY_MODE_WRITABLE; blob->mode = HB_MEMORY_MODE_WRITABLE;
} else { } else {
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, __FUNCTION__); fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, __FUNCTION__);
#endif #endif
/* Failed to make writable inplace, mark that */ /* Failed to make writable inplace, mark that */
@ -341,7 +345,7 @@ hb_blob_try_writable (hb_blob_t *blob)
{ {
char *new_data; char *new_data;
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__, fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
blob->lock_count, blob->data); blob->lock_count, blob->data);
#endif #endif
@ -351,7 +355,7 @@ hb_blob_try_writable (hb_blob_t *blob)
new_data = malloc (blob->length); new_data = malloc (blob->length);
if (new_data) { if (new_data) {
#if HB_DEBUG #if HB_DEBUG_BLOB
fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, __FUNCTION__, blob->data); fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, __FUNCTION__, blob->data);
#endif #endif
memcpy (new_data, blob->data, blob->length); memcpy (new_data, blob->data, blob->length);

View File

@ -109,13 +109,17 @@ struct Null <Type> \
* Sanitize * Sanitize
*/ */
#if HB_DEBUG #ifndef HB_DEBUG_SANITIZE
#define HB_DEBUG_SANITIZE HB_DEBUG
#endif
#if HB_DEBUG_SANITIZE
#define SANITIZE_DEBUG_ARG_DEF , unsigned int sanitize_depth #define SANITIZE_DEBUG_ARG_DEF , unsigned int sanitize_depth
#define SANITIZE_DEBUG_ARG , sanitize_depth + 1 #define SANITIZE_DEBUG_ARG , sanitize_depth + 1
#define SANITIZE_DEBUG_ARG_INIT , 1 #define SANITIZE_DEBUG_ARG_INIT , 1
#define SANITIZE_DEBUG() \ #define SANITIZE_DEBUG() \
HB_STMT_START { \ HB_STMT_START { \
if (sanitize_depth < HB_DEBUG) \ if (sanitize_depth < HB_DEBUG_SANITIZE) \
fprintf (stderr, "SANITIZE(%p) %-*d-> %s\n", \ fprintf (stderr, "SANITIZE(%p) %-*d-> %s\n", \
(CONST_CHARP (this) == NullPool) ? 0 : this, \ (CONST_CHARP (this) == NullPool) ? 0 : this, \
sanitize_depth, sanitize_depth, \ sanitize_depth, sanitize_depth, \
@ -152,7 +156,7 @@ _hb_sanitize_init (hb_sanitize_context_t *context,
context->end = context->start + hb_blob_get_length (blob); context->end = context->start + hb_blob_get_length (blob);
context->edit_count = 0; context->edit_count = 0;
#if HB_DEBUG #if HB_DEBUG_SANITIZE
fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n", fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
context->blob, context->start, context->end, context->end - context->start); context->blob, context->start, context->end, context->end - context->start);
#endif #endif
@ -162,7 +166,7 @@ static HB_GNUC_UNUSED void
_hb_sanitize_fini (hb_sanitize_context_t *context, _hb_sanitize_fini (hb_sanitize_context_t *context,
bool unlock) bool unlock)
{ {
#if HB_DEBUG #if HB_DEBUG_SANITIZE
fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n", fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
context->blob, context->start, context->end, context->edit_count); context->blob, context->start, context->end, context->edit_count);
#endif #endif
@ -180,8 +184,8 @@ _hb_sanitize_check (SANITIZE_ARG_DEF,
base <= context->end && base <= context->end &&
(unsigned int) (context->end - base) >= len; (unsigned int) (context->end - base) >= len;
#if HB_DEBUG #if HB_DEBUG_SANITIZE
if (sanitize_depth < HB_DEBUG) \ if (sanitize_depth < HB_DEBUG_SANITIZE) \
fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \ fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
base, base,
sanitize_depth, sanitize_depth, sanitize_depth, sanitize_depth,
@ -200,8 +204,8 @@ _hb_sanitize_array (SANITIZE_ARG_DEF,
{ {
bool overflows = len >= ((unsigned int) -1) / record_size; bool overflows = len >= ((unsigned int) -1) / record_size;
#if HB_DEBUG #if HB_DEBUG_SANITIZE
if (sanitize_depth < HB_DEBUG) \ if (sanitize_depth < HB_DEBUG_SANITIZE) \
fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \ fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
base, base,
sanitize_depth, sanitize_depth, sanitize_depth, sanitize_depth,
@ -220,7 +224,7 @@ _hb_sanitize_edit (SANITIZE_ARG_DEF,
bool perm = hb_blob_try_writable_inplace (context->blob); bool perm = hb_blob_try_writable_inplace (context->blob);
context->edit_count++; context->edit_count++;
#if HB_DEBUG #if HB_DEBUG_SANITIZE
fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \ fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
base, base,
sanitize_depth, sanitize_depth, sanitize_depth, sanitize_depth,
@ -268,7 +272,7 @@ struct Sanitizer
/* TODO is_sane() stuff */ /* TODO is_sane() stuff */
retry: retry:
#if HB_DEBUG #if HB_DEBUG_SANITIZE
fprintf (stderr, "Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__); fprintf (stderr, "Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__);
#endif #endif
@ -279,7 +283,7 @@ struct Sanitizer
sane = t->sanitize (SANITIZE_ARG_INIT); sane = t->sanitize (SANITIZE_ARG_INIT);
if (sane) { if (sane) {
if (context.edit_count) { if (context.edit_count) {
#if HB_DEBUG #if HB_DEBUG_SANITIZE
fprintf (stderr, "Sanitizer %p passed first round with %d edits; going a second round %s\n", fprintf (stderr, "Sanitizer %p passed first round with %d edits; going a second round %s\n",
blob, context.edit_count, __PRETTY_FUNCTION__); blob, context.edit_count, __PRETTY_FUNCTION__);
#endif #endif
@ -287,7 +291,7 @@ struct Sanitizer
context.edit_count = 0; context.edit_count = 0;
sane = t->sanitize (SANITIZE_ARG_INIT); sane = t->sanitize (SANITIZE_ARG_INIT);
if (context.edit_count) { if (context.edit_count) {
#if HB_DEBUG #if HB_DEBUG_SANITIZE
fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n", fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
blob, context.edit_count, __PRETTY_FUNCTION__); blob, context.edit_count, __PRETTY_FUNCTION__);
#endif #endif
@ -300,14 +304,14 @@ struct Sanitizer
_hb_sanitize_fini (&context, true); _hb_sanitize_fini (&context, true);
if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) { if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
/* ok, we made it writable by relocating. try again */ /* ok, we made it writable by relocating. try again */
#if HB_DEBUG #if HB_DEBUG_SANITIZE
fprintf (stderr, "Sanitizer %p retry %s\n", blob, __PRETTY_FUNCTION__); fprintf (stderr, "Sanitizer %p retry %s\n", blob, __PRETTY_FUNCTION__);
#endif #endif
goto retry; goto retry;
} }
} }
#if HB_DEBUG #if HB_DEBUG_SANITIZE
fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", __PRETTY_FUNCTION__); fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", __PRETTY_FUNCTION__);
#endif #endif
if (sane) if (sane)