有没有办法让踪迹在C#中的所有线程,就像java.lang.Thread.getAllStackTraces()? [英] Is there a way to get the stacktraces for all threads in c#, like java.lang.Thread.getAllStackTraces()?

查看:233
本文介绍了有没有办法让踪迹在C#中的所有线程,就像java.lang.Thread.getAllStackTraces()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中就可以得到所有正在运行的线程的踪迹的快照。 这与java.lang.Thread.getAllStackTraces()完成(返回地图<线程,一个StackTraceElement []>)。

In java it is possible to get a snapshot of the stacktraces of all running threads. This is done with java.lang.Thread.getAllStackTraces() (it returns Map<Thread,StackTraceElement[]>).

如何才能同样的事情用.NET做?

How can the same thing be done with .net?

推荐答案

所以,我其实只是必须弄清楚如何做到这一点 - 已不在生产中使用该解决方案还广泛,但那里有一个叫ClrMd相对较新的库。

So I actually just had to figure out how to do this -- haven't used this solution extensively in production yet, but theres a relatively new library called ClrMd.

<一个href="http://blogs.msdn.com/b/dougste/archive/2013/05/04/clrmd-net-crash-dump-and-live-process-inspection.aspx">http://blogs.msdn.com/b/dougste/archive/2013/05/04/clrmd-net-crash-dump-and-live-process-inspection.aspx

使用它,我能够连接到我自己的进程,并得到一个堆栈跟踪所有活动线程。使用此死锁时正在重新启动,像这样我们的应用程序之前检测:

Using it, I'm able to attach to my own process and get a stack trace for all live threads. Using this when a deadlock is detected before restarting our app like so:

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

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

var json = JsonConvert.SerializeObject(result);

zip.AddEntry("_threads.json", json);

要获取来自同一个进程的工作真正重要的是 AttachFlag.Passive

The really important thing to get that to work from the same process is AttachFlag.Passive

如果你只是做 DataTarget.AttachToProcess(PID,5000),它会做一个创重视它试图暂停过程。当您尝试连接到你自己的过程中,我假设,因为你不能暂停您的应用程序,而试图从你的应用程序或类似的东西附上这将引发异常。

If you just do DataTarget.AttachToProcess(pid, 5000), it'll do an "invasive" attach which attempts to pause the process. This throws an exception when you try to attach to your own process, I'm assuming because you can't pause your application while trying to attach from your application or something like that.

反正是啊,pretty的很酷的东西。

Anyway, yeah, pretty cool stuff.

如果任何人有任何原因,这是超级幼稚或任何东西,pleeeeease指出来。没有用它在生产中非常多,但(刚刚推出的第一个实例),所以希望它的作品。

If anybody has any reasons why this is super naive or anything, pleeeeease point them out. Haven't used it in production very much yet (just put out the first instance) so hoping it works.

这篇关于有没有办法让踪迹在C#中的所有线程,就像java.lang.Thread.getAllStackTraces()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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