windows 在 Visual Studio 中调试使用 CreateProcess 生成的进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3574853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
debugging a process spawned with CreateProcess in Visual Studio
提问by
I've created a Windows/C++/WTL application that spawns a child process. The two processes communicate via anonymous pipes.
我创建了一个生成子进程的 Windows/C++/WTL 应用程序。这两个进程通过匿名管道进行通信。
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?
由于父项目和子项目在 Visual Studio 2008 中都在同一个解决方案中,有什么方法可以告诉 VS2008 我希望调试器调试这两个进程?
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.
而且由于子进程是由父进程产生的,我想不出一个简单的方法来附加子进程(可能通过另一个 VS2008 实例)。
Any insights greatly appreciated!
非常感谢任何见解!
采纳答案by ngoozeff
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.
您可以在CreateProcess
调用周围放置一个全局命名的互斥锁,然后尝试在子进程中获取互斥锁。如果您随后在CreateProcess
调用上设置断点,则您应该有时间在它执行任何实质性操作之前附加到子项。
Note that you would miss anything that happens before main
in the child process.
请注意,您会错过main
子进程中之前发生的任何事情。
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.
您可能希望使用#if _DEBUG
或 环境变量检查来包装它。
回答by Hans Passant
Plenty of options:
多种选择:
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.
You could put a __debugbreak() in the child's code, the JIT debugger window will prompt you to select a debugger.
You can use the "Image File Execution Options" registry key to automatically launch a debugger as soon as the child .exe is started. Add a key named HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\YourApp.exe, set its "Debugger" value to "vsjitdebugger.exe"
- There is a VS add-inthat makes it easier yet, haven't tried it myself yet.
您可以使用 Debug + Attach 使用一个调试器实例来调试多个 .exe。然而,我总是使用两个 Visual Studio 实例,在第二个实例中使用 Debug + Attach。
您可以在子代码中放置一个 __debugbreak(),JIT 调试器窗口将提示您选择一个调试器。
您可以使用“Image File Execution Options”注册表项在子 .exe 启动后立即自动启动调试器。添加名为 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\YourApp.exe 的键,将其“Debugger”值设置为“vsjitdebugger.exe”
- 有一个VS 插件可以使它更容易,但我自己还没有尝试过。
回答by Ferruccio
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.
您可以临时调用DebugBreak()
子进程的启动代码中的某个位置。这将导致 Windows 提示您是否要调试该进程。