Can I get the stack traces of all threads in my c# app?

后端 未结 3 1152
北恋
北恋 2021-02-05 05:19

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

3条回答
  •  遥遥无期
    2021-02-05 06:01

    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.

提交回复
热议问题