Skip to content

Add a way of printing a C backtrace to faulthandler #127604

Description

@jakkdl

Feature or enhancement

Proposal:

The faulthandler module allows registering a SIGSEGV handler 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

Activity

  1. ZeroIntensity commented on Dec 7, 2024

    @ZeroIntensity
    Member

    Would you like to submit the starting PR?

  2. jakkdl commented on Dec 9, 2024

    @jakkdl
    Author

    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.

  3. ZeroIntensity commented on Dec 21, 2024

    @ZeroIntensity
    Member

    I went ahead and tried this myself: #128159

  4. gpshead commented on Apr 21, 2025

    @gpshead
    Member

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

  5. added a commit that references this issue on Apr 21, 2025
  6. pablogsal commented on Apr 21, 2025

    @pablogsal
    Member

    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 :)

  7. serhiy-storchaka commented on Apr 22, 2025

    @serhiy-storchaka
    Member

    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]);
          |             ^~~~~~~
          |             wprintf
    
  8. ZeroIntensity commented on Apr 22, 2025

    @ZeroIntensity
    Member

    Will put up a fix later today.

  9. kulikjak commented on Apr 23, 2025

    @kulikjak
    Contributor

    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_file fail 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.

  10. ZeroIntensity commented on Apr 23, 2025

    @ZeroIntensity
    Member

    I'd like to prevent conflicts, so feel free to make a PR once #132800 gets merged.

  11. added a commit that references this issue on Apr 23, 2025
  12. added a commit that references this issue on Apr 23, 2025
  13. added a commit that references this issue on Apr 23, 2025
  14. 11 remaining items

  15. added 2 commits that reference this issue on Apr 27, 2025
  16. added a commit that references this issue on Apr 30, 2025
  17. pablogsal commented on May 5, 2025

    @pablogsal
    Member

    Closing for now, if you want to reopen feel free to do that

  18. added a commit that references this issue on Jun 29, 2025
  19. added a commit that references this issue on Jun 29, 2025
  20. added a commit that references this issue on Jun 29, 2025
  21. added a commit that references this issue on Jul 11, 2025
  22. added a commit that references this issue on Jul 12, 2025
  23. added a commit that references this issue on Jul 13, 2025
  24. added a commit that references this issue on Aug 4, 2025
  25. added a commit that references this issue on Aug 19, 2025
  26. The-Compiler commented on Nov 2, 2025

    @The-Compiler
    Contributor

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

  27. pablogsal commented on Nov 3, 2025

    @pablogsal
    Member

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

    Thanks for taking the time to come back and report it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions