@@ -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
97113void 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+
104136void 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-
131164void 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 }
0 commit comments