Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug C-library from Python (ctypes)

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 python program that uses it).

My idea is: when I start the Python-program in Debug-mode, can I somehow debug the shared library, which is written in C, too? Meaning: Can I set breakpoints and when the Python-program reaches that breakpoint in the shared library, executing stops and I can change variable values etc.?

like image 784
Berschi Avatar asked Aug 26 '14 14:08

Berschi


People also ask

What is Ctypes library in Python?

ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.

What is CDLL in Python?

It is a big C++ codebase with a (very) thin python wrapper which uses CDLL to load the C++ and call some C functions that are available to allow primitive python scripting of the code.


2 Answers

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.

like image 142
Adam Avatar answered Oct 18 '22 07:10

Adam


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.

like image 44
undershock Avatar answered Oct 18 '22 06:10

undershock