如何调试 Python 分段错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16731115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:28:11  来源:igfitidea点击:

How to debug a Python segmentation fault?

pythonsegmentation-fault

提问by guettli

How can I debug a Python segmentation fault?

如何调试 Python 分段错误?

We are trying to run our python code on SuSE 12.3. We get reproducible segmentation faults. The python code has been working on other platforms without segmentation faults, for years.

我们正在尝试在 SuSE 12.3 上运行我们的 Python 代码。我们得到可重现的分段错误。多年来,python 代码一直在其他平台上运行而没有出现分段错误。

We only code Python, no C extension ....

我们只编写 Python 代码,没有 C 扩展......

What is the best way to debug this? I know a bit ansi c, but that was ten years ago ....

调试此问题的最佳方法是什么?我知道一点ansi c,但那是十年前......

Python 2.7.5

蟒蛇 2.7.5

Update

更新

The segmentation fault happens on interpreter shutdown.

分段错误发生在解释器关闭时。

I can run the script several times:

我可以多次运行脚本:

python -m pdb myscript.py arg1 arg1
continue
run
continue
run

But the segmentation faults happen, if I leave the pdb with ctrl-d.

但是如果我用 ctrl-d 离开 pdb,就会发生分段错误。

Update 2

更新 2

I now try to debug it with gdb:

我现在尝试用 gdb 调试它:

gdb 
> file python
> run myscript.py arg1 arg2
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffefbe2700 (LWP 15483)]
0x00007ffff7aef93c in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
(gdb) bt
#0  0x00007ffff7aef93c in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#1  0x00007ffff7af5303 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#2  0x00007ffff7adc858 in ?? () from /usr/lib64/libpython2.7.so.1.0
#3  0x00007ffff7ad840d in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#4  0x00007ffff7af1082 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#5  0x00007ffff7af233d in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#6  0x00007ffff7af233d in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#7  0x00007ffff7af5303 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#8  0x00007ffff7adc5b6 in ?? () from /usr/lib64/libpython2.7.so.1.0
#9  0x00007ffff7ad840d in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#10 0x00007ffff7ad9171 in ?? () from /usr/lib64/libpython2.7.so.1.0
#11 0x00007ffff7ad840d in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#12 0x00007ffff7aeeb62 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.7.so.1.0
#13 0x00007ffff7acc757 in ?? () from /usr/lib64/libpython2.7.so.1.0
#14 0x00007ffff7828e0f in start_thread () from /lib64/libpthread.so.0
#15 0x00007ffff755c7dd in clone () from /lib64/libc.so.6

Update 3

更新 3

I installed gdbinit from http://hg.python.org/cpython/file/default/Misc/gdbinitand the debugging symbols from http://download.opensuse.org/debug/distribution/12.3/repo/oss/suse/x86_64/

我安装了来自http://hg.python.org/cpython/file/default/Misc/gdbinit 的gdbinit 和来自http://download.opensuse.org/debug/distribution/12.3/repo/oss/suse/的调试符号x86_64/

(gdb) pystack
No symbol "_PyUnicode_AsString" in current context.

What now?

现在怎么办?

Update 4We installed the a new RPM (python-2.7.5-3.1.x86_64). We get less segfaults, but they still happen. Here is the link to repository:

更新 4我们安装了新的 RPM (python-2.7.5-3.1.x86_64)。我们得到的段错误更少,但它们仍然发生。这是存储库的链接:

http://download.opensuse.org/repositories/devel:/languages:/python:/Factory/openSUSE_12.3/x86_64/

http://download.opensuse.org/repositories/devel:/languages:/python:/Factory/openSUSE_12.3/x86_64/

Update 5Solved my initial problem:

更新 5解决了我最初的问题:

It was http://bugs.python.org/issue1856(shutdown (exit) can hang or segfault with daemon threads running)

它是http://bugs.python.org/issue1856(关闭(退出)可以挂起或段错误,守护线程正在运行)

Related: Detect Interpreter shut down in daemon thread

相关:检测解释器在守护线程中关闭

采纳答案by guettli

Maybe there is a daemon thread running? There is a reproduceable bug, which was fixed only for 3.x, but not for 2.x:

也许有一个守护线程正在运行?有一个可重现的错误,仅针对 3.x 修复,但不适用于 2.x:

http://bugs.python.org/issue1856:

http://bugs.python.org/issue1856

shutdown (exit) can hang or segfault with daemon threads running

This is the answer to my own question. It took some time to find the root of the problem.

这是我自己的问题的答案。花了一些时间才找到问题的根源。

Here is the next question: How to code around this bug: Detect Interpreter shut down in daemon thread

这是下一个问题:如何解决此错误:检测解释器在守护进程线程中关闭

回答by icecrime

If you're executing nothing but Python code (even through your imported third party modules), than a segfault probably means there's a bug in the interpreter or one of its builtin C modules.

如果您只执行 Python 代码(甚至通过您导入的第三方模块),那么段错误可能意味着解释器或其内置 C 模块之一存在错误。

You can either build CPython and try to debug it yourself, or try to produce the smallest script which reproduces the crash and file an issue.

您可以构建 CPython 并尝试自己调试它,或者尝试生成最小的脚本来重现崩溃并提交问题

回答by mariotomo

I got to this question because of the Segmentation fault, but not on exit, just in general, and I found that nothing else helped as effectively as faulthandler. It's part of Python 3.3, and you can install in 2.7 using pip.

我遇到这个问题是因为Segmentation fault,但不是在退出时,一般来说,我发现没有其他东西能像faulthandler那样有效。它是 Python 3.3 的一部分,您可以使用pip.

回答by Olshansk

tl;dr for python3 users.

tl; dr for python3 用户。

Firstly, from the docs:

首先,从文档:

faulthandleris a builtin module since Python 3.3

faulthandler是自 Python 3.3 以来的内置模块

Code usage:

代码用法:

faulthandler.enable()
// bad code goes here

Shell usage:

外壳用法:

$ python3 -q -X faulthandler
>>> /// bad cod goes here