Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在报告里面带上具体的分配函数 #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion library/src/main/cpp/Cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,19 @@ typedef struct {
uintptr_t trace[MAX_TRACE_DEPTH];
} Backtrace;

enum ALLOC_FUNC {
MALLOC,
CALLOC,
REALLOC,
MEMALIGN,
MMAP,
MMAP64
};

struct AllocNode {
uint32_t size;
uintptr_t addr;
ALLOC_FUNC func;
uintptr_t trace[MAX_TRACE_DEPTH];
AllocNode *next;
};
Expand All @@ -55,7 +65,7 @@ class Cache {
virtual ~Cache() {}
public:
virtual void reset() = 0;
virtual void insert(uintptr_t address, size_t size, Backtrace *backtrace) = 0;
virtual void insert(uintptr_t address, size_t size, ALLOC_FUNC func, Backtrace *backtrace) = 0;
virtual void remove(uintptr_t address) = 0;
virtual void print() = 0;
protected:
Expand Down
16 changes: 8 additions & 8 deletions library/src/main/cpp/HookProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void update_configs(Cache *pNew, uint32_t params) {
}

//**************************************************************************************************
static inline void insert_memory_backtrace(void *address, size_t size) {
static inline void insert_memory_backtrace(void *address, size_t size, ALLOC_FUNC func_name) {
Backtrace backtrace;
backtrace.depth = 0;

Expand All @@ -68,7 +68,7 @@ static inline void insert_memory_backtrace(void *address, size_t size) {
backtrace.depth = unwind_backtrace(backtrace.trace, depth + 1);
#endif

cache->insert((uintptr_t) address, size, &backtrace);
cache->insert((uintptr_t) address, size, func_name, &backtrace);
}

//**************************************************************************************************
Expand Down Expand Up @@ -96,7 +96,7 @@ static void *malloc_proxy(size_t size) {
pthread_setspecific(guard, (void *) 1);
void *address = malloc_origin(size);
if (address != NULL) {
insert_memory_backtrace(address, size);
insert_memory_backtrace(address, size, MALLOC);
}
pthread_setspecific(guard, (void *) 0);
return address;
Expand All @@ -111,7 +111,7 @@ static void *calloc_proxy(size_t count, size_t bytes) {
pthread_setspecific(guard, (void *) 1);
void *address = calloc_origin(count, bytes);
if (address != NULL) {
insert_memory_backtrace(address, size);
insert_memory_backtrace(address, size, CALLOC);
}
pthread_setspecific(guard, (void *) 0);
return address;
Expand All @@ -129,7 +129,7 @@ static void *realloc_proxy(void *ptr, size_t size) {
}

if (address != NULL && size >= limit) {
insert_memory_backtrace(address, size);
insert_memory_backtrace(address, size, REALLOC);
}
pthread_setspecific(guard, (void *) 0);
return address;
Expand All @@ -143,7 +143,7 @@ static void *memalign_proxy(size_t alignment, size_t size) {
pthread_setspecific(guard, (void *) 1);
void *address = memalign_origin(alignment, size);
if (address != NULL) {
insert_memory_backtrace(address, size);
insert_memory_backtrace(address, size, MEMALIGN);
}
pthread_setspecific(guard, (void *) 0);
return address;
Expand All @@ -168,7 +168,7 @@ static void *mmap_proxy(void *ptr, size_t size, int port, int flags, int fd, off
pthread_setspecific(guard, (void *) 1);
void *address = mmap_origin(ptr, size, port, flags, fd, offset);
if (address != MAP_FAILED) {
insert_memory_backtrace(address, size);
insert_memory_backtrace(address, size, MMAP);
}
pthread_setspecific(guard, (void *) 0);
return address;
Expand All @@ -182,7 +182,7 @@ static void *mmap64_proxy(void *ptr, size_t size, int port, int flags, int fd, o
pthread_setspecific(guard, (void *) 1);
void *address = mmap64_origin(ptr, size, port, flags, fd, offset);
if (address != MAP_FAILED) {
insert_memory_backtrace(address, size);
insert_memory_backtrace(address, size, MMAP64);
}
pthread_setspecific(guard, (void *) 0);
return address;
Expand Down
16 changes: 14 additions & 2 deletions library/src/main/cpp/MemoryCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,19 @@ inline AllocNode *remove_alloc(AllocNode **header, uintptr_t address) {
}
}

inline const char* get_func_name(ALLOC_FUNC func) {
switch(func) {
case MALLOC: return "malloc";
case CALLOC: return "calloc";
case REALLOC: return "realloc";
case MEMALIGN: return "memalign";
case MMAP: return "mmap";
case MMAP64: return "mmap64";
}
}

void write_trace(FILE *output, AllocNode *alloc_node, MapData *map_data, void **dl_cache) {
fprintf(output, STACK_FORMAT_HEADER, alloc_node->addr, alloc_node->size);
fprintf(output, STACK_FORMAT_HEADER, alloc_node->addr, alloc_node->size, get_func_name(alloc_node->func));
for (int i = 0; alloc_node->trace[i] != 0; i++) {
uintptr_t pc = alloc_node->trace[i];
Dl_info info;
Expand Down Expand Up @@ -123,7 +134,7 @@ void MemoryCache::reset() {
}
}

void MemoryCache::insert(uintptr_t address, size_t size, Backtrace *backtrace) {
void MemoryCache::insert(uintptr_t address, size_t size, ALLOC_FUNC func, Backtrace *backtrace) {
AllocNode *p = alloc_cache->apply();
if (p == nullptr) {
LOGGER("Alloc cache is full!!!!!!!!");
Expand All @@ -132,6 +143,7 @@ void MemoryCache::insert(uintptr_t address, size_t size, Backtrace *backtrace) {

p->addr = address;
p->size = size;
p->func = func;
uint depth = backtrace->depth > 2 ? backtrace->depth - 2 : 1;
memcpy(p->trace, backtrace->trace + 2, depth * sizeof(uintptr_t));
p->trace[depth] = 0;
Expand Down
6 changes: 3 additions & 3 deletions library/src/main/cpp/MemoryCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
#include "AllocPool.hpp"

#if defined(__LP64__)
#define STACK_FORMAT_HEADER "\n0x%016lx, %u, 1\n"
#define STACK_FORMAT_HEADER "\n0x%016lx, %u, %s, 1\n"
#define STACK_FORMAT_UNKNOWN "0x%016lx <unknown>\n"
#define STACK_FORMAT_ANONYMOUS "0x%016lx <anonymous:%016lx>\n"
#define STACK_FORMAT_FILE "0x%016lx %s (unknown)\n"
#define STACK_FORMAT_FILE_NAME "0x%016lx %s (%s + \?)\n"
#define STACK_FORMAT_FILE_NAME_LINE "0x%016lx %s (%s + %lu)\n"
#else
#define STACK_FORMAT_HEADER "\n0x%08x, %u, 1\n"
#define STACK_FORMAT_HEADER "\n0x%08x, %u, %s, 1\n"
#define STACK_FORMAT_UNKNOWN "0x%08x <unknown>\n"
#define STACK_FORMAT_ANONYMOUS "0x%08x <anonymous:%08x>\n"
#define STACK_FORMAT_FILE "0x%08x %s (unknown)\n"
Expand All @@ -42,7 +42,7 @@ class MemoryCache : public Cache {
~MemoryCache();
public:
void reset();
void insert(uintptr_t address, size_t size, Backtrace *backtrace);
void insert(uintptr_t address, size_t size, ALLOC_FUNC func, Backtrace *backtrace);
void remove(uintptr_t address);
void print();
private:
Expand Down
11 changes: 7 additions & 4 deletions library/src/main/python/raphael.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,18 @@ def __sub__(self, b):


class Trace:
def __init__(self, id, size, count, stack):
def __init__(self, id, size, func, count, stack):
self.id = id
self.size = int(size)
self.func = func
self.count = int(count)
self.stack = stack

def __eq__(self, b):
if len(self.stack) != len(b.stack):
return False
if self.func != b.func:
return False
for i in range(0, len(self.stack)):
if self.stack[i] != b.stack[i]:
return False
Expand Down Expand Up @@ -152,7 +155,7 @@ def print_report(writer, report):
for record in report:
retry_symbol(record)

writer.write('\n%s, %s, %s\n' % (record.id, record.size, record.count))
writer.write('\n%s, %s, %s, %s\n' % (record.id, record.size, record.func, record.count))
for frame in record.stack:
writer.write('%s %s (%s)\n' % (frame.pc, frame.path, frame.desc))

Expand Down Expand Up @@ -182,8 +185,8 @@ def parse_report(string):
stack = []
for frame in match:
stack.append(Frame(frame[0], frame[1], frame[2]))
match = re.compile(r'(0x[0-9a-f]+),\ (\d+),\ (\d+)$', re.M | re.I).findall(split)
report.append(Trace(match[0][0], match[0][1], match[0][2], stack))
match = re.compile(r'(0x[0-9a-f]+),\ (\d+),\ (\S+),\ (\d+)$', re.M | re.I).findall(split)
report.append(Trace(match[0][0], match[0][1], match[0][2], match[0][3], stack))
return report


Expand Down