在 Mac OS Catalina 上调试 Python 分段错误

Debugging Python segmentation fault on Mac OS Catalina

我正在使用 PySide2 和 scikit-learn 开发一个应用程序。如果我使用单个线程,该应用程序可以正常工作,但是如果我将 sckikit-learn 计算移动到一个 worker QThread(以在处理过程中保持 UI 响应),我会在 运行dom 上出现分段错误=35=] OS卡塔琳娜。同一个程序似乎在 Windows 上运行良好(在 Mac 上我每次 运行 程序都会遇到分段错误;我 运行 该程序至少二十次Windows 而且它从未崩溃过)。我试图遵循 this answer 中的建议,但我无法让 gdblldb 在 Catalina 上正常工作。

这是我用 lldb:

得到的
% lldb python
(lldb) target create "python"
Current executable set to 'python' (x86_64).
(lldb) run test.py
error: process exited with status -1 (attach failed (Not allowed to attach to process.  Look in the console messages (Console.app), near the debugserver entries when the attached failed.  The subsystem that denied the attach permission will likely have logged an informative message about why it was denied.))

然后我尝试使用 MacPorts 安装 gdb,然后我按照 instructions from the GDB Wiki 允许 gdb 调试另一个进程,但是 gdb要么在 run 命令后挂起,要么在 运行 一个简单的脚本(只打印一个字符串)时给我一个未知信号错误:

% ggdb python   
GNU gdb (GDB) 9.2
Copyright (C) 2020 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-apple-darwin19.5.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from python...
(No debugging symbols found in python)
(gdb) run test.py
Starting program: /usr/bin/python test.py
[New Thread 0x2603 of process 92261]
[New Thread 0x1803 of process 92261]
During startup program terminated with signal ?, Unknown signal.

有没有办法在 Catalina 上调试 Python 分段错误?

macOS 上的 SIP(​​系统完整性保护)禁止调试器附加到系统应用程序,包括 Python 的发布版本。这就是您所看到的。

您需要关闭 SIP 或构建您自己的 Python 版本。 OTOH,如果你正在调试构建的 -O0 Python,那么在 Python 中调试你的问题会容易得多,所以弄清楚如何自己构建它(实际上并不难)它可能从长远来看是值得的。