microsoft / clrmd

Microsoft.Diagnostics.Runtime is a set of APIs for introspecting processes and dumps.
MIT License
1.05k stars 255 forks source link

Please add information on how to upgrade from v1 #779

Closed Symbai closed 2 years ago

Symbai commented 4 years ago

The release notes list changes but not API changes. I wanted to upgrade from v1 and noticed a lot of compiling issues I have no idea how to fix them so I get the same result as before:

DataTarget.AttachToProcess => AttachFlag is gone, as well as the timeout parameter. Why? And what should I use now when I was previously using AttachFlag.Passive? ClrInfo.LocalMatchingDac => is gone. What's the new API for this? ClrRuntime.Flush => is gone. What's the new API for this? ClrRuntime.GetMethodByAddress => is gone. What's the new API for this?

Please update the docs and list up all API changes and alternatives when upgrading from v1.

adamsitnik commented 2 years ago

I was not able to find any docs on migrating from v1 to v2 but I was able to migrate BenchmarkDotNet in https://github.com/dotnet/BenchmarkDotNet/pull/2040.

Short summary:

using (var dataTarget = DataTarget.AttachToProcess(
    settings.ProcessId,
-    (uint)TimeSpan.FromSeconds(5).TotalMilliseconds,
-    AttachFlag.Passive))
+    suspend: false))
+ var typeWithBenchmark = state.Runtime.EnumerateModules().Select(module => module.GetTypeByName(settings.TypeName)).First(type => type != null);
- var typeWithBenchmark = state.Runtime.Heap.GetTypeByName(settings.TypeName);
private static void ConfigureSymbols(DataTarget dataTarget)
{
-    // code copied from https://github.com/Microsoft/clrmd/issues/34#issuecomment-161926535
-    var symbols = dataTarget.DebuggerInterface as IDebugSymbols;
-    symbols?.SetSymbolPath("http://msdl.microsoft.com/download/symbols");
-    var control = dataTarget.DebuggerInterface as IDebugControl;
-    control?.Execute(DEBUG_OUTCTL.NOT_LOGGED, ".reload", DEBUG_EXECUTE.NOT_LOGGED);
+    dataTarget.SetSymbolPath("http://msdl.microsoft.com/download/symbols");
}
- Runtime.DataTarget.ReadProcessMemory(startAddress, code, code.Length, out int bytesRead)
+ bytesRead = Runtime.DataTarget.DataReader.Read(startAddress, code)

We need to create SymbolReader and configure it to use MS symbol server:

SymbolReader symbolReader = new SymbolReader(TextWriter.Null) { SymbolPath = SymbolPath.MicrosoftSymbolServerPath }

PdbInfo exposes Path property which can be used to open symbol file (there is no need to use Runtime.DataTarget.SymbolLocator):

+ reader = symbolReader.OpenSymbolFile(pdbPath);
- reader = new PdbReader(pdbPath);

There were few other things I had to do to be able to repalce SymbolReader, you can take a look at this source file to get the overall idea