Wolfy
Wolfy

Reputation: 1465

Debugging Python segmentation fault on Mac OS Catalina

I'm developing an application with PySide2 and scikit-learn. The application works properly if I use a single thread, but if I move the sckikit-learn calculations to a worker QThread (to keep the UI responsive during the processing) I get random segmentation faults on Mac OS Catalina. The same program seems to work fine on Windows (on Mac I get a segmentation fault every other time I run the program; I ran the program at least twenty times on Windows and it never crashed). I was trying to follow the suggestions in this answer, but I can't have either gdb nor lldb to work properly on Catalina.

This is what I get with 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.))

I then tried to install gdb with MacPorts, then I followed the instructions from the GDB Wiki to allow gdb to debug another process, however gdb either hangs after the run command or gives me an Unknown signal error while running a simple script (just print a string):

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

Is there a way to debug Python segmentation faults on Catalina?

Upvotes: 0

Views: 1313

Answers (1)

Jim Ingham
Jim Ingham

Reputation: 27228

SIP (System Integrity Protection) on macOS prohibits the debugger from attaching to system applications, including the shipping version of Python. That's what you are seeing.

You either need to turn off SIP or build your own version of the Python. OTOH, debugging your problem in Python will be a lot easier if you are debugging a -O0 built Python, so figuring out how to build it yourself (it's actually not that hard to do) its likely to be worthwhile in the long term.

Upvotes: 5

Related Questions