简体   繁体   English

我如何调试在外部C#应用程序中切换的C ++ DLL(无法从调试器启动)

[英]How can i debug C++ DLLs SWIGed in external C# app (that can not be started from debugger)

I can't figure how to debug the C++ part of code that is used in a managed app. 我无法想象如何调试托管应用程序中使用的C ++部分代码。 I seem to be in a more specific case that the other existing similar questions 我似乎在一个更具体的情况下,其他现存的类似问题

Context : 背景:

  • I want to debug my C++ DLLs 我想调试我的C ++ DLL
  • They are wrapped in C# using swig 他们用Swig包裹在C#中
  • I also build the C# plugin for the app that uses the wrapped C# dll 我还为使用包装的C#dll的应用程序构建了C#插件
  • The app can not be launched from the debugger, i have to attach it afterwards 该应用程序无法从调试器启动,我必须在之后附加它
  • I built everything in debug and x64 mode in VS2015 (The c# dll is actually in 'AnyCPU' platform, with 'Prefer-32-bit' unticked) 我在VS2015中以调试和x64模式构建了所有内容(c#dll实际上位于'AnyCPU'平台中,'Prefer-32-bit'未标记)
  • I am in proper environmeent for the code to be run (other things of the code work), it's only a bug in the C++ part that i want to debug when it happens from the C# part 我正处于运行代码的适当环境中(代码工作的其他部分),它只是C ++部分中的一个错误,当它从C#部分发生时我想调试它

I tried to start the program as an external debug program of the C++ DLL (specifying 'mixed' debugging, or as an external debug program of the C# dll (enabling 'native code debugging'). But i think those options might ge ignored since i can not start the exe in debugger, just attach to the process afterwards. And i attached the process selecting Native and Managed debugger. I can only break in my managed code 我尝试将程序作为C ++ DLL的外部调试程序启动(指定'混合'调试,或作为C#dll的外部调试程序(启用'本机代码调试')。但我认为这些选项可能会被忽略,因为我无法在调试器中启动exe,只是在事后附加到进程。我附加了选择Native和Managed调试器的进程。我只能破解我的托管代码

In the Debug output window of VS 2015, i don't see that the executable tries to load my C++ dlls, but i see them loaded when looking in process explorer. 在VS 2015的Debug输出窗口中,我没有看到可执行文件试图加载我的C ++ dll,但我看到它们在查看进程资源管理器时被加载。 swig (or the plugin system of the managed app) might be doing some magic to load c++ dlls swig(或托管应用程序的插件系统)可能正在加载c ++ dll

Any trick to help? 有帮助的技巧吗?

You say "I tried to start the program as an external debug program of the C++ DLL... But i think those options might ge ignored since i can not start the exe in debugger...". 您说“我试图将程序作为C ++ DLL的外部调试程序启动...但是我认为这些选项可能会被忽略,因为我无法在调试器中启动exe ...”。 I think this may be where you're going wrong. 我想这可能是你出错的地方。

You need to set the C++ DLL project as the startup project (right-click the project and choose Set as StartUp Project). 您需要将C ++ DLL项目设置为启动项目(右键单击该项目,然后选择“设置为启动项目”)。 Then open property pages for the project and under Debugging, in the Command field enter the full path to the application executable. 然后打开项目的属性页面,在Debugging下,在Command字段中输入应用程序可执行文件的完整路径。 Enter any command line arguments needed in the Command Arguments field. 输入Command Arguments字段中所需的任何命令行参数。 Ensure also that your DLL project is generating PDBs under C++/General. 还要确保您的DLL项目在C ++ / General下生成PDB。 Clean and rebuild the DLL project. 清理并重建DLL项目。

Set a break point in a function you expect to be called - ideally somewhere near the entry point, and a further breakpoint in the code you want to debug. 在您希望调用的函数中设置断点 - 理想情况下,在入口点附近的某个位置,以及要调试的代码中的另一个断点。 Now start debugging (F5). 现在开始调试(F5)。 The application should run (breakpoints will probably appear "hollowed out" with a message that the breakpoint didn't bind or similar - don't worry about this yet). 应用程序应该运行(断点可能会显示为“挖空”,并显示断点未绑定或类似的消息 - 不要担心这一点)。 Perform any actions required in the application to invoke the native DLL. 执行应用程序中所需的任何操作以调用本机DLL。 When the DLL and its PDBs are loaded, the breakpoints should appear opaque and will be hit when the line of code executes. 加载DLL及其PDB时,断点应显示为不透明,并在执行代码行时被击中。

If the break point isn't hit/doesn't bind, I would suggest copying the C++ DLL and its PDB to the application executable folder to reduce the chance of the wrong DLL being loaded. 如果断点未命中/未绑定,我建议将C ++ DLL及其PDB复制到应用程序可执行文件夹中,以减少加载错误DLL的机会。 Then relaunch the debugger. 然后重新启动调试器。 If your application programmatically sets a working folder, try copying the DLL/PDB to this location also. 如果您的应用程序以编程方式设置工作文件夹,请尝试将DLL / PDB复制到此位置。

If this fails, it could be because the caller targets a configuration incompatible with the callee. 如果失败,则可能是因为呼叫者针对的目标与被呼叫者不兼容。 In general it is not advisable to mix debug and release mode code especially when marshalling data. 通常,不建议混合使用调试和释放模式代码,尤其是在编组数据时。 Rebuild the DLL in release mode, including PDBs, and run through the instructions above. 在发布模式(包括PDB)中重建DLL,并按照上述说明运行。 It's still possible to debug release mode code, albeit sometimes difficult due to optimisations. 尽管有时由于优化而很难,但是仍然可以调试发布模式代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM