views:

51

answers:

3

I've created a Windows/C++/WTL application that spawns a child process. The two processes communicate via anonymous pipes.

I'd like to be able to debug the child process.

Since both parent and child projects are in the same solution in Visual Studio 2008, is there any way to tell VS2008 that I'd like the debugger to debug both processes?

When I start the debugger with the parent process, the debugger won't break on any breakpoints in the child process code.

And since the child process is spawned by the parent process, I can't think of an easy way of attaching the child process (maybe via another instance of VS2008) when it's spawned.

Any insights greatly appreciated!

+3  A: 

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.

Ferruccio
I tried this, windows (7, x64). When the DebugBreak() call was hit in the child process, the OS told me that the child process had stopped working via a popup messagebox. I chose to debug the application, and I can only start the debug session in a new instance of VS2008. When I do this, I can't step through the code, since "no source code is available for the current location".Nice try though!
freefallr
+1  A: 

You can debug multiple .exes with one instance of the debugger by using Debug + Attach. I however always use two instances of Visual Studio, use Debug + Attach in the second one.

Or you could put a __debugbreak() in the child's code, the JIT debugger window will prompt you. Can be a bit flaky.

Or you can use the "Image File Execution Options" registry key to automatically launch a debugger as soon as the child .exe is started:

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe]
"Debugger"="vsjitdebugger.exe"

Edit the .exe name. All of this assumes you have at least VS2005.

Hans Passant
I'd be interested in attempting Debug + Attach in order to debug multiple .exes in one instance of the debugger, but I wasn't able to determine how to do it with a child process that's spawned by the parent with CreateProcess.As per my previous comment, DebugBreak() didn't work, since the the source code wasn't available in the new instance of the debugger.Would I have more success if I broke the child out into a seperate solution file??
freefallr
Debug + View + Modules, right-click the .exe, Symbol Load Information tells you where it looked for the needed .pdb file.
Hans Passant
sorry, I don't understand debug + view + modules
freefallr
I don't understand what you don't understand. Click the menu items, start with "Debug".
Hans Passant
:) The debug menu in VS2008 has no view option?Do you mean tools->options->Debugging->Symbols?
freefallr
Oops, Debug + Windows + Modules. Click around a bit to learn what's there.
Hans Passant
thanks hans, will persist away at it here.
freefallr
+1  A: 

You could put a global named mutex around your CreateProcess call, and then try to grab the mutex in the child process. If you then put a breakpoint on the CreateProcess call, you should have time to attach to the child before it does anything substantial.

Note that you would miss anything that happens before main in the child process.

edit: As an example, something like this, untested:

// parent.cpp
HANDLE hMutex = ::CreateMutex(NULL, TRUE, "Global\\some_guid");
::CreateProcess(...);
::ReleaseMutex(hMutex); // breakpoint here
::CloseHandle(hMutex);

// child.cpp
int main(...)
{
  HANDLE hMutex = ::OpenMutex(MUTEX_ALL_ACCESS, FALSE, "Global\\some_guid");
  ::WaitForSingleObject( hMutex, INFINITE );
  ::CloseHandle(hMutex);

  ...
}

You would probably want to wrap it with #if _DEBUG or environment variable checks.

ngoozeff
excellent idea, it's surprising that VS2008 / VS2010 doesn't have a facility for this kind of thing - I would've imagined this kind of thing being a common requirement.
freefallr