C# Visual Studio:生成进程时自动附加到进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1951124/
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
Visual Studio: auto attach to a process when the process is spawned
提问by Bin Chen
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.
我想在生成后立即附加到进程(a.exe),VS 是否可行?我只知道进程的名称。实际上我想要完成的是在 c# 代码中设置一个断点,但该代码属于另一个将由当前运行的应用程序(c.exe)启动的可执行文件。该代码在初始化期间内,因此我无法手动进行附加。
采纳答案by Mark Simpson
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.
当我之前遇到过这种情况(并且我控制了两个进程)时,我发现一个不错的解决方法是在生成进程的入口点调用Debugger.Launch()。然后VS会弹出一个对话框让你附加到进程中。
回答by atzz
How about this: open project for a.exe
in VS, set the breakpoints etc. Then open Project Propertiesfor a.exe, Debuggingtab, and set Commandto c.exe
. Then just hit Debug.
怎么样:a.exe
在VS中打开项目,设置断点等。然后打开a.exe的项目属性,调试选项卡,并将命令设置为c.exe
。然后只需点击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.
不幸的是,我从来没有在托管项目中这样做过,所以我在这里可能不合时宜。但是,这就是我对非托管(C++)项目的处理方式。我认为托管调试器也应该支持它。
回答by Phanidhar
If C# code being launched by unmanaged code then make sure you check "Unmanaged code debugging" @Project properties --> debug options..
如果 C# 代码由非托管代码启动,请确保选中“非托管代码调试”@Project 属性 --> 调试选项..
回答by Ruben Bartelink
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:
请参阅 MSDN 文章,如何:自动启动调试器- 这将允许人们跳过过多的繁琐点击确认对话框 [无需关闭 UAC 或其他混乱]。文章列出了以下步骤:
- Start the Registry Editor (regedit).
- In the Registry Editor, open the HKEY_LOCAL_MACHINE folder.
- Navigate to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\currentversion\image file execution options.
- In the Image File Execution Optionsfolder, 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 Optionsfolder, 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.- Right-click the myapp.exefolder, and on the shortcut menu, click New String Value.
- Right-click the new string value, and on the shortcut menu, click Rename.
- Change the name to debugger.
- Right-click the new string value, and on the shortcut menu, click Modify. The Edit Stringdialog box appears.
- In the Value databox, type vsjitdebugger.exe.
- Click OK.
- From the Registrymenu, click Exit.
- 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 Panelin Classic view, and double-click System.
b. Click Advanced System Settings.
c. In System Properties, click the Advancedtab.
d. On the Advancedtab, click Environment Variables.
e. In the Environment Variablesdialog box, under System variables, select Path, then click the Editbutton.
f. In the Edit System Variabledialog box, add the directory to the Variable valuebox. Use a semicolon to separate it from other entries in the list.
g. Click OKto close the Edit System Variabledialog box.
h. Click OKto close the Environment Variablesdialog box. i. Click OKto close the System Propertiesdialog box.- Now, use any method to start your application. Visual Studio will start and load the application
- 启动注册表编辑器 (regedit)。
- 在注册表编辑器中,打开 HKEY_LOCAL_MACHINE 文件夹。
- 导航到 HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\currentversion\image 文件执行选项。
- 在Image File Execution Options文件夹中,找到要调试的应用程序的名称,例如myapp.exe。如果你不能找到应用程序要调试:
一。右键单击Image File Execution Options文件夹,然后在快捷菜单上单击New Key。
湾 右键单击新键,然后在快捷菜单上单击重命名。C。将密钥名称编辑为您的应用程序名称;myapp.exe,在本例中。- 右键单击myapp.exe文件夹,然后在快捷菜单上单击“新建字符串值”。
- 右键单击新的字符串值,然后在快捷菜单上单击重命名。
- 将名称更改为调试器。
- 右键单击新的字符串值,然后在快捷菜单上单击修改。该编辑字符串对话框出现。
- 在数值数据框中,键入vsjitdebugger.exe。
- 单击“确定”。
- 从注册表菜单中,单击退出。
- 包含 vsjitdebugger.exe 的目录必须在您的系统路径中。将它添加到系统路径,请按照下列步骤操作:
一。在经典视图中打开控制面板,然后双击系统。
湾 单击高级系统设置。
C。在系统属性中,单击高级选项卡。
d. 在高级选项卡上,单击环境变量。
e. 在Environment Variables对话框的System variables 下,选择Path,然后单击Edit按钮。
F。在里面在 Edit System Variable对话框中,将目录添加到Variable value框中。使用分号将其与列表中的其他条目分开。
G。单击“确定”关闭“编辑系统变量”对话框。
H。单击确定关闭环境变量对话框。一世。单击确定关闭系统属性对话框。- 现在,使用任何方法启动您的应用程序。Visual Studio 将启动并加载应用程序
回答by Pablo Retyk
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)
您还可以使用 Windows 调试工具附带的 gflags.exe 实用程序,您需要做的就是打开 gflags.exe 然后转到图像文件输入进程名称 (a.exe) 按 Tab 键并选中调试器复选框,在文本框中使用选项 /debugexe 输入 vs 路径(即“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.
一旦进程运行,自动视觉工作室将打开,您可以添加断点并按运行。
回答by RichieHindle
"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.
“Entrian Attach”是一个 Visual Studio 插件,它完全可以做到这一点——你告诉它你的可执行文件的名称,它会在进程开始时附加调试器,不管它是如何启动的,在任何代码运行之前。
(Disclosure: I'm the author. I built Attach because I have this problem all the time!)
(披露:我是作者。我建立了Attach,因为我一直有这个问题!)
回答by Antony Scott
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.
它是 Visual Studio 的一个附加组件,它是一种享受。当我调试验收测试(我使用 resharper 测试运行程序)并在启动的应用程序中放置一个断点时,我现在可以在同一个 Visual Studio 实例中调试应用程序。
回答by Mark Kowalski
Another nice Solution is to use the Visual Studio Extension "ReAttach". Can be found here.
另一个不错的解决方案是使用 Visual Studio 扩展“ReAttach”。可以在这里找到。
If your process is not currently running, ReAttach will ask you to start it and attach to it as soon as it becomes available.
如果您的进程当前未运行,ReAttach 会要求您启动它并在它可用时立即附加到它。
回答by Marnix
I really liked Entrian Attachas suggested by @RichieHindle, but I also found this free tool that does something similar. It catches all processes started by the debugging process.
我真的很喜欢@RichieHindle建议的 Entrian Attach,但我也发现这个免费工具可以做类似的事情。它捕获由调试进程启动的所有进程。
Spawned Process Catcher: https://marketplace.visualstudio.com/items?itemName=Brubtsov.SpawnedProcessCatcher
生成的进程捕获器:https://marketplace.visualstudio.com/items?itemName =Brubtsov.SpawnedProcessCatcher
回答by user2328447
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).
从 VS 2013 SP2 开始,Microsoft 提供了一个免费工具,它的作用与前面提到的“Spawned Process Catcher”相同 - 附加所有进程,这些进程由已在调试中的进程启动。注意:我只用非托管 C++ 测试过这个(这完美无缺)。