Question

I have C#/VB.NET application which tests other application written in C++. If C++ application doesn't respond, I want to get callstack from it. I found various examples written in C++ (e.g. dbghelp.dll CaptureStackBackTrace or Walking the callstack), but I found nothing in written C#. Can you help me, please?

Was it helpful?

Solution 2

Here is VB.NET implementation from my team member:

  1. Download ADPlus and add it into project. The ADPlus is included in Debugging Tools for Windows.
  2. Call it with following code:

    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
    

This call creates directory with few files. One of them contains callstacks from target application.

OTHER TIPS

There are a few options I can think of, but none are using c#.

  1. Use adplus or procdump to create a dump of the process.
  2. Use the task manager and right click to create a dump of the process. Be careful about whether the process is 32 bit or 64 bit, you need to take a dump with the same bitness task manager.

Then look at the dump either using windbg or visual studio.

You can automate having adplus or procdump take the dump in c# by calling either of them with a command line parameter. That still doesn't get you the callstack but you may need to look at multiple callstacks if you have a deadlock involving multiple threads.

You can also look at this link - but it suggests you need to either write a debugger or find some library that does.

http://social.msdn.microsoft.com/Forums/en-US/90770a1c-7f83-4f81-864f-e64f3e17d02b/get-or-dispaly-call-stack-of-another-process-or-exe-file-in-my-apllication-in-c?forum=netfxbcl

Another option is to use the "Walking the callstack" example in c++. You can either make an executable that takes in a process id and possibly a filename as command line arguments (could use a guid) and then wait for that file to be written (not fun - but doable). Or you can try work with managed c++ and wrap the calls to the unmanaged stuff (might be even less fun, but probably more "correct").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top