struct target *target;
dlist_foreach(s_info.target_list, l, target) {
- if (!strcmp(target->name, name))
+ if (!strcmp(target->name, name)) {
return target;
+ }
}
return NULL;
struct target *target;
dlist_foreach(s_info.target_list, l, target) {
- if (target->begin <= addr && addr < target->end)
+ if (target->begin <= addr && addr < target->end) {
return target;
+ }
}
return NULL;
register unsigned long *stack;
register int i;
- if (s_info.target_cnt <= 0)
+ if (s_info.target_cnt <= 0) {
return NULL;
+ }
stack = (unsigned long *)&ret;
base = 0;
boundary = 0;
if (!pthread_getattr_np(tid, &attr)) {
- if (!pthread_attr_getstack(&attr, (void *)&base, &size))
+ if (!pthread_attr_getstack(&attr, (void *)&base, &size)) {
boundary = base + size;
+ }
pthread_attr_destroy(&attr);
}
} else {
}
target = find_target_by_addr(*stack++);
- if (!target)
+ if (!target) {
continue;
+ }
- if (s_info.debugger)
+ if (s_info.debugger) {
DbgPrint("Target[%s]: %d\n", target->name, target->usage);
+ }
return target;
}
} else {
for (i = 0; i < s_info.dump_depth; i++) {
target = find_target_by_addr(*(stack++));
- if (!target)
+ if (!target) {
continue;
+ }
- if (s_info.debugger)
+ if (s_info.debugger) {
DbgPrint("[%d] Target[%s]: %d\n", i, target->name, target->usage);
+ }
return target;
}
break;
case MCHECK_HEAD:
target = find_target_info();
- if (target)
+ if (target) {
ErrPrint("[HEAD] Inconsistency: %s\n", target->name);
+ }
break;
case MCHECK_TAIL:
target = find_target_info();
- if (target)
+ if (target) {
ErrPrint("[HEAD] Inconsistency: %s\n", target->name);
+ }
break;
case MCHECK_FREE:
target = find_target_info();
- if (target)
+ if (target) {
ErrPrint("[FREE] Inconsistency: %s\n", target->name);
+ }
break;
default:
break;
enum mcheck_status status;
status = mprobe(ptr);
- if (status == MCHECK_DISABLED)
+ if (status == MCHECK_DISABLED) {
return 1;
+ }
if (status != MCHECK_OK) {
mcheck_cb(status);
SET_PAD(ptr, 0);
SET_STATE(ptr, VALID);
- if (chunk->info)
+ if (chunk->info) {
chunk->info->usage += size;
+ }
}
ESTIMATE_END();
{
struct chunk *chunk;
- if (!ptr)
+ if (!ptr) {
return;
+ }
LOCK();
unhook();
}
chunk = container_of((void *)(((char *)ptr) - PAD(ptr)), struct chunk, data);
- if (chunk->info)
+ if (chunk->info) {
chunk->info->usage -= chunk->size;
+ }
if (MPROBE(chunk)) {
SET_STATE(ptr, INVALID);
unhook();
ESTIMATE_START();
if (!__ptr) {
- if (!size)
+ if (!size) {
goto out;
+ }
/* Allocation */
chunk = realloc(__ptr, size + sizeof(*chunk));
SET_PAD(ptr, 0);
SET_STATE(ptr, VALID);
- if (chunk->info)
+ if (chunk->info) {
chunk->info->usage += size;
+ }
}
} else if (size == 0) {
/* Free */
ptr = realloc(__ptr, size);
} else {
chunk = container_of((void *)(((char *)__ptr) - PAD(__ptr)), struct chunk, data);
- if (chunk->info)
+ if (chunk->info) {
chunk->info->usage -= chunk->size;
+ }
if (MPROBE(chunk)) {
SET_STATE(__ptr, INVALID);
new_chunk = ptr;
new_chunk->info = find_target_info();
- if (new_chunk->info)
+ if (new_chunk->info) {
new_chunk->info->usage += size;
+ }
new_chunk->size = size;
ptr = new_chunk->data;
SET_STATE(ptr, VALID);
int pad;
int size;
- if (!__size)
+ if (!__size) {
return NULL;
+ }
LOCK();
unhook();
pad = align - (sizeof(*chunk) % align);
size = sizeof(*chunk) + pad;
chunk = memalign(align, size + __size);
- if (!chunk)
+ if (!chunk) {
goto out;
+ }
chunk->info = find_target_info();
- if (chunk->info)
+ if (chunk->info) {
chunk->info->usage += __size;
+ }
chunk->size = __size;
hook variables as volatile. Define it as empty for
older glibc versions */
#ifndef __MALLOC_HOOK_VOLATILE
-#define __MALLOC_HOOK_VOLATILE
+ #define __MALLOC_HOOK_VOLATILE
#endif
void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook)(void) = heap_monitor_init;
target = data;
- if (strcmp(info->dlpi_name, target->name))
+ if (strcmp(info->dlpi_name, target->name)) {
return 0;
+ }
for (i = 0; i < info->dlpi_phnum; i++) {
phdr = info->dlpi_phdr + i;
if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X)) {
target->begin = info->dlpi_addr + (unsigned long)phdr->p_vaddr;
target->end = target->begin + phdr->p_memsz;
- if (s_info.debugger)
+ if (s_info.debugger) {
DbgPrint("Target: %s - [%s] 0x%lX - 0x%lX\n",
target->name, info->dlpi_name, target->begin, target->end);
+ }
break;
}
}
ret = 0;
target = find_target_by_name(name);
- if (target)
+ if (target) {
goto out;
+ }
target = malloc(sizeof(*target));
if (!target) {
const char *var;
pthread_attr_t attr;
- if (s_info.initialized)
+ if (s_info.initialized) {
return;
+ }
#if defined(_ENABLE_MCHECK)
s_info.m_check = mcheck(mcheck_cb);
- if (s_info.m_check < 0)
+ if (s_info.m_check < 0) {
ErrPrint("Failed to install mcheck[%d]\n", s_info.m_check);
- else
+ } else {
DbgPrint("mcheck installed: %d\n", s_info.m_check);
+ }
#endif
s_info._malloc = __malloc_hook;
s_info._memalign = __memalign_hook;
var = getenv("HEAP_MONITOR_DUMP_DEPTH");
- if (var)
+ if (var) {
s_info.dump_depth = atoi(var);
+ }
var = getenv("HEAP_MONITOR_DEBUGGER");
- if (var)
+ if (var) {
s_info.debugger = atoi(var);
+ }
s_info.main_tid = pthread_self();
s_info.initialized = 1;
- if (pthread_getattr_np(s_info.main_tid, &attr))
+ if (pthread_getattr_np(s_info.main_tid, &attr)) {
return;
+ }
if (pthread_attr_getstack(&attr, (void *)&s_info.stack_base, &s_info.stack_size)) {
s_info.stack_base = 0;
API void heap_monitor_start(void)
{
- if (!s_info.initialized)
+ if (!s_info.initialized) {
return;
+ }
hook();
}
API void heap_monitor_stop(void)
{
- if (!s_info.initialized)
+ if (!s_info.initialized) {
return;
+ }
unhook();
}