Skip to content

Commit bc40e66

Browse files
适配更新
1 parent 52aac94 commit bc40e66

2 files changed

Lines changed: 47 additions & 45 deletions

File tree

app/src/main/cpp/kerneltracedemo.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define MAX_VMA_NUM 10
99

1010
char module_path[PATH_MAX];
11+
char fix_module_path[PATH_MAX];
1112
static uint64_t module_base = 0;
1213
unsigned long start_addrs[MAX_VMA_NUM];
1314
unsigned long end_addrs[MAX_VMA_NUM];
@@ -62,7 +63,12 @@ bool init_vma(){
6263

6364

6465
__attribute__((noinline)) void test_kernel_trace(){
65-
LOGD("test_kernel_trace fun calling");
66+
int a=0,b=0;
67+
a = b+8;
68+
b = a+5;
69+
char test[200];
70+
snprintf(test,200,"%d %d",a,b);
71+
LOGD("test_kernel_trace fun calling,%s",test);
6672
}
6773

6874
void test(){
@@ -79,12 +85,22 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
7985
}
8086
LOGD("success parse maps files");
8187

88+
strcpy(fix_module_path,module_path);//这里设置为原模块路径就是跟没设置一样,只是用于演示api的使用
89+
8290
//为KernelTrace提供必要的初始信息
83-
set_target_uid(getuid());
84-
set_module_base(module_base);
85-
set_target_file(module_path);
91+
trace_init_info *base_info = (trace_init_info*)malloc(sizeof(trace_init_info));
92+
base_info->module_base = module_base;
93+
base_info->uid = getuid();
94+
95+
base_info->tfile_name = (char *)malloc(strlen(module_path) + 1);
96+
strcpy(base_info->tfile_name,module_path);
97+
98+
base_info->fix_file_name = (char *)malloc(strlen(fix_module_path) + 1);
99+
strcpy(base_info->fix_file_name,fix_module_path);
86100

87-
LOGD("module_base:%llx,module_path:%s",module_base,module_path);
101+
int sret = trace_init(base_info);
102+
103+
LOGD("module_base:%llx,module_path:%s,fix_module_path:%s,sret:%d",module_base,module_path,fix_module_path,sret);
88104

89105
//hook前进行的一些准备
90106
unsigned long test_fun_addr = (unsigned long)test_kernel_trace;
@@ -96,14 +112,12 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
96112
}
97113
}
98114

99-
char oins[4];
100-
memcpy(oins,(void *)test_kernel_trace,4);//获取被hook函数的第一条汇编指令
101-
LOGD("test_fun_offset:%lx,uprobe_offset:%lx",test_fun_offset,uprobe_offset);
102-
//如果so的相应汇编指令不是在so加载后才动态解密可直接设置fix_insn参数为NULL
103-
//set_fun_info(uprobe_offset,test_fun_offset,"test_kernel_trace",NULL);
104-
105-
//不过最好还是直接读取汇编指令并传入
106-
set_fun_info(uprobe_offset,test_fun_offset,"test_kernel_trace",oins);//发送hook请求
115+
uprobe_item_info *uprobe_item = (uprobe_item_info*)malloc(sizeof(uprobe_item_info));
116+
uprobe_item->uprobe_offset = uprobe_offset;
117+
uprobe_item->fun_offset = test_fun_offset;
118+
uprobe_item->fun_name = (char *)malloc(strlen("test_kernel_trace") + 1);
119+
strcpy(uprobe_item->fun_name,"test_kernel_trace");
120+
set_fun_info(uprobe_item);//发送hook请求
107121

108122
//启动测试线程开始测试
109123
std::thread test_thread(test);

app/src/main/cpp/uprobe_trace_user.h

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,41 @@
33

44

55
#define TRACE_FLAG 511
6-
#define MAX_HOOK_NUM 1000
6+
#define MAX_HOOK_NUM 2000
77
#define SET_TRACE_SUCCESS 1000
88
#define SET_TRACE_ERROR 1001
99

1010
enum trace_info {
11-
SET_TARGET_FILE,
12-
SET_MODULE_BASE,
11+
SET_TRACE_INFO,
1312
SET_FUN_INFO,
14-
FIX_ORI_INS,
15-
SET_TARGET_UPROBE,
16-
SET_TARGET_UID,
1713
CLEAR_UPROBE,
1814
};
1915

20-
// unsigned long start, size_t len, unsigned char *vec
16+
struct trace_init_info {
17+
uid_t uid;
18+
unsigned long module_base;
19+
char* tfile_name;
20+
char* fix_file_name;
21+
};
2122

22-
int set_module_base(unsigned long module_base){
23-
int ret = syscall(__NR_mincore,module_base,TRACE_FLAG+SET_MODULE_BASE,"");
24-
return ret;
25-
}
23+
struct uprobe_item_info {
24+
unsigned long uprobe_offset;
25+
unsigned long fun_offset;
26+
char *fun_name;
27+
};
2628

27-
int set_target_uid(uid_t uid){
28-
int ret = syscall(__NR_mincore,uid,TRACE_FLAG+SET_TARGET_UID,"");
29+
int clear_all_uprobes(){
30+
int ret = syscall(__NR_mincore,0,TRACE_FLAG+CLEAR_UPROBE,"");
2931
return ret;
3032
}
3133

32-
int set_target_file(char* file_name){
33-
int ret = syscall(__NR_mincore,0,TRACE_FLAG+SET_TARGET_FILE,file_name);
34+
int trace_init(trace_init_info *base_info){
35+
clear_all_uprobes();
36+
int ret = syscall(__NR_mincore,0,TRACE_FLAG+SET_TRACE_INFO,base_info);
3437
return ret;
3538
}
3639

37-
int set_fun_info(unsigned long uprobe_offset,unsigned long fun_offset,char *fun_name,char *fix_insn){
38-
int insert_key_ret = syscall(__NR_mincore,fun_offset,TRACE_FLAG+SET_FUN_INFO,fun_name);
39-
if(insert_key_ret==SET_TRACE_SUCCESS){
40-
if(fix_insn){
41-
int fix_insn_ret = syscall(__NR_mincore,uprobe_offset,TRACE_FLAG+FIX_ORI_INS,fix_insn);
42-
if(fix_insn_ret == SET_TRACE_ERROR){
43-
return SET_TRACE_ERROR;
44-
}
45-
}
46-
int ret = syscall(__NR_mincore,uprobe_offset,TRACE_FLAG+SET_TARGET_UPROBE,"");
47-
return ret;
48-
}
49-
return SET_TRACE_ERROR;
50-
}
51-
52-
int clear_all_uprobes(){
53-
int ret = syscall(__NR_mincore,0,TRACE_FLAG+CLEAR_UPROBE,"");
40+
int set_fun_info(uprobe_item_info *uprobe_item){
41+
int ret = syscall(__NR_mincore,0,TRACE_FLAG+SET_FUN_INFO,uprobe_item);
5442
return ret;
5543
}

0 commit comments

Comments
 (0)