2009-10-13 40 views
3

我試圖在Visual Studio中調試一個非常大的C++/c程序。改變一個參數的值會顯着改變結果。我想爲兩次運行記錄一個調用堆棧並對它們進行比較。在Visual Studio中錄製調用堆棧

有沒有人知道如何將調用堆棧轉儲到VS中的文件而無需設置斷點並在窗口中使用Select ALL/Copy?

謝謝。

回答

2

看看codeproject example,它使用了StackWalk64 API。

+0

我發現我的問題的另一種解決方案。但這種接縫很有希望。 –

+19

小心告訴你的解決方案是什麼? :) 我真的很討厭尋找一個問題,找到一個人問同樣的問題,然後回答他自己的問題,像「哦,從不知道,我找到了一個解決方案」... ... – Jan

+0

@BogdanKanivets:請你分享一下解? – user565739

2

您可以使用System.Diagnostics.StackTrace獲取當前調用堆棧的字符串表示形式並將其寫入文件。例如:

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()); 
     } 
    } 
} 

然後,每當你想寫當前棧,只需調用writeStack(somePath)

+0

這是C/C++還是C#,因此不適用? – grrussel

+0

雖然這是C#我正在尋找C#,所以這個答案仍然非常有用。 – rolls

相關問題