简体   繁体   中英

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. I seem to be in a more specific case that the other existing similar questions

Context :

  • I want to debug my C++ DLLs
  • They are wrapped in C# using swig
  • I also build the C# plugin for the app that uses the wrapped C# dll
  • 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)
  • 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

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

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. swig (or the plugin system of the managed app) might be doing some magic to load c++ dlls

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...". 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). Then open property pages for the project and under Debugging, in the Command field enter the full path to the application executable. Enter any command line arguments needed in the Command Arguments field. Ensure also that your DLL project is generating PDBs under C++/General. Clean and rebuild the DLL project.

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). 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. When the DLL and its PDBs are loaded, the breakpoints should appear opaque and will be hit when the line of code executes.

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. Then relaunch the debugger. If your application programmatically sets a working folder, try copying the DLL/PDB to this location also.

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. It's still possible to debug release mode code, albeit sometimes difficult due to optimisations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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