Debugging processes spawned by another

I wrote a program that spawns several processes.

By default, Visual Studio does not debug new processes — only the original process that created the new ones.

Is there a way to automatically, in code, connect Visual Studio to processes when they are created?

+4
source share
3 answers

To do this, you will need to write a VS add-in or VS package, because you need to sit in the background and wait for the child processes to load.

Here is a general recipe for what you want to do:

  • Get the debuggee process id (i.e. uint processID = DTE.Debugger.CurrentProcess.ProcessID )
  • Add a link to System.Management and use ManagementEventWatcher to listen to the creation of new processes, as described in this topic . Your request should be "SELECT ProcessID FROM Win32_ProcessStartTrace WHERE ParentProcessID = " + processID
  • When a new child process loads, find it by the processID in DTE.Debugger.LocalProcesses , then call .Attach() on it.
+1
source

From: debugging a process created using CreateProcess in Visual Studio

You can temporarily call the DebugBreak () call somewhere in the startup code of your child process. This will cause Windows to request if you want to debug this process.

EDIT: Since both projects are in the same solution, configure VS for multi-project debugging: (VS2010) In the context menu, select the node solution in Solution Explorer
Choose Set Startup Projects...
In the dialog box, select the Multiple startup projects radio button
In the project grid, change the Action for all projects that you want to debug before Start

+3
source

This still provides a prompt, but attaches a debugger:

 if (!Debugger.IsAttached && DebuggerFlagSet()) Debugger.Launch(); 

and then in the parent process

 if(Debugger.IsAttached) SetDebuggerFlag() 

You will need a mechanism for the debugger flag, such as a file on disk / reg-key / mutex, etc.

The debugger will not be the same as the original one.

+2
source

Source: https://habr.com/ru/post/1401202/


All Articles