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
|
//---------------------------------------------------------------------
// This file is part of the CLR Managed Debugger (mdbg) Sample.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------------
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Runtime.InteropServices;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Samples.Debugging.CorDebug;
using Microsoft.Samples.Debugging.CorMetadata.NativeApi;
using Microsoft.Samples.Debugging.CorDebug.NativeApi;
namespace Microsoft.Samples.Debugging.CorMetadata
{
public sealed class CorMetadataImport
{
public static Dictionary<CorElementType, Type> CoreTypes = new Dictionary<CorElementType, Type> ();
static CorMetadataImport ( )
{
CoreTypes.Add (CorElementType.ELEMENT_TYPE_BOOLEAN, typeof (bool));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_CHAR, typeof (char));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_I1, typeof (sbyte));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_U1, typeof (byte));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_I2, typeof (short));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_U2, typeof (ushort));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_I4, typeof (int));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_U4, typeof (uint));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_I8, typeof (long));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_U8, typeof (ulong));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_R4, typeof (float));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_R8, typeof (double));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_STRING, typeof (string));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_I, typeof (IntPtr));
CoreTypes.Add (CorElementType.ELEMENT_TYPE_U, typeof (UIntPtr));
}
public CorMetadataImport(CorModule managedModule)
{
// GUID Copied from Cor.h
Guid IID_IMetadataImport = new Guid("7DAC8207-D3AE-4c75-9B67-92801A497D44");
m_importer = (IMetadataImport) managedModule.GetMetaDataInterface(IID_IMetadataImport);
Debug.Assert(m_importer != null);
}
public CorMetadataImport(object metadataImport)
{
m_importer = (IMetadataImport) metadataImport;
Debug.Assert(m_importer != null);
}
// methods
public MethodInfo GetMethodInfo(int methodToken)
{
return new MetadataMethodInfo(m_importer,methodToken);
}
public Type GetType(int typeToken)
{
return new MetadataType(m_importer,typeToken);
}
// Get number of generic parameters on a given type.
// Eg, for 'Foo<t, u>', returns 2.
// for 'Foo', returns 0.
public int CountGenericParams(int typeToken)
{
// <strip>@TODO resolve if GetMetaDataInterface should throw or return null.</strip>
// This may fail in pre V2.0 debuggees.
//Guid IID_IMetadataImport2 = new Guid("FCE5EFA0-8BBA-4f8e-A036-8F2022B08466");
if( ! (m_importer is IMetadataImport2) )
return 0; // this means we're pre v2.0 debugees.
IMetadataImport2 importer2 = (m_importer as IMetadataImport2);
Debug.Assert( importer2!=null );
int dummy;
uint dummy2;
IntPtr hEnum = IntPtr.Zero;
int count;
importer2.EnumGenericParams(ref hEnum, typeToken, out dummy, 1, out dummy2);
try
{
m_importer.CountEnum(hEnum, out count);
}
finally
{
if( hEnum != IntPtr.Zero )
importer2.CloseEnum(hEnum);
}
return count;
}
// Returns filename of scope, if available.
// Returns null if filename is not available.
public string GetScopeFilename()
{
int size;
try
{
Guid mvid;
m_importer.GetScopeProps(null, 0, out size, out mvid);
StringBuilder sb = new StringBuilder(size);
m_importer.GetScopeProps(sb, sb.Capacity, out size, out mvid);
sb.Length = size;
return sb.ToString();
}
catch
{
return null;
}
}
public string GetUserString(int token)
{
int size;
m_importer.GetUserString(token,null,0,out size);
StringBuilder sb = new StringBuilder(size);
m_importer.GetUserString(token,sb,sb.Capacity,out size);
sb.Length=size;
return sb.ToString();
}
public const int TokenNotFound = -1;
public const int TokenGlobalNamespace = 0;
// returns a type token from name
// when the function fails, we return token TokenNotFound value.
public int GetTypeTokenFromName(string name)
{
int token = CorMetadataImport.TokenNotFound;
if( name.Length==0 )
// this is special global type (we'll return token 0)
token = CorMetadataImport.TokenGlobalNamespace;
else
{
try
{
m_importer.FindTypeDefByName(name,0,out token);
}
catch(COMException e)
{
token=CorMetadataImport.TokenNotFound;
if((HResult)e.ErrorCode==HResult.CLDB_E_RECORD_NOTFOUND)
{
int i = name.LastIndexOf('.');
if(i>0)
{
int parentToken = GetTypeTokenFromName(name.Substring(0,i));
if( parentToken!=CorMetadataImport.TokenNotFound )
{
try
{
m_importer.FindTypeDefByName(name.Substring(i+1),parentToken,out token);
}
catch(COMException e2)
{
token=CorMetadataImport.TokenNotFound;
if((HResult)e2.ErrorCode!=HResult.CLDB_E_RECORD_NOTFOUND)
throw;
}
}
}
}
else
throw;
}
}
return token;
}
public string GetTypeNameFromRef(int token)
{
int resScope,size;
m_importer.GetTypeRefProps(token,out resScope,null,0,out size);
StringBuilder sb = new StringBuilder(size);
m_importer.GetTypeRefProps(token,out resScope,sb,sb.Capacity,out size);
return sb.ToString();
}
public string GetTypeNameFromDef(int token,out int extendsToken)
{
int size;
TypeAttributes pdwTypeDefFlags;
m_importer.GetTypeDefProps(token,null,0,out size,
out pdwTypeDefFlags,out extendsToken);
StringBuilder sb = new StringBuilder(size);
m_importer.GetTypeDefProps(token,sb,sb.Capacity,out size,
out pdwTypeDefFlags,out extendsToken);
return sb.ToString();
}
public string GetMemberRefName(int token)
{
if(!m_importer.IsValidToken((uint)token))
throw new ArgumentException();
uint size;
int classToken;
IntPtr ppvSigBlob;
int pbSig;
m_importer.GetMemberRefProps((uint)token,
out classToken,
null,
0,
out size,
out ppvSigBlob,
out pbSig
);
StringBuilder member = new StringBuilder((int)size);
m_importer.GetMemberRefProps((uint)token,
out classToken,
member,
member.Capacity,
out size,
out ppvSigBlob,
out pbSig
);
string className=null;
switch(TokenUtils.TypeFromToken(classToken))
{
default:
Debug.Assert(false);
break;
case CorTokenType.mdtTypeRef:
className = GetTypeNameFromRef(classToken);
break;
case CorTokenType.mdtTypeDef:
{
int parentToken;
className = GetTypeNameFromDef(classToken,out parentToken);
break;
}
}
return className + "." + member.ToString();
}
public IEnumerable DefinedTypes
{
get
{
return new TypeDefEnum(this);
}
}
#if USEOLDSYMREADER
public IntPtr GetRawInterface()
{
#if MDBG_FAKE_COM
System.Guid iid = new System.Guid( "7DAC8207-D3AE-4c75-9B67-92801A497D44");
return ((Microsoft.ClrTools.GenFakeCom.IFakeComWrapper)m_importer).GetIfacePtr( iid );
#else
return Marshal.GetComInterfaceForObject(m_importer,typeof(IMetadataImport));
#endif
}
#else
public object RawCOMObject
{
get
{
return m_importer;
}
}
#endif
// properties
//////////////////////////////////////////////////////////////////////////////////
//
// CorMetadataImport variables
//
//////////////////////////////////////////////////////////////////////////////////
internal IMetadataImport m_importer;
}
//////////////////////////////////////////////////////////////////////////////////
//
// MetadataMethodInfo
//
//////////////////////////////////////////////////////////////////////////////////
public sealed class MetadataMethodInfo : MethodInfo
{
internal MetadataMethodInfo(IMetadataImport importer,int methodToken)
{
if(!importer.IsValidToken((uint)methodToken))
throw new ArgumentException();
m_importer = importer;
m_methodToken=methodToken;
int size;
uint pdwAttr;
IntPtr ppvSigBlob;
uint pulCodeRVA,pdwImplFlags;
uint pcbSigBlob;
m_importer.GetMethodProps((uint)methodToken,
out m_classToken,
null,
0,
out size,
out pdwAttr,
out ppvSigBlob,
out pcbSigBlob,
out pulCodeRVA,
out pdwImplFlags);
StringBuilder szMethodName = new StringBuilder(size);
m_importer.GetMethodProps((uint)methodToken,
out m_classToken,
szMethodName,
szMethodName.Capacity,
out size,
out pdwAttr,
out ppvSigBlob,
out pcbSigBlob,
out pulCodeRVA,
out pdwImplFlags);
CorCallingConvention callingConv = MetadataHelperFunctions.CorSigUncompressCallingConv (ref ppvSigBlob);
m_name = szMethodName.ToString ();
m_methodAttributes = (MethodAttributes)pdwAttr;
}
public override Type ReturnType
{
get
{
throw new NotImplementedException();
}
}
public override ICustomAttributeProvider ReturnTypeCustomAttributes
{
get
{
throw new NotImplementedException();
}
}
public override Type ReflectedType
{
get
{
throw new NotImplementedException();
}
}
public override Type DeclaringType
{
get
{
if(TokenUtils.IsNullToken(m_classToken))
return null; // this is method outside of class
return new MetadataType(m_importer,m_classToken);
}
}
public override string Name
{
get
{
return m_name;
}
}
public override MethodAttributes Attributes
{
get
{
return m_methodAttributes;
}
}
public override RuntimeMethodHandle MethodHandle
{
get
{
throw new NotImplementedException();
}
}
public override MethodInfo GetBaseDefinition()
{
throw new NotImplementedException();
}
public override bool IsDefined (Type attributeType, bool inherit)
{
throw new NotImplementedException();
}
public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
throw new NotImplementedException();
}
public override object[] GetCustomAttributes(bool inherit)
{
throw new NotImplementedException();
}
public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
{
throw new NotImplementedException();
}
public override System.Reflection.MethodImplAttributes GetMethodImplementationFlags()
{
throw new NotImplementedException();
}
public override System.Reflection.ParameterInfo[] GetParameters()
{
ArrayList al = new ArrayList();
IntPtr hEnum = new IntPtr();
try
{
while(true)
{
uint count;
int paramToken;
m_importer.EnumParams(ref hEnum,
m_methodToken, out paramToken,1,out count);
if(count!=1)
break;
MetadataParameterInfo mp = new MetadataParameterInfo (m_importer, paramToken,
this, DeclaringType);
if (mp.Name != string.Empty)
al.Add(mp);
}
}
finally
{
m_importer.CloseEnum(hEnum);
}
return (ParameterInfo[]) al.ToArray(typeof(ParameterInfo));
}
public override int MetadataToken
{
get
{
return m_methodToken;
}
}
public string[] GetGenericArgumentNames()
{
return MetadataHelperFunctions.GetGenericArgumentNames(m_importer,m_methodToken);
}
private IMetadataImport m_importer;
private string m_name;
private int m_classToken;
private int m_methodToken;
private MethodAttributes m_methodAttributes;
}
public enum MetadataTokenType
{
Module = 0x00000000,
TypeRef = 0x01000000,
TypeDef = 0x02000000,
FieldDef = 0x04000000,
MethodDef = 0x06000000,
ParamDef = 0x08000000,
InterfaceImpl = 0x09000000,
MemberRef = 0x0a000000,
CustomAttribute = 0x0c000000,
Permission = 0x0e000000,
Signature = 0x11000000,
Event = 0x14000000,
Property = 0x17000000,
ModuleRef = 0x1a000000,
TypeSpec = 0x1b000000,
Assembly = 0x20000000,
AssemblyRef = 0x23000000,
File = 0x26000000,
ExportedType = 0x27000000,
ManifestResource = 0x28000000,
GenericPar = 0x2a000000,
MethodSpec = 0x2b000000,
String = 0x70000000,
Name = 0x71000000,
BaseType = 0x72000000,
Invalid = 0x7FFFFFFF,
}
public enum CorCallingConvention
{
Default = 0x0,
VarArg = 0x5,
Field = 0x6,
LocalSig = 0x7,
Property = 0x8,
Unmanaged = 0x9,
GenericInst = 0xa, // generic method instantiation
NativeVarArg = 0xb, // used ONLY for 64bit vararg PInvoke calls
// The high bits of the calling convention convey additional info
Mask = 0x0f, // Calling convention is bottom 4 bits
HasThis = 0x20, // Top bit indicates a 'this' parameter
ExplicitThis = 0x40, // This parameter is explicitly in the signature
Generic = 0x10, // Generic method sig with explicit number of type arguments (precedes ordinary parameter count)
};
// <STRIP>
// 11/6/04 JKeljo:
// </STRIP>
// Struct isn't complete yet; just here for the IsTokenOfType method
// TODO: Re-do metadata token usage throughout to use this class
public struct MetadataToken
{
public MetadataToken(int value)
{
this.value = value;
}
public int Value
{
get
{
return value;
}
}
public MetadataTokenType Type
{
get
{
return (MetadataTokenType)(value & 0xFF000000);
}
}
public int Index
{
get
{
return value & 0x00FFFFFF;
}
}
public static implicit operator int(MetadataToken token) { return token.value; }
public static bool operator==(MetadataToken v1, MetadataToken v2) { return (v1.value == v2.value);}
public static bool operator!=(MetadataToken v1, MetadataToken v2) { return !(v1 == v2);}
public static bool IsTokenOfType(int token, params MetadataTokenType[] types)
{
for (int i = 0; i < types.Length; i++)
{
if ((int)(token & 0xFF000000) == (int)types[i])
return true;
}
return false;
}
public bool IsOfType(params MetadataTokenType[] types) { return IsTokenOfType(Value, types); }
public override bool Equals(object other)
{
if (other is MetadataToken)
{
MetadataToken oToken = (MetadataToken)other;
return (value == oToken.value);
}
return false;
}
public override int GetHashCode() { return value.GetHashCode();}
private int value;
}
static class MetadataHelperFunctions
{
private static uint TokenFromRid(uint rid, uint tktype) {return (rid) | (tktype);}
// The below have been translated manually from the inline C++ helpers in cor.h
// <STRIP>
// JKeljo 11/11/04: The only ones that have been tested at this point are
// CorSigUncompressCallingConv, CorSigUncompressToken, and CorSigUncompressElementType
// </STRIP>
internal static uint CorSigUncompressBigData(
ref IntPtr pData) // [IN,OUT] compressed data
{
unsafe
{
byte *pBytes = (byte*)pData;
uint res;
// 1 byte data is handled in CorSigUncompressData
// Debug.Assert(*pBytes & 0x80);
// Medium.
if ((*pBytes & 0xC0) == 0x80) // 10?? ????
{
res = (uint)((*pBytes++ & 0x3f) << 8);
res |= *pBytes++;
}
else // 110? ????
{
res = (uint)(*pBytes++ & 0x1f) << 24;
res |= (uint)(*pBytes++) << 16;
res |= (uint)(*pBytes++) << 8;
res |= (uint)(*pBytes++);
}
pData = (IntPtr)pBytes;
return res;
}
}
internal static uint CorSigUncompressData(
ref IntPtr pData) // [IN,OUT] compressed data
{
unsafe
{
byte *pBytes = (byte*)pData;
// Handle smallest data inline.
if ((*pBytes & 0x80) == 0x00) // 0??? ????
{
uint retval = (uint)(*pBytes++);
pData = (IntPtr)pBytes;
return retval;
}
return CorSigUncompressBigData(ref pData);
}
}
// Function translated directly from cor.h but never tested; included here in case someone wants to use it in future
/* internal static uint CorSigUncompressData( // return number of bytes of that compressed data occupied in pData
IntPtr pData, // [IN] compressed data
out uint pDataOut) // [OUT] the expanded *pData
{
unsafe
{
uint cb = 0xffffffff;
byte *pBytes = (byte*)(pData);
pDataOut = 0;
// Smallest.
if ((*pBytes & 0x80) == 0x00) // 0??? ????
{
pDataOut = *pBytes;
cb = 1;
}
// Medium.
else if ((*pBytes & 0xC0) == 0x80) // 10?? ????
{
pDataOut = (uint)(((*pBytes & 0x3f) << 8 | *(pBytes+1)));
cb = 2;
}
else if ((*pBytes & 0xE0) == 0xC0) // 110? ????
{
pDataOut = (uint)(((*pBytes & 0x1f) << 24 | *(pBytes+1) << 16 | *(pBytes+2) << 8 | *(pBytes+3)));
cb = 4;
}
return cb;
}
}*/
static uint[] g_tkCorEncodeToken ={(uint)MetadataTokenType.TypeDef, (uint)MetadataTokenType.TypeRef, (uint)MetadataTokenType.TypeSpec, (uint)MetadataTokenType.BaseType};
// uncompress a token
internal static uint CorSigUncompressToken( // return the token.
ref IntPtr pData) // [IN,OUT] compressed data
{
uint tk;
uint tkType;
tk = CorSigUncompressData(ref pData);
tkType = g_tkCorEncodeToken[tk & 0x3];
tk = TokenFromRid(tk >> 2, tkType);
return tk;
}
// Function translated directly from cor.h but never tested; included here in case someone wants to use it in future
/* internal static uint CorSigUncompressToken( // return number of bytes of that compressed data occupied in pData
IntPtr pData, // [IN] compressed data
out uint pToken) // [OUT] the expanded *pData
{
uint cb;
uint tk;
uint tkType;
cb = CorSigUncompressData(pData, out tk);
tkType = g_tkCorEncodeToken[tk & 0x3];
tk = TokenFromRid(tk >> 2, tkType);
pToken = tk;
return cb;
}*/
internal static CorCallingConvention CorSigUncompressCallingConv(
ref IntPtr pData) // [IN,OUT] compressed data
{
unsafe
{
byte *pBytes = (byte*) pData;
CorCallingConvention retval = (CorCallingConvention)(*pBytes++);
pData = (IntPtr)pBytes;
return retval;
}
}
// Function translated directly from cor.h but never tested; included here in case someone wants to use it in future
/* private enum SignMasks : uint {
ONEBYTE = 0xffffffc0, // Mask the same size as the missing bits.
TWOBYTE = 0xffffe000, // Mask the same size as the missing bits.
FOURBYTE = 0xf0000000, // Mask the same size as the missing bits.
};
// uncompress a signed integer
internal static uint CorSigUncompressSignedInt( // return number of bytes of that compressed data occupied in pData
IntPtr pData, // [IN] compressed data
out int pInt) // [OUT] the expanded *pInt
{
uint cb;
uint ulSigned;
uint iData;
cb = CorSigUncompressData(pData, out iData);
pInt = 0;
if (cb == 0xffffffff) return cb;
ulSigned = iData & 0x1;
iData = iData >> 1;
if (ulSigned != 0)
{
if (cb == 1)
{
iData |= (uint)SignMasks.ONEBYTE;
}
else if (cb == 2)
{
iData |= (uint)SignMasks.TWOBYTE;
}
else
{
iData |= (uint)SignMasks.FOURBYTE;
}
}
pInt = (int)iData;
return cb;
}*/
// uncompress encoded element type
internal static CorElementType CorSigUncompressElementType(//Element type
ref IntPtr pData) // [IN,OUT] compressed data
{
unsafe
{
byte *pBytes = (byte*)pData;
CorElementType retval = (CorElementType)(*pBytes++);
pData = (IntPtr)pBytes;
return retval;
}
}
// Function translated directly from cor.h but never tested; included here in case someone wants to use it in future
/* internal static uint CorSigUncompressElementType(// return number of bytes of that compressed data occupied in pData
IntPtr pData, // [IN] compressed data
out CorElementType pElementType) // [OUT] the expanded *pData
{
unsafe
{
byte *pBytes = (byte*)pData;
pElementType = (CorElementType)(*pBytes & 0x7f);
return 1;
}
}*/
static internal string[] GetGenericArgumentNames(IMetadataImport importer,
int typeOrMethodToken)
{
IMetadataImport2 importer2 = (importer as IMetadataImport2);
if(importer2 == null)
return new string[0]; // this means we're pre v2.0 debugees.
Debug.Assert( importer2!=null );
string[] genargs = null;
IntPtr hEnum = IntPtr.Zero;
try{
int i=0;
do{
uint nOut;
int genTypeToken;
importer2.EnumGenericParams(ref hEnum, typeOrMethodToken,
out genTypeToken, 1, out nOut);
if( genargs==null )
{
int count;
importer.CountEnum(hEnum, out count);
genargs = new string[count];
}
if( nOut==0 )
break;
Debug.Assert( nOut==1 );
if( nOut==1 )
{
uint genIndex;
int genFlags, ptkOwner, ptkKind;
ulong genArgNameSize;
importer2.GetGenericParamProps(genTypeToken,
out genIndex,
out genFlags,
out ptkOwner,
out ptkKind,
null,
0,
out genArgNameSize);
StringBuilder genArgName = new StringBuilder((int)genArgNameSize);
importer2.GetGenericParamProps(genTypeToken,
out genIndex,
out genFlags,
out ptkOwner,
out ptkKind,
genArgName,
(ulong)genArgName.Capacity,
out genArgNameSize);
genargs[i] = genArgName.ToString();
}
++i;
} while( i<genargs.Length );
}
finally
{
if( hEnum != IntPtr.Zero )
importer2.CloseEnum(hEnum);
}
return genargs;
}
static object[] emptyAttributes = new object[0];
static internal object[] GetDebugAttributes (IMetadataImport importer, int token)
{
ArrayList attributes = new ArrayList ();
object attr = MetadataHelperFunctions.GetCustomAttribute (importer, token, typeof (System.Diagnostics.DebuggerTypeProxyAttribute));
if (attr != null)
attributes.Add (attr);
attr = MetadataHelperFunctions.GetCustomAttribute (importer, token, typeof (System.Diagnostics.DebuggerDisplayAttribute));
if (attr != null)
attributes.Add (attr);
attr = MetadataHelperFunctions.GetCustomAttribute (importer, token, typeof (System.Diagnostics.DebuggerBrowsableAttribute));
if (attr != null)
attributes.Add (attr);
attr = MetadataHelperFunctions.GetCustomAttribute (importer, token, typeof (System.Runtime.CompilerServices.CompilerGeneratedAttribute));
if (attr != null)
attributes.Add (attr);
if (attributes.Count == 0)
return emptyAttributes;
else
return attributes.ToArray ();
}
static internal object GetCustomAttribute (IMetadataImport importer, int token, Type type)
{
uint sigSize = 0;
IntPtr ppvSig = IntPtr.Zero;
int hr = importer.GetCustomAttributeByName (token, type.FullName, out ppvSig, out sigSize);
if (hr != 0)
return null;
byte[] data = new byte[sigSize];
Marshal.Copy (ppvSig, data, 0, (int)sigSize);
BinaryReader br = new BinaryReader (new MemoryStream (data));
// Prolog
if (br.ReadUInt16 () != 1)
throw new InvalidOperationException ("Incorrect attribute prolog");
ConstructorInfo ctor = type.GetConstructors ()[0];
ParameterInfo[] pars = ctor.GetParameters ();
object[] args = new object[pars.Length];
// Fixed args
for (int n=0; n<pars.Length; n++)
args [n] = ReadValue (br, pars[n].ParameterType);
object ob = Activator.CreateInstance (type, args);
// Named args
uint nargs = br.ReadUInt16 ();
for (; nargs > 0; nargs--) {
byte fieldOrProp = br.ReadByte ();
byte atype = br.ReadByte ();
// Boxed primitive
if (atype == 0x51)
atype = br.ReadByte ();
CorElementType et = (CorElementType) atype;
string pname = br.ReadString ();
object val = ReadValue (br, CorMetadataImport.CoreTypes[et]);
if (fieldOrProp == 0x53) {
FieldInfo fi = type.GetField (pname);
fi.SetValue (ob, val);
}
else {
PropertyInfo pi = type.GetProperty (pname);
pi.SetValue (ob, val, null);
}
}
return ob;
}
static object ReadValue (BinaryReader br, Type type)
{
if (type.IsEnum) {
object ob = ReadValue (br, Enum.GetUnderlyingType (type));
return Enum.ToObject (type, Convert.ToInt64 (ob));
}
if (type == typeof (string) || type == typeof(Type))
return br.ReadString ();
if (type == typeof (int))
return br.ReadInt32 ();
throw new InvalidOperationException ("Can't parse value of type: " + type);
}
}
} // namspace Microsoft.Debugger.MetadataWrapper
|