-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathgdbjit.cpp
More file actions
3650 lines (3061 loc) · 101 KB
/
gdbjit.cpp
File metadata and controls
3650 lines (3061 loc) · 101 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
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//*****************************************************************************
// File: gdbjit.cpp
//
//
// NotifyGdb implementation.
//
//*****************************************************************************
#include "common.h"
#include "formattype.h"
#include "gdbjit.h"
#include "gdbjithelpers.h"
thread_local bool tls_isSymReaderInProgress = false;
#ifdef _DEBUG
static void DumpElf(const char* methodName, const char *addr, size_t size)
{
char dump[1024] = { 0, };
strcat(dump, methodName);
strcat(dump, ".o");
FILE *f = fopen(dump, "wb");
fwrite(addr, sizeof(char), size, f);
fclose(f);
}
#endif
TypeInfoBase*
GetTypeInfoFromTypeHandle(TypeHandle typeHandle,
NotifyGdb::PTK_TypeInfoMap pTypeMap,
FunctionMemberPtrArrayHolder &method)
{
TypeInfoBase *foundTypeInfo = nullptr;
TypeKey key = typeHandle.GetTypeKey();
PTR_MethodTable pMT = typeHandle.GetMethodTable();
if (pTypeMap->Lookup(&key, &foundTypeInfo))
{
return foundTypeInfo;
}
CorElementType corType = typeHandle.GetSignatureCorElementType();
switch (corType)
{
case ELEMENT_TYPE_I1:
case ELEMENT_TYPE_U1:
case ELEMENT_TYPE_CHAR:
case ELEMENT_TYPE_VOID:
case ELEMENT_TYPE_BOOLEAN:
case ELEMENT_TYPE_I2:
case ELEMENT_TYPE_U2:
case ELEMENT_TYPE_I4:
case ELEMENT_TYPE_U4:
case ELEMENT_TYPE_I8:
case ELEMENT_TYPE_U8:
case ELEMENT_TYPE_R4:
case ELEMENT_TYPE_R8:
case ELEMENT_TYPE_U:
case ELEMENT_TYPE_I:
{
NewHolder<PrimitiveTypeInfo> typeInfo = new PrimitiveTypeInfo(typeHandle);
pTypeMap->Add(typeInfo->GetTypeKey(), typeInfo);
typeInfo.SuppressRelease();
return typeInfo;
}
case ELEMENT_TYPE_VALUETYPE:
case ELEMENT_TYPE_CLASS:
{
ApproxFieldDescIterator fieldDescIterator(pMT,
pMT->IsString() ? ApproxFieldDescIterator::INSTANCE_FIELDS : ApproxFieldDescIterator::ALL_FIELDS);
ULONG cFields = fieldDescIterator.Count();
NewHolder<ClassTypeInfo> typeInfo = new ClassTypeInfo(typeHandle, cFields, method);
NewHolder<RefTypeInfo> refTypeInfo = nullptr;
if (!typeHandle.IsValueType())
{
refTypeInfo = new NamedRefTypeInfo(typeHandle, typeInfo);
typeInfo.SuppressRelease();
pTypeMap->Add(refTypeInfo->GetTypeKey(), refTypeInfo);
refTypeInfo.SuppressRelease();
}
else
{
pTypeMap->Add(typeInfo->GetTypeKey(), typeInfo);
typeInfo.SuppressRelease();
}
//
// Now fill in the array
//
FieldDesc *pField;
for (ULONG i = 0; i < cFields; i++)
{
pField = fieldDescIterator.Next();
LPCUTF8 szName = pField->GetName();
typeInfo->members[i].m_member_name = new char[strlen(szName) + 1];
strcpy(typeInfo->members[i].m_member_name, szName);
if (!pField->IsStatic())
{
typeInfo->members[i].m_member_offset = (ULONG)pField->GetOffset();
if (!typeHandle.IsValueType())
typeInfo->members[i].m_member_offset += Object::GetOffsetOfFirstField();
}
else
{
PTR_BYTE base = 0;
MethodTable* pMT = pField->GetEnclosingMethodTable();
base = pField->GetBase();
// TODO: add support of generics with static fields
if (pField->IsRVA() || !pMT->IsDynamicStatics())
{
PTR_VOID pAddress = pField->GetStaticAddressHandle((PTR_VOID)dac_cast<TADDR>(base));
typeInfo->members[i].m_static_member_address = dac_cast<TADDR>(pAddress);
}
}
typeInfo->members[i].m_member_type =
GetTypeInfoFromTypeHandle(pField->GetExactFieldType(typeHandle), pTypeMap, method);
// handle the System.String case:
// coerce type of the second field into array type
if (pMT->IsString() && i == 1)
{
TypeInfoBase* elemTypeInfo = typeInfo->members[1].m_member_type;
typeInfo->m_array_type = new ArrayTypeInfo(typeHandle.MakeSZArray(), 1, elemTypeInfo);
typeInfo->members[1].m_member_type = typeInfo->m_array_type;
}
}
// Ignore inheritance from System.Object and System.ValueType classes.
if (!typeHandle.IsValueType() &&
pMT->GetParentMethodTable() && pMT->GetParentMethodTable()->GetParentMethodTable())
{
typeInfo->m_parent = GetTypeInfoFromTypeHandle(typeHandle.GetParent(), pTypeMap, method);
}
if (refTypeInfo)
return refTypeInfo;
else
return typeInfo;
}
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_BYREF:
{
TypeInfoBase* valTypeInfo = GetTypeInfoFromTypeHandle(typeHandle.GetTypeParam(), pTypeMap, method);
NewHolder<RefTypeInfo> typeInfo = new RefTypeInfo(typeHandle, valTypeInfo);
typeInfo->m_type_offset = valTypeInfo->m_type_offset;
pTypeMap->Add(typeInfo->GetTypeKey(), typeInfo);
typeInfo.SuppressRelease();
return typeInfo;
}
case ELEMENT_TYPE_ARRAY:
case ELEMENT_TYPE_SZARRAY:
{
NewHolder<ClassTypeInfo> info = new ClassTypeInfo(typeHandle, pMT->GetRank() == 1 ? 2 : 3, method);
NewHolder<RefTypeInfo> refTypeInfo = new NamedRefTypeInfo(typeHandle, info);
info.SuppressRelease();
pTypeMap->Add(refTypeInfo->GetTypeKey(), refTypeInfo);
refTypeInfo.SuppressRelease();
TypeInfoBase* lengthTypeInfo = GetTypeInfoFromTypeHandle(
TypeHandle(CoreLibBinder::GetElementType(ELEMENT_TYPE_I4)), pTypeMap, method);
TypeInfoBase* valTypeInfo = GetTypeInfoFromTypeHandle(typeHandle.GetArrayElementTypeHandle(), pTypeMap, method);
info->m_array_type = new ArrayTypeInfo(typeHandle, 1, valTypeInfo);
info->members[0].m_member_name = new char[16];
strcpy(info->members[0].m_member_name, "m_NumComponents");
info->members[0].m_member_offset = ArrayBase::GetOffsetOfNumComponents();
info->members[0].m_member_type = lengthTypeInfo;
info->members[1].m_member_name = new char[7];
strcpy(info->members[1].m_member_name, "m_Data");
info->members[1].m_member_offset = ArrayBase::GetDataPtrOffset(pMT);
info->members[1].m_member_type = info->m_array_type;
if (pMT->GetRank() != 1)
{
TypeHandle dwordArray(CoreLibBinder::GetElementType(ELEMENT_TYPE_I4));
info->m_array_bounds_type = new ArrayTypeInfo(dwordArray.MakeSZArray(), pMT->GetRank(), lengthTypeInfo);
info->members[2].m_member_name = new char[9];
strcpy(info->members[2].m_member_name, "m_Bounds");
info->members[2].m_member_offset = ArrayBase::GetBoundsOffset(pMT);
info->members[2].m_member_type = info->m_array_bounds_type;
}
return refTypeInfo;
}
default:
COMPlusThrowHR(COR_E_NOTSUPPORTED);
}
}
TypeInfoBase* GetArgTypeInfo(MethodDesc* methodDescPtr,
NotifyGdb::PTK_TypeInfoMap pTypeMap,
unsigned ilIndex,
FunctionMemberPtrArrayHolder &method)
{
MetaSig sig(methodDescPtr);
TypeHandle th;
if (ilIndex == 0)
{
th = sig.GetRetTypeHandleNT();
}
else
{
while (--ilIndex)
sig.SkipArg();
sig.NextArg();
th = sig.GetLastTypeHandleNT();
}
return GetTypeInfoFromTypeHandle(th, pTypeMap, method);
}
TypeInfoBase* GetLocalTypeInfo(MethodDesc *methodDescPtr,
NotifyGdb::PTK_TypeInfoMap pTypeMap,
unsigned ilIndex,
FunctionMemberPtrArrayHolder &funcs)
{
COR_ILMETHOD_DECODER method(methodDescPtr->GetILHeader());
if (method.GetLocalVarSigTok())
{
DWORD cbSigLen;
PCCOR_SIGNATURE pComSig;
if (FAILED(methodDescPtr->GetMDImport()->GetSigFromToken(method.GetLocalVarSigTok(), &cbSigLen, &pComSig)))
{
minipal_log_print_error("\nInvalid record");
return nullptr;
}
_ASSERTE(*pComSig == IMAGE_CEE_CS_CALLCONV_LOCAL_SIG);
SigTypeContext typeContext(methodDescPtr, TypeHandle());
MetaSig sig(pComSig, cbSigLen, methodDescPtr->GetModule(), &typeContext, MetaSig::sigLocalVars);
if (ilIndex > 0)
{
while (ilIndex--)
sig.SkipArg();
}
sig.NextArg();
TypeHandle th = sig.GetLastTypeHandleNT();
return GetTypeInfoFromTypeHandle(th, pTypeMap, funcs);
}
return nullptr;
}
HRESULT GetArgNameByILIndex(MethodDesc* methodDescPtr, unsigned index, NewArrayHolder<char> ¶mName)
{
IMDInternalImport* mdImport = methodDescPtr->GetMDImport();
mdParamDef paramToken;
USHORT seq;
DWORD attr;
HRESULT status;
// Param indexing is 1-based.
ULONG32 mdIndex = index + 1;
MetaSig sig(methodDescPtr);
if (sig.HasThis())
{
mdIndex--;
}
status = mdImport->FindParamOfMethod(methodDescPtr->GetMemberDef(), mdIndex, ¶mToken);
if (status == S_OK)
{
LPCSTR name;
status = mdImport->GetParamDefProps(paramToken, &seq, &attr, &name);
paramName = new char[strlen(name) + 1];
strcpy(paramName, name);
}
return status;
}
// Copy-pasted from src/debug/di/module.cpp
HRESULT FindNativeInfoInILVariable(DWORD dwIndex,
SIZE_T ip,
ICorDebugInfo::NativeVarInfo* nativeInfoList,
unsigned int nativeInfoCount,
ICorDebugInfo::NativeVarInfo** ppNativeInfo)
{
_ASSERTE(ppNativeInfo != NULL);
*ppNativeInfo = NULL;
int lastGoodOne = -1;
for (unsigned int i = 0; i < (unsigned)nativeInfoCount; i++)
{
if (nativeInfoList[i].varNumber == dwIndex)
{
if ((lastGoodOne == -1) || (nativeInfoList[lastGoodOne].startOffset < nativeInfoList[i].startOffset))
{
lastGoodOne = i;
}
if ((nativeInfoList[i].startOffset <= ip) &&
(nativeInfoList[i].endOffset > ip))
{
*ppNativeInfo = &(nativeInfoList[i]);
return S_OK;
}
}
}
if ((lastGoodOne > -1) && (nativeInfoList[lastGoodOne].endOffset == ip))
{
*ppNativeInfo = &(nativeInfoList[lastGoodOne]);
return S_OK;
}
return CORDBG_E_IL_VAR_NOT_AVAILABLE;
}
BYTE* DebugInfoStoreNew(void * pData, size_t cBytes)
{
return new BYTE[cBytes];
}
/* Get IL to native offsets map */
HRESULT
GetMethodNativeMap(MethodDesc* methodDesc,
ULONG32* numMap,
NewArrayHolder<DebuggerILToNativeMap> &map,
ULONG32* pcVars,
ICorDebugInfo::NativeVarInfo** ppVars)
{
// Use the DebugInfoStore to get IL->Native maps.
// It doesn't matter whether we're jitted, ngenned etc.
DebugInfoRequest request;
TADDR nativeCodeStartAddr = PCODEToPINSTR(methodDesc->GetNativeCode());
request.InitFromStartingAddr(methodDesc, nativeCodeStartAddr);
// Bounds info.
ULONG32 countMapCopy;
NewHolder<ICorDebugInfo::OffsetMapping> mapCopy(NULL);
BOOL success = DebugInfoManager::GetBoundariesAndVars(request,
DebugInfoStoreNew,
NULL, // allocator
&countMapCopy,
&mapCopy,
pcVars,
ppVars);
if (!success)
{
return E_FAIL;
}
// Need to convert map formats.
*numMap = countMapCopy;
map = new DebuggerILToNativeMap[countMapCopy];
ULONG32 i;
for (i = 0; i < *numMap; i++)
{
map[i].ilOffset = mapCopy[i].ilOffset;
map[i].nativeStartOffset = mapCopy[i].nativeOffset;
if (i > 0)
{
map[i - 1].nativeEndOffset = map[i].nativeStartOffset;
}
map[i].source = mapCopy[i].source;
}
if (*numMap >= 1)
{
map[i - 1].nativeEndOffset = 0;
}
return S_OK;
}
HRESULT FunctionMember::GetLocalsDebugInfo(NotifyGdb::PTK_TypeInfoMap pTypeMap,
LocalsInfo& locals,
int startNativeOffset,
FunctionMemberPtrArrayHolder &method)
{
ICorDebugInfo::NativeVarInfo* nativeVar = NULL;
int thisOffs = 0;
if (!md->IsStatic())
{
thisOffs = 1;
}
int i;
for (i = 0; i < m_num_args - thisOffs; i++)
{
if (FindNativeInfoInILVariable(i + thisOffs, startNativeOffset, locals.vars, locals.countVars, &nativeVar) == S_OK)
{
vars[i + thisOffs].m_var_type = GetArgTypeInfo(md, pTypeMap, i + 1, method);
GetArgNameByILIndex(md, i + thisOffs, vars[i + thisOffs].m_var_name);
vars[i + thisOffs].m_il_index = i;
vars[i + thisOffs].m_native_offset = nativeVar->loc.vlStk.vlsOffset;
vars[i + thisOffs].m_var_abbrev = 6;
}
}
//Add info about 'this' as first argument
if (thisOffs == 1)
{
if (FindNativeInfoInILVariable(0, startNativeOffset, locals.vars, locals.countVars, &nativeVar) == S_OK)
{
TypeHandle th = TypeHandle(md->GetMethodTable());
if (th.IsValueType())
th = th.MakePointer();
vars[0].m_var_type = GetTypeInfoFromTypeHandle(th, pTypeMap, method);
vars[0].m_var_name = new char[strlen("this") + 1];
strcpy(vars[0].m_var_name, "this");
vars[0].m_il_index = 0;
vars[0].m_native_offset = nativeVar->loc.vlStk.vlsOffset;
vars[0].m_var_abbrev = 13;
}
i++;
}
for (; i < m_num_vars; i++)
{
if (FindNativeInfoInILVariable(
i, startNativeOffset, locals.vars, locals.countVars, &nativeVar) == S_OK)
{
int ilIndex = i - m_num_args;
vars[i].m_var_type = GetLocalTypeInfo(md, pTypeMap, ilIndex, method);
vars[i].m_var_name = new char[strlen(locals.localsName[ilIndex]) + 1];
strcpy(vars[i].m_var_name, locals.localsName[ilIndex]);
vars[i].m_il_index = ilIndex;
vars[i].m_native_offset = nativeVar->loc.vlStk.vlsOffset;
vars[i].m_var_abbrev = 5;
TADDR nativeStart;
TADDR nativeEnd;
int ilLen = locals.localsScope[ilIndex].ilEndOffset - locals.localsScope[ilIndex].ilStartOffset;
if (GetBlockInNativeCode(locals.localsScope[ilIndex].ilStartOffset, ilLen, &nativeStart, &nativeEnd))
{
vars[i].m_low_pc = md->GetNativeCode() + nativeStart;
vars[i].m_high_pc = nativeEnd - nativeStart;
}
}
}
return S_OK;
}
MethodDebugInfo::MethodDebugInfo(int numPoints, int numLocals)
{
points = (SequencePointInfo*) CoTaskMemAlloc(sizeof(SequencePointInfo) * numPoints);
if (points == nullptr)
{
COMPlusThrowOM();
}
memset(points, 0, sizeof(SequencePointInfo) * numPoints);
size = numPoints;
if (numLocals == 0)
{
locals = nullptr;
localsSize = 0;
return;
}
locals = (LocalVarInfo*) CoTaskMemAlloc(sizeof(LocalVarInfo) * numLocals);
if (locals == nullptr)
{
CoTaskMemFree(points);
COMPlusThrowOM();
}
memset(locals, 0, sizeof(LocalVarInfo) * numLocals);
localsSize = numLocals;
}
MethodDebugInfo::~MethodDebugInfo()
{
if (locals)
{
for (int i = 0; i < localsSize; i++)
CoTaskMemFree(locals[i].name);
CoTaskMemFree(locals);
}
for (int i = 0; i < size; i++)
CoTaskMemFree(points[i].fileName);
CoTaskMemFree(points);
}
/* Get mapping of IL offsets to source line numbers */
HRESULT
GetDebugInfoFromPDB(MethodDesc* methodDescPtr,
NewArrayHolder<SymbolsInfo> &symInfo,
unsigned int &symInfoLen,
LocalsInfo &locals)
{
// Guard against re-entrancy
static thread_local int t_gdbJitDebugInfoCallbackDepth = 0;
NewArrayHolder<DebuggerILToNativeMap> map;
ULONG32 numMap;
if (!getInfoForMethodDelegate)
return E_FAIL;
if (t_gdbJitDebugInfoCallbackDepth != 0)
return E_FAIL;
if (GetMethodNativeMap(methodDescPtr, &numMap, map, &locals.countVars, &locals.vars) != S_OK)
return E_FAIL;
const Module* mod = methodDescPtr->GetMethodTable()->GetModule();
SString modName { mod->GetPEAssembly()->GetPath() };
if (modName.IsEmpty())
return E_FAIL;
const char* szModName = modName.GetUTF8();
MethodDebugInfo methodDebugInfo(numMap, locals.countVars);
t_gdbJitDebugInfoCallbackDepth = 1;
if (getInfoForMethodDelegate(szModName, methodDescPtr->GetMemberDef(), &methodDebugInfo) == 0)
{
t_gdbJitDebugInfoCallbackDepth = 0;
return E_FAIL;
}
t_gdbJitDebugInfoCallbackDepth = 0;
symInfoLen = numMap;
symInfo = new SymbolsInfo[numMap];
// Only consume locals if both pointer and size are valid.
locals.size = (methodDebugInfo.locals != nullptr && methodDebugInfo.localsSize > 0) ? methodDebugInfo.localsSize : 0;
locals.localsName = new NewArrayHolder<char>[locals.size];
locals.localsScope = new LocalsInfo::Scope [locals.size];
for (int i = 0; i < locals.size; i++)
{
if (methodDebugInfo.locals[i].name == nullptr)
{
locals.localsName[i] = nullptr;
locals.localsScope[i].ilStartOffset = 0;
locals.localsScope[i].ilEndOffset = 0;
continue;
}
size_t sizeRequired = WideCharToMultiByte(CP_UTF8, 0, methodDebugInfo.locals[i].name, -1, NULL, 0, NULL, NULL);
locals.localsName[i] = new char[sizeRequired];
int len = WideCharToMultiByte(
CP_UTF8, 0, methodDebugInfo.locals[i].name, -1, locals.localsName[i], sizeRequired, NULL, NULL);
locals.localsScope[i].ilStartOffset = methodDebugInfo.locals[i].startOffset;
locals.localsScope[i].ilEndOffset = methodDebugInfo.locals[i].endOffset;
}
for (ULONG32 j = 0; j < numMap; j++)
{
SymbolsInfo& s = symInfo[j];
if (j == 0) {
s.fileName[0] = 0;
s.lineNumber = 0;
s.fileIndex = 0;
} else {
s = symInfo[j - 1];
}
s.nativeOffset = map[j].nativeStartOffset;
s.ilOffset = map[j].ilOffset;
s.source = map[j].source;
s.lineNumber = 0;
for (int i = 0; i < methodDebugInfo.size; i++)
{
const SequencePointInfo& sp = methodDebugInfo.points[i];
if ((ULONG)(methodDebugInfo.points[i].ilOffset) == map[j].ilOffset)
{
s.fileIndex = 0;
int len = WideCharToMultiByte(CP_UTF8, 0, sp.fileName, -1, s.fileName, sizeof(s.fileName), NULL, NULL);
s.fileName[len] = 0;
s.lineNumber = sp.lineNumber;
break;
}
}
}
return S_OK;
}
/* LEB128 for 32-bit unsigned integer */
int Leb128Encode(uint32_t num, char* buf, int size)
{
int i = 0;
do
{
uint8_t byte = num & 0x7F;
if (i >= size)
break;
num >>= 7;
if (num != 0)
byte |= 0x80;
buf[i++] = byte;
}
while (num != 0);
return i;
}
/* LEB128 for 32-bit signed integer */
int Leb128Encode(int32_t num, char* buf, int size)
{
int i = 0;
bool hasMore = true, isNegative = num < 0;
while (hasMore && i < size)
{
uint8_t byte = num & 0x7F;
num >>= 7;
if ((num == 0 && (byte & 0x40) == 0) || (num == -1 && (byte & 0x40) == 0x40))
hasMore = false;
else
byte |= 0x80;
buf[i++] = byte;
}
return i;
}
int GetFrameLocation(int nativeOffset, char* bufVarLoc)
{
char cnvBuf[16] = {0};
int len = Leb128Encode(static_cast<int32_t>(nativeOffset), cnvBuf, sizeof(cnvBuf));
bufVarLoc[0] = len + 1;
bufVarLoc[1] = DW_OP_fbreg;
for (int j = 0; j < len; j++)
{
bufVarLoc[j + 2] = cnvBuf[j];
}
return len + 2; // We add '2' because first 2 bytes contain length of expression and DW_OP_fbreg operation.
}
// GDB JIT interface
typedef enum
{
JIT_NOACTION = 0,
JIT_REGISTER_FN,
JIT_UNREGISTER_FN
} jit_actions_t;
struct jit_code_entry
{
struct jit_code_entry *next_entry;
struct jit_code_entry *prev_entry;
const char *symfile_addr;
UINT64 symfile_size;
};
struct jit_descriptor
{
UINT32 version;
/* This type should be jit_actions_t, but we use uint32_t
to be explicit about the bitwidth. */
UINT32 action_flag;
struct jit_code_entry *relevant_entry;
struct jit_code_entry *first_entry;
};
// GDB puts a breakpoint in this function.
// To prevent from inlining we add noinline attribute and inline assembler statement.
extern "C"
void __attribute__((noinline)) __jit_debug_register_code() { __asm__(""); };
/* Make sure to specify the version statically, because the
debugger may check the version before we can set it. */
struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
static CrstStatic g_jitDescriptorCrst;
// END of GDB JIT interface
class DebugStringsCU
{
public:
DebugStringsCU(const char *module, const char *path)
: m_producerName("CoreCLR"),
m_moduleName(module),
m_moduleDir(path),
m_producerOffset(0),
m_moduleNameOffset(0),
m_moduleDirOffset(0)
{
}
int GetProducerOffset() const { return m_producerOffset; }
int GetModuleNameOffset() const { return m_moduleNameOffset; }
int GetModuleDirOffset() const { return m_moduleDirOffset; }
void DumpStrings(char *ptr, int &offset)
{
m_producerOffset = offset;
DumpString(m_producerName, ptr, offset);
m_moduleNameOffset = offset;
DumpString(m_moduleName, ptr, offset);
m_moduleDirOffset = offset;
DumpString(m_moduleDir, ptr, offset);
}
private:
const char* m_producerName;
const char* m_moduleName;
const char* m_moduleDir;
int m_producerOffset;
int m_moduleNameOffset;
int m_moduleDirOffset;
static void DumpString(const char *str, char *ptr, int &offset)
{
if (ptr != nullptr)
{
strcpy(ptr + offset, str);
}
offset += strlen(str) + 1;
}
};
/* Static data for .debug_abbrev */
const unsigned char AbbrevTable[] = {
1, DW_TAG_compile_unit, DW_CHILDREN_yes,
DW_AT_producer, DW_FORM_strp, DW_AT_language, DW_FORM_data2, DW_AT_name, DW_FORM_strp, DW_AT_comp_dir, DW_FORM_strp,
DW_AT_stmt_list, DW_FORM_sec_offset, 0, 0,
2, DW_TAG_base_type, DW_CHILDREN_no,
DW_AT_name, DW_FORM_strp, DW_AT_encoding, DW_FORM_data1, DW_AT_byte_size, DW_FORM_data1, 0, 0,
3, DW_TAG_typedef, DW_CHILDREN_no, DW_AT_name, DW_FORM_strp,
DW_AT_type, DW_FORM_ref4, 0, 0,
4, DW_TAG_subprogram, DW_CHILDREN_yes,
DW_AT_name, DW_FORM_strp, DW_AT_linkage_name, DW_FORM_strp, DW_AT_decl_file, DW_FORM_data1, DW_AT_decl_line, DW_FORM_data1,
DW_AT_type, DW_FORM_ref4, DW_AT_external, DW_FORM_flag_present,
DW_AT_low_pc, DW_FORM_addr, DW_AT_high_pc, DW_FORM_size,
DW_AT_frame_base, DW_FORM_exprloc, 0, 0,
5, DW_TAG_variable, DW_CHILDREN_no,
DW_AT_name, DW_FORM_strp, DW_AT_decl_file, DW_FORM_data1, DW_AT_decl_line, DW_FORM_data1, DW_AT_type,
DW_FORM_ref4, DW_AT_location, DW_FORM_exprloc, 0, 0,
6, DW_TAG_formal_parameter, DW_CHILDREN_no,
DW_AT_name, DW_FORM_strp, DW_AT_decl_file, DW_FORM_data1, DW_AT_decl_line, DW_FORM_data1, DW_AT_type,
DW_FORM_ref4, DW_AT_location, DW_FORM_exprloc, 0, 0,
7, DW_TAG_class_type, DW_CHILDREN_yes,
DW_AT_name, DW_FORM_strp, DW_AT_byte_size, DW_FORM_data4, 0, 0,
8, DW_TAG_member, DW_CHILDREN_no,
DW_AT_name, DW_FORM_strp, DW_AT_type, DW_FORM_ref4, DW_AT_data_member_location, DW_FORM_data4, 0, 0,
9, DW_TAG_pointer_type, DW_CHILDREN_no,
DW_AT_type, DW_FORM_ref4, DW_AT_byte_size, DW_FORM_data1, 0, 0,
10, DW_TAG_array_type, DW_CHILDREN_yes,
DW_AT_type, DW_FORM_ref4, 0, 0,
11, DW_TAG_subrange_type, DW_CHILDREN_no,
DW_AT_upper_bound, DW_FORM_exprloc, 0, 0,
12, DW_TAG_subprogram, DW_CHILDREN_yes,
DW_AT_name, DW_FORM_strp, DW_AT_linkage_name, DW_FORM_strp, DW_AT_decl_file, DW_FORM_data1, DW_AT_decl_line, DW_FORM_data1,
DW_AT_type, DW_FORM_ref4, DW_AT_external, DW_FORM_flag_present,
DW_AT_low_pc, DW_FORM_addr, DW_AT_high_pc, DW_FORM_size,
DW_AT_frame_base, DW_FORM_exprloc, DW_AT_object_pointer, DW_FORM_ref4, 0, 0,
13, DW_TAG_formal_parameter, DW_CHILDREN_no,
DW_AT_name, DW_FORM_strp, DW_AT_decl_file, DW_FORM_data1, DW_AT_decl_line, DW_FORM_data1, DW_AT_type,
DW_FORM_ref4, DW_AT_location, DW_FORM_exprloc, DW_AT_artificial, DW_FORM_flag_present, 0, 0,
14, DW_TAG_member, DW_CHILDREN_no,
DW_AT_name, DW_FORM_strp, DW_AT_type, DW_FORM_ref4, DW_AT_external, DW_FORM_flag_present, 0, 0,
15, DW_TAG_variable, DW_CHILDREN_no, DW_AT_specification, DW_FORM_ref4, DW_AT_location, DW_FORM_exprloc,
0, 0,
16, DW_TAG_try_block, DW_CHILDREN_no,
DW_AT_low_pc, DW_FORM_addr, DW_AT_high_pc, DW_FORM_size,
0, 0,
17, DW_TAG_catch_block, DW_CHILDREN_no,
DW_AT_low_pc, DW_FORM_addr, DW_AT_high_pc, DW_FORM_size,
0, 0,
18, DW_TAG_inheritance, DW_CHILDREN_no, DW_AT_type, DW_FORM_ref4, DW_AT_data_member_location, DW_FORM_data1,
0, 0,
19, DW_TAG_subrange_type, DW_CHILDREN_no,
DW_AT_upper_bound, DW_FORM_udata, 0, 0,
20, DW_TAG_lexical_block, DW_CHILDREN_yes,
DW_AT_low_pc, DW_FORM_addr, DW_AT_high_pc, DW_FORM_size,
0, 0,
0
};
const int AbbrevTableSize = sizeof(AbbrevTable);
/* Static data for .debug_line, including header */
#define DWARF_LINE_BASE (-5)
#define DWARF_LINE_RANGE 14
#define DWARF_OPCODE_BASE 13
#ifdef FEATURE_GDBJIT_LANGID_CS
/* TODO: use corresponding constant when it will be added to llvm */
#define DW_LANG_MICROSOFT_CSHARP 0x9e57
#endif
DwarfLineNumHeader LineNumHeader = {
0, 2, 0, 1, 1, DWARF_LINE_BASE, DWARF_LINE_RANGE, DWARF_OPCODE_BASE, {0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1}
};
/* Static data for .debug_info */
struct __attribute__((packed)) DebugInfoCU
{
uint8_t m_cu_abbrev;
uint32_t m_prod_off;
uint16_t m_lang;
uint32_t m_cu_name;
uint32_t m_cu_dir;
uint32_t m_line_num;
} debugInfoCU = {
#ifdef FEATURE_GDBJIT_LANGID_CS
1, 0, DW_LANG_MICROSOFT_CSHARP, 0, 0
#else
1, 0, DW_LANG_C89, 0, 0
#endif
};
struct __attribute__((packed)) DebugInfoTryCatchSub
{
uint8_t m_sub_abbrev;
uintptr_t m_sub_low_pc, m_sub_high_pc;
};
struct __attribute__((packed)) DebugInfoSub
{
uint8_t m_sub_abbrev;
uint32_t m_sub_name;
uint32_t m_linkage_name;
uint8_t m_file, m_line;
uint32_t m_sub_type;
uintptr_t m_sub_low_pc, m_sub_high_pc;
uint8_t m_sub_loc[2];
};
struct __attribute__((packed)) DebugInfoSubMember
{
DebugInfoSub sub;
uint32_t m_obj_ptr;
};
struct __attribute__((packed)) DebugInfoLexicalBlock
{
uint8_t m_abbrev;
uintptr_t m_low_pc, m_high_pc;
};
// Holder for array of pointers to FunctionMember objects
class FunctionMemberPtrArrayHolder : public NewArrayHolder<NewHolder<FunctionMember>>
{
private:
int m_cElements;
public:
explicit FunctionMemberPtrArrayHolder(int cElements) :
NewArrayHolder<NewHolder<FunctionMember>>(new NewHolder<FunctionMember>[cElements]),
m_cElements(cElements)
{
}
int GetCount() const
{
return m_cElements;
}
};
struct __attribute__((packed)) DebugInfoType
{
uint8_t m_type_abbrev;
uint32_t m_type_name;
uint8_t m_encoding;
uint8_t m_byte_size;
};
struct __attribute__((packed)) DebugInfoVar
{
uint8_t m_var_abbrev;
uint32_t m_var_name;
uint8_t m_var_file, m_var_line;
uint32_t m_var_type;
};
struct __attribute__((packed)) DebugInfoTypeDef
{
uint8_t m_typedef_abbrev;
uint32_t m_typedef_name;
uint32_t m_typedef_type;
};
struct __attribute__((packed)) DebugInfoClassType
{
uint8_t m_type_abbrev;
uint32_t m_type_name;
uint32_t m_byte_size;
};
struct __attribute__((packed)) DebugInfoInheritance
{
uint8_t m_abbrev;
uint32_t m_type;
uint8_t m_data_member_location;
};
struct __attribute__((packed)) DebugInfoClassMember
{
uint8_t m_member_abbrev;
uint32_t m_member_name;
uint32_t m_member_type;
};
struct __attribute__((packed)) DebugInfoStaticMember
{
uint8_t m_member_abbrev;
uint32_t m_member_specification;
};
struct __attribute__((packed)) DebugInfoRefType
{
uint8_t m_type_abbrev;
uint32_t m_ref_type;
uint8_t m_byte_size;
};
struct __attribute__((packed)) DebugInfoArrayType
{
uint8_t m_abbrev;
uint32_t m_type;
};
void TypeInfoBase::DumpStrings(char* ptr, int& offset)
{
if (ptr != nullptr)
{
strcpy(ptr + offset, m_type_name);
m_type_name_offset = offset;
}
offset += strlen(m_type_name) + 1;
}
void TypeInfoBase::CalculateName()
{
// name the type
SString sName;
const TypeString::FormatFlags formatFlags = static_cast<TypeString::FormatFlags>(
TypeString::FormatNamespace |
TypeString::FormatAngleBrackets);
TypeString::AppendType(sName, typeHandle, formatFlags);
const UTF8 *utf8 = sName.GetUTF8();
if (typeHandle.IsValueType())
{
m_type_name = new char[strlen(utf8) + 1];
strcpy(m_type_name, utf8);
}
else
{
m_type_name = new char[strlen(utf8) + 1 + 2];
strcpy(m_type_name, "__");
strcpy(m_type_name + 2, utf8);
}
// Fix nested names
for (char *p = m_type_name; *p; ++p)
{
if (*p == '+')
*p = '.';
}
}
void TypeInfoBase::SetTypeHandle(TypeHandle handle)
{