77#include < map>
88#include " log.h"
99#include " xdl.h"
10- #include " dobby .h"
10+ #include " uprobe_trace_user .h"
1111#include " il2cpp_trace.h"
1212
1313#define DO_API (r, n, p ) r (*n) p
1717#undef DO_API
1818
1919char data_dir_path[PATH_MAX];
20+ char module_path[PATH_MAX];
2021static uint64_t il2cpp_base = 0 ;
21- uint64_t funaddrs[MAX_HOOK_FUN_NUM];
2222int hook_fun_num=0 ;
2323std::map<long ,std::string> fun_name_dict;
2424
@@ -47,7 +47,9 @@ int init_il2cpp_fun(){
4747 Dl_info dlInfo;
4848 if (dladdr ((void *) il2cpp_capture_memory_snapshot, &dlInfo)) {
4949 il2cpp_base = reinterpret_cast <uint64_t >(dlInfo.dli_fbase );
50+ strcpy (module_path,dlInfo.dli_fname );
5051 LOGD (" il2cpp_base: 0x%llx" , il2cpp_base);
52+ LOGD (" module_path: %s" , module_path);
5153 }
5254 }
5355 return flag;
@@ -93,42 +95,35 @@ char *get_trace_info(char *trace_file_path){
9395 return last_line;
9496}
9597
96- void trace_call_back (RegisterContext *ctx, const HookEntryInfo *info){
97- long fun_offset = (uint64_t )info->target_address -il2cpp_base;
98- LOGD (" %s is calling,offset:0x%llx" ,fun_name_dict[fun_offset].c_str (),fun_offset);
99- return ;
100- }
101-
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- }
98+ // void check_fun_instruction(){
99+ // for (int i = 0; i < hook_fun_num; i++) {
100+ // uint32_t *fun_instructions = static_cast<uint32_t *>((void *)funaddrs[i]);
101+ // if(fun_instructions[1]==0xd65f03c0){//RET
102+ // LOGW("pass hook fun 0x%llx",funaddrs[i]-il2cpp_base);
103+ // funaddrs[i] = 0;
104+ // }
105+ // }
106+ // LOGD("check all fun instruction");
107+ // }
112108
113109void hook_all_fun (){
114- for (int i = 0 ; i < hook_fun_num; i++) {
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);
110+ for (auto it = fun_name_dict.begin (); it != fun_name_dict.end (); ++it) {
111+ unsigned long fun_offset = it->first ;
112+ std::string fun_name = it->second ;
113+ int set_uprobe_ret = set_fun_info2 (fun_offset,(char *)fun_name.c_str ());
114+ if (set_uprobe_ret!=SET_TRACE_SUCCESS){
115+ LOGE (" set uprobe in fun_name:%s,fun_offset:0x%llx" ,fun_name.c_str (),fun_offset);
121116 }
122-
123117 }
124- LOGD (" success hook all fun" );
118+ LOGD (" success hook fun num:%d " ,hook_fun_num );
125119}
126120
127121void clear_all_hook (){
128- for (int i = 0 ; i < hook_fun_num; i++) {
129- DobbyDestroy ((void *)funaddrs[i]);
122+ int clear_ret = clear_all_uprobes ();
123+ if (clear_ret!=SET_TRACE_SUCCESS){
124+ LOGE (" clear all uprobes error" );
130125 }
131- LOGD (" success clear all fun " );
126+ LOGD (" success clear all uprobes " );
132127 hook_fun_num = 0 ;
133128 fun_name_dict.clear ();
134129}
@@ -138,7 +133,7 @@ void check_all_methods(void *klass,char *clazzName) {
138133 long fun_offset;
139134 while (auto method = il2cpp_class_get_methods (klass, &iter)) {
140135 // TODO attribute
141- if (method->methodPointer && hook_fun_num<MAX_HOOK_FUN_NUM ) {
136+ if (method->methodPointer && hook_fun_num<MAX_HOOK_NUM ) {
142137 fun_offset = (uint64_t )method->methodPointer - il2cpp_base;
143138 if (fun_name_dict.find (fun_offset) != fun_name_dict.end ()){
144139 continue ;
@@ -148,7 +143,6 @@ void check_all_methods(void *klass,char *clazzName) {
148143 snprintf (full_name,MAX_FULL_NAME_LEN," %s::%s" ,clazzName,method_name);
149144 std::string mfull_name (full_name);
150145 fun_name_dict[fun_offset]=mfull_name;
151- funaddrs[hook_fun_num] = (uint64_t )method->methodPointer ;
152146 hook_fun_num++;
153147 }
154148 }
@@ -171,6 +165,16 @@ void start_trace(char* data_dir_path){
171165 }
172166 LOGD (" success get il2cpp api fun" );
173167
168+ int set_module_base_ret = set_module_base (il2cpp_base);
169+ int set_target_file_ret = set_target_file (module_path);
170+ int set_target_uid_ret = set_target_uid (getuid ());
171+
172+ if (set_module_base_ret!=SET_TRACE_SUCCESS || set_target_file_ret!=SET_TRACE_SUCCESS || set_target_uid_ret!=SET_TRACE_SUCCESS){
173+ LOGE (" init uprobe hook error" );
174+ return ;
175+ }
176+ LOGD (" init uprobe hook success" );
177+
174178
175179 strcpy (trace_file_path,data_dir_path);
176180 strcat (trace_file_path," /files/test_trace.txt" );
@@ -200,7 +204,7 @@ void start_trace(char* data_dir_path){
200204 }
201205 for (int i = 0 ; i < all_type_infos_count; ++i) {
202206 if (strcmp (all_type_infos[i].name ,tmp_info)==0 ){
203- if (hook_fun_num==MAX_HOOK_FUN_NUM ){
207+ if (hook_fun_num==MAX_HOOK_NUM ){
204208 break ;
205209 }
206210 LOGD (" trace %s" ,all_type_infos[i].name );
0 commit comments