Repository navigation
Add a way of printing a C backtrace to faulthandler #127604
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on Dec 4, 2024 Would you like to submit the starting PR?
- addedextension-modulesC modules in the Modules dirC modules in the Modules dir
on Dec 7, 2024 Would you like to submit the starting PR?
I might give it a try, but if anybody else wants to you're super welcome. I'm not especially well versed with faulthandler myself.
I went ahead and tried this myself: #128159
Reacted by John LitbornFor future reference as I expect people may want something better in the future beyond what we're able to provide via this issue: https://github.com/abseil/abseil-cpp/tree/master/absl/debugging has code that does a nice job of emitting symbolized C/C++/Rust stack traces on failure. Also: a lot more complicated.
- added a commit that references this issue
on Apr 21, 2025 We do a similar thing in https://github.com/bloomberg/pystack where we have a custom DWARF parser. Also: a lot more complcated as well :)
dprintf()is not available on OpenIndiana.Python/traceback.c: In function '_Py_backtrace_symbols_fd': Python/traceback.c:1213:13: error: implicit declaration of function 'dprintf'; did you mean 'wprintf'? [-Wimplicit-function-declaration] 1213 | dprintf(fd, " Binary file '<unknown>' [%p]\n", array[i]); | ^~~~~~~ | wprintfWill put up a fix later today.
Hi, not sure whether this is a platform difference or some other issue, but on Oracle Solaris, I see all
test_dump_c_stack/test_dump_c_stack_filefail with:====================================================================== FAIL: test_dump_c_stack (test.test_faulthandler.FaultHandlerTests.test_dump_c_stack) (line=' Binary file "/builds/python/build/amd64/libpython3.14.so.1.0", at _PyEval_EvalFrameDefault+0x96a1 [7fe012df2091]') ---------------------------------------------------------------------- Traceback (most recent call last): File "/builds/python/cpython-main/Lib/test/test_faulthandler.py", line 971, in check_c_stack self.assertRegex(line, C_STACK_REGEX[1]) ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^ AssertionError: Regex didn't match: '( Binary file ".+"(, at .*(\\+|-)0x[0-9a-f]+)? \\[0x[0-9a-f]+\\])|(<.+>)' not found in ' Binary file "/builds/python/build/amd64/libpython3.14.so.1.0", at _PyEval_EvalFrameDefault+0x96a1 [7fe012df2091]'presumably because the address in square brackets is not prefixed with
0x.When I change
ADDRESS_EXPR = "0x[0-9a-f]+"to"(0x)?[0-9a-f]+", the issue goes away.I'd like to prevent conflicts, so feel free to make a PR once #132800 gets merged.
Reacted by Jakub Kulík- added a commit that references this issue
on Apr 23, 2025 - added a commit that references this issue
on Apr 23, 2025 11 remaining items
- added a commit that references this issue
on Apr 30, 2025 Closing for now, if you want to reopen feel free to do that
- added a commit that references this issue
on Jun 29, 2025 Oh wow! 🤯 I have a Python application which builts on top of a lot of C++ code (Qt/Chromium) and has an automated crash report dialog. Just got the first crash report with a C++ stacktrace, and this is nothing but amazing, makes them infinitely more useful! Thanks to everyone who was involved in this 💜.
Reacted by Victor Stinner, Peter Bierma, Gregory P. Smith, Sergey Miryanov, Pablo Galindo Salgado and John LitbornOh wow! 🤯 I have a Python application which builts on top of a lot of C++ code (Qt/Chromium) and has an automated crash report dialog. Just got the first crash report with a C++ stacktrace, and this is nothing but amazing, makes them infinitely more useful! Thanks to everyone who was involved in this 💜.
Thanks for taking the time to come back and report it!
Feature or enhancement
Proposal:
The
faulthandlermodule allows registering aSIGSEGVhandler to print a Python stacktrace if the program encounters a segfault. However, if developing C/C++ extension modules that may not be particularly useful on its own, and you also want the the C stacktrace.The suggested API would be a kwarg to faulthandler.enable().
Implementation could use https://github.com/timmaxw/cfaulthandler as a starting point, timmaxw/cfaulthandler@561dbdd in particular.
The availability/usability of the feature would likely depend on platform and/or compile flags.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/print-c-stacktrace-with-faulthandler/56834
where @gpshead approved of opening a feature request
Linked PRs
faulthandler#128159dprintf()for faulthandler C stacks #132800-ldlis passed to the linker whendladdr1is found #133040-ldlis passed only once to the linker #133071faulthandlerexample #136081faulthandlerexample (GH-136081) #136102