Skip to content

Commit d4dafdd

Browse files
修改dump相关函数逻辑
1 parent 1cdc11b commit d4dafdd

File tree

3 files changed

+150
-82
lines changed

3 files changed

+150
-82
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
一个用于在安卓平台上通过主动调用对unity游戏进行dump的so模块。
33

44
# 如何使用
5-
先在目标手游的/data/data/packageName/files目录下创建test.txt文件,test.txt文件中的**每一行**均为要进行dump的程序集名称如Assembly-CSharp.dll,**注意这里创建以及写入test.txt文件务必在手机设备的环境上进行,否则可能会出现bug**。在创建/data/data/packageName/files/test.txt文件并写入要进行dump的程序集名称后即可通过任何ptrace注入器将该so模块注入至要dump的unity游戏进程中,主动调用dump将自动执行。可在logcat中使用Perfare进行过滤查看结果。如dump成功会在/data/data/packageName/files目录下生成dump.cs文件。
5+
~~先在目标手游的/data/data/packageName/files目录下创建test.txt文件,test.txt文件中的**每一行**均为要进行dump的程序集名称如Assembly-CSharp.dll,**注意这里创建以及写入test.txt文件务必在手机设备的环境上进行,否则可能会出现bug**。在创建/data/data/packageName/files/test.txt文件并写入要进行dump的程序集名称后即可~~
6+
通过任意ptrace注入器将该so模块注入至要dump的unity游戏进程中,主动调用dump将自动执行。可在logcat中使用Perfare进行过滤查看结果。如dump成功会在/data/data/packageName/files目录下生成dump.cs文件。
67

7-
目标unity手游的所有程序集名称均可在其apk包的assets\bin\Data\ScriptingAssemblies.json中找到。推荐使用的ptrace注入器为[Android-Ptrace-Injector](https://github.com/reveny/Android-Ptrace-Injector)
8+
~~目标unity手游的所有程序集名称均可在其apk包的assets\bin\Data\ScriptingAssemblies.json中找到。~~
9+
推荐使用的ptrace注入器为[Android-Ptrace-Injector](https://github.com/reveny/Android-Ptrace-Injector)
810

911

1012
# 如何构建

app/src/main/cpp/il2cpp_dump.cpp

Lines changed: 50 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
#undef DO_API
2525

26-
#define MAX_DLL_NUM 200
27-
#define MAX_DLL_NAME_LEN 200
2826

2927
static uint64_t il2cpp_base = 0;
3028

@@ -145,8 +143,14 @@ std::string dump_method(Il2CppClass *klass) {
145143
}
146144
}
147145
auto parameter_class = il2cpp_class_from_type(param);
148-
outPut << il2cpp_class_get_name(parameter_class) << " "
149-
<< il2cpp_method_get_param_name(method, i);
146+
outPut << il2cpp_class_get_name(parameter_class)<< " ";
147+
auto param_name = il2cpp_method_get_param_name(method, i);
148+
if(param_name){
149+
outPut << param_name;
150+
} else{
151+
outPut << "null_param_name";
152+
}
153+
150154
outPut << ", ";
151155
}
152156
if (param_count > 0) {
@@ -249,38 +253,38 @@ std::string dump_field(Il2CppClass *klass) {
249253
return outPut.str();
250254
}
251255

252-
std::string dump_type(const Il2CppType *type) {
256+
std::string dump_type_info(Il2CppMetadataType type_info) {
253257
std::stringstream outPut;
254-
auto *klass = il2cpp_class_from_type(type);
255-
outPut << "\n// Namespace: " << il2cpp_class_get_namespace(klass) << "\n";
256-
auto flags = il2cpp_class_get_flags(klass);
258+
259+
auto flags = type_info.flags;
257260
if (flags & TYPE_ATTRIBUTE_SERIALIZABLE) {
258261
outPut << "[Serializable]\n";
259262
}
260-
//TODO attribute
263+
auto klass = reinterpret_cast<Il2CppClass*>(type_info.typeInfoAddress);
264+
// //TODO attribute
261265
auto is_valuetype = il2cpp_class_is_valuetype(klass);
262266
auto is_enum = il2cpp_class_is_enum(klass);
263-
auto visibility = flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
264-
switch (visibility) {
265-
case TYPE_ATTRIBUTE_PUBLIC:
266-
case TYPE_ATTRIBUTE_NESTED_PUBLIC:
267-
outPut << "public ";
268-
break;
269-
case TYPE_ATTRIBUTE_NOT_PUBLIC:
270-
case TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM:
271-
case TYPE_ATTRIBUTE_NESTED_ASSEMBLY:
272-
outPut << "internal ";
273-
break;
274-
case TYPE_ATTRIBUTE_NESTED_PRIVATE:
275-
outPut << "private ";
276-
break;
277-
case TYPE_ATTRIBUTE_NESTED_FAMILY:
278-
outPut << "protected ";
279-
break;
280-
case TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM:
281-
outPut << "protected internal ";
282-
break;
283-
}
267+
// auto visibility = flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
268+
// switch (visibility) {
269+
// case TYPE_ATTRIBUTE_PUBLIC:
270+
// case TYPE_ATTRIBUTE_NESTED_PUBLIC:
271+
// outPut << "public ";
272+
// break;
273+
// case TYPE_ATTRIBUTE_NOT_PUBLIC:
274+
// case TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM:
275+
// case TYPE_ATTRIBUTE_NESTED_ASSEMBLY:
276+
// outPut << "internal ";
277+
// break;
278+
// case TYPE_ATTRIBUTE_NESTED_PRIVATE:
279+
// outPut << "private ";
280+
// break;
281+
// case TYPE_ATTRIBUTE_NESTED_FAMILY:
282+
// outPut << "protected ";
283+
// break;
284+
// case TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM:
285+
// outPut << "protected internal ";
286+
// break;
287+
// }
284288
if (flags & TYPE_ATTRIBUTE_ABSTRACT && flags & TYPE_ATTRIBUTE_SEALED) {
285289
outPut << "static ";
286290
} else if (!(flags & TYPE_ATTRIBUTE_INTERFACE) && flags & TYPE_ATTRIBUTE_ABSTRACT) {
@@ -297,7 +301,7 @@ std::string dump_type(const Il2CppType *type) {
297301
} else {
298302
outPut << "class ";
299303
}
300-
outPut << il2cpp_class_get_name(klass); //TODO genericContainerIndex
304+
outPut << type_info.name; //TODO genericContainerIndex
301305
std::vector<std::string> extends;
302306
auto parent = il2cpp_class_get_parent(klass);
303307
if (!is_valuetype && !is_enum && parent) {
@@ -321,7 +325,7 @@ std::string dump_type(const Il2CppType *type) {
321325
outPut << dump_property(klass);
322326
outPut << dump_method(klass);
323327
//TODO EventInfo
324-
outPut << "}\n";
328+
outPut << "}\n\n";
325329
return outPut.str();
326330
}
327331

@@ -348,60 +352,26 @@ void il2cpp_api_init(void *handle) {
348352

349353
void il2cpp_dump(const char *outDir) {
350354
LOGI("dumping...");
351-
char assembly_names[MAX_DLL_NUM][MAX_DLL_NAME_LEN];
352-
size_t size=0;
353-
auto testPath = std::string(outDir).append("/files/test.txt");
354-
std::ifstream test_file(testPath);
355-
if (!test_file.is_open()) {
356-
LOGE("Failed to open test_file: %s", testPath.c_str());
357-
return;
358-
}
355+
std::stringstream imageOutput;
356+
std::vector<std::string> outPuts;
359357

360-
std::string line;
361-
while (std::getline(test_file, line)) {
362-
if(size==MAX_DLL_NUM){
363-
break;
358+
if (il2cpp_capture_memory_snapshot && il2cpp_free_captured_memory_snapshot) {
359+
auto memorySnapshot = il2cpp_capture_memory_snapshot();
360+
auto all_type_infos_count = memorySnapshot->metadata.typeCount;
361+
auto all_type_infos = memorySnapshot->metadata.types;
362+
LOGD("all_typeCount:%d",all_type_infos_count);
363+
for (int k = 0; k < all_type_infos_count; ++k) {
364+
auto tmp_type_info = all_type_infos[k];
365+
auto outPut = std::string(tmp_type_info.assemblyName)+".dll\n"+ dump_type_info(tmp_type_info);
366+
outPuts.push_back(outPut);
364367
}
365-
strcpy(assembly_names[size],line.c_str());
366-
size++;
368+
il2cpp_free_captured_memory_snapshot(memorySnapshot);
369+
} else {
370+
LOGE("can not find il2cpp_capture_memory_snapshot!!!");
367371
}
368372

369-
test_file.close();
370373

371-
std::stringstream imageOutput;
372374

373-
std::vector<std::string> outPuts;
374-
if (il2cpp_image_get_class) {
375-
LOGI("Version greater than 2018.3");
376-
//使用il2cpp_image_get_class
377-
for (int i = 0; i < size; ++i) {
378-
const char *tassembly_name = assembly_names[i];
379-
LOGD("dumping:%s\n",tassembly_name);
380-
auto tassembly = il2cpp_domain_assembly_open(
381-
NULL, tassembly_name);
382-
if(!tassembly){
383-
LOGE("erro assembly name:%s\n",tassembly_name);
384-
continue;
385-
}
386-
LOGD("test:il2cpp_assembly_get_image\n");
387-
auto image = il2cpp_assembly_get_image(tassembly);
388-
LOGD("test:il2cpp_image_get_name\n");
389-
imageOutput << "// Image " << i << ": " << il2cpp_image_get_name(image) << "\n";
390-
std::stringstream imageStr;
391-
imageStr << "\n// Dll : " << il2cpp_image_get_name(image);
392-
LOGD("test:il2cpp_image_get_class_count\n");
393-
auto classCount = il2cpp_image_get_class_count(image);
394-
for (int j = 0; j < classCount; ++j) {
395-
auto klass = il2cpp_image_get_class(image, j);
396-
auto type = il2cpp_class_get_type(const_cast<Il2CppClass *>(klass));
397-
//LOGD("type name : %s", il2cpp_type_get_name(type));
398-
auto outPut = imageStr.str() + dump_type(type);
399-
outPuts.push_back(outPut);
400-
}
401-
}
402-
} else {
403-
LOGI("Version less than 2018.3");
404-
}
405375
LOGI("write dump file");
406376
auto outPath = std::string(outDir).append("/files/dump.cs");
407377
std::ofstream outStream(outPath);

app/src/main/cpp/il2cpp_dump.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,102 @@
55
#ifndef ZYGISK_IL2CPPDUMPER_IL2CPP_DUMP_H
66
#define ZYGISK_IL2CPPDUMPER_IL2CPP_DUMP_H
77

8+
9+
#include <stdint.h>
10+
11+
struct Il2CppMetadataField
12+
{
13+
uint32_t offset;
14+
uint32_t typeIndex;
15+
const char* name;
16+
bool isStatic;
17+
};
18+
19+
enum Il2CppMetadataTypeFlags
20+
{
21+
kNone = 0,
22+
kValueType = 1 << 0,
23+
kArray = 1 << 1,
24+
kArrayRankMask = 0xFFFF0000
25+
};
26+
27+
struct Il2CppMetadataType
28+
{
29+
Il2CppMetadataTypeFlags flags; // If it's an array, rank is encoded in the upper 2 bytes
30+
Il2CppMetadataField* fields;
31+
uint32_t fieldCount;
32+
uint32_t staticsSize;
33+
uint8_t* statics;
34+
uint32_t baseOrElementTypeIndex;
35+
char* name;
36+
const char* assemblyName;
37+
uint64_t typeInfoAddress;
38+
uint32_t size;
39+
};
40+
41+
struct Il2CppMetadataSnapshot
42+
{
43+
uint32_t typeCount;
44+
Il2CppMetadataType* types;
45+
};
46+
47+
struct Il2CppManagedMemorySection
48+
{
49+
uint64_t sectionStartAddress;
50+
uint32_t sectionSize;
51+
uint8_t* sectionBytes;
52+
};
53+
54+
struct Il2CppManagedHeap
55+
{
56+
uint32_t sectionCount;
57+
Il2CppManagedMemorySection* sections;
58+
};
59+
60+
struct Il2CppStacks
61+
{
62+
uint32_t stackCount;
63+
Il2CppManagedMemorySection* stacks;
64+
};
65+
66+
struct NativeObject
67+
{
68+
uint32_t gcHandleIndex;
69+
uint32_t size;
70+
uint32_t instanceId;
71+
uint32_t classId;
72+
uint32_t referencedNativeObjectIndicesCount;
73+
uint32_t* referencedNativeObjectIndices;
74+
};
75+
76+
struct Il2CppGCHandles
77+
{
78+
uint32_t trackedObjectCount;
79+
uint64_t* pointersToObjects;
80+
};
81+
82+
struct Il2CppRuntimeInformation
83+
{
84+
uint32_t pointerSize;
85+
uint32_t objectHeaderSize;
86+
uint32_t arrayHeaderSize;
87+
uint32_t arrayBoundsOffsetInHeader;
88+
uint32_t arraySizeOffsetInHeader;
89+
uint32_t allocationGranularity;
90+
};
91+
92+
struct Il2CppManagedMemorySnapshot
93+
{
94+
Il2CppManagedHeap heap;
95+
Il2CppStacks stacks;
96+
Il2CppMetadataSnapshot metadata;
97+
Il2CppGCHandles gcHandles;
98+
Il2CppRuntimeInformation runtimeInformation;
99+
void* additionalUserInformation;
100+
};
101+
102+
103+
8104
void il2cpp_api_init(void *handle);
9105

10106
void il2cpp_dump(const char *outDir);

0 commit comments

Comments
 (0)