il2cpp-api.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. #include "il2cpp-api.h"
  2. #include "il2cpp-object-internals.h"
  3. #include "il2cpp-runtime-stats.h"
  4. #include "gc/WriteBarrier.h"
  5. #include "os/StackTrace.h"
  6. #include "os/Image.h"
  7. #include "vm/AndroidRuntime.h"
  8. #include "vm/Array.h"
  9. #include "vm/Assembly.h"
  10. #include "vm/Class.h"
  11. #include "vm/Domain.h"
  12. #include "vm/Exception.h"
  13. #include "vm/Field.h"
  14. #include "vm/Image.h"
  15. #include "vm/InternalCalls.h"
  16. #include "vm/Liveness.h"
  17. #include "vm/MemoryInformation.h"
  18. #include "vm/Method.h"
  19. #include "vm/Monitor.h"
  20. #include "vm/Object.h"
  21. #include "vm/Path.h"
  22. #include "vm/PlatformInvoke.h"
  23. #include "vm/Profiler.h"
  24. #include "vm/Property.h"
  25. #include "vm/Reflection.h"
  26. #include "vm/Runtime.h"
  27. #include "vm/StackTrace.h"
  28. #include "vm/String.h"
  29. #include "vm/Thread.h"
  30. #include "vm/Type.h"
  31. #include "utils/Exception.h"
  32. #include "utils/Logging.h"
  33. #include "utils/Memory.h"
  34. #include "utils/StringUtils.h"
  35. #include "utils/Runtime.h"
  36. #include "utils/Environment.h"
  37. #include "vm-utils/Debugger.h"
  38. #include "vm-utils/NativeSymbol.h"
  39. #include "vm/MetadataAlloc.h"
  40. #include "utils/MemoryPool.h"
  41. #include "gc/GarbageCollector.h"
  42. #include "gc/GCHandle.h"
  43. #include "gc/WriteBarrierValidation.h"
  44. #include <locale.h>
  45. #include <fstream>
  46. #include <string>
  47. #if IL2CPP_TARGET_JAVASCRIPT
  48. #include <iostream> //for cout
  49. #endif
  50. using namespace il2cpp::vm;
  51. using il2cpp::utils::Memory;
  52. using namespace il2cpp::gc;
  53. #if IL2CPP_API_DYNAMIC_NO_DLSYM
  54. #include <map>
  55. struct SymbolCompare : public std::binary_function<char*, char*, bool>
  56. {
  57. bool operator()(const char* lhs, const char* rhs) const
  58. {
  59. return strcmp(lhs, rhs) < 0;
  60. }
  61. };
  62. typedef std::map<const char*, void*, SymbolCompare> SymbolTable;
  63. static SymbolTable s_SymbolTable;
  64. static void RegisterAPIFunction(const char* name, void* symbol)
  65. {
  66. s_SymbolTable.insert(std::make_pair(name, symbol));
  67. }
  68. void il2cpp_api_register_symbols(void)
  69. {
  70. #define DO_API(r, n, p) RegisterAPIFunction(#n, (void*)n);
  71. #define DO_API_NO_RETURN(r, n, p) DO_API(r, n, p)
  72. #include "il2cpp-api-functions.h"
  73. #undef DO_API
  74. #undef DO_API_NO_RETURN
  75. }
  76. void* il2cpp_api_lookup_symbol(const char* name)
  77. {
  78. SymbolTable::iterator it = s_SymbolTable.find(name);
  79. if (it != s_SymbolTable.end())
  80. {
  81. return it->second;
  82. }
  83. return NULL;
  84. }
  85. #endif // IL2CPP_API_DYNAMIC_NO_DLSYM
  86. int il2cpp_init(const char* domain_name)
  87. {
  88. // Use environment's default locale
  89. setlocale(LC_ALL, "");
  90. return Runtime::Init(domain_name);
  91. }
  92. int il2cpp_init_utf16(const Il2CppChar* domain_name)
  93. {
  94. return il2cpp_init(il2cpp::utils::StringUtils::Utf16ToUtf8(domain_name).c_str());
  95. }
  96. void il2cpp_shutdown()
  97. {
  98. Runtime::Shutdown();
  99. }
  100. void il2cpp_set_config_dir(const char *config_path)
  101. {
  102. il2cpp::vm::Runtime::SetConfigDir(config_path);
  103. }
  104. void il2cpp_set_data_dir(const char *data_path)
  105. {
  106. il2cpp::utils::Runtime::SetDataDir(data_path);
  107. }
  108. void il2cpp_set_temp_dir(const char *temp_dir)
  109. {
  110. il2cpp::vm::Path::SetTempPath(temp_dir);
  111. }
  112. void il2cpp_set_commandline_arguments(int argc, const char* const argv[], const char* basedir)
  113. {
  114. il2cpp::utils::Environment::SetMainArgs(argv, argc);
  115. }
  116. void il2cpp_set_commandline_arguments_utf16(int argc, const Il2CppChar* const argv[], const char* basedir)
  117. {
  118. il2cpp::utils::Environment::SetMainArgs(argv, argc);
  119. }
  120. void il2cpp_set_config_utf16(const Il2CppChar* executablePath)
  121. {
  122. il2cpp::vm::Runtime::SetConfigUtf16(executablePath);
  123. }
  124. void il2cpp_set_config(const char* executablePath)
  125. {
  126. il2cpp::vm::Runtime::SetConfig(executablePath);
  127. }
  128. void il2cpp_set_memory_callbacks(Il2CppMemoryCallbacks* callbacks)
  129. {
  130. Memory::SetMemoryCallbacks(callbacks);
  131. }
  132. const Il2CppImage* il2cpp_get_corlib()
  133. {
  134. return Image::GetCorlib();
  135. }
  136. void il2cpp_add_internal_call(const char* name, Il2CppMethodPointer method)
  137. {
  138. return InternalCalls::Add(name, method);
  139. }
  140. Il2CppMethodPointer il2cpp_resolve_icall(const char* name)
  141. {
  142. return InternalCalls::Resolve(name);
  143. }
  144. void* il2cpp_alloc(size_t size)
  145. {
  146. return IL2CPP_MALLOC(size, IL2CPP_MEM_il2cpp_alloc);
  147. }
  148. void il2cpp_free(void* ptr)
  149. {
  150. IL2CPP_FREE(ptr, IL2CPP_MEM_il2cpp_alloc);
  151. }
  152. // array
  153. Il2CppClass *il2cpp_array_class_get(Il2CppClass *element_class, uint32_t rank)
  154. {
  155. return Class::GetArrayClass(element_class, rank);
  156. }
  157. uint32_t il2cpp_array_length(Il2CppArray* array)
  158. {
  159. return Array::GetLength(array);
  160. }
  161. uint32_t il2cpp_array_get_byte_length(Il2CppArray *array)
  162. {
  163. return Array::GetByteLength(array);
  164. }
  165. Il2CppArray* il2cpp_array_new(Il2CppClass *elementTypeInfo, il2cpp_array_size_t length)
  166. {
  167. return Array::New(elementTypeInfo, length);
  168. }
  169. Il2CppArray* il2cpp_array_new_specific(Il2CppClass *arrayTypeInfo, il2cpp_array_size_t length)
  170. {
  171. return Array::NewSpecific(arrayTypeInfo, length);
  172. }
  173. Il2CppArray* il2cpp_array_new_full(Il2CppClass *array_class, il2cpp_array_size_t *lengths, il2cpp_array_size_t *lower_bounds)
  174. {
  175. return Array::NewFull(array_class, lengths, lower_bounds);
  176. }
  177. Il2CppClass* il2cpp_bounded_array_class_get(Il2CppClass *element_class, uint32_t rank, bool bounded)
  178. {
  179. return Class::GetBoundedArrayClass(element_class, rank, bounded);
  180. }
  181. int il2cpp_array_element_size(const Il2CppClass* klass)
  182. {
  183. return Array::GetElementSize(klass);
  184. }
  185. // assembly
  186. const Il2CppImage* il2cpp_assembly_get_image(const Il2CppAssembly *assembly)
  187. {
  188. return Assembly::GetImage(assembly);
  189. }
  190. // class
  191. const Il2CppType* il2cpp_class_enum_basetype(Il2CppClass *klass)
  192. {
  193. return Class::GetEnumBaseType(klass);
  194. }
  195. Il2CppClass* il2cpp_class_from_system_type(Il2CppReflectionType *type)
  196. {
  197. return Class::FromSystemType(type);
  198. }
  199. bool il2cpp_class_is_inited(const Il2CppClass *klass)
  200. {
  201. return klass->initialized;
  202. }
  203. bool il2cpp_class_is_generic(const Il2CppClass *klass)
  204. {
  205. return Class::IsGeneric(klass);
  206. }
  207. bool il2cpp_class_is_inflated(const Il2CppClass *klass)
  208. {
  209. return Class::IsInflated(klass);
  210. }
  211. bool il2cpp_class_is_assignable_from(Il2CppClass *klass, Il2CppClass *oklass)
  212. {
  213. return Class::IsAssignableFrom(klass, oklass);
  214. }
  215. bool il2cpp_class_is_subclass_of(Il2CppClass *klass, Il2CppClass *klassc, bool check_interfaces)
  216. {
  217. return Class::IsSubclassOf(klass, klassc, check_interfaces);
  218. }
  219. bool il2cpp_class_has_parent(Il2CppClass *klass, Il2CppClass *klassc)
  220. {
  221. return Class::HasParent(klass, klassc);
  222. }
  223. Il2CppClass* il2cpp_class_from_il2cpp_type(const Il2CppType* type)
  224. {
  225. return Class::FromIl2CppType(type);
  226. }
  227. Il2CppClass* il2cpp_class_from_name(const Il2CppImage* image, const char* namespaze, const char *name)
  228. {
  229. return Class::FromName(image, namespaze, name);
  230. }
  231. Il2CppClass* il2cpp_class_get_element_class(Il2CppClass *klass)
  232. {
  233. return Class::GetElementClass(klass);
  234. }
  235. const EventInfo* il2cpp_class_get_events(Il2CppClass *klass, void* *iter)
  236. {
  237. return Class::GetEvents(klass, iter);
  238. }
  239. FieldInfo* il2cpp_class_get_fields(Il2CppClass *klass, void* *iter)
  240. {
  241. return Class::GetFields(klass, iter);
  242. }
  243. Il2CppClass* il2cpp_class_get_nested_types(Il2CppClass *klass, void* *iter)
  244. {
  245. return Class::GetNestedTypes(klass, iter);
  246. }
  247. Il2CppClass* il2cpp_class_get_interfaces(Il2CppClass *klass, void* *iter)
  248. {
  249. return Class::GetInterfaces(klass, iter);
  250. }
  251. const PropertyInfo* il2cpp_class_get_properties(Il2CppClass *klass, void* *iter)
  252. {
  253. return Class::GetProperties(klass, iter);
  254. }
  255. const PropertyInfo* il2cpp_class_get_property_from_name(Il2CppClass *klass, const char *name)
  256. {
  257. return Class::GetPropertyFromName(klass, name);
  258. }
  259. FieldInfo* il2cpp_class_get_field_from_name(Il2CppClass* klass, const char *name)
  260. {
  261. return Class::GetFieldFromName(klass, name);
  262. }
  263. const MethodInfo* il2cpp_class_get_methods(Il2CppClass *klass, void* *iter)
  264. {
  265. return Class::GetMethods(klass, iter);
  266. }
  267. const MethodInfo* il2cpp_class_get_method_from_name(Il2CppClass *klass, const char* name, int argsCount)
  268. {
  269. return Class::GetMethodFromName(klass, name, argsCount);
  270. }
  271. const char* il2cpp_class_get_name(Il2CppClass *klass)
  272. {
  273. return Class::GetName(klass);
  274. }
  275. const char* il2cpp_class_get_namespace(Il2CppClass *klass)
  276. {
  277. return Class::GetNamespace(klass);
  278. }
  279. Il2CppClass* il2cpp_class_get_parent(Il2CppClass *klass)
  280. {
  281. return Class::GetParent(klass);
  282. }
  283. Il2CppClass* il2cpp_class_get_declaring_type(Il2CppClass* klass)
  284. {
  285. return Class::GetDeclaringType(klass);
  286. }
  287. int32_t il2cpp_class_instance_size(Il2CppClass *klass)
  288. {
  289. return Class::GetInstanceSize(klass);
  290. }
  291. size_t il2cpp_class_num_fields(const Il2CppClass* klass)
  292. {
  293. return Class::GetNumFields(klass);
  294. }
  295. bool il2cpp_class_is_valuetype(const Il2CppClass* klass)
  296. {
  297. return Class::IsValuetype(klass);
  298. }
  299. bool il2cpp_class_is_blittable(const Il2CppClass* klass)
  300. {
  301. return Class::IsBlittable(klass);
  302. }
  303. int32_t il2cpp_class_value_size(Il2CppClass *klass, uint32_t *align)
  304. {
  305. return Class::GetValueSize(klass, align);
  306. }
  307. int il2cpp_class_get_flags(const Il2CppClass *klass)
  308. {
  309. return Class::GetFlags(klass);
  310. }
  311. bool il2cpp_class_is_abstract(const Il2CppClass *klass)
  312. {
  313. return Class::IsAbstract(klass);
  314. }
  315. bool il2cpp_class_is_interface(const Il2CppClass *klass)
  316. {
  317. return Class::IsInterface(klass);
  318. }
  319. int il2cpp_class_array_element_size(const Il2CppClass *klass)
  320. {
  321. return Class::GetArrayElementSize(klass);
  322. }
  323. Il2CppClass* il2cpp_class_from_type(const Il2CppType *type)
  324. {
  325. return Class::FromIl2CppType(type);
  326. }
  327. const Il2CppType* il2cpp_class_get_type(Il2CppClass *klass)
  328. {
  329. return Class::GetType(klass);
  330. }
  331. uint32_t il2cpp_class_get_type_token(Il2CppClass *klass)
  332. {
  333. return klass->token;
  334. }
  335. bool il2cpp_class_has_attribute(Il2CppClass *klass, Il2CppClass *attr_class)
  336. {
  337. return Class::HasAttribute(klass, attr_class);
  338. }
  339. bool il2cpp_class_has_references(Il2CppClass *klass)
  340. {
  341. return Class::HasReferences(klass);
  342. }
  343. bool il2cpp_class_is_enum(const Il2CppClass *klass)
  344. {
  345. return Class::IsEnum(klass);
  346. }
  347. const Il2CppImage* il2cpp_class_get_image(Il2CppClass* klass)
  348. {
  349. return Class::GetImage(klass);
  350. }
  351. const char *il2cpp_class_get_assemblyname(const Il2CppClass *klass)
  352. {
  353. return Class::GetAssemblyNameNoExtension(klass);
  354. }
  355. int il2cpp_class_get_rank(const Il2CppClass *klass)
  356. {
  357. return klass->rank;
  358. }
  359. uint32_t il2cpp_class_get_data_size(const Il2CppClass *klass)
  360. {
  361. return klass->static_fields_size;
  362. }
  363. void* il2cpp_class_get_static_field_data(const Il2CppClass *klass)
  364. {
  365. return klass->static_fields;
  366. }
  367. // testing only
  368. size_t il2cpp_class_get_bitmap_size(const Il2CppClass *klass)
  369. {
  370. return Class::GetBitmapSize(klass);
  371. }
  372. void il2cpp_class_get_bitmap(Il2CppClass *klass, size_t* bitmap)
  373. {
  374. size_t dummy = 0;
  375. Class::GetBitmap(klass, bitmap, dummy);
  376. }
  377. // stats
  378. extern Il2CppRuntimeStats il2cpp_runtime_stats;
  379. bool il2cpp_stats_dump_to_file(const char *path)
  380. {
  381. #if IL2CPP_TARGET_JAVASCRIPT
  382. std::ostream& fs = std::cout;
  383. #else
  384. std::fstream fs;
  385. fs.open(path, std::fstream::out | std::fstream::trunc);
  386. #endif
  387. fs << "=================Object statistics=================\n";
  388. fs << "New object count: " << il2cpp_stats_get_value(IL2CPP_STAT_NEW_OBJECT_COUNT) << "\n";
  389. fs << "Method count: " << il2cpp_stats_get_value(IL2CPP_STAT_METHOD_COUNT) << "\n";
  390. fs << "Class static data size: " << il2cpp_stats_get_value(IL2CPP_STAT_CLASS_STATIC_DATA_SIZE) << "\n";
  391. fs << "Inflated method count: " << il2cpp_stats_get_value(IL2CPP_STAT_INFLATED_METHOD_COUNT) << "\n";
  392. fs << "Inflated type count: " << il2cpp_stats_get_value(IL2CPP_STAT_INFLATED_TYPE_COUNT) << "\n";
  393. fs << "Initialized class count: " << il2cpp_stats_get_value(IL2CPP_STAT_INITIALIZED_CLASS_COUNT) << "\n";
  394. fs << "Generic instance count: " << il2cpp_stats_get_value(IL2CPP_STAT_GENERIC_INSTANCE_COUNT) << "\n";
  395. fs << "Generic class count: " << il2cpp_stats_get_value(IL2CPP_STAT_GENERIC_CLASS_COUNT) << "\n";
  396. fs << "metadata mem: " << il2cpp_stats_get_value(IL2CPP_STAT_META_MEM) << "\n";
  397. fs << "hash table mem: " << il2cpp_stats_get_value(IL2CPP_STAT_HASHTABLE_MEM) << "\n";
  398. fs << "global-metadata.dat size:" << il2cpp_stats_get_value(IL2CPP_STAT_GLOBAL_METADATA_FILE_SIZE) << "\n";
  399. #if IL2CPP_ENABLE_MEM_STATS
  400. size_t realUsage = il2cpp_mem_stats.meta.meta_total - il2cpp_mem_stats.meta.meta_wasted - mem_stats_get_metadata_free();
  401. realUsage -= il2cpp::utils::MemoryPool::RegionSize() * il2cpp_mem_stats.meta.meta_region_count;
  402. size_t total = il2cpp_mem_stats.il2cpp_malloc;
  403. #if !IL2CPP_TARGET_JAVASCRIPT
  404. total += il2cpp_mem_stats.hash_table + il2cpp_mem_stats.globalMetadataMapfile;
  405. #endif
  406. fs.precision(2);//set float precision
  407. fs << "\n";
  408. fs << "=================Memory Usage statistics=================\n";
  409. fs << "Total usage " << total << "\n";
  410. fs << "\n";
  411. fs << "1: Metadata usage " << il2cpp_mem_stats.meta.meta_total << " (" << double(il2cpp_mem_stats.meta.meta_total) / double(total) << ")\n";
  412. fs << " Real usage " << realUsage << " , wasted " << il2cpp_mem_stats.meta.meta_wasted << " (rate: " << double(il2cpp_mem_stats.meta.meta_wasted) / double(il2cpp_mem_stats.meta.meta_total) << ")\n";
  413. fs << "\n";
  414. fs << "Metadata Type usage " << il2cpp_mem_stats.meta.sizes[IL2CPP_MSTAT_TYPE] << "\n";
  415. fs << "Metadata Class usage " << il2cpp_mem_stats.meta.sizes[IL2CPP_MSTAT_CLASS] << "\n";
  416. fs << "Metadata Method usage " << il2cpp_mem_stats.meta.sizes[IL2CPP_MSTAT_METHOD] << "\n";
  417. fs << "Metadata Field usage " << il2cpp_mem_stats.meta.sizes[IL2CPP_MSTAT_FIELD] << "\n";
  418. fs << "Metadata Event usage " << il2cpp_mem_stats.meta.sizes[IL2CPP_MSTAT_EVENT] << "\n";
  419. fs << "Metadata Property usage " << il2cpp_mem_stats.meta.sizes[IL2CPP_MSTAT_PROPERTY] << "\n";
  420. fs << "Metadata GenericInst usage " << il2cpp_mem_stats.meta.sizes[IL2CPP_MSTAT_GENERIC_INST] << "\n";
  421. fs << "Metadata Interface usage " << il2cpp_mem_stats.meta.sizes[IL2CPP_MSTAT_INTERFACE] << "\n";
  422. fs << "Metadata RGCTX usage " << il2cpp_mem_stats.meta.sizes[IL2CPP_MSTAT_RGCTX] << "\n";
  423. fs << "Metadata GenericClass usage " << il2cpp_mem_stats.meta.generic_class_size << " count " << il2cpp_mem_stats.meta.generic_class_count << "\n";
  424. fs << "Metadata GenericMethod usage " << il2cpp_mem_stats.meta.generic_method_size << " count " << il2cpp_mem_stats.meta.generic_method_count << "\n";
  425. size_t sum = il2cpp_mem_stats.meta.generic_class_size;
  426. sum += il2cpp_mem_stats.meta.generic_method_size;
  427. for (int i = 0; i < IL2CPP_MSTAT_COUNT; ++i) {
  428. sum += il2cpp_mem_stats.meta.sizes[i];
  429. }
  430. fs << "Sum: " << sum << "\n";
  431. fs << "\n";
  432. #if IL2CPP_USE_SPARSEHASH
  433. fs << "2: HashTable(sparse) usage ";
  434. #else
  435. fs << "2: HashTable(dense) usage ";
  436. #endif
  437. fs << il2cpp_mem_stats.hash_table << " (" << double(il2cpp_mem_stats.hash_table) / double(total) << ")\n";
  438. size_t other = total - il2cpp_mem_stats.meta.meta_total - il2cpp_mem_stats.hash_table - il2cpp_mem_stats.globalMetadataMapfile;
  439. fs << "\n";
  440. fs << "3: Mapped global-metadata.dat size: " << il2cpp_mem_stats.globalMetadataMapfile << " (" << double(il2cpp_mem_stats.globalMetadataMapfile) / double(total) << ")\n";
  441. fs << "\n";
  442. fs << "4: Other usage " << other << " (" << double(other) / double(total) <<")\n";
  443. fs << "\n";
  444. sum = 0;
  445. for (int i = 0; i < IL2CPP_MEM_LABLE_COUNT; ++i) {
  446. if (i == IL2CPP_MEM_META_POOL
  447. || i == IL2CPP_MEM_HASH_TABLE
  448. || i == IL2CPP_MEM_MEMORY_MAP)
  449. continue;
  450. sum += il2cpp_mem_stats.lableSizes[i];
  451. if(i == IL2CPP_MEM_FromTypeDefinition)
  452. fs << g_Il2CppMemLabelName[i] <<" usage " << il2cpp_mem_stats.lableSizes[i] << " count " << il2cpp_mem_stats.classFromTypeDef_count << "\n";
  453. else
  454. fs << g_Il2CppMemLabelName[i] <<" usage " << il2cpp_mem_stats.lableSizes[i] << "\n";
  455. }
  456. fs << "Sum: " << sum << "\n";
  457. fs << "\n";
  458. fs << "Uncounted:\n";
  459. fs << "InternalCalls total: " << il2cpp_mem_stats.interal_calls_total << "\n";
  460. fs << "InternalCalls resolved: " << il2cpp_mem_stats.interal_calls_resolved << "\n";
  461. fs << "\n";
  462. fs << "sizeof(Il2CppType) = " << sizeof(Il2CppType) << "\n";
  463. fs << "sizeof(Il2CppClass) = " << sizeof(Il2CppClass) << "\n";
  464. fs << "sizeof(MethodInfo) = " << sizeof(MethodInfo) << "\n";
  465. fs << "sizeof(FieldInfo) = " << sizeof(FieldInfo) << "\n";
  466. fs << "sizeof(EventInfo) = " << sizeof(EventInfo) << "\n";
  467. fs << "sizeof(PropertyInfo) = " << sizeof(PropertyInfo) << "\n";
  468. fs << "sizeof(Il2CppGenericInst) = " << sizeof(Il2CppGenericInst) << "\n";
  469. fs << "sizeof(Il2CppRGCTXData) = " << sizeof(Il2CppRGCTXData) << "\n";
  470. fs << "sizeof(Il2CppGenericClass) = " << sizeof(Il2CppGenericClass) << "\n";
  471. fs << "sizeof(Il2CppGenericMethod) = " << sizeof(Il2CppGenericMethod) << "\n";
  472. fs << "\n";
  473. #endif //IL2CPP_ENABLE_MEM_STATS
  474. #if !IL2CPP_TARGET_JAVASCRIPT
  475. fs.close();
  476. #endif
  477. return true;
  478. }
  479. uint64_t il2cpp_stats_get_value(Il2CppStat stat)
  480. {
  481. switch (stat)
  482. {
  483. case IL2CPP_STAT_NEW_OBJECT_COUNT:
  484. return il2cpp_runtime_stats.new_object_count;
  485. case IL2CPP_STAT_INITIALIZED_CLASS_COUNT:
  486. return il2cpp_runtime_stats.initialized_class_count;
  487. /*case IL2CPP_STAT_GENERIC_VTABLE_COUNT:
  488. return il2cpp_runtime_stats.generic_vtable_count;
  489. case IL2CPP_STAT_USED_CLASS_COUNT:
  490. return il2cpp_runtime_stats.used_class_count;*/
  491. case IL2CPP_STAT_METHOD_COUNT:
  492. return il2cpp_runtime_stats.method_count;
  493. /*case IL2CPP_STAT_CLASS_VTABLE_SIZE:
  494. return il2cpp_runtime_stats.class_vtable_size;*/
  495. case IL2CPP_STAT_CLASS_STATIC_DATA_SIZE:
  496. return il2cpp_runtime_stats.class_static_data_size;
  497. case IL2CPP_STAT_GENERIC_INSTANCE_COUNT:
  498. return il2cpp_runtime_stats.generic_instance_count;
  499. case IL2CPP_STAT_GENERIC_CLASS_COUNT:
  500. return il2cpp_runtime_stats.generic_class_count;
  501. case IL2CPP_STAT_INFLATED_METHOD_COUNT:
  502. return il2cpp_runtime_stats.inflated_method_count;
  503. case IL2CPP_STAT_INFLATED_TYPE_COUNT:
  504. return il2cpp_runtime_stats.inflated_type_count;
  505. /*case IL2CPP_STAT_DELEGATE_CREATIONS:
  506. return il2cpp_runtime_stats.delegate_creations;
  507. case IL2CPP_STAT_MINOR_GC_COUNT:
  508. return il2cpp_runtime_stats.minor_gc_count;
  509. case IL2CPP_STAT_MAJOR_GC_COUNT:
  510. return il2cpp_runtime_stats.major_gc_count;
  511. case IL2CPP_STAT_MINOR_GC_TIME_USECS:
  512. return il2cpp_runtime_stats.minor_gc_time_usecs;
  513. case IL2CPP_STAT_MAJOR_GC_TIME_USECS:
  514. return il2cpp_runtime_stats.major_gc_time_usecs;*/
  515. case IL2CPP_STAT_META_MEM:
  516. return MetadataTotalMemSize();
  517. case IL2CPP_STAT_HASHTABLE_MEM:
  518. return il2cpp_runtime_stats.hashtable_mem;
  519. case IL2CPP_STAT_GLOBAL_METADATA_FILE_SIZE:
  520. return il2cpp_runtime_stats.global_metadata_file_size;
  521. }
  522. return 0;
  523. }
  524. // domain
  525. Il2CppDomain* il2cpp_domain_get()
  526. {
  527. return Domain::GetCurrent();
  528. }
  529. const Il2CppAssembly* il2cpp_domain_assembly_open(Il2CppDomain *domain, const char *name)
  530. {
  531. return Assembly::Load(name);
  532. }
  533. const Il2CppAssembly** il2cpp_domain_get_assemblies(const Il2CppDomain* domain, size_t* size)
  534. {
  535. il2cpp::vm::AssemblyVector* assemblies = Assembly::GetAllAssemblies();
  536. *size = assemblies->size();
  537. return &(*assemblies)[0];
  538. }
  539. // exception
  540. void il2cpp_raise_exception(Il2CppException* exc)
  541. {
  542. Exception::Raise(exc);
  543. }
  544. Il2CppException* il2cpp_exception_from_name_msg(const Il2CppImage* image, const char *name_space, const char *name, const char *msg)
  545. {
  546. return Exception::FromNameMsg(image, name_space, name, msg);
  547. }
  548. Il2CppException* il2cpp_get_exception_argument_null(const char *arg)
  549. {
  550. return Exception::GetArgumentNullException(arg);
  551. }
  552. void il2cpp_format_exception(const Il2CppException* ex, char* message, int message_size)
  553. {
  554. strncpy(message, il2cpp::utils::Exception::FormatException(ex).c_str(), message_size);
  555. }
  556. void il2cpp_format_stack_trace(const Il2CppException* ex, char* output, int output_size)
  557. {
  558. strncpy(output, il2cpp::utils::Exception::FormatStackTrace(ex).c_str(), output_size);
  559. }
  560. void il2cpp_unhandled_exception(Il2CppException* exc)
  561. {
  562. Runtime::UnhandledException(exc);
  563. }
  564. void il2cpp_native_stack_trace(const Il2CppException * ex, uintptr_t** addresses, int* numFrames, char** imageUUID, char** imageName)
  565. {
  566. #if IL2CPP_ENABLE_NATIVE_INSTRUCTION_POINTER_EMISSION && !IL2CPP_TINY
  567. if (ex == NULL || ex->native_trace_ips == NULL)
  568. {
  569. *numFrames = 0;
  570. *addresses = NULL;
  571. *imageUUID = NULL;
  572. *imageName = NULL;
  573. return;
  574. }
  575. *numFrames = il2cpp_array_length(ex->native_trace_ips);
  576. if (*numFrames <= 0)
  577. {
  578. *addresses = NULL;
  579. *imageUUID = NULL;
  580. *imageName = NULL;
  581. }
  582. else
  583. {
  584. *addresses = static_cast<uintptr_t*>(il2cpp_alloc((*numFrames) * sizeof(uintptr_t)));
  585. for (int i = 0; i < *numFrames; i++)
  586. {
  587. uintptr_t ptrAddr = il2cpp_array_get(ex->native_trace_ips, uintptr_t, i);
  588. (*addresses)[i] = ptrAddr;
  589. }
  590. *imageUUID = il2cpp::os::Image::GetImageUUID();
  591. *imageName = il2cpp::os::Image::GetImageName();
  592. }
  593. #endif
  594. }
  595. // field
  596. const char* il2cpp_field_get_name(FieldInfo *field)
  597. {
  598. return Field::GetName(field);
  599. }
  600. int il2cpp_field_get_flags(FieldInfo *field)
  601. {
  602. return Field::GetFlags(field);
  603. }
  604. Il2CppClass* il2cpp_field_get_parent(FieldInfo *field)
  605. {
  606. return Field::GetParent(field);
  607. }
  608. size_t il2cpp_field_get_offset(FieldInfo *field)
  609. {
  610. return Field::GetOffset(field);
  611. }
  612. const Il2CppType* il2cpp_field_get_type(FieldInfo *field)
  613. {
  614. return Field::GetType(field);
  615. }
  616. void il2cpp_field_get_value(Il2CppObject *obj, FieldInfo *field, void *value)
  617. {
  618. return Field::GetValue(obj, field, value);
  619. }
  620. Il2CppObject* il2cpp_field_get_value_object(FieldInfo *field, Il2CppObject *obj)
  621. {
  622. return Field::GetValueObject(field, obj);
  623. }
  624. bool il2cpp_field_has_attribute(FieldInfo *field, Il2CppClass *attr_class)
  625. {
  626. return Field::HasAttribute(field, attr_class);
  627. }
  628. void il2cpp_field_set_value(Il2CppObject *obj, FieldInfo *field, void *value)
  629. {
  630. Field::SetValue(obj, field, value);
  631. }
  632. void il2cpp_field_set_value_object(Il2CppObject* objectInstance, FieldInfo* field, Il2CppObject* value)
  633. {
  634. Field::SetInstanceFieldValueObject(objectInstance, field, value);
  635. }
  636. void il2cpp_field_static_get_value(FieldInfo *field, void *value)
  637. {
  638. Field::StaticGetValue(field, value);
  639. }
  640. void il2cpp_field_static_set_value(FieldInfo *field, void *value)
  641. {
  642. Field::StaticSetValue(field, value);
  643. }
  644. bool il2cpp_field_is_literal(FieldInfo *field)
  645. {
  646. return (field->type->attrs & FIELD_ATTRIBUTE_LITERAL) != 0;
  647. }
  648. // gc
  649. void il2cpp_gc_collect(int maxGenerations)
  650. {
  651. GarbageCollector::Collect(maxGenerations);
  652. }
  653. int32_t il2cpp_gc_collect_a_little()
  654. {
  655. return GarbageCollector::CollectALittle();
  656. }
  657. void il2cpp_gc_start_incremental_collection()
  658. {
  659. GarbageCollector::StartIncrementalCollection();
  660. }
  661. void il2cpp_gc_enable()
  662. {
  663. GarbageCollector::Enable();
  664. }
  665. void il2cpp_gc_disable()
  666. {
  667. GarbageCollector::Disable();
  668. }
  669. bool il2cpp_gc_is_disabled()
  670. {
  671. return GarbageCollector::IsDisabled();
  672. }
  673. void il2cpp_gc_set_mode(Il2CppGCMode mode)
  674. {
  675. GarbageCollector::SetMode(mode);
  676. }
  677. bool il2cpp_gc_is_incremental()
  678. {
  679. return GarbageCollector::IsIncremental();
  680. }
  681. int64_t il2cpp_gc_get_max_time_slice_ns()
  682. {
  683. return GarbageCollector::GetMaxTimeSliceNs();
  684. }
  685. void il2cpp_gc_set_max_time_slice_ns(int64_t maxTimeSlice)
  686. {
  687. GarbageCollector::SetMaxTimeSliceNs(maxTimeSlice);
  688. }
  689. int64_t il2cpp_gc_get_used_size()
  690. {
  691. return GarbageCollector::GetUsedHeapSize();
  692. }
  693. int64_t il2cpp_gc_get_heap_size()
  694. {
  695. return GarbageCollector::GetAllocatedHeapSize();
  696. }
  697. void il2cpp_gc_foreach_heap(void(*func)(void* data, void* context), void* userData)
  698. {
  699. MemoryInformation::IterationContext ctx;
  700. ctx.callback = func;
  701. ctx.userData = userData;
  702. il2cpp::gc::GarbageCollector::ForEachHeapSection(&ctx, MemoryInformation::ReportGcHeapSection);
  703. }
  704. void il2cpp_stop_gc_world()
  705. {
  706. il2cpp::gc::GarbageCollector::StopWorld();
  707. }
  708. void il2cpp_start_gc_world()
  709. {
  710. il2cpp::gc::GarbageCollector::StartWorld();
  711. }
  712. void* il2cpp_gc_alloc_fixed(size_t size)
  713. {
  714. return il2cpp::gc::GarbageCollector::AllocateFixed(size, NULL);
  715. }
  716. void il2cpp_gc_free_fixed(void* address)
  717. {
  718. il2cpp::gc::GarbageCollector::FreeFixed(address);
  719. }
  720. // gchandle
  721. uint32_t il2cpp_gchandle_new(Il2CppObject *obj, bool pinned)
  722. {
  723. return GCHandle::New(obj, pinned);
  724. }
  725. uint32_t il2cpp_gchandle_new_weakref(Il2CppObject *obj, bool track_resurrection)
  726. {
  727. // Note that the call to Get will assert if an error occurred.
  728. return GCHandle::NewWeakref(obj, track_resurrection).Get();
  729. }
  730. Il2CppObject* il2cpp_gchandle_get_target(uint32_t gchandle)
  731. {
  732. return GCHandle::GetTarget(gchandle);
  733. }
  734. void il2cpp_gchandle_foreach_get_target(void(*func)(void*, void*), void* userData)
  735. {
  736. MemoryInformation::IterationContext ctx;
  737. ctx.callback = func;
  738. ctx.userData = userData;
  739. il2cpp::gc::GCHandle::WalkStrongGCHandleTargets(MemoryInformation::ReportGcHandleTarget, &ctx);
  740. }
  741. void il2cpp_gc_wbarrier_set_field(Il2CppObject *obj, void **targetAddress, void *object)
  742. {
  743. il2cpp::gc::WriteBarrier::GenericStore(targetAddress, object);
  744. }
  745. bool il2cpp_gc_has_strict_wbarriers()
  746. {
  747. #if IL2CPP_ENABLE_STRICT_WRITE_BARRIERS
  748. return true;
  749. #else
  750. return false;
  751. #endif
  752. }
  753. void il2cpp_gc_set_external_allocation_tracker(void(*func)(void*, size_t, int))
  754. {
  755. #if IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  756. il2cpp::gc::WriteBarrierValidation::SetExternalAllocationTracker(func);
  757. #endif
  758. }
  759. void il2cpp_gc_set_external_wbarrier_tracker(void(*func)(void**))
  760. {
  761. #if IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  762. il2cpp::gc::WriteBarrierValidation::SetExternalWriteBarrierTracker(func);
  763. #endif
  764. }
  765. void il2cpp_gchandle_free(uint32_t gchandle)
  766. {
  767. GCHandle::Free(gchandle);
  768. }
  769. // vm runtime info
  770. uint32_t il2cpp_object_header_size()
  771. {
  772. return static_cast<uint32_t>(sizeof(Il2CppObject));
  773. }
  774. uint32_t il2cpp_array_object_header_size()
  775. {
  776. return static_cast<uint32_t>(kIl2CppSizeOfArray);
  777. }
  778. uint32_t il2cpp_offset_of_array_length_in_array_object_header()
  779. {
  780. return kIl2CppOffsetOfArrayLength;
  781. }
  782. uint32_t il2cpp_offset_of_array_bounds_in_array_object_header()
  783. {
  784. return kIl2CppOffsetOfArrayBounds;
  785. }
  786. uint32_t il2cpp_allocation_granularity()
  787. {
  788. return static_cast<uint32_t>(2 * sizeof(void*));
  789. }
  790. // liveness
  791. void* il2cpp_unity_liveness_allocate_struct(Il2CppClass* filter, int max_object_count, il2cpp_register_object_callback callback, void* userdata, il2cpp_liveness_reallocate_callback reallocate)
  792. {
  793. return Liveness::AllocateStruct(filter, max_object_count, callback, userdata, reallocate);
  794. }
  795. void il2cpp_unity_liveness_calculation_from_root(Il2CppObject* root, void* state)
  796. {
  797. Liveness::FromRoot(root, state);
  798. }
  799. void il2cpp_unity_liveness_calculation_from_statics(void* state)
  800. {
  801. Liveness::FromStatics(state);
  802. }
  803. void il2cpp_unity_liveness_finalize(void* state)
  804. {
  805. Liveness::Finalize(state);
  806. }
  807. void il2cpp_unity_liveness_free_struct(void* state)
  808. {
  809. Liveness::FreeStruct(state);
  810. }
  811. // method
  812. const Il2CppType* il2cpp_method_get_return_type(const MethodInfo* method)
  813. {
  814. return Method::GetReturnType(method);
  815. }
  816. const MethodInfo* il2cpp_method_get_from_reflection(const Il2CppReflectionMethod *method)
  817. {
  818. return Reflection::GetMethod(method);
  819. }
  820. Il2CppReflectionMethod* il2cpp_method_get_object(const MethodInfo *method, Il2CppClass *refclass)
  821. {
  822. return Reflection::GetMethodObject(method, refclass);
  823. }
  824. const char* il2cpp_method_get_name(const MethodInfo *method)
  825. {
  826. return Method::GetName(method);
  827. }
  828. bool il2cpp_method_is_generic(const MethodInfo *method)
  829. {
  830. return Method::IsGeneric(method);
  831. }
  832. bool il2cpp_method_is_inflated(const MethodInfo *method)
  833. {
  834. return Method::IsInflated(method);
  835. }
  836. bool il2cpp_method_is_instance(const MethodInfo *method)
  837. {
  838. return Method::IsInstance(method);
  839. }
  840. uint32_t il2cpp_method_get_param_count(const MethodInfo *method)
  841. {
  842. return Method::GetParamCount(method);
  843. }
  844. const Il2CppType* il2cpp_method_get_param(const MethodInfo *method, uint32_t index)
  845. {
  846. return Method::GetParam(method, index);
  847. }
  848. Il2CppClass* il2cpp_method_get_class(const MethodInfo *method)
  849. {
  850. return Method::GetClass(method);
  851. }
  852. bool il2cpp_method_has_attribute(const MethodInfo *method, Il2CppClass *attr_class)
  853. {
  854. return Method::HasAttribute(method, attr_class);
  855. }
  856. Il2CppClass* il2cpp_method_get_declaring_type(const MethodInfo* method)
  857. {
  858. return Method::GetDeclaringType(method);
  859. }
  860. uint32_t il2cpp_method_get_flags(const MethodInfo *method, uint32_t *iflags)
  861. {
  862. if (iflags != 0)
  863. *iflags = Method::GetImplementationFlags(method);
  864. return Method::GetFlags(method);
  865. }
  866. uint32_t il2cpp_method_get_token(const MethodInfo *method)
  867. {
  868. return Method::GetToken(method);
  869. }
  870. const char *il2cpp_method_get_param_name(const MethodInfo *method, uint32_t index)
  871. {
  872. return Method::GetParamName(method, index);
  873. }
  874. // profiler
  875. #if IL2CPP_ENABLE_PROFILER
  876. void il2cpp_profiler_install(Il2CppProfiler *prof, Il2CppProfileFunc shutdown_callback)
  877. {
  878. Profiler::Install(prof, shutdown_callback);
  879. }
  880. void il2cpp_profiler_set_events(Il2CppProfileFlags events)
  881. {
  882. Profiler::SetEvents(events);
  883. }
  884. void il2cpp_profiler_install_enter_leave(Il2CppProfileMethodFunc enter, Il2CppProfileMethodFunc fleave)
  885. {
  886. Profiler::InstallEnterLeave(enter, fleave);
  887. }
  888. void il2cpp_profiler_install_allocation(Il2CppProfileAllocFunc callback)
  889. {
  890. Profiler::InstallAllocation(callback);
  891. }
  892. void il2cpp_profiler_install_gc(Il2CppProfileGCFunc callback, Il2CppProfileGCResizeFunc heap_resize_callback)
  893. {
  894. Profiler::InstallGC(callback, heap_resize_callback);
  895. }
  896. void il2cpp_profiler_install_fileio(Il2CppProfileFileIOFunc callback)
  897. {
  898. Profiler::InstallFileIO(callback);
  899. }
  900. void il2cpp_profiler_install_thread(Il2CppProfileThreadFunc start, Il2CppProfileThreadFunc end)
  901. {
  902. Profiler::InstallThread(start, end);
  903. }
  904. #endif
  905. // property
  906. const char* il2cpp_property_get_name(PropertyInfo *prop)
  907. {
  908. return Property::GetName(prop);
  909. }
  910. const MethodInfo* il2cpp_property_get_get_method(PropertyInfo *prop)
  911. {
  912. return Property::GetGetMethod(prop);
  913. }
  914. const MethodInfo* il2cpp_property_get_set_method(PropertyInfo *prop)
  915. {
  916. return Property::GetSetMethod(prop);
  917. }
  918. Il2CppClass* il2cpp_property_get_parent(PropertyInfo *prop)
  919. {
  920. return Property::GetParent(prop);
  921. }
  922. uint32_t il2cpp_property_get_flags(PropertyInfo *prop)
  923. {
  924. return Property::GetFlags(prop);
  925. }
  926. // object
  927. Il2CppClass* il2cpp_object_get_class(Il2CppObject* obj)
  928. {
  929. return Object::GetClass(obj);
  930. }
  931. uint32_t il2cpp_object_get_size(Il2CppObject* obj)
  932. {
  933. return Object::GetSize(obj);
  934. }
  935. const MethodInfo* il2cpp_object_get_virtual_method(Il2CppObject *obj, const MethodInfo *method)
  936. {
  937. return Object::GetVirtualMethod(obj, method);
  938. }
  939. Il2CppObject* il2cpp_object_new(const Il2CppClass *klass)
  940. {
  941. try
  942. {
  943. return Object::New(const_cast<Il2CppClass*>(klass));
  944. }
  945. catch (const Il2CppExceptionWrapper&)
  946. {
  947. // If a static constructor throws, that exception will occur here.
  948. // We don't want that to escape across the embedding API.
  949. return NULL;
  950. }
  951. }
  952. void* il2cpp_object_unbox(Il2CppObject* obj)
  953. {
  954. return Object::Unbox(obj);
  955. }
  956. Il2CppObject* il2cpp_value_box(Il2CppClass *klass, void* data)
  957. {
  958. return Object::Box(klass, data);
  959. }
  960. // monitor
  961. void il2cpp_monitor_enter(Il2CppObject* obj)
  962. {
  963. Monitor::Enter(obj);
  964. }
  965. bool il2cpp_monitor_try_enter(Il2CppObject* obj, uint32_t timeout)
  966. {
  967. return Monitor::TryEnter(obj, timeout);
  968. }
  969. void il2cpp_monitor_exit(Il2CppObject* obj)
  970. {
  971. Monitor::Exit(obj);
  972. }
  973. void il2cpp_monitor_pulse(Il2CppObject* obj)
  974. {
  975. Monitor::Pulse(obj);
  976. }
  977. void il2cpp_monitor_pulse_all(Il2CppObject* obj)
  978. {
  979. Monitor::PulseAll(obj);
  980. }
  981. void il2cpp_monitor_wait(Il2CppObject* obj)
  982. {
  983. Monitor::Wait(obj);
  984. }
  985. bool il2cpp_monitor_try_wait(Il2CppObject* obj, uint32_t timeout)
  986. {
  987. return Monitor::TryWait(obj, timeout);
  988. }
  989. // runtime
  990. Il2CppObject* il2cpp_runtime_invoke_convert_args(const MethodInfo *method, void *obj, Il2CppObject **params, int paramCount, Il2CppException **exc)
  991. {
  992. return Runtime::InvokeConvertArgs(method, obj, params, paramCount, exc);
  993. }
  994. Il2CppObject* il2cpp_runtime_invoke(const MethodInfo *method,
  995. void *obj, void **params, Il2CppException **exc)
  996. {
  997. return Runtime::Invoke(method, obj, params, exc);
  998. }
  999. void il2cpp_runtime_class_init(Il2CppClass* klass)
  1000. {
  1001. return Runtime::ClassInit(klass);
  1002. }
  1003. void il2cpp_runtime_object_init(Il2CppObject *obj)
  1004. {
  1005. Runtime::ObjectInit(obj);
  1006. }
  1007. void il2cpp_runtime_object_init_exception(Il2CppObject *obj, Il2CppException **exc)
  1008. {
  1009. Runtime::ObjectInitException(obj, exc);
  1010. }
  1011. void il2cpp_runtime_unhandled_exception_policy_set(Il2CppRuntimeUnhandledExceptionPolicy value)
  1012. {
  1013. Runtime::SetUnhandledExceptionPolicy(value);
  1014. }
  1015. // string
  1016. int32_t il2cpp_string_length(Il2CppString* str)
  1017. {
  1018. return il2cpp::utils::StringUtils::GetLength(str);
  1019. }
  1020. Il2CppChar* il2cpp_string_chars(Il2CppString* str)
  1021. {
  1022. return il2cpp::utils::StringUtils::GetChars(str);
  1023. }
  1024. // Same as il2cpp_string_new_wrapper, because other normally takes a domain
  1025. Il2CppString* il2cpp_string_new(const char* str)
  1026. {
  1027. return String::New(str);
  1028. }
  1029. Il2CppString* il2cpp_string_new_wrapper(const char* str)
  1030. {
  1031. return String::NewWrapper(str);
  1032. }
  1033. Il2CppString* il2cpp_string_new_utf16(const Il2CppChar *text, int32_t len)
  1034. {
  1035. return String::NewUtf16(text, len);
  1036. }
  1037. Il2CppString* il2cpp_string_new_len(const char* str, uint32_t length)
  1038. {
  1039. return String::NewLen(str, length);
  1040. }
  1041. Il2CppString* il2cpp_string_intern(Il2CppString* str)
  1042. {
  1043. return String::Intern(str);
  1044. }
  1045. Il2CppString* il2cpp_string_is_interned(Il2CppString* str)
  1046. {
  1047. return String::IsInterned(str);
  1048. }
  1049. // thread
  1050. Il2CppThread *il2cpp_thread_current()
  1051. {
  1052. return Thread::Current();
  1053. }
  1054. Il2CppThread *il2cpp_thread_attach(Il2CppDomain *domain)
  1055. {
  1056. return Thread::Attach(domain);
  1057. }
  1058. void il2cpp_thread_detach(Il2CppThread *thread)
  1059. {
  1060. Thread::Detach(thread);
  1061. }
  1062. Il2CppThread **il2cpp_thread_get_all_attached_threads(size_t *size)
  1063. {
  1064. return Thread::GetAllAttachedThreads(*size);
  1065. }
  1066. bool il2cpp_is_vm_thread(Il2CppThread *thread)
  1067. {
  1068. return Thread::IsVmThread(thread);
  1069. }
  1070. // stacktrace
  1071. void il2cpp_current_thread_walk_frame_stack(Il2CppFrameWalkFunc func, void* user_data)
  1072. {
  1073. StackTrace::WalkFrameStack(func, user_data);
  1074. }
  1075. void il2cpp_thread_walk_frame_stack(Il2CppThread *thread, Il2CppFrameWalkFunc func, void *user_data)
  1076. {
  1077. return StackTrace::WalkThreadFrameStack(thread, func, user_data);
  1078. }
  1079. bool il2cpp_current_thread_get_top_frame(Il2CppStackFrameInfo* frame)
  1080. {
  1081. IL2CPP_ASSERT(frame);
  1082. return StackTrace::GetTopStackFrame(*frame);
  1083. }
  1084. bool il2cpp_thread_get_top_frame(Il2CppThread* thread, Il2CppStackFrameInfo* frame)
  1085. {
  1086. IL2CPP_ASSERT(frame);
  1087. return StackTrace::GetThreadTopStackFrame(thread, *frame);
  1088. }
  1089. bool il2cpp_current_thread_get_frame_at(int32_t offset, Il2CppStackFrameInfo* frame)
  1090. {
  1091. IL2CPP_ASSERT(frame);
  1092. return StackTrace::GetStackFrameAt(offset, *frame);
  1093. }
  1094. bool il2cpp_thread_get_frame_at(Il2CppThread* thread, int32_t offset, Il2CppStackFrameInfo* frame)
  1095. {
  1096. IL2CPP_ASSERT(frame);
  1097. return StackTrace::GetThreadStackFrameAt(thread, offset, *frame);
  1098. }
  1099. int32_t il2cpp_current_thread_get_stack_depth()
  1100. {
  1101. return static_cast<int32_t>(StackTrace::GetStackDepth());
  1102. }
  1103. int32_t il2cpp_thread_get_stack_depth(Il2CppThread *thread)
  1104. {
  1105. return StackTrace::GetThreadStackDepth(thread);
  1106. }
  1107. void il2cpp_set_default_thread_affinity(int64_t affinity_mask)
  1108. {
  1109. Thread::SetDefaultAffinityMask(affinity_mask);
  1110. }
  1111. void il2cpp_override_stack_backtrace(Il2CppBacktraceFunc stackBacktraceFunc)
  1112. {
  1113. il2cpp::os::StackTrace::OverrideStackBacktrace(stackBacktraceFunc);
  1114. }
  1115. // type
  1116. Il2CppObject* il2cpp_type_get_object(const Il2CppType *type)
  1117. {
  1118. return (Il2CppObject*)Reflection::GetTypeObject(type);
  1119. }
  1120. int il2cpp_type_get_type(const Il2CppType *type)
  1121. {
  1122. return Type::GetType(type);
  1123. }
  1124. Il2CppClass* il2cpp_type_get_class_or_element_class(const Il2CppType *type)
  1125. {
  1126. return Type::GetClassOrElementClass(type);
  1127. }
  1128. char* il2cpp_type_get_name(const Il2CppType *type)
  1129. {
  1130. std::string name = Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_IL);
  1131. char* buffer = static_cast<char*>(il2cpp_alloc(name.length() + 1));
  1132. memcpy(buffer, name.c_str(), name.length() + 1);
  1133. return buffer;
  1134. }
  1135. char* il2cpp_type_get_assembly_qualified_name(const Il2CppType * type)
  1136. {
  1137. std::string name = Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED);
  1138. char* buffer = static_cast<char*>(il2cpp_alloc(name.length() + 1));
  1139. memcpy(buffer, name.c_str(), name.length() + 1);
  1140. return buffer;
  1141. }
  1142. char* il2cpp_type_get_reflection_name(const Il2CppType *type)
  1143. {
  1144. std::string name = Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_REFLECTION);
  1145. char* buffer = static_cast<char*>(il2cpp_alloc(name.length() + 1));
  1146. memcpy(buffer, name.c_str(), name.length() + 1);
  1147. return buffer;
  1148. }
  1149. bool il2cpp_type_is_byref(const Il2CppType *type)
  1150. {
  1151. return type->byref;
  1152. }
  1153. uint32_t il2cpp_type_get_attrs(const Il2CppType *type)
  1154. {
  1155. return type->attrs;
  1156. }
  1157. bool il2cpp_type_equals(const Il2CppType* type, const Il2CppType *otherType)
  1158. {
  1159. return Type::IsEqualToType(type, otherType);
  1160. }
  1161. bool il2cpp_type_is_static(const Il2CppType *type)
  1162. {
  1163. return (type->attrs & FIELD_ATTRIBUTE_STATIC) != 0;
  1164. }
  1165. bool il2cpp_type_is_pointer_type(const Il2CppType *type)
  1166. {
  1167. return type->type == IL2CPP_TYPE_PTR;
  1168. }
  1169. // image
  1170. const Il2CppAssembly* il2cpp_image_get_assembly(const Il2CppImage *image)
  1171. {
  1172. return Image::GetAssembly(image);
  1173. }
  1174. const char* il2cpp_image_get_name(const Il2CppImage *image)
  1175. {
  1176. return Image::GetName(image);
  1177. }
  1178. const char* il2cpp_image_get_filename(const Il2CppImage *image)
  1179. {
  1180. return Image::GetFileName(image);
  1181. }
  1182. const MethodInfo* il2cpp_image_get_entry_point(const Il2CppImage *image)
  1183. {
  1184. return Image::GetEntryPoint(image);
  1185. }
  1186. size_t il2cpp_image_get_class_count(const Il2CppImage * image)
  1187. {
  1188. return Image::GetNumTypes(image);
  1189. }
  1190. const Il2CppClass* il2cpp_image_get_class(const Il2CppImage * image, size_t index)
  1191. {
  1192. return Image::GetType(image, static_cast<AssemblyTypeIndex>(index));
  1193. }
  1194. Il2CppManagedMemorySnapshot* il2cpp_capture_memory_snapshot()
  1195. {
  1196. return MemoryInformation::CaptureManagedMemorySnapshot();
  1197. }
  1198. void il2cpp_free_captured_memory_snapshot(Il2CppManagedMemorySnapshot* snapshot)
  1199. {
  1200. MemoryInformation::FreeCapturedManagedMemorySnapshot(snapshot);
  1201. }
  1202. void il2cpp_set_find_plugin_callback(Il2CppSetFindPlugInCallback method)
  1203. {
  1204. il2cpp::vm::PlatformInvoke::SetFindPluginCallback(method);
  1205. }
  1206. // Logging
  1207. void il2cpp_register_log_callback(Il2CppLogCallback method)
  1208. {
  1209. il2cpp::utils::Logging::SetLogCallback(method);
  1210. }
  1211. // Debugger
  1212. void il2cpp_debugger_set_agent_options(const char* options)
  1213. {
  1214. #if IL2CPP_MONO_DEBUGGER
  1215. il2cpp::utils::Debugger::SetAgentOptions(options);
  1216. #endif
  1217. }
  1218. bool il2cpp_is_debugger_attached()
  1219. {
  1220. return il2cpp::utils::Debugger::GetIsDebuggerAttached();
  1221. }
  1222. void il2cpp_register_debugger_agent_transport(Il2CppDebuggerTransport * debuggerTransport)
  1223. {
  1224. #if IL2CPP_MONO_DEBUGGER
  1225. il2cpp::utils::Debugger::RegisterTransport(debuggerTransport);
  1226. #endif
  1227. }
  1228. bool il2cpp_debug_get_method_info(const MethodInfo* method, Il2CppMethodDebugInfo* methodDebugInfo)
  1229. {
  1230. #if IL2CPP_ENABLE_NATIVE_STACKTRACES
  1231. return il2cpp::utils::NativeSymbol::GetMethodDebugInfo(method, methodDebugInfo);
  1232. #else
  1233. return false;
  1234. #endif
  1235. }
  1236. void il2cpp_unity_install_unitytls_interface(const void* unitytlsInterfaceStruct)
  1237. {
  1238. il2cpp::vm::Runtime::SetUnityTlsInterface(unitytlsInterfaceStruct);
  1239. }
  1240. // Custom Attributes
  1241. Il2CppCustomAttrInfo* il2cpp_custom_attrs_from_class(Il2CppClass *klass)
  1242. {
  1243. return (Il2CppCustomAttrInfo*)(MetadataCache::GetCustomAttributeTypeToken(klass->image, klass->token));
  1244. }
  1245. Il2CppCustomAttrInfo* il2cpp_custom_attrs_from_method(const MethodInfo * method)
  1246. {
  1247. return (Il2CppCustomAttrInfo*)(MetadataCache::GetCustomAttributeTypeToken(method->klass->image, method->token));
  1248. }
  1249. Il2CppCustomAttrInfo* il2cpp_custom_attrs_from_field(const FieldInfo * field)
  1250. {
  1251. return (Il2CppCustomAttrInfo*)(MetadataCache::GetCustomAttributeTypeToken(field->parent->image, field->token));
  1252. }
  1253. bool il2cpp_custom_attrs_has_attr(Il2CppCustomAttrInfo *ainfo, Il2CppClass *attr_klass)
  1254. {
  1255. return Reflection::HasAttribute(reinterpret_cast<Il2CppMetadataCustomAttributeHandle>(ainfo), attr_klass);
  1256. }
  1257. Il2CppObject* il2cpp_custom_attrs_get_attr(Il2CppCustomAttrInfo *ainfo, Il2CppClass *attr_klass)
  1258. {
  1259. return Reflection::GetCustomAttribute(reinterpret_cast<Il2CppMetadataCustomAttributeHandle>(ainfo), attr_klass);
  1260. }
  1261. Il2CppArray* il2cpp_custom_attrs_construct(Il2CppCustomAttrInfo *ainfo)
  1262. {
  1263. return Reflection::ConstructCustomAttributes(reinterpret_cast<Il2CppMetadataCustomAttributeHandle>(ainfo));
  1264. }
  1265. void il2cpp_custom_attrs_free(Il2CppCustomAttrInfo *ainfo)
  1266. {
  1267. // nothing to free, we cache everything
  1268. }
  1269. void il2cpp_type_get_name_chunked(const Il2CppType * type, void(*chunkReportFunc)(void* data, void* userData), void* userData)
  1270. {
  1271. Type::GetNameChunkedRecurse(type, IL2CPP_TYPE_NAME_FORMAT_IL, chunkReportFunc, userData);
  1272. }
  1273. void il2cpp_class_set_userdata(Il2CppClass* klass, void* userdata)
  1274. {
  1275. klass->unity_user_data = userdata;
  1276. }
  1277. int il2cpp_class_get_userdata_offset()
  1278. {
  1279. return offsetof(struct Il2CppClass, unity_user_data);
  1280. }
  1281. void il2cpp_class_for_each(void(*klassReportFunc)(Il2CppClass* klass, void* userData), void* userData)
  1282. {
  1283. MemoryInformation::ReportIL2CppClasses(klassReportFunc, userData);
  1284. }
  1285. // Android
  1286. void il2cpp_unity_set_android_network_up_state_func(Il2CppAndroidUpStateFunc func)
  1287. {
  1288. AndroidRuntime::SetNetworkUpStateFunc(func);
  1289. }
  1290. #if ENABLE_HMI_MODE && (IL2CPP_TARGET_ANDROID || PLATFORM_ANDROID)
  1291. #include "os/Android/Context.h"
  1292. void il2cpp_set_android_context(void* env, void* context)
  1293. {
  1294. AndroidSetContext((JNIEnv*)env, *((jobject*)context));
  1295. }
  1296. #endif