il2cpp-api.cpp 36 KB

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