Debug C-library from Python (ctypes)

前端 未结 2 941
生来不讨喜
生来不讨喜 2021-02-19 21:00

I have a Python-program that uses ctypes and a C-shared library (dll-file). As an IDE, I am using Eclipse, where both projects will be developed (the C-shared library and the py

相关标签:
2条回答
  • 2021-02-19 21:06

    As far as I know, not in eclipse.

    However, Python tools for visual studio has this capability:

    https://pytools.codeplex.com/wikipage?title=Mixed-mode%20debugging

    It is also possible to get this for free. From the Microsoft website, you'll need (as well as a copy of Windows)

    1) Visual Studio (paid Pro+ version or free Express versions (starting w 2.1Beta))

    2) PTVS extension (this gives VS Python support)

    3) A Python interpreter and Python Libraries (these are not bundled with PTVS)

    This means that you can debug python and c side-by-side. Calls to libraries written in c are captured by the debugger, provided that they were compiled with symbols by visual studio.

    Note that the capabilities of mixed mode debugging tend to be less featured for native python, but it is still capable of using the regular native debugger.

    0 讨论(0)
  • 2021-02-19 21:27

    Actually, it is a fairly simple thing to do using the CDT and PyDev environments in Eclipse.

    I assume here that you have already configured the projects correctly, so you can build and debug each one seperately.

    Basically, you simply need to start the Python project in Debug mode and then to attach the CDT debugger to the running python process. To make it easier I'll try to describe it step by step:

    1. Run your Python project in debug mode. Put a breakpoint somewhere after the loading of the dll using ctypes. Make note of the pid of the python process created (you should see a first line in the console view stating the pid. something like: pydev debugger: starting (pid: 1234))

    2. Create a Debug configuration for your CDT project, choosing the type "C/C++ Attach to Application". You can use the default configuration.

    3. Debug your project using the configuration you've created. A window should appear, asking you which process you want to attach to. Choose the python process having the right pid.

    4. You can now add breakpoints to you C code.

    You'll have two debuggers in the debug perspective, as if they were two different processes. You should always make sure the C/C++ debugging session is running when you work with the python debugger - as long as the C/C++ debugging session is suspended, the python debugger will be unresponsive.

    0 讨论(0)
提交回复
热议问题