Issue 30998: faulthandler: Show C stacktrace (original) (raw)

As the faulthandler documentation notes:

The fault handler is called on catastrophic cases and therefore can only use signal-safe functions (e.g. it cannot allocate memory on the heap). Because of this limitation traceback dumping is minimal compared to normal Python tracebacks:

This immediately disqualifies glibc's backtrace function as it is explicitly marked as AS-Unsafe.

The Windows code you linked also has a heap allocation, it isn't open source like backtrace but I'd imagine its implementation is fairly complex underneath.

Overall, adding more complexity especially to a handler dealing with a catastrophic failure is generally not a very good idea and it's really not a trivial problem to have easy cross platform stack traces. As much as I like this idea I don't think implementing is going to be possible and this is one of the points where you just have to attach a debugger like gdb for good information.

Hmm, fair point. I thought I had seen this being used in a SEGV handler in some other software I use, but I can't find that anymore - so either I was dreaming, or they noticed it was problematic and removed it again.

I'm closing this then. My goal was to get more useful crash reports from users (who typically don't want to install/attach gdb and reproduce the bug again, if even possible), but I guess my only remaining option is automatically getting the stacktrace from "coredumpctl" on the next start, at least on Linux systems using systemd...

While faulthandler's output is already quite useful when dealing with crashes in C libraries, it'd be much more useful when it could also show a low-level C/C++ stack.

It's a deliberate choice to not read the "C" backtrace: it requires complex non-portable libraries. faulthandler has a "simple" design, but the implementation is non trivial especially because it is designed to be async-signal-safe (no malloc, no printf, no lock, etc.).

faulthandler design is to read global Python internal structures and dumps them on stderr. The code is as portable as Python is: faulthandler works on all platforms! There are only tiny limitations on Windows on faulthandler.register(), but IMHO the main usage of faulthandler is to dump Python tracebacks on a crash and that works on Windows as well. By the way, I recently added support to Windows exceptions, so it uses better Windows native APIs.

You can write a Python module which depends on a specific library and only works on a limited set of platforms. But such module belongs to PyPI, I consider that it shouldn't be part of the Python stdlib. Usually, Python avoids dependencies whenever possible. Again, to get the maximum portability.

As much as I like this idea I don't think implementing is going to be possible and this is one of the points where you just have to attach a debugger like gdb for good information.

While it's "nice" to have a "C" traceback, I also prefer to get a debugger like gdb to inspect more data: variables, registers, threads, etc.

Maybe it would be useful to have an option to say always generate a core dump? Something like the stuff listed here: https://stackoverflow.com/questions/979141/how-to-programmatically-cause-a-core-dump-in-c-c

10 years ago, it was common to dump cores in the current directory. Nowadays, operating systems provide a program which collects core dumps, send them to a server to get a reliable stacktrace, automatically open a bug report, etc. Getting a core dump requires to configure your operating system.

I'm not a big fan of coredump. Coredumps are not portable at all. Don't try to open a Ubuntu coredump on Fedora. I wouldn't even try to open a Fedora 25 coredump on Fedora 26.

On Linux, changing how where and how coredumps are written requires to be a root user. You may want to support coredumpctl, etc.

Again, it's a complex task and it's hard to get a portable behaviour.

If someone is interested to get this feature, please create a small project on PyPI. Come back later when you get enough user feedback and supports enough platforms to consider that it can be added to Python stdlib.