How to debug a Windows DLL used inside Python?

2019-06-20 17:45发布

问题:

On Windows7 I have a python script that uses Windows DLLs, using the .NET Common Language Runtime (CLR). An error occurs inside one of the used DLL, but the standard Python debugger only debugs on the Python code level (and not the DLL).

How can I debug what is going on inside the DLL(s)?

回答1:

If you have Microsoft Visual Studio available,

1) open the Visual Studio project that your DLL is part of (or create a new project).

2) If you have set up your DLL for debugging (you've built it with debugging info, and it will be the one that your python program will use), you can set breakpoints in the DLL code.

3) Start the program you want to debug as you would do normally.

4) Go back to the Visual Studio IDE and go to the Debug menu. Choose the Attach to Process option. You will then get a list of all the running processes.

5) Choose the process you want to debug, which will be your python program, or runtime that is running your program.

6) Sit back and wait for one of your breakpoints to be hit, or you can try a Break All from the Debug menu to temporarily halt the program.

This is a general way of starting out debugging not only python programs, but any program where you need to debug a DLL that is being used by the program.

Note that the above advice works best if you have built the DLL yourself with debugging information and is being utilized by your python application. If it is a third-party DLL where you have no source code, you can still debug from Visual Studio, but will need to know assembly language (since the source code is usually not available).



回答2:

These 3 debugging scenarios with pythonnet are based on PTVS docs:

https://github.com/pythonnet/pythonnet/wiki/Various-debugging-scenarios-of-embedded-CPython