我使用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
发布于 2015-07-31 12:00:46
ClrMD目前不支持.NET 4.6。有一个打开的拉请求on 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);
}
}我知道,这很可怕,靠的是反思。但至少它现在起作用了,而且您不必更改代码。
https://stackoverflow.com/questions/31633541
复制相似问题