Automatically attach vs2005 debugger to a child processes - visual-studio

I have a main C++ app built in Visual Studio 2005, called A.exe. It spawns a child process, B.exe. I run process A in the debugger by hitting F5 -- the only way I know to hit breakpoints in process B is to wait for A to kick it off, then run Debug -> Attach to Process, and manually select B.exe. This doesn't work very well if I need to debug initialization code in process B -- I have to start putting in 10 second sleeps at the beginning.
Is there some trick in the vs2005 GUI that I'm missing?
I'm using native code, by the way.
Thanks,
Nathan

You can tell Windows to automatically attach the debugger when a certain process is started (by specifying the process name in a registry setting).
The details are here:
http://msdn.microsoft.com/en-us/library/a329t4ed(v=vs.100).aspx

You'd be hard pushed to make use of the debugbreak command in the child process as the debug process is not yet attached.
However, there is another that may be of use. Seeing as your creating the process, you'll have the handle to it. So give the DebugBreakProcess function a whirl.

Related

How to break a process on start

With visual studio you can attach to a running process, hit 'pause' (or called break), and even without symbol files or source, you've paused the process and can see a disassembled view.
I would like to achieve this but at the very start of the process. Attaching and pausing as quickly as possible is not the solution I'm looking for :)
For example, if the application was a console based c++ app, gdb can set a break point on main() [or any named function it can find for that matter]. Can something similar be done with visual studio?
But this question is for the more general case - I'd like to be able to start a process and have it pause immediately upon entry (immediately after the kernel launches the process).
http://blogs.msdn.com/b/greggm/archive/2008/09/12/attaching-a-debugger-at-startup.aspx
There are some level of support. But if I were you, I would use WinDbg directly.

Visual studio process Unable to copy file

i have a visual studio 2010 , when i run project normal runs, and when i close window then make some changes and run, they says
"Error 5 Unable to copy file "obj\x86\Debug\Passport.exe" to "bin\Debug\Passport.exe". The process cannot access the file 'bin\Debug\Passport.exe' because it is being used by another process. Passport"
as i see in Passport.exe still in task manager. why?
(when i end that process again i can build project)
NOTE for my other projects no that problem, only for this.
Many thanks who can help :)
There are many reasons why this could happen, but basically it boils down to your passport process or a child process of it, not closing as soon as you "close the window".
Things to check:
1. Extra threads not being terminated (worker threads not complete etc.)
2. Debug, breakpoints causing closure process to hang.
First I would remove all breakpoints and then look at what processes you may be running that could be holding up process closure e.g. worker threads, file writing, hardware access.
Sometimes it's best to kill processes using the stop button in Visual Studio.

Killing child processes when debugging ends

I have a C++ project in Visual Studio 2008 that, when executed, spawns another process (due to a GUI library that is being used). However when I exit the main process (either using Ctrl-C or by stopping the debugger), the spawned process (and hence the GUI) remains. This is a side-effect of the library being used, and the nature of the project - it is designed to be embedded, and hence never really "exit", but debugging and testing is Windows-based. The orphan process then stops me being able to run the software again, without first killing it.
Using Process Explorer I can see that the spawned process is a child of the process being debugged. I would like a way to, when I end debugging of a project, kill all child processes.
The Chromium project has a macro which will grab child processes in order to attach to them. This is designed to be associated with a button in the VS GUI, and seems to be chrome-specific (that is, you would need a different button for a different project/parent-process).
I have looked into using the DebuggerProcessEvents::OnProcessStateChanged macro, the idea being that when the debugger stops I could walk through the child processes using a similar idea to the Chromium macro and <evil voice>kill them</evil voice>... but this event never seems to be triggered.
Any thoughts? The project is Makefile-based, due to the code being generated outside of Visual Studio, so I will be limited in the options I can set project-wise.

What's the advantage for 'attach to process' compared with 'Start Debugging'?

I am new to programming.
I know only Start debug before. Maybe start debug suit for some small application develop better.
I found Visual studio IDE provide another method of attach to process for using.
When & Why must I use the attach debugging?
Such as multi-threading application debugging. Client/Service application debugging. etc. Thank you.
Sometimes you need to debug a process started by another program.
For example you need a reliable solution and in order to protect against access violations, memory leaks and other barely recoverable stuff, you have a master program and several worker programs. The master program starts the worker program and passes parameters to it. How do you debug a worker program which is not intended to be started by anything except the master program?
You use "attach to process for that".
Typically you do it this way: insert a statement that blocks the worker program for some time - for example, call Sleep() for 15 seconds. Then you kindly ask the master program to start the worker program. When the worker program is started it blocks and you now have 15 seconds to attach to it.
This way you can debug almost any issues - problems at early startup stages, wrong parameters, etc, which you wouldn't reliably reproduce with "run with debugging".
Attaching to a process is useful if you don't want to debug right from starting the process. For example, debugging usually slows down execution, so it can be quicker to start the app, get it to a state where a bug appears, and then attach a debugger.
It's also useful if you already have an external means of launching the process that you don't want or can't to import into the IDE.
Start debugging from VS launches an instance of the VS webserver and attaches the debugger to it.
Attach to process allows you to attach to any process and debug it, usually you'd do this to your instance of w3wp.exe running your code in IIS
Attach to process is mostly used when you can't run the application from Visual Studio.
For example, if it's a service or if it is a process that has run for a long time and now you want to start debugging it.
Sometimes you also want to debug a remote process, not on your machine - and you can do that using attach to process.

How can I debug a process (1.exe) running under another process (2.exe)?

1.exe doesn't give enough time for me to launch the IDE and attach 1.exe to the debugger to break into.
I would suggest taking the same approach as with NT services in this case. They will also start and usually not give you enough time to attach the debugger for the start-up routines.
Details are described here: http://www.debuginfo.com/articles/debugstartup.html
In short you add a registry entry for the second exe:
HKLM\Software\Microsoft\Windows
NT\CurrentVersion\Image File Execution
Options\2.exe Debugger =
"c:\progs\msvs\common7\ide\devenv.exe
/debugexe" (REG_SZ)
Change the c:\progrs\msms\ to match your settings.
Hope that helps.
I assume you have the source to 1.exe (if you're debugging it), then just insert a statement near the beginning that will cause it to hang around long enough to attach a debugger. ( getch() if you're desperate and it's not interactive. )
After the attach, just skip to the next statement and let it go.
You could put in some preprocessor commands for debug builds - just remember to build your release in release mode:
#ifdef DEBUG
Thread.Sleep(10000);
#endif
How is 1.exe launched? If you can launch it using CreateProcess(), you can start the process in a suspended state, attach the debugger, then release the new process.
If you are willing to consider a debugger other than Visual Studio, WinDBG can auto-debug child processes (native code only).
You did not mention what language you are using. But if you using C# or VB.NET you can add Debug.Break() or Stop to trigger the prompt to attach debugger to the process.
Or as mentioned above just use something like Console.Readline() or MessageBox.Show() to pause starting of process untill you can attach debugger to it.

Resources