【发布时间】:2016-01-24 10:11:54
【问题描述】:
我正在使用 CLR 内存诊断库来获取正在运行的进程中所有线程的堆栈跟踪:
var result = new Dictionary<int, string[]>();
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()
);
}
}
我从here 获得此代码,它似乎对其他人有用,但它在指定行对我抛出异常,并显示消息This runtime is not initialized and contains no data.
dacLocation 设置为C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\mscordacwks.dll
【问题讨论】:
-
我的水晶球说你安装了.NET 4.6。这段代码目前死在永恒的DAC版本号上,4.6版本的DAC是4.6.81.0。看起来微软切换到“语义版本控制”。不是 ClrMD 所期望的,它需要找到 10000 或更大的内部版本号才能使用 V45Runtime 包装器类。我没有看到明显的解决方法,破解版本号也不起作用,微软需要更新 ClrMD 以处理 4.6。