KEMBAR78
Add a method to print the stack trace by aaronfranke · Pull Request #64205 · godotengine/godot · GitHub
Skip to content

Conversation

aaronfranke
Copy link
Member

This adds a new method to the error macros that allows printing the stack trace. Add print_stack_trace(); to any method to print the stack trace at that point. You can also specify if you want to skip a part of the stack trace at the beginning or end, to avoid any noisy lines you don't care about. It's only available in debug builds.

There is no way to print the stack trace in a cross-platform way, so we need platform-specific code. On Mac and Linux, the code to print the stack trace is simple, since there is a handy library that does all the work for us, but we don't have fine control over the output. On Windows, it's more complicated, the code was designed based on this SO post and this article. I made the Windows code's output somewhat mimic the Mac/Linux code for consistency between platforms. Also, Windows stack trace printing only works on x86_32 and x86_64 (and technically Itanium, but Godot doesn't run on that).

This work was sponsored by The Mirror, we plan to use this by adding print_stack_trace(2); to the void _err_print_error method so that we will get stack traces for every error (and the 2 skips the calls to _err_print_error itself). This is not included in this PR since it would be spammy, but it's one line away for anyone who wants it.

@aaronfranke aaronfranke added this to the 4.0 milestone Aug 10, 2022
@aaronfranke aaronfranke requested a review from a team as a code owner August 10, 2022 06:59
@aaronfranke aaronfranke force-pushed the stack branch 2 times, most recently from 4034658 to b530bd3 Compare August 10, 2022 07:15
@Calinou
Copy link
Member

Calinou commented Aug 10, 2022

This work was sponsored by The Mirror, we plan to use this by adding print_stack_trace(2); to the void _err_print_error method so that we will get stack traces for every error (and the 2 skips the calls to _err_print_error itself). This is not included in this PR since it would be spammy, but it's one line away for anyone who wants it.

Doing this would address godotengine/godot-proposals#963 🙂

I agree it should be off by default though (and there should be two project settings to toggle it: one for errors, one for warnings).

@Zylann
Copy link
Contributor

Zylann commented Aug 11, 2022

How about obtaining the stack trace in a data structure instead of directly printing it? This way it can be routed elsewhere or formatted in other ways.

@Calinou
Copy link
Member

Calinou commented Aug 12, 2022

How about obtaining the stack trace in a data structure instead of directly printing it? This way it can be routed elsewhere or formatted in other ways.

I think this is worth adding as a separate method, but the method for quick printing should remain available for convenience.

@aaronfranke aaronfranke marked this pull request as draft August 19, 2022 00:17
@akien-mga akien-mga modified the milestones: 4.0, 4.x Sep 1, 2022
@akien-mga
Copy link
Member

akien-mga commented Sep 1, 2022

For the record (Aaron is already aware), this partially overlaps with the existing crash handler implementations in platform/*/crash_handler_*.cpp so it should likely be refactored to use a common base for both features. The platform specific code should likely be kept in platform so it's more modular (and allows implementing it for third-party platforms too, if relevant).

See also #61006.

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