55#include < sstream>
66#include < thread>
77#include < map>
8+ #include < fcntl.h>
9+ #include < sys/mman.h>
810#include " log.h"
911#include " xdl.h"
1012#include " uprobe_trace_user.h"
@@ -181,12 +183,10 @@ bool init_vma(){
181183 if (fields==8 ){
182184 if (strcmp (path,module_path)==0 ){
183185// LOGD("start:%lx,end:%lx,permissions:%s,tbase:%lx\n",tstart,tend,permissions,tbase);
184- if (permissions[2 ]==' x' ){
185- start_addrs[vma_num] = tstart;
186- end_addrs[vma_num] = tend;
187- vma_base[vma_num] = tbase;
188- vma_num++;
189- }
186+ start_addrs[vma_num] = tstart;
187+ end_addrs[vma_num] = tend;
188+ vma_base[vma_num] = tbase;
189+ vma_num++;
190190 }
191191 }
192192
@@ -198,6 +198,47 @@ bool init_vma(){
198198 return true ;
199199}
200200
201+ void dump_so (){
202+ LOGD (" start dump libil2cpp.so,base:%llx,end:%llx" ,il2cpp_base,end_addrs[vma_num-1 ]);
203+ char il2cpp_dump_path[PATH_MAX];
204+ sprintf (il2cpp_dump_path," %s/files/libil2cpp_%llx_dump.so" ,data_dir_path,il2cpp_base);
205+ LOGD (" il2cpp_dump_path:%s" ,il2cpp_dump_path);
206+
207+ // 打开文件(创建并截断)
208+ int fd = open (il2cpp_dump_path, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
209+ if (fd == -1 ) {
210+ LOGE (" Failed to open il2cpp_dump_path:%s" ,il2cpp_dump_path);
211+ return ;
212+ }
213+
214+ size_t total_written = 0 ;
215+ size_t remaining = end_addrs[vma_num-1 ] - il2cpp_base;
216+ char * current_ptr = (char *)il2cpp_base;
217+ char fill_buf[CHUNK_SIZE];
218+ memset (fill_buf,0 ,CHUNK_SIZE);
219+
220+ // 分块写入,避免大内存一次性写入
221+ while (remaining > 0 ) {
222+ size_t chunk_size = (remaining > CHUNK_SIZE) ? CHUNK_SIZE : remaining;
223+ ssize_t written = write (fd, current_ptr, chunk_size);
224+
225+ if (written <= 0 ) {
226+ LOGE (" Write error at offset %zu" , total_written);
227+ written = write (fd,fill_buf,chunk_size);
228+ }
229+
230+ total_written += written;
231+ remaining -= written;
232+ current_ptr += written;
233+ }
234+
235+ // 确保数据写入磁盘
236+ fsync (fd);
237+ close (fd);
238+
239+ LOGD (" success dump:%s" ,module_path);
240+ }
241+
201242
202243void start_trace (char * data_dir_path){
203244 char trace_file_path[PATH_MAX];
@@ -214,6 +255,12 @@ void start_trace(char* data_dir_path){
214255 return ;
215256 }
216257
258+ if (access (Fix_Module_Path, F_OK) != 0 ){
259+ LOGE (" Fix_Module_Path:%s not exit" ,Fix_Module_Path);
260+ dump_so ();
261+ return ;
262+ }
263+
217264 trace_init_info *base_info = (trace_init_info*)malloc (sizeof (trace_init_info));
218265 base_info->module_base = il2cpp_base;
219266 base_info->uid = getuid ();
0 commit comments