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