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
is set as C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\mscordacwks.dll