首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ClrMd在创建运行时抛出异常

ClrMd在创建运行时抛出异常
EN

Stack Overflow用户
提问于 2015-07-26 04:09:53
回答 1查看 2.3K关注 0票数 7

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

代码语言:javascript
复制
        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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-31 12:00:46

ClrMD目前不支持.NET 4.6。有一个打开的拉请求on GitHub,它只用一行就解决了这个问题。当然,您可以克隆该项目,并构建您自己的ClrMD,而不存在此问题。

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

代码语言:javascript
复制
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);
    }
}

我知道,这很可怕,靠的是反思。但至少它现在起作用了,而且您不必更改代码。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31633541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档