从Python调试C库(ctypes) [英] Debug C-library from Python (ctypes)

查看:126
本文介绍了从Python调试C库(ctypes)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Ctypes和C-shared库(dll-file)的Python程序。作为IDE,我正在使用Eclipse,这两个项目都将被开发(C共享库和使用它的python程序)。



我的想法是:我在Debug模式下启动Python程序,我可以以某种方式调试共享库,这也是用C编写的?含义:可以设置断点,当Python程序到达共享库中的断点时,执行停止,我可以更改变量值等。



感谢您的帮助

解决方案

实际上,使用Eclipse中的CDT和PyDev环境是一件非常简单的事情。 b
$ b

我假设您已经正确配置了项目,因此您可以单独构建和调试每个项目。



基本上,你只需要在调试模式下启动Python项目,然后将CDT调试器附加到正在运行的python进程。为了更容易,我会尝试一步一步地描述它:


  1. 以调试模式运行您的Python项目。在使用ctypes加载dll之后放置一个断点。记下创建的python进程的pid(你应该在控制台视图中看到一个第一行,说明pid,例如: pydev debugger:starting(pid:1234)


  2. 为您的CDT项目创建一个调试配置,选择类型C / C ++附加到应用程序。您可以使用默认配置。


  3. 使用您创建的配置调试项目。应该出现一个窗口,询问你要附加哪个进程。选择具有正确pid的python进程。


  4. 现在可以为C代码添加断点。


调试透视图中将有两个调试器,就好像它们是两个不同的进程。使用python调试器时,应始终确保C / C ++调试会话正在运行 - 只要C / C ++调试会话被暂停,python调试器将无响应。


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.?

Thanks for the help.

解决方案

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.

这篇关于从Python调试C库(ctypes)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆