准系统 SWIG python C 接口在 OSX+clang 上出现段错误,而不是在 Linux+gcc 中
Posted
技术标签:
【中文标题】准系统 SWIG python C 接口在 OSX+clang 上出现段错误,而不是在 Linux+gcc 中【英文标题】:Barebones SWIG python C interface segfaulting on OSX+clang, not in Linux+gcc 【发布时间】:2014-08-17 13:55:33 【问题描述】:我在 ***.com 上注意到了几个类似的问题,但不幸的是,它们并没有解决我手头的问题:
SWIG & C/C++ Python API connected - SEGFAULT
how to investigate python2 segfault on imp.load_module
我正在尝试构建一个可在 OSX(w/clang)和 Linux(w/gcc)上运行的准系统 C-python SWIG 界面,但我在 OSX 上没有这样做。
请克隆以下 repo 并在本地测试:
https://github.com/brainstorm/swig-python-skel
以下是在我的 Macbook Air 上使用最新的开发工具和 clang 运行 make
时发生的情况:
$ uname -a
Darwin mba 13.3.0 Darwin Kernel Version 13.3.0: Tue Jun 3 21:27:35 PDT 2014; root:xnu- 2422.110.17~1/RELEASE_X86_64 x86_64
$ cc -v
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
$ make
cc -O3 -Wall -g -fPIC -c 'example'.c -o _'example'.so
swig -Wall -python -builtin -module 'example' -o 'example'_wrap.c 'example'.i
cc -O3 -Wall -g -fPIC -I/Users/roman/.anaconda/anaconda/include/python2.7 -I/Users/roman/.anaconda/anaconda/include/python2.7 -c 'example'_wrap.c
cc -shared -undefined dynamic_lookup -O3 -Wall -g -fPIC -I/Users/roman/.anaconda/anaconda/include/python2.7 -I/Users/roman/.anaconda/anaconda/include/python2.7 -o _'example'.so 'example'_wrap.o -lpython
python -c 'import 'example'' && echo "Python build is ok!"
/bin/sh: line 1: 11917 Segmentation fault: 11 (core dumped) python -c 'import 'example''
make: *** [test-python] Error 139
在 OSX 中生成核心文件的结果回溯(在强制 ulimit -c unlimited
能够生成核心文件之后):
$ lldb -c /cores/core.11917
(lldb) bt
* thread #1: tid = 0x0000, 0x0000000000000000, stop reason = signal SIGSTOP
* frame #0: 0x0000000000000000
frame #1: 0x000000010068637d Python`PyEval_GetGlobals + 23
frame #2: 0x000000010064b9ed Python`___lldb_unnamed_function1014$$Python + 954
frame #3: 0x000000010064b0f9 Python`___lldb_unnamed_function1007$$Python + 50
frame #4: 0x00000001002f496b _example.so`init_example + 75 at example_wrap.c:4117
frame #5: 0x00000001000e5082 libpython2.7.dylib`_PyImport_LoadDynamicModule + 178
frame #6: 0x00000001000e499a libpython2.7.dylib`imp_load_module + 186
frame #7: 0x00000001000c8f49 libpython2.7.dylib`PyEval_EvalFrameEx + 24313
frame #8: 0x00000001000c983b libpython2.7.dylib`PyEval_EvalFrameEx + 26603
frame #9: 0x00000001000cad35 libpython2.7.dylib`PyEval_EvalCodeEx + 1733
frame #10: 0x00000001000cb036 libpython2.7.dylib`PyEval_EvalCode + 54
frame #11: 0x00000001000e17b1 libpython2.7.dylib`PyImport_ExecCodeModuleEx + 209
frame #12: 0x00000001000e2502 libpython2.7.dylib`load_source_module + 626
frame #13: 0x00000001000e2f66 libpython2.7.dylib`import_submodule + 310
frame #14: 0x00000001000e31ba libpython2.7.dylib`load_next + 234
frame #15: 0x00000001000e3c45 libpython2.7.dylib`PyImport_ImportModuleLevel + 581
frame #16: 0x00000001000bc573 libpython2.7.dylib`builtin___import__ + 131
frame #17: 0x000000010000d382 libpython2.7.dylib`PyObject_Call + 98
frame #18: 0x00000001000c1fd7 libpython2.7.dylib`PyEval_CallObjectWithKeywords + 87
frame #19: 0x00000001000c556e libpython2.7.dylib`PyEval_EvalFrameEx + 9502
frame #20: 0x00000001000cad35 libpython2.7.dylib`PyEval_EvalCodeEx + 1733
frame #21: 0x00000001000cb036 libpython2.7.dylib`PyEval_EvalCode + 54
frame #22: 0x00000001000f0db8 libpython2.7.dylib`PyRun_StringFlags + 280
frame #23: 0x00000001000f0ea1 libpython2.7.dylib`PyRun_SimpleStringFlags + 65
frame #24: 0x0000000100107dc2 libpython2.7.dylib`Py_Main + 1586
frame #25: 0x0000000100000f54 python`start + 52
(lldb)
另一方面,在普通的x86_64
Linux 机器上,相同的代码并且完全没有问题:
$ uname -a
Linux milou 2.6.32-431.20.3.el6.x86_64 #1 SMP Thu Jun 19 14:01:59 CDT 2014 x86_64 x86_64 x86_64 GNU/Linux
$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
$ make
cc -O3 -Wall -g -fPIC -c 'example'.c -o _'example'.so
swig -Wall -python -builtin -module 'example' -o 'example'_wrap.c 'example'.i
cc -O3 -Wall -g -fPIC -I/home/roman/.pyenv/versions/2.7.6/include/python2.7 -I/home/roman/.pyenv/versions/2.7.6/include/python2.7 -c 'example'_wrap.c
cc -pthread -shared -Wl,-O1 -O3 -Wall -g -fPIC -o _'example'.so 'example'_wrap.o
python -c 'import 'example'' && echo "Python build is ok!"
Python build is ok!
我在两个系统上都运行 conda 3.4.1
作为 virtualenv 系统。
有趣的是,以下 SWIG 支持的项目也在我的 OSX 机器中出现了段错误:
https://github.com/ilveroluca/rapi/blob/master/pyrapi/Makefile
https://github.com/brainstorm/facs/blob/swig/facs/Makefile
但不是这个(适用于 OSX 和 Linux),例如:
https://github.com/PacificBiosciences/ConsensusCore
有什么想法吗?你能在你的机器上重现这个吗?
【问题讨论】:
【参考方案1】:感谢 GitHub 上的 @dfroger 提供解决方案,这是 OSX 上的链接问题:
http://swig.10945.n7.nabble.com/SWIG-python-segfault-on-OSX-fine-on-Linux-td13926.html
https://github.com/dfroger/swig-python-skel/commit/28539f1273
【讨论】:
以上是关于准系统 SWIG python C 接口在 OSX+clang 上出现段错误,而不是在 Linux+gcc 中的主要内容,如果未能解决你的问题,请参考以下文章
python/c++接口库比较(SWIG,boost.python, pycxx, py++, sip, Weave, Pyrex )