1

我有 C#/VB.NET 应用程序,它测试用 C++ 编写的其他应用程序。如果 C++ 应用程序没有响应,我想从中获取调用堆栈。我发现了各种用 C++ 编写的示例(例如 dbghelp.dll CaptureStackBackTraceWalking the callstack),但我在编写的C# 中一无所获。你能帮我吗?

4

2 回答 2

0

我能想到几个选项,但没有一个使用 c#。

  1. 使用 adplus 或 procdump 创建进程的转储。
  2. 使用任务管理器并右键单击以创建进程转储。请注意进程是 32 位还是 64 位,您需要使用相同位数的任务管理器进行转储。

然后使用 windbg 或 Visual Studio 查看转储。

您可以通过使用命令行参数调用其中任何一个来自动让 adplus 或 procdump 在 c# 中进行转储。这仍然无法为您提供调用堆栈,但如果您遇到涉及多个线程的死锁,您可能需要查看多个调用堆栈。

您也可以查看此链接 - 但它建议您需要编写调试器或找到一些可以执行此操作的库。

http://social.msdn.microsoft.com/Forums/en-US/90770a1c-7f83-4f81-864f-e64f3e17d02b/get-or-dispaly-call-stack-of-another-process-or-exe-file-在我的应用程序中?forum=netfxbcl

另一种选择是使用 C++ 中的“Walking the callstack”示例。您可以制作一个可执行文件,该可执行文件将进程 ID 和可能的文件名作为命令行参数(可以使用 guid),然后等待该文件被写入(不好玩 - 但可行)。或者您可以尝试使用托管 c++ 并将调用包装到非托管的东西(可能更不有趣,但可能更“正确”)。

于 2014-03-11T10:56:13.627 回答
0

这是我的团队成员的 VB.NET 实现:

  1. 下载ADPlus并将其添加到项目中。ADPlus 包含在Windows 调试工具中
  2. 使用以下代码调用它:

    Public Shared Sub DumpCallStack(processID As Integer, outputFolder As String)
    
        Const serverSymbolPath As String = "http://msdl.microsoft.com/download/symbols"
        Const localSymbolFolder As String = "c:\temp\localSymbols"
        Dim symbolFolderPath As String = String.Format("SRV*{0}* {1}", serverSymbolPath, localSymbolFolder)
    
        Directory.CreateDirectory(localSymbolFolder)
        Directory.CreateDirectory(outputFolder)
    
        Dim arguments As String = String.Format("/c Cscript //nologo ""{0}"" -quiet -quick -NoTlist -p {1} -dbg ""{2}"" -yp ""{3}"" -o ""{4}""",
                                "c:\Adplus\x64\adplus.vbs",
                                processID,
                                "CDB.exe",
                                symbolFolderPath,
                                outputFolder)
    
        Dim pro As Process = New Process()
        pro.StartInfo.FileName = "cmd.exe"
        pro.StartInfo.Arguments = arguments
        pro.StartInfo.UseShellExecute = False
        pro.StartInfo.EnvironmentVariables("_NT_SYMBOL_PATH") = symbolFolderPath
    
        pro.Start()
    
        'wait up to 1 minute for the cmd.exe to exit
        pro.WaitForExit(60000)
    
        'Wait up to 1 minute for the windgb.exe to exit
        WaitForProcessExit("cdb", 60000)
    End Sub
    
    Private Shared Sub WaitForProcessExit(processName As String, milliseconds As Integer)
        Dim pros As Process() = Process.GetProcessesByName(processName)
        If pros Is Nothing Then Return
    
        For Each pro As Process In pros
            pro.WaitForExit(milliseconds)
        Next
    End Sub
    

此调用创建包含少量文件的目录。其中之一包含来自目标应用程序的调用堆栈。

于 2014-04-16T09:42:58.633 回答