Debugger.cpp 29 KB

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