简体   繁体   English

从 Python (ctypes) 调试 C 库

[英]Debug C-library from Python (ctypes)

I have a Python-program that uses ctypes and a C-shared library (dll-file).我有一个使用 ctypes 和 C 共享库(dll 文件)的 Python 程序。 As an IDE, I am using Eclipse, where both projects will be developed (the C-shared library and the python program that uses it).作为 IDE,我使用 Eclipse,其中将开发两个项目(C 共享库和使用它的 Python 程序)。

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?我的想法是:当我在调试模式下启动 Python 程序时,我能否以某种方式调试用 C 编写的共享库? 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.?含义:我可以设置断点,当 Python 程序到达共享库中的断点时,执行停止并且我可以更改变量值等吗?

Actually, it is a fairly simple thing to do using the CDT and PyDev environments in Eclipse.实际上,在 Eclipse 中使用 CDT 和 PyDev 环境是一件相当简单的事情。

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.基本上,您只需要在 Debug 模式下启动 Python 项目,然后将 CDT 调试器附加到正在运行的 Python 进程。 To make it easier I'll try to describe it step by step:为了方便起见,我将尝试逐步描述它:

  1. Run your Python project in debug mode.在调试模式下运行您的 Python 项目。 Put a breakpoint somewhere after the loading of the dll using ctypes.在使用 ctypes 加载 dll 后在某处放置一个断点。 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) )记下创建的 python 进程的 pid(您应该在控制台视图中看到第一行说明 pid。类似于: pydev debugger: starting (pid: 1234)

  2. Create a Debug configuration for your CDT project, choosing the type "C/C++ Attach to Application".为您的 CDT 项目创建调试配置,选择类型“C/C++ 附加到应用程序”。 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.选择具有正确 pid 的 python 进程。

  4. You can now add breakpoints to you C code.您现在可以向 C 代码添加断点。

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.当您使用 python 调试器时,您应该始终确保 C/C++ 调试会话正在运行 - 只要 C/C++ 调试会话被挂起,python 调试器就会无响应。

As far as I know, not in eclipse.据我所知,不在日食中。

However, Python tools for visual studio has this capability:但是,用于 Visual Studio 的 Python 工具具有此功能:

https://pytools.codeplex.com/wikipage?title=Mixed-mode%20debugging 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)在 Microsoft 网站上,您需要(以及 Windows 的副本)

1) Visual Studio (paid Pro+ version or free Express versions (starting w 2.1Beta)) 1) Visual Studio(付费 Pro+ 版本或免费 Express 版本(从 2.1Beta 开始))

2) PTVS extension (this gives VS Python support) 2) PTVS 扩展(这提供了 VS Python 支持)

3) A Python interpreter and Python Libraries (these are not bundled with PTVS) 3) Python 解释器和 Python 库(这些与 PTVS 捆绑在一起)

This means that you can debug python and c side-by-side.这意味着您可以并排调试 python 和 c。 Calls to libraries written in c are captured by the debugger, provided that they were compiled with symbols by visual studio.调试器会捕获对用 c 编写的库的调用,前提是它们是由 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.请注意,本机 Python 的混合模式调试功能往往较少,但它仍然能够使用常规本机调试器。

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

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