Csharp/C#教程:ClrMd在创建运行时抛出exception分享


ClrMd在创建运行时抛出exception

我正在使用CLR内存诊断库来获取正在运行的进程中所有线程的堆栈跟踪:

var result = new Dictionary(); var pid = Process.GetCurrentProcess().Id; using (var dataTarget = DataTarget.AttachToProcess(pid, 5000, AttachFlag.Passive)) { string dacLocation = dataTarget.ClrVersions[0].TryGetDacLocation(); var runtime = dataTarget.CreateRuntime(dacLocation); //throws exception foreach (var t in runtime.Threads) { result.Add( t.ManagedThreadId, t.StackTrace.Select(f => { if (f.Method != null) { return f.Method.Type.Name + "." + f.Method.Name; } return null; }).ToArray() ); } } 

我从这里得到了这个代码,它似乎正在为其他人工作,但它在指定的行上抛出一个exception,消息This runtime is not initialized and contains no data.

dacLocation设置为C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscordacwks.dll

ClrMD目前不支持.NET 4.6。 在GitHub上有一个开放的拉取请求只用一行就可以解决这个问题。 您当然可以克隆项目并构建自己的ClrMD,但不会出现此问题。

或者,我可以分享我过去几周一直在使用的临时黑客:

 public static ClrRuntime CreateRuntimeHack(this DataTarget target, string dacLocation, int major, int minor) { string dacFileNoExt = Path.GetFileNameWithoutExtension(dacLocation); if (dacFileNoExt.Contains("mscordacwks") && major == 4 && minor >= 5) { Type dacLibraryType = typeof(DataTarget).Assembly.GetType("Microsoft.Diagnostics.Runtime.DacLibrary"); object dacLibrary = Activator.CreateInstance(dacLibraryType, target, dacLocation); Type v45RuntimeType = typeof(DataTarget).Assembly.GetType("Microsoft.Diagnostics.Runtime.Desktop.V45Runtime"); object runtime = Activator.CreateInstance(v45RuntimeType, target, dacLibrary); return (ClrRuntime)runtime; } else { return target.CreateRuntime(dacLocation); } } 

我知道,这太可怕了,依赖于反思。 但至少它现在可以工作,而且你不必更改代码。

您可以通过下载最新版本的Microsoft.Diagnostics.Runtime.dll (v0.8.31-beta)来解决此问题: https ://www.nuget.org/packages/Microsoft.Diagnostics.Runtime

版本v0.8.31-beta标记了许多function已经过时,因此Alois Kraus提到, runtime.GetHeap()可以破解。 我能够通过创建运行时来解决此问题,如下所示:

 DataTarget target = DataTarget.AttachProcess(pid, timeout, mode); ClrRuntime runtime = target.ClrVersions.First().CreateRuntime(); ClrHeap heap = runtime.GetHeap(); 

现在不需要使用TryGetDacLocation()所有废话。

上述就是C#学习教程:ClrMd在创建运行时抛出exception分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/985145.html

(0)
上一篇 2021年12月21日
下一篇 2021年12月21日

精彩推荐