Debugger.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_MONO_DEBUGGER
  3. #include "il2cpp-class-internals.h"
  4. #include "il2cpp-object-internals.h"
  5. #include "il2cpp-metadata.h"
  6. #include "Debugger.h"
  7. #include "os/Thread.h"
  8. #include "os/c-api/Allocator.h"
  9. #include "os/SocketBridge.h"
  10. #include "vm/Assembly.h"
  11. #include "vm/Image.h"
  12. #include "vm/MetadataCache.h"
  13. #include "vm/Method.h"
  14. #include "vm/StackTrace.h"
  15. #include "vm/Thread.h"
  16. #include "utils/Environment.h"
  17. #include "utils/dynamic_array.h"
  18. #include "utils/StringUtils.h"
  19. #include "utils/StringViewUtils.h"
  20. #include "utils/Il2CppHashMap.h"
  21. #include "utils/HashUtils.h"
  22. #include "VmStringUtils.h"
  23. #include <deque>
  24. #include <string>
  25. #include <algorithm>
  26. il2cpp::os::ThreadLocalValue s_ExecutionContexts; // Il2CppThreadUnwindState*
  27. struct MonoDebuggerRuntimeCallbacks
  28. {
  29. void(*il2cpp_debugger_save_thread_context)(Il2CppThreadUnwindState* context, int frameCountAdjust);
  30. void(*il2cpp_debugger_free_thread_context)(Il2CppThreadUnwindState* context);
  31. };
  32. struct DebuggerTransport
  33. {
  34. const char *name;
  35. void(*connect) (const char *address);
  36. int (*wait_for_attach) (void);
  37. void(*close1) (void);
  38. void(*close2) (void);
  39. int (*send) (void *buf, int len);
  40. int(*recv) (void *buf, int len);
  41. };
  42. struct MonoContext;
  43. extern "C"
  44. {
  45. void mono_debugger_agent_parse_options(const char *options);
  46. void mono_debugger_agent_init_minimal();
  47. void mono_debugger_agent_init();
  48. void mono_debugger_run_debugger_thread_func(void* arg);
  49. void debugger_agent_single_step_from_context(MonoContext *ctx, Il2CppSequencePoint* sequencePoint);
  50. void mono_debugger_il2cpp_init();
  51. void unity_debugger_agent_breakpoint(Il2CppSequencePoint* sequencePoint);
  52. void unity_debugger_agent_pausepoint();
  53. void mono_debugger_install_runtime_callbacks(MonoDebuggerRuntimeCallbacks* cbs);
  54. int32_t unity_debugger_agent_is_global_breakpoint_active(void* singleStepRequest);
  55. int32_t unity_debugger_agent_is_single_stepping();
  56. void unity_debugger_agent_handle_exception(Il2CppException *exc);
  57. int32_t il2cpp_mono_methods_match(const MethodInfo* left, const MethodInfo* right);
  58. void debugger_agent_user_break();
  59. int32_t debugger_agent_debug_log_is_enabled();
  60. void debugger_agent_debug_log(int level, Il2CppString *category, Il2CppString *message);
  61. int32_t unity_pause_point_active();
  62. void il2cpp_save_current_thread_context_func_exit();
  63. void mono_debugger_agent_register_transport(DebuggerTransport *trans);
  64. void unity_debugger_agent_thread_startup(uintptr_t tid);
  65. void unity_debugger_agent_thread_end(uintptr_t tid);
  66. void unity_debugger_agent_runtime_shutdown();
  67. static void* il2cpp_malloc(size_t size)
  68. {
  69. return IL2CPP_MALLOC(size);
  70. }
  71. static void il2cpp_mfree(void* memory)
  72. {
  73. IL2CPP_FREE(memory);
  74. }
  75. }
  76. static const Il2CppDebuggerMetadataRegistration *g_metadata;
  77. namespace il2cpp
  78. {
  79. namespace utils
  80. {
  81. static os::Thread* s_DebuggerThread;
  82. static bool s_IsDebuggerAttached = false;
  83. static bool s_IsDebuggerInitialized = false;
  84. static std::string s_AgentOptions;
  85. static os::Mutex s_Il2CppMonoLoaderLock(false);
  86. static uint64_t s_Il2CppMonoLoaderLockThreadId = 0;
  87. static Il2CppMonoInterpCallbacks s_InterpCallbacks;
  88. typedef dynamic_array<Il2CppSequencePoint*> SequencePointList;
  89. typedef Il2CppHashMap<const MethodInfo*, SequencePointList*, il2cpp::utils::PointerHash<MethodInfo> > MethodToSequencePointsMap;
  90. static MethodToSequencePointsMap s_methodToSequencePoints;
  91. typedef dynamic_array<Il2CppCatchPoint*> CatchPointList;
  92. typedef Il2CppHashMap<const MethodInfo*, CatchPointList*, il2cpp::utils::PointerHash<MethodInfo> > MethodToCatchPointsMap;
  93. static MethodToCatchPointsMap s_methodToCatchPoints;
  94. typedef Il2CppHashMap<const MethodInfo*, const MethodInfo*, il2cpp::utils::PointerHash<MethodInfo> > MethodToMethodMap;
  95. static MethodToMethodMap s_uninflatedMethodToInflated;
  96. SequencePointList s_sequencePoints;
  97. typedef dynamic_array<const char*> FileNameList;
  98. typedef Il2CppHashMap<const Il2CppClass*, FileNameList, il2cpp::utils::PointerHash<Il2CppClass> > TypeSourceFileMap;
  99. static TypeSourceFileMap *s_typeSourceFiles;
  100. static MethodToSequencePointsMap::const_iterator GetMethodSequencePointIterator(const MethodInfo *method);
  101. static void* FrameGetArg(Il2CppSequencePointExecutionContext* frame, int pos)
  102. {
  103. return frame->params[pos];
  104. }
  105. static void* FrameGetLocal(Il2CppSequencePointExecutionContext* frame, int pos)
  106. {
  107. return frame->locals[pos];
  108. }
  109. static void* FrameGetThis(Il2CppSequencePointExecutionContext* frame)
  110. {
  111. return *frame->thisArg;
  112. }
  113. static void InitializeInterpCallbacks()
  114. {
  115. s_InterpCallbacks.frame_get_arg = FrameGetArg;
  116. s_InterpCallbacks.frame_get_local = FrameGetLocal;
  117. s_InterpCallbacks.frame_get_this = FrameGetThis;
  118. }
  119. void Debugger::RegisterMetadata(const Il2CppDebuggerMetadataRegistration *data)
  120. {
  121. g_metadata = data;
  122. }
  123. #if defined(RUNTIME_IL2CPP)
  124. void breakpoint_callback(Il2CppSequencePoint* sequencePoint)
  125. {
  126. unity_debugger_agent_breakpoint(sequencePoint);
  127. }
  128. void pausepoint_callback()
  129. {
  130. unity_debugger_agent_pausepoint();
  131. }
  132. #endif
  133. static void InitializeMonoSoftDebugger(const char* options)
  134. {
  135. #if defined(RUNTIME_IL2CPP)
  136. InitializeInterpCallbacks();
  137. os::SocketBridge::WaitForInitialization();
  138. mono_debugger_il2cpp_init();
  139. mono_debugger_agent_parse_options(options);
  140. mono_debugger_agent_init();
  141. s_typeSourceFiles = new TypeSourceFileMap();
  142. MonoDebuggerRuntimeCallbacks cbs;
  143. cbs.il2cpp_debugger_save_thread_context = Debugger::SaveThreadContext;
  144. cbs.il2cpp_debugger_free_thread_context = Debugger::FreeThreadContext;
  145. mono_debugger_install_runtime_callbacks(&cbs);
  146. il2cpp::utils::Debugger::RegisterCallbacks(breakpoint_callback, pausepoint_callback);
  147. register_allocator(il2cpp_malloc, il2cpp_mfree);
  148. s_IsDebuggerInitialized = true;
  149. #else
  150. IL2CPP_ASSERT(0 && "The managed debugger is only supported for the libil2cpp runtime backend.");
  151. #endif
  152. }
  153. void Debugger::SetAgentOptions(const char* options)
  154. {
  155. s_AgentOptions = options;
  156. }
  157. void Debugger::RegisterTransport(const Il2CppDebuggerTransport* transport)
  158. {
  159. #if defined(RUNTIME_IL2CPP)
  160. DebuggerTransport mono_transport;
  161. mono_transport.name = transport->name;
  162. mono_transport.connect = transport->connect;
  163. mono_transport.wait_for_attach = transport->wait_for_attach;
  164. mono_transport.close1 = transport->close1;
  165. mono_transport.close2 = transport->close2;
  166. mono_transport.send = transport->send;
  167. mono_transport.recv = transport->recv;
  168. mono_debugger_agent_register_transport(&mono_transport);
  169. #endif
  170. }
  171. void Debugger::InitializeTypeSourceFileMap()
  172. {
  173. Il2CppClass* lastKlass = NULL;
  174. Il2CppClass *klass = NULL;
  175. FileNameList files;
  176. vm::AssemblyVector* assemblies = vm::Assembly::GetAllAssemblies();
  177. for (vm::AssemblyVector::const_iterator iter = assemblies->begin(); iter != assemblies->end(); ++iter)
  178. {
  179. const Il2CppImage* image = vm::Assembly::GetImage(*iter);
  180. const Il2CppDebuggerMetadataRegistration* debuggerMetadata = image->codeGenModule->debuggerMetadata;
  181. if (debuggerMetadata == NULL)
  182. continue;
  183. for (int i = 0; i < debuggerMetadata->numTypeSourceFileEntries; ++i)
  184. {
  185. Il2CppTypeSourceFilePair& pair = debuggerMetadata->typeSourceFiles[i];
  186. const char *file = debuggerMetadata->sequencePointSourceFiles[pair.sourceFileIndex].file;
  187. klass = il2cpp::vm::MetadataCache::GetTypeInfoFromTypeSourcePair(image, &pair);
  188. if (klass != lastKlass && lastKlass != NULL)
  189. {
  190. s_typeSourceFiles->add(lastKlass, files);
  191. files.clear();
  192. }
  193. lastKlass = klass;
  194. files.push_back(file);
  195. }
  196. }
  197. if (files.size() > 0)
  198. s_typeSourceFiles->add(klass, files);
  199. }
  200. void Debugger::Start()
  201. {
  202. if (s_IsDebuggerInitialized)
  203. {
  204. vm::MetadataCache::InitializeAllMethodMetadata();
  205. InitializeTypeSourceFileMap();
  206. InitializeMethodToSequencePointMap();
  207. InitializeMethodToCatchPointMap();
  208. Debugger::StartDebuggerThread();
  209. }
  210. }
  211. static bool TryInitializeDebugger(const std::string& options)
  212. {
  213. if (StringUtils::StartsWith(STRING_TO_STRINGVIEW(options), "--debugger-agent"))
  214. {
  215. InitializeMonoSoftDebugger(options.c_str() + options.find("=") + 1);
  216. return true;
  217. }
  218. return false;
  219. }
  220. void Debugger::Init()
  221. {
  222. bool debuggerIsInitialized = false;
  223. if (!s_AgentOptions.empty())
  224. {
  225. debuggerIsInitialized = TryInitializeDebugger(s_AgentOptions);
  226. }
  227. else
  228. {
  229. const std::vector<UTF16String>& args = Environment::GetMainArgs();
  230. for (std::vector<UTF16String>::const_iterator arg = args.begin(); arg != args.end(); ++arg)
  231. {
  232. std::string argument = StringUtils::Utf16ToUtf8(*arg);
  233. debuggerIsInitialized = TryInitializeDebugger(argument);
  234. if (debuggerIsInitialized)
  235. break;
  236. }
  237. }
  238. if (!debuggerIsInitialized)
  239. mono_debugger_agent_init_minimal();
  240. }
  241. static Debugger::OnBreakPointHitCallback s_BreakCallback;
  242. static Debugger::OnPausePointHitCallback s_PauseCallback;
  243. void Debugger::RegisterCallbacks(OnBreakPointHitCallback breakCallback, OnPausePointHitCallback pauseCallback)
  244. {
  245. s_BreakCallback = breakCallback;
  246. s_PauseCallback = pauseCallback;
  247. }
  248. void Debugger::StartDebuggerThread()
  249. {
  250. #if defined(RUNTIME_IL2CPP)
  251. // This thread is allocated here once and never deallocated.
  252. s_DebuggerThread = new os::Thread();
  253. s_DebuggerThread->Run(mono_debugger_run_debugger_thread_func, NULL);
  254. #else
  255. IL2CPP_ASSERT(0 && "The managed debugger is only supported for the libil2cpp runtime backend.");
  256. #endif
  257. }
  258. Il2CppThreadUnwindState* Debugger::GetThreadStatePointer()
  259. {
  260. if (!s_IsDebuggerInitialized)
  261. return NULL;
  262. Il2CppThreadUnwindState* unwindState;
  263. s_ExecutionContexts.GetValue(reinterpret_cast<void**>(&unwindState));
  264. return unwindState;
  265. }
  266. void Debugger::SaveThreadContext(Il2CppThreadUnwindState* context, int frameCountAdjust)
  267. {
  268. if (!s_IsDebuggerInitialized)
  269. return;
  270. IL2CPP_ASSERT(!IsDebuggerThread(os::Thread::GetCurrentThread()));
  271. }
  272. void Debugger::FreeThreadContext(Il2CppThreadUnwindState* context)
  273. {
  274. if (!s_IsDebuggerInitialized)
  275. return;
  276. IL2CPP_ASSERT(!IsDebuggerThread(os::Thread::GetCurrentThread()));
  277. }
  278. void Debugger::OnBreakPointHit(Il2CppSequencePoint *sequencePoint)
  279. {
  280. #if defined(RUNTIME_IL2CPP)
  281. if (IsGlobalBreakpointActive() || unity_debugger_agent_is_single_stepping())
  282. {
  283. debugger_agent_single_step_from_context(NULL, sequencePoint);
  284. }
  285. else if (s_BreakCallback)
  286. {
  287. s_BreakCallback(sequencePoint);
  288. }
  289. else
  290. IL2CPP_DEBUG_BREAK();
  291. #else
  292. IL2CPP_ASSERT(0 && "The managed debugger is only supported for the libil2cpp runtime backend.");
  293. #endif
  294. }
  295. void Debugger::OnPausePointHit()
  296. {
  297. #if defined(RUNTIME_IL2CPP)
  298. if (s_PauseCallback)
  299. s_PauseCallback();
  300. #else
  301. IL2CPP_ASSERT(0 && "The managed debugger is only supported for the libil2cpp runtime backend.");
  302. #endif
  303. }
  304. bool Debugger::IsGlobalBreakpointActive()
  305. {
  306. if (!Debugger::GetIsDebuggerAttached())
  307. return false;
  308. #if defined(RUNTIME_IL2CPP)
  309. return unity_debugger_agent_is_global_breakpoint_active(NULL);
  310. #else
  311. IL2CPP_ASSERT(0 && "The managed debugger is only supported for the libil2cpp runtime backend.");
  312. return false;
  313. #endif
  314. }
  315. bool Debugger::GetIsDebuggerAttached()
  316. {
  317. return s_IsDebuggerAttached;
  318. }
  319. void Debugger::SetIsDebuggerAttached(bool attached)
  320. {
  321. s_IsDebuggerAttached = attached;
  322. }
  323. bool Debugger::IsDebuggerThread(os::Thread* thread)
  324. {
  325. return thread == s_DebuggerThread;
  326. }
  327. static void InitializeUnwindState(Il2CppThreadUnwindState* unwindState, uint32_t frameCapacity)
  328. {
  329. unwindState->frameCount = 0;
  330. unwindState->frameCapacity = frameCapacity;
  331. unwindState->executionContexts = (Il2CppSequencePointExecutionContext**)calloc(frameCapacity, sizeof(Il2CppSequencePointExecutionContext*));
  332. }
  333. void Debugger::AllocateThreadLocalData()
  334. {
  335. Il2CppThreadUnwindState* unwindState;
  336. s_ExecutionContexts.GetValue(reinterpret_cast<void**>(&unwindState));
  337. if (unwindState == NULL)
  338. {
  339. unwindState = (Il2CppThreadUnwindState*)calloc(1, sizeof(Il2CppThreadUnwindState));
  340. InitializeUnwindState(unwindState, 512);
  341. s_ExecutionContexts.SetValue(unwindState);
  342. }
  343. }
  344. void Debugger::GrowFrameCapacity(Il2CppThreadUnwindState* unwindState)
  345. {
  346. // Create a new unwind state object to hold the large array of execution context pointers
  347. Il2CppThreadUnwindState newUnwindState;
  348. InitializeUnwindState(&newUnwindState, unwindState->frameCapacity * 2);
  349. // Copy the existing execution context pointers into the new one
  350. memcpy(newUnwindState.executionContexts, unwindState->executionContexts, unwindState->frameCapacity * sizeof(Il2CppSequencePointExecutionContext*));
  351. // Free the existing one
  352. free(unwindState->executionContexts);
  353. // Set the new data into the existing one, so the client can keep its object reference
  354. unwindState->frameCapacity = newUnwindState.frameCapacity;
  355. unwindState->executionContexts = newUnwindState.executionContexts;
  356. }
  357. void Debugger::FreeThreadLocalData()
  358. {
  359. if (s_IsDebuggerInitialized)
  360. {
  361. Il2CppThreadUnwindState* unwindState;
  362. s_ExecutionContexts.GetValue(reinterpret_cast<void**>(&unwindState));
  363. s_ExecutionContexts.SetValue(NULL);
  364. if (unwindState != NULL)
  365. {
  366. free(unwindState->executionContexts);
  367. free(unwindState);
  368. }
  369. }
  370. }
  371. Il2CppSequencePoint* Debugger::GetSequencePoint(const Il2CppImage* image, size_t id)
  372. {
  373. if (image->codeGenModule->debuggerMetadata->numSequencePoints == 0)
  374. return NULL;
  375. return &image->codeGenModule->debuggerMetadata->sequencePoints[id];
  376. }
  377. struct SeqPointIter
  378. {
  379. SequencePointList::iterator iter, end;
  380. };
  381. Il2CppSequencePoint* Debugger::GetSequencePoints(const MethodInfo* method, void** iter)
  382. {
  383. if (!iter)
  384. return NULL;
  385. SeqPointIter *pIter = NULL;
  386. if (!*iter)
  387. {
  388. MethodToSequencePointsMap::const_iterator entry = GetMethodSequencePointIterator(method);
  389. if (entry == s_methodToSequencePoints.end())
  390. return NULL;
  391. pIter = new SeqPointIter();
  392. *iter = pIter;
  393. pIter->iter = entry->second->begin();
  394. pIter->end = entry->second->end();
  395. return *(pIter->iter);
  396. }
  397. pIter = (SeqPointIter*)*iter;
  398. pIter->iter++;
  399. if (pIter->iter != pIter->end)
  400. {
  401. return *(pIter->iter);
  402. }
  403. else
  404. {
  405. delete pIter;
  406. *iter = NULL;
  407. }
  408. return NULL;
  409. }
  410. Il2CppSequencePoint* Debugger::GetAllSequencePoints(void* *iter)
  411. {
  412. size_t index = (size_t)(intptr_t)*iter;
  413. if (index >= s_sequencePoints.size())
  414. return NULL;
  415. Il2CppSequencePoint* retVal = s_sequencePoints[index];
  416. *iter = (void*)(intptr_t)(index + 1);
  417. return retVal;
  418. }
  419. MethodToSequencePointsMap::const_iterator GetMethodSequencePointIterator(const MethodInfo *method)
  420. {
  421. if (method->is_inflated)
  422. method = method->genericMethod->methodDefinition;
  423. MethodToSequencePointsMap::const_iterator entry = s_methodToSequencePoints.find(method);
  424. if (entry == s_methodToSequencePoints.end())
  425. {
  426. //the sequence point map doesn't have uninflated methods, need to map the incoming method to
  427. //an inflated method. il2cpp_mono_methods_match has a case for this.
  428. MethodToMethodMap::const_iterator inflated = s_uninflatedMethodToInflated.find(method);
  429. if (inflated != s_uninflatedMethodToInflated.end())
  430. {
  431. method = inflated->second;
  432. }
  433. else
  434. {
  435. for (MethodToSequencePointsMap::iterator mapIter = s_methodToSequencePoints.begin(); mapIter != s_methodToSequencePoints.end(); ++mapIter)
  436. {
  437. if (il2cpp_mono_methods_match(method, mapIter->first))
  438. {
  439. s_uninflatedMethodToInflated.add(method, mapIter->first);
  440. method = mapIter->first;
  441. break;
  442. }
  443. }
  444. }
  445. return s_methodToSequencePoints.find(method);
  446. }
  447. return entry;
  448. }
  449. Il2CppSequencePoint* Debugger::GetSequencePoint(const Il2CppImage* image, Il2CppCatchPoint* cp)
  450. {
  451. const MethodInfo *method = GetCatchPointMethod(image, cp);
  452. MethodToSequencePointsMap::const_iterator entry = GetMethodSequencePointIterator(method);
  453. if (entry == s_methodToSequencePoints.end())
  454. return NULL;
  455. SequencePointList::iterator iter = entry->second->begin();
  456. while (iter != entry->second->end())
  457. {
  458. if ((*iter)->ilOffset >= cp->ilOffset)
  459. return *iter;
  460. ++iter;
  461. }
  462. return NULL;
  463. }
  464. struct CatchPointIter
  465. {
  466. CatchPointList::iterator iter, end;
  467. };
  468. Il2CppCatchPoint* Debugger::GetCatchPoints(const MethodInfo* method, void** iter)
  469. {
  470. if (!iter)
  471. return NULL;
  472. CatchPointIter *pIter = NULL;
  473. if (!*iter)
  474. {
  475. MethodToCatchPointsMap::const_iterator entry = s_methodToCatchPoints.find(method);
  476. if (entry == s_methodToCatchPoints.end())
  477. return NULL;
  478. pIter = new CatchPointIter();
  479. *iter = pIter;
  480. pIter->iter = entry->second->begin();
  481. pIter->end = entry->second->end();
  482. return *(pIter->iter);
  483. }
  484. pIter = (CatchPointIter*)*iter;
  485. pIter->iter++;
  486. if (pIter->iter != pIter->end)
  487. {
  488. return *(pIter->iter);
  489. }
  490. else
  491. {
  492. delete pIter;
  493. *iter = NULL;
  494. }
  495. return NULL;
  496. }
  497. void Debugger::HandleException(Il2CppException *exc)
  498. {
  499. if (s_IsDebuggerInitialized)
  500. unity_debugger_agent_handle_exception(exc);
  501. }
  502. bool SequencePointOffsetLess(const Il2CppSequencePoint *s1, const Il2CppSequencePoint *s2)
  503. {
  504. return s1->ilOffset < s2->ilOffset;
  505. }
  506. bool CatchPointOffsetLess(const Il2CppCatchPoint *c1, const Il2CppCatchPoint *c2)
  507. {
  508. return c1->ilOffset < c2->ilOffset;
  509. }
  510. void Debugger::InitializeMethodToSequencePointMap()
  511. {
  512. size_t count = 0;
  513. vm::AssemblyVector* assemblies = vm::Assembly::GetAllAssemblies();
  514. for (vm::AssemblyVector::const_iterator iter = assemblies->begin(); iter != assemblies->end(); ++iter)
  515. {
  516. const Il2CppImage* image = vm::Assembly::GetImage(*iter);
  517. const Il2CppDebuggerMetadataRegistration* debuggerMetadata = image->codeGenModule->debuggerMetadata;
  518. if (debuggerMetadata == NULL)
  519. continue;
  520. for (int i = 0; i < debuggerMetadata->numSequencePoints; ++i)
  521. {
  522. Il2CppSequencePoint& seqPoint = debuggerMetadata->sequencePoints[i];
  523. const MethodInfo *method = GetSequencePointMethod(image, &seqPoint);
  524. if (method != NULL)
  525. {
  526. IL2CPP_ASSERT(!method->is_inflated && "Only open generic methods should have sequence points");
  527. SequencePointList* list;
  528. MethodToSequencePointsMap::iterator existingList = s_methodToSequencePoints.find(method);
  529. if (existingList == s_methodToSequencePoints.end())
  530. {
  531. list = new SequencePointList();
  532. s_methodToSequencePoints.add(method, list);
  533. }
  534. else
  535. {
  536. list = existingList->second;
  537. }
  538. list->push_back(&seqPoint);
  539. count++;
  540. }
  541. }
  542. }
  543. s_sequencePoints.reserve(count);
  544. for (MethodToSequencePointsMap::iterator methods = s_methodToSequencePoints.begin(); methods != s_methodToSequencePoints.end(); ++methods)
  545. {
  546. SequencePointList *seqPoints = methods->second;
  547. std::sort(seqPoints->begin(), seqPoints->end(), SequencePointOffsetLess);
  548. s_sequencePoints.insert(s_sequencePoints.end(), seqPoints->begin(), seqPoints->end());
  549. }
  550. }
  551. void Debugger::InitializeMethodToCatchPointMap()
  552. {
  553. vm::AssemblyVector* assemblies = vm::Assembly::GetAllAssemblies();
  554. for (vm::AssemblyVector::const_iterator iter = assemblies->begin(); iter != assemblies->end(); ++iter)
  555. {
  556. const Il2CppImage* image = vm::Assembly::GetImage(*iter);
  557. const Il2CppDebuggerMetadataRegistration* debuggerMetadata = image->codeGenModule->debuggerMetadata;
  558. if (debuggerMetadata == NULL)
  559. continue;
  560. for (int i = 0; i < debuggerMetadata->numCatchPoints; ++i)
  561. {
  562. Il2CppCatchPoint& catchPoint = debuggerMetadata->catchPoints[i];
  563. const MethodInfo *method = GetCatchPointMethod(image, &catchPoint);
  564. if (method != NULL)
  565. {
  566. CatchPointList* list;
  567. MethodToCatchPointsMap::iterator existingList = s_methodToCatchPoints.find(method);
  568. if (existingList == s_methodToCatchPoints.end())
  569. {
  570. list = new CatchPointList();
  571. s_methodToCatchPoints.add(method, list);
  572. }
  573. else
  574. {
  575. list = existingList->second;
  576. }
  577. list->push_back(&catchPoint);
  578. }
  579. }
  580. }
  581. for (MethodToCatchPointsMap::iterator methods = s_methodToCatchPoints.begin(); methods != s_methodToCatchPoints.end(); ++methods)
  582. {
  583. CatchPointList *catchPoints = methods->second;
  584. std::sort(catchPoints->begin(), catchPoints->end(), CatchPointOffsetLess);
  585. }
  586. }
  587. const char** Debugger::GetTypeSourceFiles(const Il2CppClass *klass, int& count)
  588. {
  589. TypeSourceFileMap::iterator it = s_typeSourceFiles->find(klass);
  590. if (it == s_typeSourceFiles->end())
  591. {
  592. count = 0;
  593. return NULL;
  594. }
  595. count = (int)it->second.size();
  596. return it->second.data();
  597. }
  598. void Debugger::UserBreak()
  599. {
  600. if (s_IsDebuggerAttached)
  601. debugger_agent_user_break();
  602. }
  603. bool Debugger::IsLoggingEnabled()
  604. {
  605. return debugger_agent_debug_log_is_enabled();
  606. }
  607. void Debugger::Log(int level, Il2CppString *category, Il2CppString *message)
  608. {
  609. if (s_IsDebuggerAttached)
  610. debugger_agent_debug_log(level, category, message);
  611. }
  612. bool Debugger::IsPausePointActive()
  613. {
  614. return unity_pause_point_active();
  615. }
  616. void Debugger::CheckPausePoint()
  617. {
  618. if (il2cpp::utils::Debugger::IsPausePointActive())
  619. il2cpp::utils::Debugger::OnPausePointHit();
  620. }
  621. const MethodInfo* Debugger::GetSequencePointMethod(const Il2CppImage* image, Il2CppSequencePoint *seqPoint)
  622. {
  623. if (seqPoint == NULL)
  624. return NULL;
  625. return il2cpp::vm::MetadataCache::GetMethodInfoFromSequencePoint(image, seqPoint);
  626. }
  627. const MethodInfo* Debugger::GetCatchPointMethod(const Il2CppImage* image, Il2CppCatchPoint *catchPoint)
  628. {
  629. if (catchPoint == NULL)
  630. return NULL;
  631. return il2cpp::vm::MetadataCache::GetMethodInfoFromCatchPoint(image, catchPoint);
  632. }
  633. const char* Debugger::GetLocalName(const MethodInfo* method, int32_t index)
  634. {
  635. const Il2CppDebuggerMetadataRegistration* debuggerMetadata = method->klass->image->codeGenModule->debuggerMetadata;
  636. return debuggerMetadata->methodExecutionContextInfoStrings[index];
  637. }
  638. const Il2CppMethodScope* Debugger::GetLocalScope(const MethodInfo* method, int32_t index)
  639. {
  640. const Il2CppDebuggerMetadataRegistration* debuggerMetadata = method->klass->image->codeGenModule->debuggerMetadata;
  641. return &debuggerMetadata->methodScopes[index];
  642. }
  643. void Debugger::GetMethodExecutionContextInfo(const MethodInfo* method, uint32_t* executionContextInfoCount, const Il2CppMethodExecutionContextInfo **executionContextInfo, const Il2CppMethodHeaderInfo **headerInfo, const Il2CppMethodScope **scopes)
  644. {
  645. if (il2cpp::vm::Method::IsInflated(method))
  646. method = il2cpp::vm::MetadataCache::GetGenericMethodDefinition(method);
  647. const Il2CppDebuggerMetadataRegistration* debuggerMetadata = method->klass->image->codeGenModule->debuggerMetadata;
  648. Il2CppMethodExecutionContextInfoIndex *index = &debuggerMetadata->methodExecutionContextInfoIndexes[GetTokenRowId(method->token) - 1];
  649. if (index->count != -1)
  650. {
  651. *executionContextInfoCount = index->count;
  652. *executionContextInfo = &debuggerMetadata->methodExecutionContextInfos[index->startIndex];
  653. }
  654. else
  655. {
  656. *executionContextInfoCount = 0;
  657. *executionContextInfo = NULL;
  658. }
  659. *headerInfo = &debuggerMetadata->methodHeaderInfos[GetTokenRowId(method->token) - 1];
  660. *scopes = &debuggerMetadata->methodScopes[(*headerInfo)->startScope];
  661. }
  662. void Debugger::GetStackFrames(void* context)
  663. {
  664. il2cpp::vm::StackFrames* stackFrames = static_cast<il2cpp::vm::StackFrames*>(context);
  665. Il2CppThreadUnwindState* unwindState = GetThreadStatePointer();
  666. if (unwindState == NULL)
  667. return; // There might not be any managed code executing yet.
  668. for (uint32_t i = 0; i < unwindState->frameCount; ++i)
  669. {
  670. const MethodInfo* method = unwindState->executionContexts[i]->method;
  671. if (method != NULL)
  672. {
  673. Il2CppStackFrameInfo frameInfo = { 0 };
  674. frameInfo.method = method;
  675. if (unwindState->executionContexts[i]->currentSequencePoint != NULL)
  676. {
  677. const Il2CppDebuggerMetadataRegistration* debuggerMetadata = method->klass->image->codeGenModule->debuggerMetadata;
  678. if (debuggerMetadata != NULL)
  679. {
  680. int32_t sourceFileIndex = unwindState->executionContexts[i]->currentSequencePoint->sourceFileIndex;
  681. frameInfo.filePath = debuggerMetadata->sequencePointSourceFiles[sourceFileIndex].file;
  682. frameInfo.sourceCodeLineNumber = unwindState->executionContexts[i]->currentSequencePoint->lineStart;
  683. frameInfo.ilOffset = unwindState->executionContexts[i]->currentSequencePoint->ilOffset;
  684. }
  685. }
  686. stackFrames->push_back(frameInfo);
  687. }
  688. }
  689. }
  690. void Debugger::AcquireLoaderLock()
  691. {
  692. s_Il2CppMonoLoaderLock.Lock();
  693. s_Il2CppMonoLoaderLockThreadId = os::Thread::CurrentThreadId();
  694. }
  695. void Debugger::ReleaseLoaderLock()
  696. {
  697. s_Il2CppMonoLoaderLockThreadId = 0;
  698. s_Il2CppMonoLoaderLock.Unlock();
  699. }
  700. bool Debugger::LoaderLockIsOwnedByThisThread()
  701. {
  702. return s_Il2CppMonoLoaderLockThreadId == os::Thread::CurrentThreadId();
  703. }
  704. Il2CppMonoInterpCallbacks* Debugger::GetInterpCallbacks()
  705. {
  706. return &s_InterpCallbacks;
  707. }
  708. void Debugger::RuntimeShutdownEnd()
  709. {
  710. unity_debugger_agent_runtime_shutdown();
  711. }
  712. void Debugger::ThreadStarted(uintptr_t tid)
  713. {
  714. unity_debugger_agent_thread_startup(tid);
  715. }
  716. void Debugger::ThreadStopped(uintptr_t tid)
  717. {
  718. unity_debugger_agent_thread_end(tid);
  719. }
  720. }
  721. }
  722. #else
  723. #include "Debugger.h"
  724. #include "os/Debug.h"
  725. namespace il2cpp
  726. {
  727. namespace utils
  728. {
  729. bool Debugger::GetIsDebuggerAttached()
  730. {
  731. return os::Debug::IsDebuggerPresent();
  732. }
  733. }
  734. }
  735. #endif // IL2CPP_MONO_DEBUGGER