4

Code:

using System.Diagnostics;
using System.Linq;
using Microsoft.Diagnostics.Runtime;
using Microsoft.Diagnostics.Runtime.Utilities;
using Microsoft.Diagnostics.Runtime.Utilities.Pdb;

namespace myDiagnostics
{
    public class myStackTraceInfo
    {
        public void Atach()
        {
            using (DataTarget target = DataTarget.AttachToProcess(Process.GetCurrentProcess().Id, 5000, AttachFlag.Passive))
            {
                ClrRuntime runtime = target.ClrVersions.First().CreateRuntime();
                foreach (ClrThread thread in runtime.Threads)
                {
                    foreach (ClrStackFrame frame in thread.StackTrace)
                        Console.Write(frame.Method.ToString());
                }
            }
        }
    }
}

As a result, I get instead of method names - "UNKNOWN". But the in method is the field "InstructionPointer", maybe it will give more information?

Jeroen Mostert
  • 27,176
  • 2
  • 52
  • 85
Евгений
  • 125
  • 1
  • 8
  • That's not a valid use of ClrMD. You are looking at your own threads instead of those in a frozen process. Like you'd get from a minidump or a debugger breakpoint. Your own threads are busy executing code so their stack constantly changes. At the exact moment you use thread.StackTrace, your code is probably running inside the DAC. Native C++ code, it will be unknown. – Hans Passant Oct 26 '16 at 06:58
  • If I atach to another process, I get the same result. – Евгений Oct 26 '16 at 07:25
  • See if its can help you https://github.com/dudikeleti/DumpMiner – Dudi Keleti Oct 26 '16 at 07:31
  • I used pretty much your same code and got some good information. My situation was that I wanted to see what was causing my production code to slowdown. I created a System.Timers.Timer that would run every 500ms and dump stackframes from all threads to the log, during a critical portion of program. – mdiehl13 Mar 17 '17 at 03:15

1 Answers1

1

The UNKNOWN indicates that the method does not have a managed method associated with it. Try to attach it to other process, also walk on all frames in all stacks, I'm sure you will find something.

Here you can find a working example of dump the stack including the stack objects.

Example of dump stack

Dudi Keleti
  • 2,946
  • 18
  • 33
  • 1. If you look at StackTrace conventional methods from inside the application, we can see a similar set, but with the names of the methods. So I started looking for ways to get them using Miсrosoft.Diagnostiсs.Runtime for all streams. 2. With these lines I found a class of types that cause these methods if (runtime.ReadPointer(frame.StackPointer, out obj)) { ClrType type = heap.GetObjectType(obj); ... } then the frame is one frame in thread.StackTrace – Евгений Oct 26 '16 at 09:23
  • If I understand correctly, one of the methods of this class is on the stack. It remains to determine which of them. – Евгений Oct 26 '16 at 09:27
  • @Евгений I'm not sure I understand your comment. Is it work or still not? Have you checked the code? The picture is a random stack that I dumped from a running WPF application (attach to process). What you see is a list of method on a specific stack and the expander show you the objects (and thier values) lying in that method – Dudi Keleti Oct 26 '16 at 09:32
  • I only see that you are complemented by objects from the heap right here ClrType type = heap.GetObjectType (obj); but it does not solve the problem – Евгений Oct 26 '16 at 09:37
  • @Евгений The `GetObjectType` is just for get the object from the pointer on the stack. You don't need to do that if you aren't interested in dereference these objects. – Dudi Keleti Oct 26 '16 at 09:40
  • I can not to build project. Maybe you just offer a solution using only Microsoft.Diagnostics.Runtime? – Евгений Oct 26 '16 at 13:13
  • @Евгений Try to replace `DebuggerSession.Instance.Runtime.Threads` with the code you wrote in the question. You just need a `ClrRuntime` to enumerate the threads. The rest of the code is ClrMD or just an object to hold the result. Check if it's help. Anyway I'll check why it doesn't compile. – Dudi Keleti Oct 26 '16 at 13:45