-
Notifications
You must be signed in to change notification settings - Fork 357
/
SymbolServiceWrapper.cs
1080 lines (986 loc) · 43.8 KB
/
SymbolServiceWrapper.cs
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.
// See the LICENSE file in the project root for more information.
using Microsoft.Diagnostics.DebugServices;
using Microsoft.Diagnostics.Runtime.Utilities;
using Microsoft.FileFormats;
using Microsoft.FileFormats.ELF;
using Microsoft.FileFormats.MachO;
using Microsoft.FileFormats.PE;
using Microsoft.SymbolStore;
using Microsoft.SymbolStore.KeyGenerators;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace SOS.Hosting
{
public sealed class SymbolServiceWrapper : COMCallableIUnknown
{
/// <summary>
/// Matches the IRuntime::RuntimeConfiguration in runtime.h
/// </summary>
public enum RuntimeConfiguration
{
WindowsDesktop = 0,
WindowsCore = 1,
UnixCore = 2,
OSXCore = 3
}
private sealed class OpenedReader : IDisposable
{
public readonly MetadataReaderProvider Provider;
public readonly MetadataReader Reader;
public OpenedReader(MetadataReaderProvider provider, MetadataReader reader)
{
Debug.Assert(provider != null);
Debug.Assert(reader != null);
Provider = provider;
Reader = reader;
}
public void Dispose() => Provider.Dispose();
}
/// <summary>
/// Writeline delegate for symbol store logging
/// </summary>
/// <param name="message"></param>
private delegate void WriteLine([MarshalAs(UnmanagedType.LPStr)] string message);
/// <summary>
/// The LoadNativeSymbols callback
/// </summary>
/// <param name="moduleFileName">module file name</param>
/// <param name="symbolFileName">symbol file name and path</param>
private delegate void SymbolFileCallback(
IntPtr parameter,
[MarshalAs(UnmanagedType.LPStr)] string moduleFileName,
[MarshalAs(UnmanagedType.LPStr)] string symbolFileName);
public static readonly Guid IID_ISymbolService = new Guid("7EE88D46-F8B3-4645-AD3E-01FE7D4F70F1");
private readonly Func<IMemoryService> _getMemoryService;
private readonly ISymbolService _symbolService;
public SymbolServiceWrapper(IHost host, Func<IMemoryService> getMemoryService)
{
Debug.Assert(host != null);
Debug.Assert(getMemoryService != null);
_getMemoryService = getMemoryService;
_symbolService = host.Services.GetService<ISymbolService>();
Debug.Assert(_symbolService != null);
VTableBuilder builder = AddInterface(IID_ISymbolService, validate: false);
builder.AddMethod(new IsSymbolStoreEnabledDelegate((IntPtr self) => _symbolService.IsSymbolStoreEnabled));
builder.AddMethod(new InitializeSymbolStoreDelegate(InitializeSymbolStore));
builder.AddMethod(new ParseSymbolPathDelegate(ParseSymbolPath));
builder.AddMethod(new DisplaySymbolStoreDelegate(DisplaySymbolStore));
builder.AddMethod(new DisableSymbolStoreDelegate((IntPtr self) => _symbolService.DisableSymbolStore()));
builder.AddMethod(new LoadNativeSymbolsDelegate(LoadNativeSymbols));
builder.AddMethod(new LoadNativeSymbolsFromIndexDelegate(LoadNativeSymbolsFromIndex));
builder.AddMethod(new LoadSymbolsForModuleDelegate(LoadSymbolsForModule));
builder.AddMethod(new DisposeDelegate(Dispose));
builder.AddMethod(new ResolveSequencePointDelegate(ResolveSequencePoint));
builder.AddMethod(new GetLocalVariableNameDelegate(GetLocalVariableName));
builder.AddMethod(new GetLineByILOffsetDelegate(GetLineByILOffset));
builder.AddMethod(new GetExpressionValueDelegate(GetExpressionValue));
builder.AddMethod(new GetMetadataLocatorDelegate(GetMetadataLocator));
builder.AddMethod(new GetICorDebugMetadataLocatorDelegate(GetICorDebugMetadataLocator));
builder.Complete();
AddRef();
}
protected override void Destroy()
{
Trace.TraceInformation("SymbolServiceWrapper.Destroy");
}
/// <summary>
/// Initializes symbol loading. Adds the symbol server and/or the cache path (if not null) to the list of
/// symbol servers. This API can be called more than once to add more servers to search.
/// </summary>
/// <param name="msdl">if true, use the public Microsoft server</param>
/// <param name="symweb">if true, use symweb internal server and protocol (file.ptr)</param>
/// <param name="symbolServerPath">symbol server url (optional)</param>
/// <param name="timeoutInMinutes">symbol server timeout in minutes (optional)</param>
/// <param name="symbolCachePath">symbol cache directory path (optional)</param>
/// <param name="symbolDirectoryPath">symbol directory path to search (optional)</param>
/// <returns>if false, failure</returns>
private bool InitializeSymbolStore(
IntPtr self,
bool msdl,
bool symweb,
string symbolServerPath,
string authToken,
int timeoutInMinutes,
string symbolCachePath,
string symbolDirectoryPath)
{
if (msdl || symweb || symbolServerPath != null)
{
// Add the default symbol cache if no cache specified and adding server
if (symbolCachePath == null)
{
symbolCachePath = _symbolService.DefaultSymbolCache;
}
if (!_symbolService.AddSymbolServer(msdl, symweb, symbolServerPath, authToken, timeoutInMinutes))
{
return false;
}
}
if (symbolCachePath != null)
{
_symbolService.AddCachePath(symbolCachePath);
}
if (symbolDirectoryPath != null)
{
_symbolService.AddDirectoryPath(symbolDirectoryPath);
}
return true;
}
/// <summary>
/// Parse the Windows sympath format
/// </summary>
/// <param name="windowsSymbolPath">windows symbol path</param>
/// <returns>if false, failure</returns>
private bool ParseSymbolPath(
IntPtr self,
string windowsSymbolPath)
{
if (windowsSymbolPath == null) {
return false;
}
return _symbolService.ParseSymbolPath(windowsSymbolPath);
}
/// <summary>
/// Displays the symbol server and cache configuration
/// </summary>
private void DisplaySymbolStore(IntPtr self, WriteLine writeLine)
{
writeLine(_symbolService.ToString());
}
/// <summary>
/// Load native symbols and modules (i.e. DAC, DBI).
/// </summary>
/// <param name="callback">called back for each symbol file loaded</param>
/// <param name="parameter">callback parameter</param>
/// <param name="config">Target configuration: Windows, Linux or OSX</param>
/// <param name="moduleFilePath">module path</param>
/// <param name="address">module base address</param>
/// <param name="size">module size</param>
/// <param name="readMemory">read memory callback delegate</param>
private void LoadNativeSymbols(
IntPtr self,
SymbolFileCallback callback,
IntPtr parameter,
RuntimeConfiguration config,
string moduleFilePath,
ulong address,
uint size)
{
if (_symbolService.IsSymbolStoreEnabled)
{
try
{
Stream stream = MemoryService.CreateMemoryStream(address, size);
KeyGenerator generator = null;
if (config == RuntimeConfiguration.UnixCore)
{
var elfFile = new ELFFile(new StreamAddressSpace(stream), 0, true);
generator = new ELFFileKeyGenerator(Tracer.Instance, elfFile, moduleFilePath);
}
else if (config == RuntimeConfiguration.OSXCore)
{
var machOFile = new MachOFile(new StreamAddressSpace(stream), 0, true);
generator = new MachOFileKeyGenerator(Tracer.Instance, machOFile, moduleFilePath);
}
else if (config == RuntimeConfiguration.WindowsCore || config == RuntimeConfiguration.WindowsDesktop)
{
var peFile = new PEFile(new StreamAddressSpace(stream), true);
generator = new PEFileKeyGenerator(Tracer.Instance, peFile, moduleFilePath);
}
else
{
Trace.TraceError("LoadNativeSymbols: unsupported config {0}", config);
}
if (generator != null)
{
IEnumerable<SymbolStoreKey> keys = generator.GetKeys(KeyTypeFlags.SymbolKey | KeyTypeFlags.DacDbiKeys);
foreach (SymbolStoreKey key in keys)
{
string moduleFileName = Path.GetFileName(key.FullPathName);
Trace.TraceInformation("{0} {1}", key.FullPathName, key.Index);
string downloadFilePath = _symbolService.DownloadFile(key);
if (downloadFilePath != null)
{
Trace.TraceInformation("{0}: {1}", moduleFileName, downloadFilePath);
callback(parameter, moduleFileName, downloadFilePath);
}
}
}
}
catch (Exception ex) when
(ex is DiagnosticsException ||
ex is BadInputFormatException ||
ex is InvalidVirtualAddressException ||
ex is ArgumentOutOfRangeException ||
ex is IndexOutOfRangeException ||
ex is TaskCanceledException)
{
Trace.TraceError("{0} address {1:X16}: {2}", moduleFilePath, address, ex.Message);
}
}
}
/// <summary>
/// Load native modules (i.e. DAC, DBI) from the runtime build id.
/// </summary>
/// <param name="callback">called back for each symbol file loaded</param>
/// <param name="parameter">callback parameter</param>
/// <param name="config">Target configuration: Windows, Linux or OSX</param>
/// <param name="moduleFilePath">module path</param>
/// <param name="specialKeys">if true, returns the DBI/DAC keys, otherwise the identity key</param>
/// <param name="moduleIndexSize">build id size</param>
/// <param name="moduleIndex">pointer to build id</param>
private void LoadNativeSymbolsFromIndex(
IntPtr self,
SymbolFileCallback callback,
IntPtr parameter,
RuntimeConfiguration config,
string moduleFilePath,
bool specialKeys,
int moduleIndexSize,
IntPtr moduleIndex)
{
try
{
KeyTypeFlags flags = specialKeys ? KeyTypeFlags.DacDbiKeys : KeyTypeFlags.IdentityKey;
byte[] id = new byte[moduleIndexSize];
Marshal.Copy(moduleIndex, id, 0, moduleIndexSize);
IEnumerable<SymbolStoreKey> keys = null;
switch (config)
{
case RuntimeConfiguration.UnixCore:
keys = ELFFileKeyGenerator.GetKeys(flags, moduleFilePath, id, symbolFile: false, symbolFileName: null);
break;
case RuntimeConfiguration.OSXCore:
keys = MachOFileKeyGenerator.GetKeys(flags, moduleFilePath, id, symbolFile: false, symbolFileName: null);
break;
case RuntimeConfiguration.WindowsCore:
case RuntimeConfiguration.WindowsDesktop:
uint timeStamp = BitConverter.ToUInt32(id, 0);
uint fileSize = BitConverter.ToUInt32(id, 4);
SymbolStoreKey key = PEFileKeyGenerator.GetKey(moduleFilePath, timeStamp, fileSize);
keys = new SymbolStoreKey[] { key };
break;
default:
Trace.TraceError("LoadNativeSymbolsFromIndex: unsupported platform {0}", config);
return;
}
foreach (SymbolStoreKey key in keys)
{
string moduleFileName = Path.GetFileName(key.FullPathName);
Trace.TraceInformation("{0} {1}", key.FullPathName, key.Index);
string downloadFilePath = _symbolService.DownloadFile(key);
if (downloadFilePath != null)
{
Trace.TraceInformation("{0}: {1}", moduleFileName, downloadFilePath);
callback(parameter, moduleFileName, downloadFilePath);
}
}
}
catch (Exception ex) when (ex is BadInputFormatException || ex is InvalidVirtualAddressException || ex is TaskCanceledException)
{
Trace.TraceError("{0} - {1}", ex.Message, moduleFilePath);
}
}
/// <summary>
/// Checks availability of debugging information for given assembly.
/// </summary>
/// <param name="assemblyPath">
/// File path of the assembly or null
/// </param>
/// <param name="isFileLayout">type of in-memory PE layout, if true, file based layout otherwise, loaded layout</param>
/// <param name="loadedPeAddress">
/// Loaded PE image address or zero if the module is dynamic (generated by Reflection.Emit).
/// Dynamic modules have their PDBs (if any) generated to an in-memory stream
/// (pointed to by <paramref name="inMemoryPdbAddress"/> and <paramref name="inMemoryPdbSize"/>).
/// </param>
/// <param name="loadedPeSize">loaded PE image size</param>
/// <param name="inMemoryPdbAddress">in memory PDB address or zero</param>
/// <param name="inMemoryPdbSize">in memory PDB size</param>
/// <returns>Symbol reader handle or zero if error</returns>
private IntPtr LoadSymbolsForModule(
IntPtr self,
string assemblyPath,
bool isFileLayout,
ulong loadedPeAddress,
uint loadedPeSize,
ulong inMemoryPdbAddress,
uint inMemoryPdbSize)
{
try
{
Stream peStream = null;
if (loadedPeAddress != 0)
{
peStream = MemoryService.CreateMemoryStream(loadedPeAddress, loadedPeSize);
}
Stream pdbStream = null;
if (inMemoryPdbAddress != 0)
{
pdbStream = MemoryService.CreateMemoryStream(inMemoryPdbAddress, inMemoryPdbSize);
}
OpenedReader openedReader = GetReader(assemblyPath, isFileLayout, peStream, pdbStream);
if (openedReader != null)
{
GCHandle gch = GCHandle.Alloc(openedReader);
return GCHandle.ToIntPtr(gch);
}
}
catch (Exception ex)
{
Trace.TraceError($"LoadSymbolsForModule: {ex.Message}");
}
return IntPtr.Zero;
}
/// <summary>
/// Cleanup and dispose of symbol reader handle
/// </summary>
/// <param name="symbolReaderHandle">symbol reader handle returned by LoadSymbolsForModule</param>
private void Dispose(
IntPtr self,
IntPtr symbolReaderHandle)
{
Debug.Assert(symbolReaderHandle != IntPtr.Zero);
try
{
GCHandle gch = GCHandle.FromIntPtr(symbolReaderHandle);
((OpenedReader)gch.Target).Dispose();
gch.Free();
}
catch (Exception ex)
{
Trace.TraceError($"Dispose: {ex.Message}");
}
}
/// <summary>
/// Get expression helper for native SOS.
/// </summary>
/// <param name="expression">hex number</param>
/// <returns>value</returns>
internal static ulong GetExpressionValue(
IntPtr self,
string expression)
{
if (expression != null)
{
if (ulong.TryParse(expression.Replace("0x", ""), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong result))
{
return result;
}
}
return 0;
}
/// <summary>
/// Returns method token and IL offset for given source line number.
/// </summary>
/// <param name="symbolReaderHandle">symbol reader handle returned by LoadSymbolsForModule</param>
/// <param name="filePath">source file name and path</param>
/// <param name="lineNumber">source line number</param>
/// <param name="methodToken">method token return</param>
/// <param name="ilOffset">IL offset return</param>
/// <returns> true if information is available</returns>
private bool ResolveSequencePoint(
IntPtr self,
IntPtr symbolReaderHandle,
string filePath,
int lineNumber,
out int methodToken,
out int ilOffset)
{
Debug.Assert(symbolReaderHandle != IntPtr.Zero);
methodToken = 0;
ilOffset = 0;
GCHandle gch = GCHandle.FromIntPtr(symbolReaderHandle);
MetadataReader reader = ((OpenedReader)gch.Target).Reader;
try
{
string fileName = GetFileName(filePath);
foreach (MethodDebugInformationHandle methodDebugInformationHandle in reader.MethodDebugInformation)
{
MethodDebugInformation methodDebugInfo = reader.GetMethodDebugInformation(methodDebugInformationHandle);
SequencePointCollection sequencePoints = methodDebugInfo.GetSequencePoints();
foreach (SequencePoint point in sequencePoints)
{
string sourceName = reader.GetString(reader.GetDocument(point.Document).Name);
if (point.StartLine == lineNumber && GetFileName(sourceName) == fileName)
{
methodToken = MetadataTokens.GetToken(methodDebugInformationHandle.ToDefinitionHandle());
ilOffset = point.Offset;
return true;
}
}
}
}
catch (Exception ex)
{
Trace.TraceError($"ResolveSequencePoint: {ex.Message}");
}
return false;
}
/// <summary>
/// Returns source line number and source file name for given IL offset and method token.
/// </summary>
/// <param name="symbolReaderHandle">symbol reader handle returned by LoadSymbolsForModule</param>
/// <param name="methodToken">method token</param>
/// <param name="ilOffset">IL offset</param>
/// <param name="lineNumber">source line number return</param>
/// <param name="fileName">source file name return</param>
/// <returns> true if information is available</returns>
private bool GetLineByILOffset(
IntPtr self,
IntPtr symbolReaderHandle,
int methodToken,
long ilOffset,
out int lineNumber,
out IntPtr fileName)
{
Debug.Assert(symbolReaderHandle != IntPtr.Zero);
fileName = IntPtr.Zero;
GCHandle gch = GCHandle.FromIntPtr(symbolReaderHandle);
OpenedReader openedReader = (OpenedReader)gch.Target;
if (!GetSourceLineByILOffset(openedReader, methodToken, ilOffset, out lineNumber, out string sourceFileName))
{
return false;
}
fileName = Marshal.StringToBSTR(sourceFileName);
return true;
}
/// <summary>
/// Helper method to return source line number and source file name for given IL offset and method token.
/// </summary>
/// <param name="openedReader">symbol reader returned by LoadSymbolsForModule</param>
/// <param name="methodToken">method token</param>
/// <param name="ilOffset">IL offset</param>
/// <param name="lineNumber">source line number return</param>
/// <param name="fileName">source file name return</param>
/// <returns> true if information is available</returns>
private bool GetSourceLineByILOffset(
OpenedReader openedReader,
int methodToken,
long ilOffset,
out int lineNumber,
out string fileName)
{
lineNumber = 0;
fileName = null;
MetadataReader reader = openedReader.Reader;
try
{
Handle handle = MetadataTokens.Handle(methodToken);
if (handle.Kind != HandleKind.MethodDefinition)
return false;
MethodDebugInformationHandle methodDebugHandle = ((MethodDefinitionHandle)handle).ToDebugInformationHandle();
if (methodDebugHandle.IsNil)
return false;
MethodDebugInformation methodDebugInfo = reader.GetMethodDebugInformation(methodDebugHandle);
SequencePointCollection sequencePoints = methodDebugInfo.GetSequencePoints();
SequencePoint? nearestPoint = null;
foreach (SequencePoint point in sequencePoints)
{
if (point.Offset > ilOffset)
break;
if (point.StartLine != 0 && !point.IsHidden)
nearestPoint = point;
}
if (nearestPoint.HasValue)
{
lineNumber = nearestPoint.Value.StartLine;
fileName = reader.GetString(reader.GetDocument(nearestPoint.Value.Document).Name);
return true;
}
}
catch (Exception ex)
{
Trace.TraceError($"GetSourceLineByILOffset: {ex.Message}");
}
return false;
}
/// <summary>
/// Returns local variable name for given local index and IL offset.
/// </summary>
/// <param name="symbolReaderHandle">symbol reader handle returned by LoadSymbolsForModule</param>
/// <param name="methodToken">method token</param>
/// <param name="localIndex">local variable index</param>
/// <param name="localVarName">local variable name return</param>
/// <returns>true if name has been found</returns>
private bool GetLocalVariableName(
IntPtr self,
IntPtr symbolReaderHandle,
int methodToken,
int localIndex,
out IntPtr localVarName)
{
Debug.Assert(symbolReaderHandle != IntPtr.Zero);
localVarName = IntPtr.Zero;
GCHandle gch = GCHandle.FromIntPtr(symbolReaderHandle);
OpenedReader openedReader = (OpenedReader)gch.Target;
if (!GetLocalVariableByIndex(openedReader, methodToken, localIndex, out string localVar))
{
return false;
}
localVarName = Marshal.StringToBSTR(localVar);
return true;
}
/// <summary>
/// Helper method to return local variable name for given local index and IL offset.
/// </summary>
/// <param name="openedReader">symbol reader returned by LoadSymbolsForModule</param>
/// <param name="methodToken">method token</param>
/// <param name="localIndex">local variable index</param>
/// <param name="localVarName">local variable name return</param>
/// <returns>true if name has been found</returns>
private bool GetLocalVariableByIndex(
OpenedReader openedReader,
int methodToken,
int localIndex,
out string localVarName)
{
localVarName = null;
MetadataReader reader = openedReader.Reader;
try
{
Handle handle = MetadataTokens.Handle(methodToken);
if (handle.Kind != HandleKind.MethodDefinition)
return false;
MethodDebugInformationHandle methodDebugHandle = ((MethodDefinitionHandle)handle).ToDebugInformationHandle();
LocalScopeHandleCollection localScopes = reader.GetLocalScopes(methodDebugHandle);
foreach (LocalScopeHandle scopeHandle in localScopes)
{
LocalScope scope = reader.GetLocalScope(scopeHandle);
LocalVariableHandleCollection localVars = scope.GetLocalVariables();
foreach (LocalVariableHandle varHandle in localVars)
{
LocalVariable localVar = reader.GetLocalVariable(varHandle);
if (localVar.Index == localIndex)
{
if (localVar.Attributes == LocalVariableAttributes.DebuggerHidden)
return false;
localVarName = reader.GetString(localVar.Name);
return true;
}
}
}
}
catch (Exception ex)
{
Trace.TraceError($"GetLocalVariableByIndex: {ex.Message}");
}
return false;
}
/// <summary>
/// Metadata locator helper for the DAC.
/// </summary>
/// <param name="imagePath">file name and path to module</param>
/// <param name="imageTimestamp">module timestamp</param>
/// <param name="imageSize">module image</param>
/// <param name="mvid">not used</param>
/// <param name="mdRva">not used</param>
/// <param name="flags">not used</param>
/// <param name="bufferSize">size of incoming buffer (pMetadata)</param>
/// <param name="pMetadata">pointer to buffer</param>
/// <param name="pMetadataSize">size of outgoing metadata</param>
/// <returns>HRESULT</returns>
internal int GetMetadataLocator(
IntPtr self,
string imagePath,
uint imageTimestamp,
uint imageSize,
byte[] mvid,
uint mdRva,
uint flags,
uint bufferSize,
IntPtr pMetadata,
IntPtr pMetadataSize)
{
Debug.Assert(imageTimestamp != 0);
Debug.Assert(imageSize != 0);
if (pMetadata == IntPtr.Zero) {
return HResult.E_INVALIDARG;
}
int hr = HResult.S_OK;
int dataSize = 0;
ImmutableArray<byte> metadata = _symbolService.GetMetadata(imagePath, imageTimestamp, imageSize);
if (!metadata.IsEmpty)
{
dataSize = metadata.Length;
int size = Math.Min((int)bufferSize, dataSize);
Marshal.Copy(metadata.ToArray(), 0, pMetadata, size);
}
else
{
hr = HResult.E_FAIL;
}
if (pMetadataSize != IntPtr.Zero) {
Marshal.WriteInt32(pMetadataSize, dataSize);
}
return hr;
}
/// <summary>
/// Metadata locator helper for the DAC.
/// </summary>
/// <param name="imagePath">file name and path to module</param>
/// <param name="imageTimestamp">module timestamp</param>
/// <param name="imageSize">module image</param>
/// <param name="pathBufferSize">output buffer size</param>
/// <param name="pPathBufferSize">native pointer to put actual path size</param>
/// <param name="pwszPathBuffer">native pointer to WCHAR path buffer</param>
/// <returns>HRESULT</returns>
internal int GetICorDebugMetadataLocator(
IntPtr self,
string imagePath,
uint imageTimestamp,
uint imageSize,
uint pathBufferSize,
IntPtr pPathBufferSize,
IntPtr pwszPathBuffer)
{
return _symbolService.GetICorDebugMetadataLocator(imagePath, imageTimestamp, imageSize, pathBufferSize, pPathBufferSize, pwszPathBuffer);
}
/// <summary>
/// Returns the portable PDB reader for the assembly path
/// </summary>
/// <param name="assemblyPath">file path of the assembly or null if the module is in-memory or dynamic</param>
/// <param name="isFileLayout">type of in-memory PE layout, if true, file based layout otherwise, loaded layout</param>
/// <param name="peStream">in-memory PE stream</param>
/// <param name="pdbStream">optional in-memory PDB stream</param>
/// <returns>reader/provider wrapper instance</returns>
/// <remarks>
/// Assumes that neither PE image nor PDB loaded into memory can be unloaded or moved around.
/// </remarks>
private OpenedReader GetReader(string assemblyPath, bool isFileLayout, Stream peStream, Stream pdbStream)
{
return (pdbStream != null) ? TryOpenReaderForInMemoryPdb(pdbStream) : TryOpenReaderFromAssembly(assemblyPath, isFileLayout, peStream);
}
private OpenedReader TryOpenReaderForInMemoryPdb(Stream pdbStream)
{
Debug.Assert(pdbStream != null);
byte[] buffer = new byte[sizeof(uint)];
if (pdbStream.Read(buffer, 0, sizeof(uint)) != sizeof(uint))
{
return null;
}
uint signature = BitConverter.ToUInt32(buffer, 0);
// quick check to avoid throwing exceptions below in common cases:
const uint ManagedMetadataSignature = 0x424A5342;
if (signature != ManagedMetadataSignature)
{
// not a Portable PDB
return null;
}
OpenedReader result = null;
MetadataReaderProvider provider = null;
try
{
pdbStream.Position = 0;
provider = MetadataReaderProvider.FromPortablePdbStream(pdbStream);
result = new OpenedReader(provider, provider.GetMetadataReader());
}
catch (Exception e) when (e is BadImageFormatException || e is IOException)
{
return null;
}
finally
{
if (result == null)
{
provider?.Dispose();
}
}
return result;
}
private OpenedReader TryOpenReaderFromAssembly(string assemblyPath, bool isFileLayout, Stream peStream)
{
if (assemblyPath == null && peStream == null)
return null;
PEStreamOptions options = isFileLayout ? PEStreamOptions.Default : PEStreamOptions.IsLoadedImage;
if (peStream == null)
{
peStream = TryOpenFile(assemblyPath);
if (peStream == null)
return null;
options = PEStreamOptions.Default;
}
try
{
using (var peReader = new PEReader(peStream, options))
{
ReadPortableDebugTableEntries(peReader, out DebugDirectoryEntry codeViewEntry, out DebugDirectoryEntry embeddedPdbEntry);
// First try .pdb file specified in CodeView data (we prefer .pdb file on disk over embedded PDB
// since embedded PDB needs decompression which is less efficient than memory-mapping the file).
if (codeViewEntry.DataSize != 0)
{
var result = TryOpenReaderFromCodeView(peReader, codeViewEntry, assemblyPath);
if (result != null)
{
return result;
}
}
// if it failed try Embedded Portable PDB (if available):
if (embeddedPdbEntry.DataSize != 0)
{
return TryOpenReaderFromEmbeddedPdb(peReader, embeddedPdbEntry);
}
}
}
catch (Exception e) when (e is BadImageFormatException || e is IOException)
{
// nop
}
return null;
}
private void ReadPortableDebugTableEntries(PEReader peReader, out DebugDirectoryEntry codeViewEntry, out DebugDirectoryEntry embeddedPdbEntry)
{
// See spec: https://github.com/dotnet/runtime/blob/main/docs/design/specs/PE-COFF.md
codeViewEntry = default;
embeddedPdbEntry = default;
foreach (DebugDirectoryEntry entry in peReader.ReadDebugDirectory())
{
if (entry.Type == DebugDirectoryEntryType.CodeView)
{
if (entry.MinorVersion != ImageDebugDirectory.PortablePDBMinorVersion)
{
continue;
}
codeViewEntry = entry;
}
else if (entry.Type == DebugDirectoryEntryType.EmbeddedPortablePdb)
{
embeddedPdbEntry = entry;
}
}
}
private OpenedReader TryOpenReaderFromCodeView(PEReader peReader, DebugDirectoryEntry codeViewEntry, string assemblyPath)
{
OpenedReader result = null;
MetadataReaderProvider provider = null;
try
{
CodeViewDebugDirectoryData data = peReader.ReadCodeViewDebugDirectoryData(codeViewEntry);
string pdbPath = data.Path;
Stream pdbStream = null;
if (assemblyPath != null)
{
try
{
pdbPath = Path.Combine(Path.GetDirectoryName(assemblyPath), GetFileName(pdbPath));
}
catch
{
// invalid characters in CodeView path
return null;
}
pdbStream = TryOpenFile(pdbPath);
}
if (pdbStream == null)
{
if (_symbolService.IsSymbolStoreEnabled)
{
Debug.Assert(codeViewEntry.MinorVersion == ImageDebugDirectory.PortablePDBMinorVersion);
SymbolStoreKey key = PortablePDBFileKeyGenerator.GetKey(pdbPath, data.Guid);
pdbStream = _symbolService.GetSymbolStoreFile(key)?.Stream;
}
if (pdbStream == null)
{
return null;
}
// Make sure the stream is at the beginning of the pdb.
pdbStream.Position = 0;
}
provider = MetadataReaderProvider.FromPortablePdbStream(pdbStream);
MetadataReader reader = provider.GetMetadataReader();
// Validate that the PDB matches the assembly version
if (data.Age == 1 && new BlobContentId(reader.DebugMetadataHeader.Id) == new BlobContentId(data.Guid, codeViewEntry.Stamp))
{
result = new OpenedReader(provider, reader);
}
}
catch (Exception e) when (e is BadImageFormatException || e is IOException)
{
return null;
}
finally
{
if (result == null)
{
provider?.Dispose();
}
}
return result;
}
private OpenedReader TryOpenReaderFromEmbeddedPdb(PEReader peReader, DebugDirectoryEntry embeddedPdbEntry)
{
OpenedReader result = null;
MetadataReaderProvider provider = null;
try
{
// TODO: We might want to cache this provider globally (across stack traces),
// since decompressing embedded PDB takes some time.
provider = peReader.ReadEmbeddedPortablePdbDebugDirectoryData(embeddedPdbEntry);
result = new OpenedReader(provider, provider.GetMetadataReader());
}
catch (Exception e) when (e is BadImageFormatException || e is IOException)
{
return null;
}
finally
{
if (result == null)
{
provider?.Dispose();
}
}
return result;
}
/// <summary>
/// Attempt to open a file stream.
/// </summary>
/// <param name="path">file path</param>
/// <returns>stream or null if doesn't exist or error</returns>
private Stream TryOpenFile(string path)
{
if (File.Exists(path))
{
try
{
return File.OpenRead(path);
}
catch (Exception ex) when (ex is UnauthorizedAccessException || ex is NotSupportedException || ex is IOException)
{
}
}
return null;
}
/// <summary>
/// Quick fix for Path.GetFileName which incorrectly handles Windows-style paths on Linux
/// </summary>
/// <param name="pathName"> File path to be processed </param>
/// <returns>Last component of path</returns>
private static string GetFileName(string pathName)
{
int pos = pathName.LastIndexOfAny(new char[] { '/', '\\'});
if (pos < 0)
{
return pathName;
}
return pathName.Substring(pos + 1);
}
private IMemoryService MemoryService => _getMemoryService() ?? throw new DiagnosticsException("SymbolServiceWrapper: no current target");
#region Symbol service delegates
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate bool IsSymbolStoreEnabledDelegate(
[In] IntPtr self);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate bool InitializeSymbolStoreDelegate(
[In] IntPtr self,
[In] bool msdl,
[In] bool symweb,
[In] string symbolServerPath,
[In] string authToken,
[In] int timeoutInMinutes,
[In] string symbolCachePath,
[In] string symbolDirectoryPath);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate bool ParseSymbolPathDelegate(
[In] IntPtr self,
[In] string windowsSymbolPath);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate void DisplaySymbolStoreDelegate(
[In] IntPtr self,
[In] WriteLine writeLine);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate void DisableSymbolStoreDelegate(
[In] IntPtr self);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate void LoadNativeSymbolsDelegate(
[In] IntPtr self,
[In] SymbolFileCallback callback,
[In] IntPtr parameter,
[In] RuntimeConfiguration config,
[In] string moduleFilePath,
[In] ulong address,
[In] uint size);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate void LoadNativeSymbolsFromIndexDelegate(
[In] IntPtr self,