forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugStore.cs
More file actions
1158 lines (970 loc) · 43.3 KB
/
DebugStore.cs
File metadata and controls
1158 lines (970 loc) · 43.3 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.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Reflection.PortableExecutable;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using Microsoft.CodeAnalysis.Debugging;
using System.IO.Compression;
using System.Reflection;
using System.Collections.Immutable;
namespace Microsoft.WebAssembly.Diagnostics
{
internal static class PortableCustomDebugInfoKinds
{
public static readonly Guid AsyncMethodSteppingInformationBlob = new Guid("54FD2AC5-E925-401A-9C2A-F94F171072F8");
public static readonly Guid StateMachineHoistedLocalScopes = new Guid("6DA9A61E-F8C7-4874-BE62-68BC5630DF71");
public static readonly Guid DynamicLocalVariables = new Guid("83C563C4-B4F3-47D5-B824-BA5441477EA8");
public static readonly Guid TupleElementNames = new Guid("ED9FDF71-8879-4747-8ED3-FE5EDE3CE710");
public static readonly Guid DefaultNamespace = new Guid("58b2eab6-209f-4e4e-a22c-b2d0f910c782");
public static readonly Guid EncLocalSlotMap = new Guid("755F52A8-91C5-45BE-B4B8-209571E552BD");
public static readonly Guid EncLambdaAndClosureMap = new Guid("A643004C-0240-496F-A783-30D64F4979DE");
public static readonly Guid SourceLink = new Guid("CC110556-A091-4D38-9FEC-25AB9A351A6A");
public static readonly Guid EmbeddedSource = new Guid("0E8A571B-6926-466E-B4AD-8AB04611F5FE");
public static readonly Guid CompilationMetadataReferences = new Guid("7E4D4708-096E-4C5C-AEDA-CB10BA6A740D");
public static readonly Guid CompilationOptions = new Guid("B5FEEC05-8CD0-4A83-96DA-466284BB4BD8");
}
internal static class HashKinds
{
public static readonly Guid SHA1 = new Guid("ff1816ec-aa5e-4d10-87f7-6f4963833460");
public static readonly Guid SHA256 = new Guid("8829d00f-11b8-4213-878b-770e8597ac16");
}
internal class BreakpointRequest
{
public string Id { get; private set; }
public string Assembly { get; private set; }
public string File { get; private set; }
public int Line { get; private set; }
public int Column { get; private set; }
public string Condition { get; private set; }
public MethodInfo Method { get; set; }
private JObject request;
public bool IsResolved => Assembly != null;
public List<Breakpoint> Locations { get; set; } = new List<Breakpoint>();
public override string ToString() => $"BreakpointRequest Assembly: {Assembly} File: {File} Line: {Line} Column: {Column}";
public object AsSetBreakpointByUrlResponse(IEnumerable<object> jsloc) => new { breakpointId = Id, locations = Locations.Select(l => l.Location.AsLocation()).Concat(jsloc) };
public BreakpointRequest()
{ }
public BreakpointRequest(string id, MethodInfo method)
{
Id = id;
Method = method;
}
public BreakpointRequest(string id, JObject request)
{
Id = id;
this.request = request;
Condition = request?["condition"]?.Value<string>();
}
public static BreakpointRequest Parse(string id, JObject args)
{
return new BreakpointRequest(id, args);
}
public BreakpointRequest Clone() => new BreakpointRequest { Id = Id, request = request };
public bool IsMatch(SourceFile sourceFile)
{
string url = request?["url"]?.Value<string>();
if (url == null)
{
string urlRegex = request?["urlRegex"].Value<string>();
var regex = new Regex(urlRegex);
return regex.IsMatch(sourceFile.Url.ToString()) || regex.IsMatch(sourceFile.DocUrl);
}
return sourceFile.Url.ToString() == url || sourceFile.DotNetUrl == url;
}
public bool TryResolve(SourceFile sourceFile)
{
if (!IsMatch(sourceFile))
return false;
int? line = request?["lineNumber"]?.Value<int>();
int? column = request?["columnNumber"]?.Value<int>();
if (line == null || column == null)
return false;
Assembly = sourceFile.AssemblyName;
File = sourceFile.DebuggerFileName;
Line = line.Value;
Column = column.Value;
return true;
}
public bool TryResolve(DebugStore store)
{
if (request == null || store == null)
return false;
return store.AllSources().FirstOrDefault(source => TryResolve(source)) != null;
}
}
internal class VarInfo
{
public VarInfo(LocalVariable v, MetadataReader pdbReader)
{
this.Name = pdbReader.GetString(v.Name);
this.Index = v.Index;
}
public VarInfo(Parameter p, MetadataReader pdbReader)
{
this.Name = pdbReader.GetString(p.Name);
this.Index = (p.SequenceNumber) * -1;
}
public string Name { get; }
public int Index { get; }
public override string ToString() => $"(var-info [{Index}] '{Name}')";
}
internal class IlLocation
{
public IlLocation(MethodInfo method, int offset)
{
Method = method;
Offset = offset;
}
public MethodInfo Method { get; }
public int Offset { get; }
}
internal class SourceLocation
{
private SourceId id;
private int line;
private int column;
private IlLocation ilLocation;
public SourceLocation(SourceId id, int line, int column)
{
this.id = id;
this.line = line;
this.column = column;
}
public SourceLocation(MethodInfo mi, SequencePoint sp)
{
this.id = mi.SourceId;
this.line = sp.StartLine - 1;
this.column = sp.StartColumn - 1;
this.ilLocation = new IlLocation(mi, sp.Offset);
}
public SourceId Id { get => id; }
public int Line { get => line; }
public int Column { get => column; }
public IlLocation IlLocation => this.ilLocation;
public override string ToString() => $"{id}:{Line}:{Column}";
public static SourceLocation Parse(JObject obj)
{
if (obj == null)
return null;
if (!SourceId.TryParse(obj["scriptId"]?.Value<string>(), out SourceId id))
return null;
int? line = obj["lineNumber"]?.Value<int>();
int? column = obj["columnNumber"]?.Value<int>();
if (id == null || line == null || column == null)
return null;
return new SourceLocation(id, line.Value, column.Value);
}
internal class LocationComparer : EqualityComparer<SourceLocation>
{
public override bool Equals(SourceLocation l1, SourceLocation l2)
{
if (l1 == null && l2 == null)
return true;
else if (l1 == null || l2 == null)
return false;
return (l1.Line == l2.Line &&
l1.Column == l2.Column &&
l1.Id == l2.Id);
}
public override int GetHashCode(SourceLocation loc)
{
int hCode = loc.Line ^ loc.Column;
return loc.Id.GetHashCode() ^ hCode.GetHashCode();
}
}
internal object AsLocation() => new
{
scriptId = id.ToString(),
lineNumber = line,
columnNumber = column
};
}
internal class SourceId
{
private const string Scheme = "dotnet://";
private readonly int assembly, document;
public int Assembly => assembly;
public int Document => document;
internal SourceId(int assembly, int document)
{
this.assembly = assembly;
this.document = document;
}
public SourceId(string id)
{
if (!TryParse(id, out assembly, out document))
throw new ArgumentException("invalid source identifier", nameof(id));
}
public static bool TryParse(string id, out SourceId source)
{
source = null;
if (!TryParse(id, out int assembly, out int document))
return false;
source = new SourceId(assembly, document);
return true;
}
private static bool TryParse(string id, out int assembly, out int document)
{
assembly = document = 0;
if (id == null || !id.StartsWith(Scheme, StringComparison.Ordinal))
return false;
string[] sp = id.Substring(Scheme.Length).Split('_');
if (sp.Length != 2)
return false;
if (!int.TryParse(sp[0], out assembly))
return false;
if (!int.TryParse(sp[1], out document))
return false;
return true;
}
public override string ToString() => $"{Scheme}{assembly}_{document}";
public override bool Equals(object obj)
{
if (obj == null)
return false;
SourceId that = obj as SourceId;
return that.assembly == this.assembly && that.document == this.document;
}
public override int GetHashCode() => assembly.GetHashCode() ^ document.GetHashCode();
public static bool operator ==(SourceId a, SourceId b) => a is null ? b is null : a.Equals(b);
public static bool operator !=(SourceId a, SourceId b) => !a.Equals(b);
}
internal class MethodInfo
{
private MethodDefinition methodDef;
private SourceFile source;
public SourceId SourceId => source.SourceId;
public string Name { get; }
public MethodDebugInformation DebugInformation;
public MethodDefinitionHandle methodDefHandle;
private MetadataReader pdbMetadataReader;
public SourceLocation StartLocation { get; set; }
public SourceLocation EndLocation { get; set; }
public AssemblyInfo Assembly { get; }
public int Token { get; }
internal bool IsEnCMethod;
internal LocalScopeHandleCollection localScopes;
public bool IsStatic() => (methodDef.Attributes & MethodAttributes.Static) != 0;
public int IsAsync { get; set; }
public bool IsHiddenFromDebugger { get; }
public TypeInfo TypeInfo { get; }
public MethodInfo(AssemblyInfo assembly, MethodDefinitionHandle methodDefHandle, int token, SourceFile source, TypeInfo type, MetadataReader asmMetadataReader, MetadataReader pdbMetadataReader)
{
this.IsAsync = -1;
this.Assembly = assembly;
this.methodDef = asmMetadataReader.GetMethodDefinition(methodDefHandle);
this.DebugInformation = pdbMetadataReader.GetMethodDebugInformation(methodDefHandle.ToDebugInformationHandle());
this.source = source;
this.Token = token;
this.methodDefHandle = methodDefHandle;
this.Name = asmMetadataReader.GetString(methodDef.Name);
this.pdbMetadataReader = pdbMetadataReader;
this.IsEnCMethod = false;
this.TypeInfo = type;
if (!DebugInformation.SequencePointsBlob.IsNil)
{
var sps = DebugInformation.GetSequencePoints();
SequencePoint start = sps.First();
SequencePoint end = sps.First();
foreach (SequencePoint sp in sps)
{
if (sp.StartLine < start.StartLine)
start = sp;
else if (sp.StartLine == start.StartLine && sp.StartColumn < start.StartColumn)
start = sp;
if (sp.EndLine > end.EndLine)
end = sp;
else if (sp.EndLine == end.EndLine && sp.EndColumn > end.EndColumn)
end = sp;
}
StartLocation = new SourceLocation(this, start);
EndLocation = new SourceLocation(this, end);
foreach (var cattr in methodDef.GetCustomAttributes())
{
var ctorHandle = asmMetadataReader.GetCustomAttribute(cattr).Constructor;
if (ctorHandle.Kind == HandleKind.MemberReference)
{
var container = asmMetadataReader.GetMemberReference((MemberReferenceHandle)ctorHandle).Parent;
var name = asmMetadataReader.GetString(asmMetadataReader.GetTypeReference((TypeReferenceHandle)container).Name);
if (name == "DebuggerHiddenAttribute")
{
this.IsHiddenFromDebugger = true;
break;
}
}
}
}
localScopes = pdbMetadataReader.GetLocalScopes(methodDefHandle);
}
public void UpdateEnC(MetadataReader asmMetadataReader, MetadataReader pdbMetadataReaderParm, int method_idx)
{
this.DebugInformation = pdbMetadataReaderParm.GetMethodDebugInformation(MetadataTokens.MethodDebugInformationHandle(method_idx));
this.pdbMetadataReader = pdbMetadataReaderParm;
this.IsEnCMethod = true;
if (!DebugInformation.SequencePointsBlob.IsNil)
{
var sps = DebugInformation.GetSequencePoints();
SequencePoint start = sps.First();
SequencePoint end = sps.First();
foreach (SequencePoint sp in sps)
{
if (sp.StartLine < start.StartLine)
start = sp;
else if (sp.StartLine == start.StartLine && sp.StartColumn < start.StartColumn)
start = sp;
if (sp.EndLine > end.EndLine)
end = sp;
else if (sp.EndLine == end.EndLine && sp.EndColumn > end.EndColumn)
end = sp;
}
StartLocation = new SourceLocation(this, start);
EndLocation = new SourceLocation(this, end);
}
localScopes = pdbMetadataReader.GetLocalScopes(MetadataTokens.MethodDefinitionHandle(method_idx));
}
public SourceLocation GetLocationByIl(int pos)
{
SequencePoint? prev = null;
if (!DebugInformation.SequencePointsBlob.IsNil) {
foreach (SequencePoint sp in DebugInformation.GetSequencePoints())
{
if (sp.Offset > pos)
{
//get the earlier line number if the offset is in a hidden sequence point and has a earlier line number available
// if is doesn't continue and get the next line number that is not in a hidden sequence point
if (sp.IsHidden && prev == null)
continue;
break;
}
if (!sp.IsHidden)
prev = sp;
}
if (prev.HasValue)
return new SourceLocation(this, prev.Value);
}
return null;
}
public VarInfo[] GetLiveVarsAt(int offset)
{
var res = new List<VarInfo>();
foreach (var parameterHandle in methodDef.GetParameters())
{
var parameter = Assembly.asmMetadataReader.GetParameter(parameterHandle);
res.Add(new VarInfo(parameter, Assembly.asmMetadataReader));
}
foreach (var localScopeHandle in localScopes)
{
var localScope = pdbMetadataReader.GetLocalScope(localScopeHandle);
if (localScope.StartOffset <= offset && localScope.EndOffset > offset)
{
var localVariables = localScope.GetLocalVariables();
foreach (var localVariableHandle in localVariables)
{
var localVariable = pdbMetadataReader.GetLocalVariable(localVariableHandle);
if (localVariable.Attributes != LocalVariableAttributes.DebuggerHidden)
res.Add(new VarInfo(localVariable, pdbMetadataReader));
}
}
}
return res.ToArray();
}
public override string ToString() => "MethodInfo(" + Name + ")";
}
internal class TypeInfo
{
internal AssemblyInfo assembly;
private TypeDefinition type;
private List<MethodInfo> methods;
internal int Token { get; }
internal string Namespace { get; }
public TypeInfo(AssemblyInfo assembly, TypeDefinitionHandle typeHandle, TypeDefinition type)
{
this.assembly = assembly;
var metadataReader = assembly.asmMetadataReader;
Token = MetadataTokens.GetToken(metadataReader, typeHandle);
this.type = type;
methods = new List<MethodInfo>();
Name = metadataReader.GetString(type.Name);
var declaringType = type;
while (declaringType.IsNested)
{
declaringType = metadataReader.GetTypeDefinition(declaringType.GetDeclaringType());
Name = metadataReader.GetString(declaringType.Name) + "." + Name;
}
Namespace = metadataReader.GetString(declaringType.Namespace);
if (Namespace.Length > 0)
FullName = Namespace + "." + Name;
else
FullName = Name;
}
public TypeInfo(AssemblyInfo assembly, string name)
{
Name = name;
FullName = name;
}
public string Name { get; }
public string FullName { get; }
public List<MethodInfo> Methods => methods;
public override string ToString() => "TypeInfo('" + FullName + "')";
}
internal class AssemblyInfo
{
private static int next_id;
private readonly int id;
private readonly ILogger logger;
private Dictionary<int, MethodInfo> methods = new Dictionary<int, MethodInfo>();
private Dictionary<string, string> sourceLinkMappings = new Dictionary<string, string>();
private readonly List<SourceFile> sources = new List<SourceFile>();
internal string Url { get; }
//The caller must keep the PEReader alive and undisposed throughout the lifetime of the metadata reader
internal PEReader peReader;
internal MetadataReader asmMetadataReader { get; }
internal MetadataReader pdbMetadataReader { get; set; }
internal List<MetadataReader> enCMetadataReader = new List<MetadataReader>();
public int DebugId { get; set; }
internal int PdbAge { get; }
internal System.Guid PdbGuid { get; }
internal string PdbName { get; }
internal bool PdbInformationAvailable { get; }
public bool TriedToLoadSymbolsOnDemand { get; set; }
public unsafe AssemblyInfo(MonoProxy monoProxy, SessionId sessionId, string url, byte[] assembly, byte[] pdb, CancellationToken token)
{
this.id = Interlocked.Increment(ref next_id);
using var asmStream = new MemoryStream(assembly);
peReader = new PEReader(asmStream);
var entries = peReader.ReadDebugDirectory();
if (entries.Length > 0)
{
var codeView = entries[0];
CodeViewDebugDirectoryData codeViewData = peReader.ReadCodeViewDebugDirectoryData(codeView);
PdbAge = codeViewData.Age;
PdbGuid = codeViewData.Guid;
PdbName = codeViewData.Path;
PdbInformationAvailable = true;
}
asmMetadataReader = PEReaderExtensions.GetMetadataReader(peReader);
var asmDef = asmMetadataReader.GetAssemblyDefinition();
Name = asmDef.GetAssemblyName().Name + ".dll";
if (pdb != null)
{
var pdbStream = new MemoryStream(pdb);
try
{
// MetadataReaderProvider.FromPortablePdbStream takes ownership of the stream
pdbMetadataReader = MetadataReaderProvider.FromPortablePdbStream(pdbStream).GetMetadataReader();
}
catch (BadImageFormatException)
{
monoProxy.SendLog(sessionId, $"Warning: Unable to read debug information of: {Name} (use DebugType=Portable/Embedded)", token);
}
}
else
{
var embeddedPdbEntry = entries.FirstOrDefault(e => e.Type == DebugDirectoryEntryType.EmbeddedPortablePdb);
if (embeddedPdbEntry.DataSize != 0)
{
pdbMetadataReader = peReader.ReadEmbeddedPortablePdbDebugDirectoryData(embeddedPdbEntry).GetMetadataReader();
}
}
Populate();
}
public bool EnC(byte[] meta, byte[] pdb)
{
var asmStream = new MemoryStream(meta);
MetadataReader asmMetadataReader = MetadataReaderProvider.FromMetadataStream(asmStream).GetMetadataReader();
var pdbStream = new MemoryStream(pdb);
MetadataReader pdbMetadataReader = MetadataReaderProvider.FromPortablePdbStream(pdbStream).GetMetadataReader();
enCMetadataReader.Add(asmMetadataReader);
enCMetadataReader.Add(pdbMetadataReader);
PopulateEnC(asmMetadataReader, pdbMetadataReader);
return true;
}
public AssemblyInfo(ILogger logger)
{
this.logger = logger;
}
private void PopulateEnC(MetadataReader asmMetadataReaderParm, MetadataReader pdbMetadataReaderParm)
{
int i = 1;
foreach (EntityHandle encMapHandle in asmMetadataReaderParm.GetEditAndContinueMapEntries())
{
if (encMapHandle.Kind == HandleKind.MethodDebugInformation)
{
var method = methods[asmMetadataReader.GetRowNumber(encMapHandle)];
method.UpdateEnC(asmMetadataReaderParm, pdbMetadataReaderParm, i);
i++;
}
}
}
private void Populate()
{
var d2s = new Dictionary<int, SourceFile>();
SourceFile FindSource(DocumentHandle doc, int rowid, string documentName)
{
if (d2s.TryGetValue(rowid, out SourceFile source))
return source;
var src = new SourceFile(this, sources.Count, doc, GetSourceLinkUrl(documentName), documentName);
sources.Add(src);
d2s[rowid] = src;
return src;
};
foreach (DocumentHandle dh in asmMetadataReader.Documents)
{
var document = asmMetadataReader.GetDocument(dh);
}
if (pdbMetadataReader != null)
ProcessSourceLink();
foreach (TypeDefinitionHandle type in asmMetadataReader.TypeDefinitions)
{
var typeDefinition = asmMetadataReader.GetTypeDefinition(type);
var typeInfo = new TypeInfo(this, type, typeDefinition);
TypesByName[typeInfo.FullName] = typeInfo;
TypesByToken[typeInfo.Token] = typeInfo;
if (pdbMetadataReader != null)
{
foreach (MethodDefinitionHandle method in typeDefinition.GetMethods())
{
var methodDefinition = asmMetadataReader.GetMethodDefinition(method);
if (!method.ToDebugInformationHandle().IsNil)
{
var methodDebugInformation = pdbMetadataReader.GetMethodDebugInformation(method.ToDebugInformationHandle());
if (!methodDebugInformation.Document.IsNil)
{
var document = pdbMetadataReader.GetDocument(methodDebugInformation.Document);
var documentName = pdbMetadataReader.GetString(document.Name);
SourceFile source = FindSource(methodDebugInformation.Document, asmMetadataReader.GetRowNumber(methodDebugInformation.Document), documentName);
var methodInfo = new MethodInfo(this, method, asmMetadataReader.GetRowNumber(method), source, typeInfo, asmMetadataReader, pdbMetadataReader);
methods[asmMetadataReader.GetRowNumber(method)] = methodInfo;
if (source != null)
source.AddMethod(methodInfo);
typeInfo.Methods.Add(methodInfo);
}
}
}
}
}
}
private void ProcessSourceLink()
{
var sourceLinkDebugInfo =
(from cdiHandle in pdbMetadataReader.GetCustomDebugInformation(EntityHandle.ModuleDefinition)
let cdi = pdbMetadataReader.GetCustomDebugInformation(cdiHandle)
where pdbMetadataReader.GetGuid(cdi.Kind) == PortableCustomDebugInfoKinds.SourceLink
select pdbMetadataReader.GetBlobBytes(cdi.Value)).SingleOrDefault();
if (sourceLinkDebugInfo != null)
{
var sourceLinkContent = System.Text.Encoding.UTF8.GetString(sourceLinkDebugInfo, 0, sourceLinkDebugInfo.Length);
if (sourceLinkContent != null)
{
JToken jObject = JObject.Parse(sourceLinkContent)["documents"];
sourceLinkMappings = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObject.ToString());
}
}
}
private Uri GetSourceLinkUrl(string document)
{
if (sourceLinkMappings.TryGetValue(document, out string url))
return new Uri(url);
foreach (KeyValuePair<string, string> sourceLinkDocument in sourceLinkMappings)
{
string key = sourceLinkDocument.Key;
if (!key.EndsWith("*"))
{
continue;
}
string keyTrim = key.TrimEnd('*');
if (document.StartsWith(keyTrim, StringComparison.OrdinalIgnoreCase))
{
string docUrlPart = document.Replace(keyTrim, "");
return new Uri(sourceLinkDocument.Value.TrimEnd('*') + docUrlPart);
}
}
return null;
}
public IEnumerable<SourceFile> Sources => this.sources;
public Dictionary<int, MethodInfo> Methods => this.methods;
public Dictionary<string, TypeInfo> TypesByName { get; } = new();
public Dictionary<int, TypeInfo> TypesByToken { get; } = new();
public int Id => id;
public string Name { get; }
public bool HasSymbols => pdbMetadataReader != null;
// "System.Threading", instead of "System.Threading, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
public string AssemblyNameUnqualified { get; }
public SourceFile GetDocById(int document)
{
return sources.FirstOrDefault(s => s.SourceId.Document == document);
}
public MethodInfo GetMethodByToken(int token)
{
methods.TryGetValue(token, out MethodInfo value);
return value;
}
public TypeInfo GetTypeByName(string name)
{
TypesByName.TryGetValue(name, out TypeInfo res);
return res;
}
internal void UpdatePdbInformation(Stream streamToReadFrom)
{
var pdbStream = new MemoryStream();
streamToReadFrom.CopyTo(pdbStream);
pdbMetadataReader = MetadataReaderProvider.FromPortablePdbStream(pdbStream).GetMetadataReader();
}
}
internal class SourceFile
{
private Dictionary<int, MethodInfo> methods;
private AssemblyInfo assembly;
private int id;
private Document doc;
private DocumentHandle docHandle;
private string url;
internal SourceFile(AssemblyInfo assembly, int id, DocumentHandle docHandle, Uri sourceLinkUri, string url)
{
this.methods = new Dictionary<int, MethodInfo>();
this.SourceLinkUri = sourceLinkUri;
this.assembly = assembly;
this.id = id;
this.doc = assembly.pdbMetadataReader.GetDocument(docHandle);
this.docHandle = docHandle;
this.url = url;
this.DebuggerFileName = url.Replace("\\", "/").Replace(":", "");
this.SourceUri = new Uri((Path.IsPathRooted(url) ? "file://" : "") + url, UriKind.RelativeOrAbsolute);
if (SourceUri.IsFile && File.Exists(SourceUri.LocalPath))
{
this.Url = this.SourceUri.ToString();
}
else
{
this.Url = DotNetUrl;
}
}
internal void AddMethod(MethodInfo mi)
{
if (!this.methods.ContainsKey(mi.Token))
{
this.methods[mi.Token] = mi;
}
}
public string DebuggerFileName { get; }
public string Url { get; }
public string AssemblyName => assembly.Name;
public string DotNetUrl => $"dotnet://{assembly.Name}/{DebuggerFileName}";
public SourceId SourceId => new SourceId(assembly.Id, this.id);
public Uri SourceLinkUri { get; }
public Uri SourceUri { get; }
public IEnumerable<MethodInfo> Methods => this.methods.Values;
public string DocUrl => url;
public (int startLine, int startColumn, int endLine, int endColumn) GetExtents()
{
MethodInfo start = Methods.OrderBy(m => m.StartLocation.Line).ThenBy(m => m.StartLocation.Column).First();
MethodInfo end = Methods.OrderByDescending(m => m.EndLocation.Line).ThenByDescending(m => m.EndLocation.Column).First();
return (start.StartLocation.Line, start.StartLocation.Column, end.EndLocation.Line, end.EndLocation.Column);
}
private async Task<MemoryStream> GetDataAsync(Uri uri, CancellationToken token)
{
var mem = new MemoryStream();
try
{
if (uri.IsFile && File.Exists(uri.LocalPath))
{
using (FileStream file = File.Open(SourceUri.LocalPath, FileMode.Open))
{
await file.CopyToAsync(mem, token).ConfigureAwait(false);
mem.Position = 0;
}
}
else if (uri.Scheme == "http" || uri.Scheme == "https")
{
using (var client = new HttpClient())
using (Stream stream = await client.GetStreamAsync(uri, token))
{
await stream.CopyToAsync(mem, token).ConfigureAwait(false);
mem.Position = 0;
}
}
}
catch (Exception)
{
return null;
}
return mem;
}
private static HashAlgorithm GetHashAlgorithm(Guid algorithm)
{
if (algorithm.Equals(HashKinds.SHA1))
#pragma warning disable CA5350 // Do Not Use Weak Cryptographic Algorithms
return SHA1.Create();
#pragma warning restore CA5350 // Do Not Use Weak Cryptographic Algorithms
if (algorithm.Equals(HashKinds.SHA256))
return SHA256.Create();
return null;
}
private bool CheckPdbHash(byte[] computedHash)
{
var hash = assembly.pdbMetadataReader.GetBlobBytes(doc.Hash);
if (computedHash.Length != hash.Length)
return false;
for (int i = 0; i < computedHash.Length; i++)
if (computedHash[i] != hash[i])
return false;
return true;
}
private byte[] ComputePdbHash(Stream sourceStream)
{
HashAlgorithm algorithm = GetHashAlgorithm(assembly.pdbMetadataReader.GetGuid(doc.HashAlgorithm));
if (algorithm != null)
using (algorithm)
return algorithm.ComputeHash(sourceStream);
return Array.Empty<byte>();
}
public async Task<Stream> GetSourceAsync(bool checkHash, CancellationToken token = default(CancellationToken))
{
var reader = assembly.pdbMetadataReader;
byte[] bytes = (from handle in reader.GetCustomDebugInformation(docHandle)
let cdi = reader.GetCustomDebugInformation(handle)
where reader.GetGuid(cdi.Kind) == PortableCustomDebugInfoKinds.EmbeddedSource
select reader.GetBlobBytes(cdi.Value)).SingleOrDefault();
if (bytes != null)
{
int uncompressedSize = BitConverter.ToInt32(bytes, 0);
var stream = new MemoryStream(bytes, sizeof(int), bytes.Length - sizeof(int));
if (uncompressedSize != 0)
{
return new DeflateStream(stream, CompressionMode.Decompress);
}
}
foreach (Uri url in new[] { SourceUri, SourceLinkUri })
{
MemoryStream mem = await GetDataAsync(url, token).ConfigureAwait(false);
if (mem != null && mem.Length > 0 && (!checkHash || CheckPdbHash(ComputePdbHash(mem))))
{
mem.Position = 0;
return mem;
}
}
return MemoryStream.Null;
}
public object ToScriptSource(int executionContextId, object executionContextAuxData)
{
return new
{
scriptId = SourceId.ToString(),
url = Url,
executionContextId,
executionContextAuxData,
//hash: should be the v8 hash algo, managed implementation is pending
dotNetUrl = DotNetUrl,
};
}
}
internal class DebugStore
{
internal List<AssemblyInfo> assemblies = new List<AssemblyInfo>();
private readonly HttpClient client;
private readonly ILogger logger;
private readonly MonoProxy monoProxy;
public DebugStore(MonoProxy monoProxy, ILogger logger, HttpClient client)
{
this.client = client;
this.logger = logger;
this.monoProxy = monoProxy;
}
public DebugStore(MonoProxy monoProxy, ILogger logger) : this(monoProxy, logger, new HttpClient())
{ }
private class DebugItem
{
public string Url { get; set; }
public Task<byte[][]> Data { get; set; }
}
public IEnumerable<MethodInfo> EnC(AssemblyInfo asm, byte[] meta_data, byte[] pdb_data)
{
asm.EnC(meta_data, pdb_data);
foreach (var method in asm.Methods)
{
if (method.Value.IsEnCMethod)
yield return method.Value;
}
}
public IEnumerable<SourceFile> Add(SessionId id, string name, byte[] assembly_data, byte[] pdb_data, CancellationToken token)
{
AssemblyInfo assembly = null;
try
{
assembly = new AssemblyInfo(monoProxy, id, name, assembly_data, pdb_data, token);
}
catch (Exception e)
{
logger.LogDebug($"Failed to load assembly: ({e.Message})");
yield break;
}
if (assembly == null)
yield break;
if (GetAssemblyByName(assembly.Name) != null)
{
logger.LogDebug($"Skipping adding {assembly.Name} into the debug store, as it already exists");
yield break;
}
assemblies.Add(assembly);
foreach (var source in assembly.Sources)
{
yield return source;
}
}
public async IAsyncEnumerable<SourceFile> Load(SessionId id, string[] loaded_files, [EnumeratorCancellation] CancellationToken token)
{
var asm_files = new List<string>();
var pdb_files = new List<string>();
foreach (string file_name in loaded_files)
{
if (file_name.EndsWith(".pdb", StringComparison.OrdinalIgnoreCase))
pdb_files.Add(file_name);
else
asm_files.Add(file_name);
}
List<DebugItem> steps = new List<DebugItem>();
foreach (string url in asm_files)
{
try
{