Skip to content

Commit a45f502

Browse files
做了一些优化
1 parent 770cf49 commit a45f502

File tree

4 files changed

+74
-48
lines changed

4 files changed

+74
-48
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44

55
# 如何使用?
6-
使用方法目前和[frida-il2cpp-trace-module](https://github.com/AndroidReverser-Test/frida-il2cpp-trace-module)保持一致
6+
在要进行trace的手游的私有目录(即/data/data/包名/files/)下创建test_trace.txt文件,并向其中输入要trace的类名即可,类名的获取以PtraceIl2cppDumper这个项目dump下来的为准,输入样例如:echo "test_clazz_name" >> test_trace.txt, 推荐使用echo向文件输入要trace的类,程序是默认定时获取test_trace.txt文件的最后一行的内容来作为类名进行trace。然后再通过任意ptrace注入器将本项目编译生成的so注入至游戏进程后可在logcat中使用Test-Log进行过滤查看结果。
77

8+
# 如何构建
9+
克隆本项目后,使用androidStudio打开,然后等待项目配置自动完成,之后在本项目目录下使用gradlew :app:externalNativeBuildRelease命令进行编译,编译完成后会在<项目目录>\app\build\intermediates\cmake\release\obj\arm64-v8a下生成相应so文件。
810

911
# 已知的问题
1012
部分游戏可能会在trace时崩溃,这与hook框架有关

app/src/main/cpp/il2cpp_trace.cpp

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ int init_il2cpp_fun(){
4242
if (handle) {
4343
int flag = -1;
4444
init_il2cpp_api(handle);
45-
if(il2cpp_capture_memory_snapshot && il2cpp_free_captured_memory_snapshot && il2cpp_class_get_methods && il2cpp_method_get_name){
45+
if(il2cpp_capture_memory_snapshot && il2cpp_start_gc_world && il2cpp_stop_gc_world && il2cpp_class_get_methods && il2cpp_method_get_name){
4646
flag = 0;
4747
Dl_info dlInfo;
4848
if (dladdr((void *) il2cpp_capture_memory_snapshot, &dlInfo)) {
4949
il2cpp_base = reinterpret_cast<uint64_t>(dlInfo.dli_fbase);
50-
LOGD("il2cpp_base: %llx", il2cpp_base);
50+
LOGD("il2cpp_base: 0x%llx", il2cpp_base);
5151
}
5252
}
5353
return flag;
@@ -84,6 +84,12 @@ char *get_trace_info(char *trace_file_path){
8484
}
8585

8686
fclose(file);
87+
88+
if (last_line == NULL || last_line[0] == '\0') {
89+
LOGE("can not get any trace item");
90+
return NULL;
91+
}
92+
8793
return last_line;
8894
}
8995

@@ -93,25 +99,53 @@ void trace_call_back(RegisterContext *ctx, const HookEntryInfo *info){
9399
return;
94100
}
95101

102+
void check_fun_instruction(){
103+
for (int i = 0; i < hook_fun_num; i++) {
104+
uint32_t *fun_instructions = static_cast<uint32_t *>((void *)funaddrs[i]);
105+
if(fun_instructions[1]==0xd65f03c0){//RET
106+
LOGW("pass hook fun 0x%llx",funaddrs[i]-il2cpp_base);
107+
funaddrs[i] = 0;
108+
}
109+
}
110+
LOGD("check all fun instruction");
111+
}
96112

97113
void hook_all_fun(){
98114
for (int i = 0; i < hook_fun_num; i++) {
99-
DobbyInstrument((void *)funaddrs[i], trace_call_back);
115+
if(funaddrs[i]==0){
116+
continue;
117+
}
118+
// LOGD("fun 0x%llx hook",funaddrs[i]-il2cpp_base);
119+
if(DobbyInstrument((void *)funaddrs[i], trace_call_back)!=0){
120+
LOGD("fun 0x%llx hook error",funaddrs[i]-il2cpp_base);
121+
}
122+
100123
}
101124
LOGD("success hook all fun");
102125
}
103126

127+
void clear_all_hook(){
128+
for (int i = 0; i < hook_fun_num; i++) {
129+
DobbyDestroy ((void *)funaddrs[i]);
130+
}
131+
LOGD("success clear all fun");
132+
hook_fun_num = 0;
133+
fun_name_dict.clear();
134+
}
135+
104136
void check_all_methods(void *klass,char *clazzName) {
105137
void *iter = nullptr;
106138
long fun_offset;
107139
while (auto method = il2cpp_class_get_methods(klass, &iter)) {
108140
//TODO attribute
109-
if (method->methodPointer) {
141+
if (method->methodPointer && hook_fun_num<MAX_HOOK_FUN_NUM) {
142+
fun_offset = (uint64_t)method->methodPointer - il2cpp_base;
143+
if(fun_name_dict.find(fun_offset) != fun_name_dict.end()){
144+
continue;
145+
}
110146
char full_name[MAX_FULL_NAME_LEN];
111147
auto method_name = il2cpp_method_get_name(method);
112148
snprintf(full_name,MAX_FULL_NAME_LEN,"%s::%s",clazzName,method_name);
113-
// LOGD("method_name:%s",full_name);
114-
fun_offset = (uint64_t)method->methodPointer - il2cpp_base;
115149
std::string mfull_name(full_name);
116150
fun_name_dict[fun_offset]=mfull_name;
117151
funaddrs[hook_fun_num] = (uint64_t)method->methodPointer;
@@ -127,7 +161,6 @@ void trace_type_info(Il2CppMetadataType type_info,char *clazzName) {
127161
}
128162

129163

130-
131164
void start_trace(char* data_dir_path){
132165
char trace_file_path[PATH_MAX];
133166

@@ -143,45 +176,44 @@ void start_trace(char* data_dir_path){
143176
strcat(trace_file_path,"/files/test_trace.txt");
144177
LOGD("get trace_file_path:%s",trace_file_path);
145178

146-
char* tinfo = get_trace_info(trace_file_path);
147-
if (tinfo == NULL || tinfo[0] == '\0') {
148-
LOGE("can not get any trace item");
149-
return;
150-
}
151-
LOGD("get trace item:%s",tinfo);
152-
153-
// char test_assemblyName[100];
154-
char test_clazzName[240];
155-
strcpy(test_clazzName,tinfo);
156-
test_clazzName[strlen(test_clazzName)-1] = '\0';
157-
158-
// char* split_str = strstr(tinfo,"+");
159-
// if(split_str==NULL){
160-
// LOGE("can not find split char +");
161-
// return;
162-
// }
163-
//
164-
// strncpy(test_assemblyName,tinfo,split_str-tinfo);
165-
// strcpy(test_clazzName,split_str+1);
166-
// test_clazzName[strlen(test_clazzName)-1] = '\0';
167-
// LOGD("assemblyName:%s,clazzName:%s",test_assemblyName,test_clazzName);
179+
168180

169181
if (il2cpp_base!=0) {
170182
auto memorySnapshot = il2cpp_capture_memory_snapshot();
171183
auto all_type_infos_count = memorySnapshot->metadata.typeCount;
172184
auto all_type_infos = memorySnapshot->metadata.types;
173185
LOGD("all_typeCount:%d",all_type_infos_count);
174-
for (int i = 0; i < all_type_infos_count; ++i) {
175-
if(strcmp(all_type_infos[i].name,test_clazzName)==0){
176-
LOGD("trace start");
177-
trace_type_info(all_type_infos[i],all_type_infos[i].name);
178-
break;
186+
char tmp_info[240]="test";
187+
while (true){
188+
char test_info[240];
189+
strcpy(test_info,get_trace_info(trace_file_path));
190+
test_info[strlen(test_info)-1] = '\0';
191+
if(strcmp(tmp_info,test_info)==0){
192+
sleep(2);
193+
continue;
194+
} else{
195+
strcpy(tmp_info,test_info);
196+
//清除之前的hook
197+
il2cpp_stop_gc_world();
198+
clear_all_hook();
199+
il2cpp_start_gc_world();
200+
}
201+
for (int i = 0; i < all_type_infos_count; ++i) {
202+
if(strcmp(all_type_infos[i].name,tmp_info)==0){
203+
if(hook_fun_num==MAX_HOOK_FUN_NUM){
204+
break;
205+
}
206+
LOGD("trace %s",all_type_infos[i].name);
207+
trace_type_info(all_type_infos[i],all_type_infos[i].name);
208+
break;
209+
}
179210
}
211+
// check_fun_instruction();
212+
il2cpp_stop_gc_world();
213+
hook_all_fun();
214+
il2cpp_start_gc_world();
180215
}
181-
il2cpp_stop_gc_world();
182-
hook_all_fun();
183-
il2cpp_start_gc_world();
184-
il2cpp_free_captured_memory_snapshot(memorySnapshot);
216+
// il2cpp_free_captured_memory_snapshot(memorySnapshot);
185217
} else {
186218
LOGE("unknow error");
187219
}

app/src/main/cpp/il2cpp_trace.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
//
2-
// Created by wuwenhui on 2025/5/19.
3-
//
4-
51
#ifndef IL2CPP_TRACE_MODULE_IL2CPP_TRACE_H
62
#define IL2CPP_TRACE_MODULE_IL2CPP_TRACE_H
73

84
#include <stdint.h>
95

106
#define MAX_FULL_NAME_LEN 200
11-
#define MAX_HOOK_FUN_NUM 1000
7+
#define MAX_HOOK_FUN_NUM 10000
128

139
struct Il2CppMetadataField
1410
{

app/src/main/cpp/log.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//
2-
// Created by wuwenhui on 2025/5/28.
3-
//
4-
51
#ifndef IL2CPP_TRACE_MODULE_LOG_H
62
#define IL2CPP_TRACE_MODULE_LOG_H
73

0 commit comments

Comments
 (0)