This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: faulthandler: Show C stacktrace
Type: enhancement Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: The Compiler, ammar2, vstinner
Priority: normal Keywords:

Created on 2017-07-23 20:50 by The Compiler, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg298914 - (view) Author: Florian Bruhin (The Compiler) * Date: 2017-07-23 20:50
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.

glibc has functions to do that: https://www.gnu.org/software/libc/manual/html_node/Backtraces.html

Windows seems to have something similar too: https://stackoverflow.com/a/5699483/2085149
msg298915 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2017-07-23 21:20
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.
msg298916 - (view) Author: Florian Bruhin (The Compiler) * Date: 2017-07-23 21:32
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...
msg298917 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2017-07-23 21:46
No need to close it off just yet, what I posted was just my opinion. We can wait for haypo's expert opinion seeing as he implemented faulthandler. 

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
msg298947 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-07-24 09:49
> 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.
History
Date User Action Args
2022-04-11 14:58:49adminsetgithub: 75181
2017-07-24 09:49:22vstinnersetstatus: open -> closed
resolution: wont fix -> rejected
messages: + msg298947

stage: resolved
2017-07-23 21:46:54ammar2setmessages: + msg298917
2017-07-23 21:32:31The Compilersetresolution: wont fix
messages: + msg298916
2017-07-23 21:20:26ammar2setnosy: + ammar2
messages: + msg298915
2017-07-23 20:50:52The Compilercreate