23

I'm debugging an apparent concurrency issue in a largish app that I hack on at work. The bug in question only manifests on certain lower-performance machines after running for many (12+) hours, and I have never reproduced it in the debugger. Because of this, my debugging tools are basically limited to analyzing log files.

C# makes it easy to get the stack trace of the thread throwing the exception, but I'd like to additionally get the stack traces of every other thread currently executing in my AppDomain at the time the exception was thrown.

Is this possible?

Drew Shafer
  • 4,740
  • 4
  • 30
  • 42
  • 3
    I was just looking for something like this myself the other day (and came upon this question: http://stackoverflow.com/questions/190236/how-do-i-make-a-thread-dump-in-net-a-la-jvm-thread-dumps). Looks like you may be out of luck, but it's still good to see what those who answered that question had to say. – Dan Tao Apr 27 '10 at 18:58
  • Thanks Dan, I pulled some pretty useful stuff from your linked question... – Drew Shafer Apr 27 '10 at 21:39

3 Answers3

7

There is a tool on CodePlex called Managed Stack Explorer (that I believe originated from Microsoft). It uses the debugging and profiling API to capture the stack traces of the threads in a running .Net application, without the need to modify the application.

You could run your application until you experience the issue, then analyse it using this tool to capture the current stack traces of all running threads. The benefit of this approach is that you leave your application unmodified (instrumenting it may change its behaviour), and the tool is free.

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • This looks like the best solution. It would certainly be nice if the framework supported it natively within the language, though... – Drew Shafer Apr 27 '10 at 21:39
  • 4
    Don't think this is supported in .NET4 anymore, even the sample app does not show up in the list on a PC with .NET4 installed. – angularsen Mar 21 '12 at 11:59
  • I just found this (yet to try) for .net4: https://github.com/artisticcheese/MSE/ – Jako Jan 31 '22 at 17:49
5

I suggest taking a dump of the process when the exception occurs. In the same place where you are logging the exception call the MakeDumpFile() method as below.

This assumes you have Debugging Tools For Windows installed on the problematic machine.

private static void MakeDumpFile()
    {            
        int pid = Process.GetCurrentProcess().Id;
        Console.WriteLine("Creating dump for pid " + pid);

        //path to adplus executable; ensure you have Debugging tools installed;
        string program = @"C:\Program Files (x86)\Debugging Tools for Windows (x86)\adplus.exe";

        //args for adplus; ensure the crashdump folder exists!
        string args = string.Format(@"-hang -p {0} -o c:\crashdump", pid);

        var startInfo = new ProcessStartInfo(program, args);
        startInfo.UseShellExecute = false;
        startInfo.ErrorDialog = false;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardOutput = true;

        var process = Process.Start(startInfo);
        Console.WriteLine("The following is output from adplus");
        Console.WriteLine(process.StandardOutput.ReadToEnd());
        Console.WriteLine("Finished creating dump.");
    }

Navigate to the dump dir and you should see a new folder with a file in it named FULLDUMP_something_.dmp.

If you are on .NET4 you can simply drag this into VS2010 and check out all the threads or use parallel threads to see what is going on (this is awesome!)

If on NET3.5 or earlier you will need to use windbg to analyse. Use the following command

~*e !clrstack

to print the callstack of all managed threads. If you need more help getting windbg going post back or google for a tutorial.

wal
  • 17,409
  • 8
  • 74
  • 109
  • When I installed the debugging tools, I found the installation path to be: C:\Program Files\Windows Kits\8.1\Debuggers\x86 – Charlie Mar 17 '14 at 17:10
1

I haven´t tried this my self but it might be of use http://www.debuginspector.com/

Jens Granlund
  • 4,950
  • 1
  • 31
  • 31