How do I make a thread dump in .NET ? (a la JVM thread dumps)

前端 未结 6 1729
暖寄归人
暖寄归人 2021-01-30 17:52

I have found no way of dumping the stack on all threads in .NET. Neither a signal to be send to the process nor programatic access to all the threads. I can only get access to t

6条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 18:39

    If you need to do this programmatically (maybe you want automatic dumps during your CI process), you can use the info from this answer to a different question.

    Basically, attach to your own process using CLR MD:

    using Microsoft.Diagnostics.Runtime;
    
    using (DataTarget target = DataTarget.AttachToProcess(
        Process.GetCurrentProcess().Id, 5000, AttachFlag.Passive))
    {
        ClrRuntime runtime = target.ClrVersions.First().CreateRuntime();
        foreach (ClrThread thread in runtime.Threads)
        {
            IList stackFrames = thread.StackTrace;
            PrintStackTrace(stackFrames);            
        }
    }
    

    Here PrintStackTrace is left as an exercise for the reader.

提交回复
热议问题