Visual Studio: auto attach to a process when the p

2019-01-16 23:13发布

问题:

I want to attach to a process(a.exe) as soon as it is spawned, is it doable with VS? I only know the name of the process. Actually what I want to accomplish is set a breakpoint in c# code, but the code is belonging to another executable which will be launched by current running application(c.exe). The code is inside the initialize period so it is impossible for me to do the attach manually.

回答1:

When I've faced this situation before (and I controlled both processes), I found a decent workaround is to put a call to Debugger.Launch() in the spawning process' entry point. VS will then pop up a dialog box and let you attach to the process.



回答2:

See the MSDN article, How to: Launch the Debugger Automatically - this would allow one to skip the plethora of busywork clicking confirmation dialog boxes [without turning off UAC or other messing]. The article lists the following steps:

  1. Start the Registry Editor (regedit).
  2. In the Registry Editor, open the HKEY_LOCAL_MACHINE folder.
  3. Navigate to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\currentversion\image file execution options.
  4. In the Image File Execution Options folder, locate the name of the application you want to debug, such as myapp.exe. If you cannot find the application you want to debug:
    a. Right-click the Image File Execution Options folder, and on the shortcut menu, click New Key.
    b. Right-click the new key, and on the shortcut menu, click Rename. c. Edit the key name to the name of your application; myapp.exe, in this example.
  5. Right-click the myapp.exe folder, and on the shortcut menu, click New String Value.
  6. Right-click the new string value, and on the shortcut menu, click Rename.
  7. Change the name to debugger.
  8. Right-click the new string value, and on the shortcut menu, click Modify. The Edit String dialog box appears.
  9. In the Value data box, type vsjitdebugger.exe.
  10. Click OK.
  11. From the Registry menu, click Exit.
  12. The directory containing vsjitdebugger.exe must be in your system path. To add it to the system path, follow these steps:
    a. Open the Control Panel in Classic view, and double-click System.
    b. Click Advanced System Settings.
    c. In System Properties, click the Advanced tab.
    d. On the Advanced tab, click Environment Variables.
    e. In the Environment Variables dialog box, under System variables, select Path, then click the Edit button.
    f. In the Edit System Variable dialog box, add the directory to the Variable value box. Use a semicolon to separate it from other entries in the list.
    g. Click OK to close the Edit System Variable dialog box.
    h. Click OK to close the Environment Variables dialog box. i. Click OK to close the System Properties dialog box.
  13. Now, use any method to start your application. Visual Studio will start and load the application


回答3:

Another nice Solution is to use the Visual Studio Extension "ReAttach". Can be found here.

If your process is not currently running, ReAttach will ask you to start it and attach to it as soon as it becomes available.



回答4:

I have been looking for some way to do this when I launch a console application within an acceptance test.

I found this today - https://blogs.msdn.microsoft.com/visualstudioalm/2014/11/24/introducing-the-child-process-debugging-power-tool/

It's an add-on to visual studio, and it works a treat. When I debug an acceptance test (I use resharper test runner) and place a breakpoint within the app that gets launched, I can now debug the app in the same visual studio instance.



回答5:

"Entrian Attach" is a Visual Studio add-in that does exactly this - you tell it the name of your executable and it attaches the debugger as the process starts, regardless of how it's started, before any code has run.

(Disclosure: I'm the author. I built Attach because I have this problem all the time!)



回答6:

How about this: open project for a.exe in VS, set the breakpoints etc. Then open Project Properties for a.exe, Debugging tab, and set Command to c.exe. Then just hit Debug.

Unfortunately I never did this with managed projects, so I can be off the mark here. However, that's how I would do it with unmanaged (C++) projects. I think managed debugger should support it too.



回答7:

You can also use gflags.exe util that comes with Windows Debugging tools, all you need to do is open gflags.exe then go to image file enter the process name (a.exe) press tab and check the debugger checkbox, in the TextBox enter the vs path with the option /debugexe (i.e. "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" /debugexe)

The automatically visual studio will open once the process is running you can add your breakpoints and press Run.



回答8:

If C# code being launched by unmanaged code then make sure you check "Unmanaged code debugging" @Project properties --> debug options..



回答9:

I really liked Entrian Attach as suggested by @RichieHindle, but I also found this free tool that does something similar. It catches all processes started by the debugging process.

Spawned Process Catcher: https://marketplace.visualstudio.com/items?itemName=Brubtsov.SpawnedProcessCatcher



回答10:

As of VS 2013 SP2, there's a free tool from Microsoft, which does the same as "Spawned Process Catcher" mentioned before - attaching all processes, which are started by a process already in debugging. Note: I have only tested this with unmanaged C++ (this works flawlessly).

Microsoft Child Process Debugging Power Tool

MSDN Blog entry