提交 a533f99f 编写于 作者: L lutao

add memtrace interface

Signed-off-by: Nlutao <lutao31@huawei.com>
上级 e25fd6f1
......@@ -140,7 +140,6 @@
__lutimes_time64;
__lxstat;
__lxstat64;
__mem_trace;
__memchr_diagnose;
__memcpy_chk;
__memmove_aarch64;
......@@ -1113,6 +1112,7 @@
mempcpy;
memrchr;
memset;
memtrace;
mincore;
mkdir;
mkdirat;
......
......@@ -2150,8 +2150,7 @@ musl_src_porting_file = [
musl_inc_hook_files = [
"porting/linux/user/src/internal/hilog_adapter.h",
"porting/linux/user/src/internal/musl_log.h",
"porting/linux/user/src/hook/common_def.h",
"porting/linux/user/src/hook/memory_tag.h",
"porting/linux/user/src/hook/memory_trace.h",
"porting/linux/user/src/hook/musl_malloc_dispatch_table.h",
"porting/linux/user/src/hook/musl_malloc_dispatch.h",
"porting/linux/user/src/hook/musl_preinit_common.h",
......
......@@ -701,7 +701,7 @@ template("musl_libs") {
source_set("soft_musl_hook") {
sources = [
"./porting/linux/user/src/hook/malloc_common.c",
"./porting/linux/user/src/hook/memory_tag.c",
"./porting/linux/user/src/hook/memory_trace.c",
"./porting/linux/user/src/hook/musl_preinit.c",
"./porting/linux/user/src/hook/musl_preinit_common.c",
"./porting/linux/user/src/hook/musl_socket_preinit.c",
......
#include "memory_tag.h"
volatile atomic_llong __mem_trace;
#ifndef _MEMORY_TAG_H
#define _MEMORY_TAG_H
#include <ctype.h>
#include <unistd.h>
#include "common_def.h"
#include "musl_preinit_common.h"
typedef int (*mmemtrace)(void* addr, size_t size, const char* tag, bool is_using);
extern volatile atomic_llong __mem_trace;
#define MEM_TRACE(addr, size, tag, is_using) \
do { \
volatile mmemtrace func = (volatile mmemtrace)atomic_load_explicit(&__mem_trace, memory_order_acquire); \
if (__predict_false(!__get_global_hook_flag())) { \
if (func != NULL) { \
func(addr, size, tag, is_using); \
} \
} \
} while (0)
#endif
#include "memory_trace.h"
#ifdef HOOK_ENABLE
#include "common_def.h"
#include "musl_preinit_common.h"
#endif
void memtrace(void* addr, size_t size, const char* tag, bool is_using)
{
#ifdef HOOK_ENABLE
volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
if (__predict_false(dispatch_table != NULL)) {
dispatch_table->memtrace(addr, size, tag, is_using);
}
#endif
return;
}
#ifndef _MEMORY_TRACE_H
#define _MEMORY_TRACE_H
#include <stdbool.h>
#include <unistd.h>
#ifdef __cplusplus
extern "C" {
#endif
void memtrace(void* addr, size_t size, const char* tag, bool is_using);
#ifdef __cplusplus
}
#endif
#endif
......@@ -28,6 +28,7 @@ typedef int (*MallocInfoType)(int, FILE*);
typedef void (*MallocStatsPrintType)(void (*) (void *, const char *), void *, const char *);
typedef int (*MalloptType)(int, int);
typedef ssize_t (*MallocBacktraceType)(void*, uintptr_t*, size_t);
typedef void (*Memtrace)(void*, size_t, const char*, bool);
typedef bool (*GetHookFlagType)();
typedef bool (*SetHookFlagType)(bool);
......@@ -53,6 +54,7 @@ struct MallocDispatchType {
MallocBacktraceType malloc_backtrace;
GetHookFlagType get_hook_flag;
SetHookFlagType set_hook_flag;
Memtrace memtrace;
};
#ifdef __cplusplus
}
......
......@@ -19,7 +19,6 @@ which need be escaped.
#include <signal.h>
#include "musl_malloc_dispatch_table.h"
#include "musl_malloc.h"
#include "memory_tag.h"
#include "musl_preinit_common.h"
#ifdef OHOS_ENABLE_PARAMETER
#include "sys_param.h"
......@@ -37,6 +36,7 @@ which need be escaped.
#include "musl_log.h"
void* ohos_malloc_hook_init_function(size_t bytes);
void default_memtrace(void* addr, size_t size, const char* tag, bool is_using) {}
static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = {
.malloc = ohos_malloc_hook_init_function,
......@@ -45,6 +45,7 @@ static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = {
.munmap = MuslMalloc(munmap),
.calloc = MuslMalloc(calloc),
.realloc = MuslMalloc(realloc),
.memtrace = default_memtrace,
};
#define MAX_SYM_NAME_SIZE 1000
#define MAX_PROC_NAME_SIZE 256
......@@ -208,15 +209,14 @@ static bool init_munmap_function(void* malloc_shared_library_handler, MallocMunm
return true;
}
static bool init_memtrace_function(void* malloc_shared_library_handler, const char* prefix)
static bool init_memtrace_function(void* malloc_shared_library_handler, Memtrace* func, const char* prefix)
{
char symbol[MAX_SYM_NAME_SIZE];
snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "memtrace");
mmemtrace func = (mmemtrace)(dlsym(malloc_shared_library_handler, symbol));
if (func == NULL) {
*func = (Memtrace)(dlsym(malloc_shared_library_handler, symbol));
if (*func == NULL) {
return false;
}
atomic_store_explicit(&__mem_trace, (volatile long long)func, memory_order_seq_cst);
return true;
}
......@@ -273,7 +273,7 @@ static bool init_hook_functions(void* shared_library_handler, struct MallocDispa
if (!init_realloc_function(shared_library_handler, &table->realloc, prefix)) {
return false;
}
if (!init_memtrace_function(shared_library_handler, prefix)) {
if (!init_memtrace_function(shared_library_handler, &table->memtrace, prefix)) {
return false;
}
if (!init_malloc_usable_size_function(shared_library_handler, &table->malloc_usable_size, prefix)) {
......@@ -610,7 +610,6 @@ __attribute__((constructor(1))) static void __musl_initialize()
{
atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)false, memory_order_seq_cst);
atomic_store_explicit(&__memleak_hook_flag, (volatile bool)false, memory_order_seq_cst);
atomic_store_explicit(&__mem_trace, (volatile const long long)NULL, memory_order_seq_cst);
__set_default_malloc();
char hook_process_path[MAX_PROC_NAME_SIZE + 1] = {0};
parse_hook_variable(&__hook_mode, hook_process_path, sizeof(hook_process_path) - 1);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册