【问题标题】:Cython debugging, put a break pointCython调试,放一个断点
【发布时间】:2014-01-20 10:47:09
【问题描述】:

我正在尝试使用 cython 调试器设置断点:

这是我的代码:

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"])]
)

我在关注这个guide

这是我在 ubuntu shell 中所做的:

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

现在我在调试器里面,我应该可以设置一个断点,但是当我 试试:

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

应该如何正确设置断点?

更新:即使使用 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: 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

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

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

这可能是问题吗?

我使用的是 Cython 版本 0.19.1、Python 2.7.3 和 ubuntu 12.10。

【问题讨论】:

    标签: debugging gdb cython


    【解决方案1】:

    我相信您正在正确设置断点。 cython_file.so 共享库 在导入模块之前,解释器不会加载由 cython 创建的。所以 pending gdb 断点很好,因为当cython_file.so 是 gdb 时会设置这个断点 动态加载。

    更新 1:我确实根据发布的内容稍微修改了 setup.py。我的setup.py 基于这些cython debugging instructions...主要区别在于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)
    )
    

    更新 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
    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_file(请参阅我的更新) .另外,您似乎是从一开始就逐步执行 big_sum,而不是从断点开始。
    • 更新了我的 setup.py 和一个 gdb 会话,我在没有断点的情况下运行,然后使用断点集重新运行。
    • 更新了 gdb 会话以显示在 big_sum 上设置断点,然后在第一个断点被命中后在 cython_file.pyx 第 7 行设置第二个断点。
    • 我无法设置cy break :8,我能得到的最接近的是第二个for循环开始处的第11行。
    • 我仍然有同样的问题,我重新更新了我的会话。您是否像我一样运行前两行(在我的会话中)?你还跑别的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 2016-07-19
    相关资源
    最近更新 更多