创建运行时,ClrMd引发异常 [英] ClrMd throws exception when creating runtime

查看:108
本文介绍了创建运行时,ClrMd引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the CLR Memory Diagnostics library to get the stack trace of all threads in a running process:

        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()
                );
            }
        }

我从

I got this code from here and it seems to be working for others, but it throws an exception for me on the indicated line, with the message 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,而不会出现此问题.

ClrMD currently doesn't support .NET 4.6. There's an open pull request on GitHub that fixes this issue with just one line. You can of course clone the project and build your own ClrMD that doesn't exhibit this issue.

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

Or, I can share a temporary hack that I've been using over the last few weeks:

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);
    }
}

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

I know, it's horrible, and relies on Reflection. But at least it works for now, and you don't have to change the code.

这篇关于创建运行时,ClrMd引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆