locked
Custom Unittest-Adapter: how to attach to unmanaged process RRS feed

  • Question

  • In the TestExecutor of my unittest adapter I create a new System.Diagnostics.Process and run my program. The actual test binary is run from a test runner (cmake). So I don't want to attach to the process directly, but to any of its children. I can get all the children and attach to them like this:

    Process process; // my "ctest" process
    DTE dte;
    try {
        List<Process> ctestChildren = ProcessExtensions.GetChildProcesses(process);
        var dteChildren = dte.Debugger.LocalProcesses;
        foreach (EnvDTE.Process dteChild in dteChildren)
        {
            foreach (var ctestChild in ctestChildren)
            {
                if (dteChild.ProcessID == ctestChild.Id)
                {
                    frameworkHandle.SendMessage(TestMessageLevel.Informational,
                        $"TestExecutor.RunTests: attaching to process ({ctestChild.Id}) ...");
                    try {
                        dteChild.Attach();
                        frameworkHandle.SendMessage(TestMessageLevel.Informational,
                            $"TestExecutor.RunTests: ... done");
                        processId = ctestChild.Id;
                    }
                    catch (COMException)
                    {
                        frameworkHandle.SendMessage(TestMessageLevel.Informational,
                            $"TestExecutor.RunTests: ... failed");
                    }
                    break;
                }
            }
            if(processId != 0)
            {
                break;
            }
        }
    }
    catch (COMException)
    {
        System.Threading.Thread.Sleep(1000);
    }
    

    But this only works for managed/C# binaries.

    How can I attach with the debugger to a plain unmanaged executable so it will stop at the breakpoints I define?

    Thursday, January 21, 2016 10:49 AM

Answers

  • Ok, it turns out that everything works fine just as shown above ...

    as long as the process to attach to isn't finished too early :-(. So this question changes to: how do I monitor a process and attach any child processes to the debugger as fast as possible (if not directly on creation)?

    • Marked as answer by micst Thursday, January 21, 2016 4:02 PM
    Thursday, January 21, 2016 4:01 PM

All replies

  • Ok, it turns out that everything works fine just as shown above ...

    as long as the process to attach to isn't finished too early :-(. So this question changes to: how do I monitor a process and attach any child processes to the debugger as fast as possible (if not directly on creation)?

    • Marked as answer by micst Thursday, January 21, 2016 4:02 PM
    Thursday, January 21, 2016 4:01 PM
  • Thanks for sharing your solution here, micst!

    Have a nice day,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Saturday, January 23, 2016 3:36 AM