Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recording call stack in Visual Studio

I'm trying to debug a really large c++/c program in Visual Studio. Changing value of one parameter changes the result dramatically. I want to record a call stacks for both runs and diff them.

Does anybody know how to dump a call stack to a file in VS without setting breakpoints and using Select ALL/Copy in the window?

Thanks.

like image 645
Bogdan Kanivets Avatar asked Oct 15 '25 09:10

Bogdan Kanivets


2 Answers

You can use System.Diagnostics.StackTrace to get a string representation of the current call stack and write that to a file. For example:

private static writeStack(string file)
{
    StackTrace trace = new StackTrace(true); // the "true" param here allows you to get the file name, etc.
    using (StreamWriter writer = new StreamWriter(file))
    {
    for (int i = 0; i < trace.FrameCount; i++)
        {
            StackFrame frame = trace.GetFrame(i);
            writer.WriteLine("{0}\t{1}\t{2}", frame.GetFileName(), frame.GetFileLineNumber(), frame.GetMethod());
        }
    }
}

Then, whenever you want to write the current stack, just call writeStack(somePath).

like image 82
Justin R. Avatar answered Oct 18 '25 05:10

Justin R.


Take a look at this codeproject example which uses the StackWalk64 API.

like image 25
Dolphin Avatar answered Oct 18 '25 06:10

Dolphin