2014-01-09 53 views
10

我想用用Cython調試器放在一個破發點:用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外殼做:

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. 

我該如何正確設置斷點?

更新:我還有一個問題,甚至當我使用由德魯·麥金尼斯提供setup.py:

[email protected]:~/PythonStuff/CythonStuff/cython_debug_2$ cython --gdb cython_file.pyx 
[email protected]:~/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 
[email protected]:~/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 

我注意到,我得到這樣的警告:

[email protected]:~/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。

回答

4

我相信你正確設置斷點。直到模塊被導入之前,由cython創建的cython_file.so共享庫 不會被解釋器加載。所以等待 gdb斷點沒問題,因爲動態加載cython_file.so爲 時,gdb將設置此斷點。

更新1:我修改了setup.py從發佈的內容略微。我對這些cython debugging instructions基於我setup.py ...的主要區別在於使用的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:作爲參考,下面是我用的命令得到設置。它們與問題中發佈的內容略有不同,因爲我在運行安裝程序時沒有添加--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運行完,然後我設置的斷點,並重新運行:

[email protected]:~/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  
+0

謝謝對於你的帖子有幫助,但是當我運行確切的東西時,我得不到同樣的東西,我甚至無法得到cython_file(請參閱我的更新)。另外,你似乎從一開始就通過big_sum,而不是從中斷點開始。 – Akavall

+0

用我的setup.py和一個gdb會話更新,我沒有斷點運行,然後用斷點集重新運行。 –

+0

更新了gdb會話以在big_sum上顯示設置斷點,然後在命中第一個斷點時在cython_file.pyx第7行設置第二個斷點。 –