Cython调试,放一个断点 [英] Cython debugging, put a break point

查看:896
本文介绍了Cython调试,放一个断点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的代码:



我正在尝试使用cython调试器放入一个断点: p> cython_file.pyx

  cimport cython 

def big_sum() :
cdef int a [10000]

for i in range(10000):
a [i] = i
#< ======= =============我想在这里打破
cdef int my_sum
my_sum = 0
为我在范围(1000):
my_sum + = a [i]
return my_sum

python_file.py

 从cython_file import big_sum 

result = big_sum()
print result

setup.py



从distutils.core导入设置
从distutils.extension import扩展
从Cython.Distutils导入build_ext

设置(
cmdclass = {'build_ext':build_ext},
ext_modules = [Extension(cython_file,
[cython_file.pyx],pyrex_gdb = True,
extra_compile_args = [ - g],extra_link_args = [ - g])

我正在关注这个指南



这是我在ubuntu shell:

  cython --gdb cython_file.pyx 
python setup.py build_ext --inplace
cygdb

现在我在调试器中,我应该能够放在一个断点,但是当我
尝试:

 (gdb)cy break cython_file.big_sum:8 

我得到这个错误:

函数__pyx_pw_11cython_file_1big_sum未定义。
断点1(__pyx_pw_11cython_file_1big_sum)待处理。
当前未选择帧。

如何正确设置断点?



更新:即使我使用Drew McInnis提供的setup.py,我仍然有问题:

  user @ Ubuntu-K56CA:〜/ PythonStuff / CythonStuff / cython_debug_2 $ cython --gdb cython_file.pyx 
user @ Ubuntu-K56CA:〜/ PythonStuff / CythonStuff / cython_debug_2 $ python setup.py build_ext --inplace
/usr/lib/python2.7/distutils/dist.py:267:UserWarning:未知分发选项:'extensions'
warnings.warn(msg)
运行build_ext
构建'cython_file'扩展
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I / usr / include / python2.7 -c cython_file.c - o build / temp.linux-x86_64-2.7 / cython_file.o
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build /temp.linux-x86_64-2.7/cython_file.o -o /home/user/PythonStuff/CythonStuff/cython_debug_2/cython_file.so
user @ Ubuntu-K56CA:〜/ PythonStuff / CythonStuff / cython_debug_2 $ cygdb。
GNU gdb(GDB)7.5-ubuntu
版权所有(C)2012免费软件基金会,
许可证GPLv3 +:GNU GPL版本3或更高版本< http://gnu.org/ licenses / gpl.html>
这是免费软件:您可以自由更改并重新分发。
在法律允许的范围内,没有任何保证。输入显示复制
和显示保修的详细信息。
这个GDB被配置为x86_64-linux-gnu。
有关错误报告说明,请参阅:
< http://www.gnu.org/software/gdb/bugs/> ;.
(gdb)cy run python_file.py
499500


(gdb)cy break cython_file.big_sum
断点1在0x7ffff63e7780:文件cython_file.c ,第649行
(gdb)cy run python_file.py
1 cimport cython

我注意到我收到这个警告:

  user @ Ubuntu-K56CA:〜/ PythonStuff / CythonStuff / cython_debug_2 $ python setup .py build_ext --inplace 

/usr/lib/python2.7/distutils/dist.py:267:UserWarning:未知分发选项:'extensions

可能是问题吗?



我正在使用Cython版本0.19.1,Python 2.7 .3和ubuntu 12.10。

解决方案

我相信你正确设置断点。由cyton创建的 cython_file.so 共享库
在导入模块之前不被解释器加载。因此,当 cython_file.so
动态加载时,gdb将会设置此断点。



更新1 :我已经从发布的内容稍微修改了 setup.py 。我根据这些 setup.py cython调试指令 ...主要区别在于使用 cythonize



setup.py

  from distutils.core import setup 
from distutils.extension import Extension
从Cython.Build导入cythonize

设置(
extensions = [Extension('cython_file',[cython_file.pyx])],
ext_modules = cythonize(Extension cython_file,[cython_file.pyx]),
gdb_debug = True)

更新3 :为了参考,这里是我用来获取设置的命令。它们与问题中发布的略有不同,因为我在运行setup.py时没有添加 - pyrex-gdb 选项:

  $ ls 
cython_file.pyx python_file.py setup.py
$ cython --gdb cython_file.pyx
$ python setup。 py build_ext --inplace

更新2 :这是我的示例 cygdb 会话使用您的文件,首先我让 python_file.py 运行完成,然后我设置断点并重新运行:

  drew @ ubuntu:〜/ stackoverflow / 21033553-cython $ cygdb。 

GNU gdb(Ubuntu / Linaro 7.4-2012.04-0ubuntu2.1)7.4-2012.04
版权所有(C)2012免费软件基金会,
许可证GPLv3 +:GNU GPL版本3或更新版本< http://gnu.org/licenses/gpl.html>
这是免费软件:您可以自由更改并重新分发。
在法律允许的范围内,没有任何保证。输入显示复制
和显示保修的详细信息。
这个GDB被配置为x86_64-linux-gnu。
有关错误报告说明,请参阅:
< http://bugs.launchpad.net/gdb-linaro/> ;.
安装用于着色源代码的pygments。
Python没有使用调试符号编译(或被剥离)。
某些功能可能无法正常工作(正常)。
(gdb)cy run python_file.py
499500

(gdb)cy break cython_file.big_sum
断点1在0x7ffff5db0270:文件cython_file.c,第435行。
当前未选择帧。
(gdb)cy run python_file.py
3 def big_sum():
(gdb)cy break:10
断点2在0x7ffff5db02a6:文件cython_file.c,行468。
(gdb)cy cont
11 for i in range(1000):
(gdb)cy list
6 for i in range(10000):
7 a [i] = i
8#< ====================我想在这里打破
9 cdef int my_sum
10 my_sum = 0
> 11为我在范围(1000):
12 my_sum + = a [i]
13返回my_sum
14


I am trying to use cython debugger to put in a break point:

Here is my code:

cython_file.pyx

cimport cython

def big_sum():
    cdef int a[10000]

    for i in range(10000):
        a[i] = i
    # <==================== I want to put a break here     
    cdef int my_sum
    my_sum = 0
    for i in range(1000):
        my_sum += a[i]
    return my_sum

python_file.py

from cython_file import big_sum

result = big_sum()
print result

setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("cython_file",
                             ["cython_file.pyx"], pyrex_gdb=True,
                             extra_compile_args=["-g"], extra_link_args=["-g"])]
)

I was following this guide:

This is what I did in the ubuntu shell:

cython --gdb cython_file.pyx
python setup.py build_ext --inplace
cygdb

Now I am inside the debugger, and I should be able to put in a break point, but when i try:

(gdb) cy break cython_file.big_sum :8

I get this error:

Function "__pyx_pw_11cython_file_1big_sum" not defined.
Breakpoint 1 (__pyx_pw_11cython_file_1big_sum) pending.
No frame is currently selected.

How should I set the break point correctly?

Update: I still have a problem even when I use setup.py provided by Drew McInnis:

user@Ubuntu-K56CA:~/PythonStuff/CythonStuff/cython_debug_2$ cython --gdb cython_file.pyx
user@Ubuntu-K56CA:~/PythonStuff/CythonStuff/cython_debug_2$ python setup.py build_ext --inplace
/usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'extensions'
  warnings.warn(msg)
running build_ext
building 'cython_file' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c cython_file.c -o build/temp.linux-x86_64-2.7/cython_file.o
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-x86_64-2.7/cython_file.o -o /home/user/PythonStuff/CythonStuff/cython_debug_2/cython_file.so
user@Ubuntu-K56CA:~/PythonStuff/CythonStuff/cython_debug_2$ cygdb .
GNU gdb (GDB) 7.5-ubuntu
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) cy run python_file.py
499500


(gdb) cy break cython_file.big_sum
Breakpoint 1 at 0x7ffff63e7780: file cython_file.c, line 649.
(gdb) cy run python_file.py
1    cimport cython

I notice that I get this warning:

user@Ubuntu-K56CA:~/PythonStuff/CythonStuff/cython_debug_2$ python setup.py build_ext --inplace

/usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'extensions

Could that be the problem?

I am using Cython version 0.19.1, Python 2.7.3, and ubuntu 12.10.

解决方案

I believe you are setting the breakpoint correctly. The cython_file.so shared library created by cython isn't loaded by the interpreter until the module is imported. So the pending gdb breakpoint is fine as gdb will set this breakpoint when cython_file.so is dynamically loaded.

UPDATE 1: I did modify the setup.py slightly from what was posted. I based my setup.py on these cython debugging instructions... the main difference being the use of cythonize:

setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

setup(
    extensions = [Extension('cython_file', ["cython_file.pyx"])],
    ext_modules=cythonize(Extension("cython_file", ["cython_file.pyx"]),
                          gdb_debug=True)
)

UPDATE 3: For reference, here are the commands I used to get setup. They differ slightly from those posted in the question as I didn't add the --pyrex-gdb option when running setup.py:

$ ls
cython_file.pyx  python_file.py  setup.py
$ cython --gdb cython_file.pyx 
$ python setup.py build_ext --inplace

UPDATE 2: Here is my sample cygdb session using your files, first I let python_file.py run to completion and then I set the breakpoint and re-ran:

drew@ubuntu:~/stackoverflow/21033553-cython$ cygdb .

GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>.
Install pygments for colorized source code.
Python was not compiled with debug symbols (or it was stripped). 
Some functionality may not work (properly).
(gdb) cy run python_file.py
499500

(gdb) cy break cython_file.big_sum
Breakpoint 1 at 0x7ffff5db0270: file cython_file.c, line 435.
No frame is currently selected.
(gdb) cy run python_file.py
3    def big_sum():
(gdb) cy break :10
Breakpoint 2 at 0x7ffff5db02a6: file cython_file.c, line 468.
(gdb) cy cont
11        for i in range(1000):
(gdb) cy list
     6        for i in range(10000):
     7            a[i] = i
     8        # <==================== I want to put a break here
     9        cdef int my_sum
    10        my_sum = 0
>   11        for i in range(1000):
    12            my_sum += a[i]
    13        return my_sum
    14    

这篇关于Cython调试,放一个断点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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