Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-127604: Add C stack dumps to faulthandler #128159

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

ZeroIntensity
Copy link
Member

@ZeroIntensity ZeroIntensity commented Dec 21, 2024

This only adds it for glibc right now. I think it's possible to implement it for Windows with StackWalk64, but I'd prefer to get a solid working implementation for other systems before I dive into that.


📚 Documentation preview 📚: https://cpython-previews--128159.org.readthedocs.build/en/128159/library/faulthandler.html#dumping-the-c-stack

Copy link
Contributor

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some PEP-7 nits but I don't know if this was done to be aligned with the rest of the code so you can freely ignore those nits.

Doc/library/faulthandler.rst Show resolved Hide resolved
Doc/whatsnew/3.14.rst Outdated Show resolved Hide resolved
Doc/library/faulthandler.rst Outdated Show resolved Hide resolved
Modules/faulthandler.c Outdated Show resolved Hide resolved
Modules/faulthandler.c Outdated Show resolved Hide resolved
Modules/faulthandler.c Outdated Show resolved Hide resolved
Modules/faulthandler.c Show resolved Hide resolved
Doc/whatsnew/3.14.rst Show resolved Hide resolved
Modules/faulthandler.c Outdated Show resolved Hide resolved
Modules/faulthandler.c Show resolved Hide resolved
@picnixz
Copy link
Contributor

picnixz commented Dec 21, 2024

@ZeroIntensity If you ever use the web UI, don't forget to replace my tabs with spaces... hopefully your IDE does it but I see that I have tabs instead of spaces on my suggestions (I've corrected them but just double-check)

Doc/library/faulthandler.rst Outdated Show resolved Hide resolved
Modules/faulthandler.c Show resolved Hide resolved
Modules/faulthandler.c Outdated Show resolved Hide resolved
Doc/library/faulthandler.rst Outdated Show resolved Hide resolved
@ZeroIntensity ZeroIntensity added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Dec 21, 2024
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @ZeroIntensity for commit e79e661 🤖

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Dec 21, 2024
Copy link

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I did start playing around with an implementation myself, and looks like we made a couple different implementation decisions:

Instead of adding a dump_c_stack function, I added a kwarg to dump_traceback. I think I prefer your approach here, since a user may want to see the c backtrace but doesn't care about the python traceback. Though that does clash somewhat with it being a kwarg to enable (and register/dump_traceback_later).

I named my kwarg c_backtrace, though I think I prefer c_stack to avoid backtrace/traceback confusion.

I also added a kwarg to register(), and dump_traceback_later could also get it.

I made all functions raise an exception if passed c_stack=True and it wasn't available, and added a global faulthandler.HAS_C_BACKTRACE. I'm not sure what the best approach is here.

Comment on lines +261 to +264
faulthandler_stack_dump_impl(int fd)
{
PUTS(fd, " <cannot get C stack on this system>\n");
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could make this error message more informative, mentioning glibc and/or execinfo.h

edit: although bsd makes this more complicated... smh

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's worth it. faulthandler is just the first step to debugging a crash, nobody is going to try and switch systems to enable the C stack (they'll just use valgrind or something like that).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, switching system may also hide the failure (the issue could come from the platform they are working on)

PyObject *file = NULL;
int all_threads = 1;
int fd;
int c_stack = 1;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it should be defaulted to True, especially when only available on a subset of systems.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I don't think it matters. People don't use faulthandler for compatibility reasons, they just enable it for debugging. I feel like not enabling the C stack trace by default will just add an extra, annoying step for users.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also in favor of keeping the stack trace.

especially when only available on a subset of systems

Well.. I'd say it's available on many popular systems except FreeBSD/CentoS. Ubuntu and Fedora being supported are engouh IMO, at least for many users.

Is it available on macOS by the way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it available on macOS by the way?

Probably, but I haven't checked.

@@ -93,18 +93,20 @@ def check_error(self, code, lineno, fatal_error, *,
fd=None, know_current_thread=True,
py_fatal_error=False,
garbage_collecting=False,
c_stack=True,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want tests for:

  1. c_stack=False
  2. checking for "cannot get C stack..." on unsupported systems
  3. dump_c_stack

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking for "cannot get C stack..." on unsupported systems

That's checked by the regex, there's a case for <whatever> messages in there.

configure.ac Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants