4

I have written a program that spawns a handful of processes.

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

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

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202

3 Answers3

3

From: debugging a process spawned with CreateProcess in Visual Studio

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

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

Community
  • 1
  • 1
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • I don't want to do something temporary; in my parent process, I want to check if it is being debugged and automatically attach the debugger. – David Pfeffer Mar 13 '12 at 16:39
  • Well this answer solves it from the child side.. but `Temporary` is all about whether you remove the code or not.. completely up to you. – Sam Axe Mar 13 '12 at 16:43
  • 1
    Even with the popup, it'll be a second debugger, not the primary debugger instance. – David Pfeffer Mar 13 '12 at 16:53
  • 1
    As per the original question, the processes are spawned by my main application. I can't start it up in VS. – David Pfeffer Mar 14 '12 at 12:17
  • As a side note, you can achieve the same effect as temporarily adding DebugBreak(), but without having to change code and recompile, by using GFlags http://bugslasher.net/2011/03/26/how-to-debug-a-process-as-soon-as-it-starts-with-windbg-or-visual-studio-2010/ – Omer Raviv Mar 18 '12 at 12:53
2

This still provides a prompt, but does attach the debugger:

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

and then in the parent process

if(Debugger.IsAttached)
    SetDebuggerFlag()

You'll need a mechanism for the debugger flag such as a file on disk/reg key/mutex etc.

The debugger being launched won't be the same as the initial instance.

James
  • 9,774
  • 5
  • 34
  • 58
1

To do this, you'll have to write a VS Add-in or a VS Package, because you'll want to sit in the background and wait for child processes to load.

Here's a general recipe of what you'll want to do:

  1. Get the ID of the debuggee process (ie, uint processID = DTE.Debugger.CurrentProcess.ProcessID)
  2. Add reference to System.Management and use a ManagementEventWatcher to listen for new processes creation as described in this thread. Your query should be "SELECT ProcessID FROM Win32_ProcessStartTrace WHERE ParentProcessID = " + processID
  3. When a new child process loads, find it by its processID in DTE.Debugger.LocalProcesses, then call .Attach() on it.
Community
  • 1
  • 1
Omer Raviv
  • 11,409
  • 5
  • 43
  • 82