views:

127

answers:

2

I have an application that spawns multiple child processes. Before launching a child, I create stdOut and stdErr handles to a log file (for example, if I am about to launch procA, i create handles to logA.log). I set these handles on the child processes.

By looking with ProcExplorer, I can see that each child process has handles to each log file (so procA has handles to logA, logB, etc.). This is creates later problems.

I want to be able to see when procA creates the handle to logB. Any ideeas?

+1  A: 

One possible solution can be that handle to file are shared between any child processes beacause they are created by the parent process.

That's the only solution I can see because I don't see how you can have multpile handle (one to each log file) in each child processes if you design so that you only have one var to handle it.

Why not creating your handles in the child processes ? I know it does respond pricesly to the question but obviously if procA only need the handles to logA it will better to create the handle to logA in the child process procA.

MarmouCorp
Thanks, you have put me on the right track. The child processes share the handles of the parent. I can't go forward with your solution, but I am trying to find a way to list all opened handles.
Bogdan Gavril
Solved, the child processes inherit the handles from the parent, as a side effect of output redirection.
Bogdan Gavril
+1  A: 

Are you asking how to break into a debugger when child process procA creates the handle to logB? I'll assume that you're using Windows since you mentioned Process Explorer.

One way to do this is to use the Image File Execution Options registry key to specify that every time procA.exe is started, you want to launch the debugger. When the debugger starts, you can set a breakpoint in the code that creates the handle to logB and then let the process continue. This works with any debugger (such as WinDbg or ntsd, or profiling tools such as AQTime), not just Visual Studio.

Another way to do this is to tell the debugger to attach to all child processes. There are several ways to enable this behavior with WinDbg or ntsd. That way, you attach the debugger to the parent process, and it will auto-attach to child process procA, and you can set a breakpoint in the appropriate code.

Yet another way is to temporarily modify your code to generate a breakpoint exception using the DebugBreak() function when it creates the handle to logB, then attach a debugger using just-in-time debugging. Note that if your code handles structured exceptions without an exception filter expression (which is a bad idea), this won't work, and may have surprising results (deadlock, memory leak, etc.).

bk1e
Nice debugging techniques!
Bogdan Gavril