Skip to content

Commit 0e856f5

Browse files
author
gaohaoyuan
committed
update hook code
1 parent 6bf37d1 commit 0e856f5

File tree

5 files changed

+80
-250
lines changed

5 files changed

+80
-250
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
.vscode/
2+
.cache/
13
build/
24
out/
3-
.vscode/
45
compile_commands.json

CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
2121
endif()
2222

2323
add_subdirectory(aosp_file)
24-
add_subdirectory(backtrace_helper)
24+
add_subdirectory(unwindstack/cmake)
25+
add_subdirectory(backtrace)
2526

2627
if (ANDROID_ABI MATCHES "arm64-v8a")
2728
set(VNDK_LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/aosp_file/com.android.vndk.v30/lib64")
@@ -32,9 +33,9 @@ else ()
3233
endif ()
3334

3435
# 添加 .so
35-
add_library(alloc_hook SHARED ${CMAKE_SOURCE_DIR}/src/host_alloc_hook.cpp ${CMAKE_SOURCE_DIR}/src/dma_alloc_hook.cpp)
36+
add_library(alloc_hook SHARED ${CMAKE_SOURCE_DIR}/src/alloc_hook.cpp)
3637
# 链接需要的库和头文件
37-
target_link_libraries(alloc_hook PRIVATE backtrace_helper)
38+
target_link_libraries(alloc_hook PRIVATE helper)
3839
# 安装到 out/lib 目录
3940
install(TARGETS alloc_hook DESTINATION ${CMAKE_INSTALL_PREFIX}/out/lib)
4041

src/alloc_hook.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <dlfcn.h>
2+
#include <pthread.h>
3+
#include <cstddef>
4+
#include <type_traits>
5+
6+
#include "DebugData.h"
7+
#include "PointerData.h"
8+
#include "malloc_debug.h"
9+
10+
class AllocHook {
11+
public:
12+
AllocHook() {
13+
void* ptr [2] = { &Db_storage, &Pd_storage };
14+
debug_initialize(ptr);
15+
}
16+
~AllocHook() { debug_finalize(); }
17+
18+
void* malloc(size_t size) { return debug_malloc(size); }
19+
void free(void* ptr) { debug_free(ptr); }
20+
void* calloc(size_t a, size_t b) { return debug_calloc(a, b); }
21+
void* realloc(void* ptr, size_t size) { return debug_realloc(ptr, size); }
22+
int posix_memalign(void** ptr, size_t alignment, size_t size) { return debug_posix_memalign(ptr, alignment, size); }
23+
int ioctl(int fd, int request, void* arg) { return debug_ioctl(fd, request, arg); }
24+
int close(int fd) { return debug_close(fd); }
25+
26+
static AllocHook& inst();
27+
28+
private:
29+
static std::aligned_storage<sizeof(DebugData), alignof(DebugData)>::type Db_storage;
30+
static std::aligned_storage<sizeof(PointerData), alignof(PointerData)>::type Pd_storage;
31+
};
32+
std::aligned_storage<sizeof(DebugData), alignof(DebugData)>::type AllocHook::Db_storage;
33+
std::aligned_storage<sizeof(PointerData), alignof(PointerData)>::type AllocHook::Pd_storage;
34+
35+
AllocHook& AllocHook::inst() {
36+
static AllocHook hook;
37+
return hook;
38+
}
39+
40+
extern "C" {
41+
void* malloc(size_t size) {
42+
return AllocHook::inst().malloc(size);
43+
}
44+
45+
void free(void* ptr) {
46+
AllocHook::inst().free(ptr);
47+
}
48+
49+
void* calloc(size_t a, size_t b) {
50+
return AllocHook::inst().calloc(a, b);
51+
}
52+
53+
void* realloc(void* ptr, size_t size) {
54+
return AllocHook::inst().realloc(ptr, size);
55+
}
56+
57+
int posix_memalign(void** ptr, size_t alignment, size_t size) {
58+
return AllocHook::inst().posix_memalign(ptr, alignment, size);
59+
}
60+
61+
int ioctl(int fd, int request, ...) {
62+
va_list ap;
63+
va_start(ap, request);
64+
void* arg = va_arg(ap, void*);
65+
va_end(ap);
66+
67+
return AllocHook::inst().ioctl(fd, request, arg);
68+
}
69+
70+
int close(int fd) {
71+
return AllocHook::inst().close(fd);
72+
}
73+
74+
}

src/dma_alloc_hook.cpp

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/host_alloc_hook.cpp

Lines changed: 0 additions & 172 deletions
This file was deleted.

0 commit comments

Comments
 (0)