views:

162

answers:

2

Hello all,

We have a C++ Win32 application which spawns, using Qt's QProcess (undoubtedly a wrapper for CreateProcess()), a secondary 'slave' program.

Unfortunately, when debugging the system with Visual Studio 2008, the debugger does not autoamtically attach to the spawned process.

I know it's possible to programatically trigger a debugger breakpoint with __debugbreak() but what I would to know if it's possible for the slave program to throw up the 'Choose a Program to Debug This' window immediately on startup since at the moment we have to race to manually attach to the new process.

Many thanks, L

+4  A: 

Use Image File Execution Options. You can specify the VS Just-in-time debugger as the default debugger to attach to the process.

If you're into using the command-line debuggers, you can use ntsd -o to automatically debug child processes as well.

jeffamaphone
Ahh fantastic, that's certainly helped - it's now throwing up an error box (Windows cannot find '<full-path-to-exe-file>'. Make sure you typed the name correctly blah blah) but I'm certainly on the right track now; many thanks.
leegent
Yeah, you want to replace that with your actual path to the jit debugger. Should be in c:\program files\microsoft visual blah blah blah...
jeffamaphone
+1  A: 

Another neat trick that I learnt from reading "Programming Applications for Windows" by Jeffrey Richter

  1. Create a DLL with a call to DebugBreak() in DLLMain() inside DLL_PROCESS_ATTACH case
  2. Add path to your new DLL in the registry (it is a comma separated list):
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs

The image loader will create your process and load all DLLs entered in the registry. This will call DLLmain and your breakpoint will get hit.

Note: To avoid debugging all applications, check the process name with "GetModuleBasename" and call the break point only for processes you are interested.

hackworks
Since it's a documented trick, it's likely to continue to work. But be aware that you'll hit that breakpoint holding the Loader Lock.
MSalters