Add sugar syntax for debug messages

Buffer debugging coming soon.
This commit is contained in:
Behdad Esfahbod 2011-07-25 16:47:02 -04:00
parent 3a81b1db89
commit decd4e3e16
4 changed files with 44 additions and 51 deletions

View File

@ -243,32 +243,27 @@ _try_make_writable_inplace_unix (hb_blob_t *blob)
#endif #endif
if ((uintptr_t) -1L == pagesize) { if ((uintptr_t) -1L == pagesize) {
(void) (HB_DEBUG_BLOB && DEBUG (BLOB, fprintf (stderr, "%p %s: failed to get pagesize: %s\n", (void *) blob, HB_FUNC, strerror (errno)));
fprintf (stderr, "%p %s: failed to get pagesize: %s\n", (void *) blob, HB_FUNC, strerror (errno)));
return FALSE; return FALSE;
} }
(void) (HB_DEBUG_BLOB && DEBUG (BLOB, fprintf (stderr, "%p %s: pagesize is %lu\n", (void *) blob, HB_FUNC, (unsigned long) pagesize));
fprintf (stderr, "%p %s: pagesize is %lu\n", (void *) blob, HB_FUNC, (unsigned long) pagesize));
mask = ~(pagesize-1); mask = ~(pagesize-1);
addr = (const char *) (((uintptr_t) blob->data) & mask); addr = (const char *) (((uintptr_t) blob->data) & mask);
length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr; length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr;
(void) (HB_DEBUG_BLOB && DEBUG (BLOB, fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%lu bytes)\n",
fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%lu bytes)\n", (void *) blob, HB_FUNC,
(void *) blob, HB_FUNC, addr, addr+length, (unsigned long) length));
addr, addr+length, (unsigned long) length));
if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) { if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
(void) (HB_DEBUG_BLOB && DEBUG (BLOB, fprintf (stderr, "%p %s: %s\n", (void *) blob, HB_FUNC, strerror (errno)));
fprintf (stderr, "%p %s: %s\n", (void *) blob, HB_FUNC, strerror (errno)));
return FALSE; return FALSE;
} }
blob->mode = HB_MEMORY_MODE_WRITABLE; blob->mode = HB_MEMORY_MODE_WRITABLE;
(void) (HB_DEBUG_BLOB && DEBUG (BLOB, fprintf (stderr, "%p %s: successfully made [%p..%p] (%lu bytes) writable\n",
fprintf (stderr, "%p %s: successfully made [%p..%p] (%lu bytes) writable\n", (void *) blob, HB_FUNC,
(void *) blob, HB_FUNC, addr, addr+length, (unsigned long) length));
addr, addr+length, (unsigned long) length));
return TRUE; return TRUE;
#else #else
return FALSE; return FALSE;
@ -278,14 +273,12 @@ _try_make_writable_inplace_unix (hb_blob_t *blob)
static bool static bool
_try_writable_inplace (hb_blob_t *blob) _try_writable_inplace (hb_blob_t *blob)
{ {
(void) (HB_DEBUG_BLOB && DEBUG (BLOB, fprintf (stderr, "%p %s: making writable inplace\n", (void *) blob, HB_FUNC));
fprintf (stderr, "%p %s: making writable inplace\n", (void *) blob, HB_FUNC));
if (_try_make_writable_inplace_unix (blob)) if (_try_make_writable_inplace_unix (blob))
return TRUE; return TRUE;
(void) (HB_DEBUG_BLOB && DEBUG (BLOB, fprintf (stderr, "%p %s: making writable -> FAILED\n", (void *) blob, HB_FUNC));
fprintf (stderr, "%p %s: making writable -> FAILED\n", (void *) blob, HB_FUNC));
/* Failed to make writable inplace, mark that */ /* Failed to make writable inplace, mark that */
blob->mode = HB_MEMORY_MODE_READONLY; blob->mode = HB_MEMORY_MODE_READONLY;
@ -308,8 +301,7 @@ _try_writable (hb_blob_t *blob)
return TRUE; return TRUE;
(void) (HB_DEBUG_BLOB && DEBUG (BLOB, fprintf (stderr, "%p %s -> %p\n", (void *) blob, HB_FUNC, blob->data));
fprintf (stderr, "%p %s -> %p\n", (void *) blob, HB_FUNC, blob->data));
char *new_data; char *new_data;
@ -317,8 +309,7 @@ _try_writable (hb_blob_t *blob)
if (unlikely (!new_data)) if (unlikely (!new_data))
return FALSE; return FALSE;
(void) (HB_DEBUG_BLOB && DEBUG (BLOB, fprintf (stderr, "%p %s: dupped successfully -> %p\n", (void *) blob, HB_FUNC, blob->data));
fprintf (stderr, "%p %s: dupped successfully -> %p\n", (void *) blob, HB_FUNC, blob->data));
memcpy (new_data, blob->data, blob->length); memcpy (new_data, blob->data, blob->length);
_hb_blob_destroy_user_data (blob); _hb_blob_destroy_user_data (blob);

View File

@ -191,11 +191,10 @@ struct _hb_object_header_t {
} }
inline void trace (const char *function) const { inline void trace (const char *function) const {
(void) (HB_DEBUG_OBJECT && DEBUG (OBJECT, fprintf (stderr, "OBJECT(%p) refcount=%d %s\n",
fprintf (stderr, "OBJECT(%p) refcount=%d %s\n", (void *) this,
(void *) this, this ? ref_count.get () : 0,
this ? ref_count.get () : 0, function));
function));
} }
}; };

View File

@ -220,13 +220,13 @@ struct hb_sanitize_context_t
p <= this->end && p <= this->end &&
(unsigned int) (this->end - p) >= len; (unsigned int) (this->end - p) >= len;
(void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE && DEBUG_DEPTH (SANITIZE, this->debug_depth,
fprintf (stderr, "SANITIZE(%p) %-*d-> range [%p..%p] (%d bytes) in [%p..%p] -> %s\n", fprintf (stderr, "SANITIZE(%p) %-*d-> range [%p..%p] (%d bytes) in [%p..%p] -> %s\n",
p, p,
this->debug_depth, this->debug_depth, this->debug_depth, this->debug_depth,
p, p + len, len, p, p + len, len,
this->start, this->end, this->start, this->end,
ret ? "pass" : "FAIL")); ret ? "pass" : "FAIL"));
return likely (ret); return likely (ret);
} }
@ -236,13 +236,13 @@ struct hb_sanitize_context_t
const char *p = (const char *) base; const char *p = (const char *) base;
bool overflows = _hb_unsigned_int_mul_overflows (len, record_size); bool overflows = _hb_unsigned_int_mul_overflows (len, record_size);
(void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE && DEBUG_DEPTH (SANITIZE, this->debug_depth,
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",
p, p,
this->debug_depth, this->debug_depth, this->debug_depth, this->debug_depth,
p, p + (record_size * len), record_size, len, (unsigned long) record_size * len, p, p + (record_size * len), record_size, len, (unsigned long) record_size * len,
this->start, this->end, this->start, this->end,
!overflows ? "does not overflow" : "OVERFLOWS FAIL")); !overflows ? "does not overflow" : "OVERFLOWS FAIL"));
return likely (!overflows && this->check_range (base, record_size * len)); return likely (!overflows && this->check_range (base, record_size * len));
} }
@ -258,14 +258,14 @@ struct hb_sanitize_context_t
const char *p = (const char *) base; const char *p = (const char *) base;
this->edit_count++; this->edit_count++;
(void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE && DEBUG_DEPTH (SANITIZE, this->debug_depth,
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",
p, p,
this->debug_depth, this->debug_depth, this->debug_depth, this->debug_depth,
this->edit_count, this->edit_count,
p, p + len, len, p, p + len, len,
this->start, this->end, this->start, this->end,
this->writable ? "granted" : "REJECTED")); this->writable ? "granted" : "REJECTED"));
return this->writable; return this->writable;
} }

View File

@ -466,14 +466,17 @@ static inline unsigned char TOLOWER (unsigned char c)
#define HB_DEBUG 0 #define HB_DEBUG 0
#endif #endif
#define DEBUG(WHAT, CODE) (void) (HB_DEBUG_##WHAT && (CODE))
#define DEBUG_DEPTH(WHAT, DEPTH, CODE) (void) (HB_DEBUG_##WHAT && (int) (DEPTH) < (int) (HB_DEBUG_##WHAT) && (CODE))
static inline bool /* always returns TRUE */ static inline bool /* always returns TRUE */
_hb_trace (const char *what, _hb_trace (const char *what,
const char *function, const char *message,
const void *obj, const void *obj,
unsigned int depth, unsigned int depth,
unsigned int max_depth) unsigned int max_depth)
{ {
(void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function)); (void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, message));
return TRUE; return TRUE;
} }