Skip to content

Color stacktraces printed by the crash handler when in a TTY - #33505

Draft
Calinou wants to merge 2 commits into
godotengine:masterfrom
Calinou:crash-handler-color-stacktrace
Draft

Calinou wants to merge 2 commits into
godotengine:masterfrom
Calinou:crash-handler-color-stacktrace

Conversation

@Calinou

@Calinou Calinou commented Nov 10, 2019 •

Copy link
Copy Markdown
Member

Follow-up to #44118.

This makes crash backtraces easier to read, which in turn improves the developer experience.

Preview

Windows 10 (MSVC)

Windows 10 (MSVC)

Linux

Linux

PS: For testing purposes, you can call CRASH_NOW(); anywhere to make Godot crash.

@aaronfranke

Copy link
Copy Markdown
Member

@Calinou Is this still desired? If so, it needs to be rebased on the latest master branch.

@akien-mga

Copy link
Copy Markdown
Member

Needs rebasing. Should be fine to merge then if it works.

@akien-mga akien-mga modified the milestones: 4.0, 4.x Jul 26, 2022
@Calinou
Calinou force-pushed the crash-handler-color-stacktrace branch from 9e2e275 to cc4c4e2 Compare July 26, 2022 15:39
@Calinou

Calinou commented Jul 26, 2022

Copy link
Copy Markdown
Member Author

Rebased and tested again on Linux, it works as expected.

Please test this on macOS before merging. The Windows portion of this PR won't work correctly until #44118 is merged (which still needs testers on Windows 7/8.1).

@Calinou
Calinou force-pushed the crash-handler-color-stacktrace branch from cc4c4e2 to 95d37cf Compare July 26, 2022 17:03
@Anutrix

Anutrix commented Sep 8, 2022

Copy link
Copy Markdown
Contributor

Since, #44118 is merged now, maybe we can get this merged after a rebase.

@Calinou

Calinou commented Sep 9, 2022 •

Copy link
Copy Markdown
Member Author

Rebased and tested again, it works as expected on Linux and Windows (MSVC). See OP for updated screenshots.

@bruvzg If you have time, could you test and review the macOS side of this just in case? Thanks in advance 🙂

@Calinou
Calinou force-pushed the crash-handler-color-stacktrace branch from 95d37cf to a27ed52 Compare September 9, 2022 17:11
@Calinou
Calinou marked this pull request as ready for review September 9, 2022 17:12
@Calinou
Calinou requested review from a team as code owners September 9, 2022 17:12

@bruvzg bruvzg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems to be working fine, with a few minor changes.

Screenshot 2022-09-09 at 20 09 15

Note: at least on macOS, CRASH_NOW will generate SIGTRAP, so it will not be processed by Godot crash handler unless signal(SIGTRAP, handle_crash); is added.

Comment thread platform/macos/crash_handler_macos.mm Outdated
Comment thread platform/macos/crash_handler_macos.mm Outdated
@Calinou

Calinou commented Sep 9, 2022 •

Copy link
Copy Markdown
Member Author

Note: at least on macOS, CRASH_NOW will generate SIGTRAP, so it will not be processed by Godot crash handler unless signal(SIGTRAP, handle_crash); is added.

Should this be changed in another PR, either in the crash handler or the CRASH_NOW() macro?

@Calinou
Calinou force-pushed the crash-handler-color-stacktrace branch from a27ed52 to 8b20d4a Compare September 9, 2022 18:14
@bruvzg

bruvzg commented Sep 9, 2022

Copy link
Copy Markdown
Member

Probably a crash handler should be changed to handle SIGTRAP. CRASH_NOW is calling __builtin_trap, so I'm not sure if it can be changed, it's likely platform dependent how it's work.

@deralmas

Copy link
Copy Markdown
Member

Just noticed this, nice work!

Is it really needed to explicitly add TTY checks and ANSI codes everywhere instead of having some sort of generic, optionally colored print system though? That might sound a bit excessive (and can definitely become such) but I'm pretty sure that there might be a clean and simple way of going along with it (hell, perhaps even a bunch of macros?). If it's done in more places that might be material for another PR but I can't stop wondering about this.

@Calinou

Calinou commented Mar 16, 2023 •

Copy link
Copy Markdown
Member Author

Is it really needed to explicitly add TTY checks and ANSI codes everywhere instead of having some sort of generic, optionally colored print system though? That might sound a bit excessive (and can definitely become such) but I'm pretty sure that there might be a clean and simple way of going along with it (hell, perhaps even a bunch of macros?). If it's done in more places that might be material for another PR but I can't stop wondering about this.

print_line(), print_error(), print_line_rich() and all other print functions could have a check that strips ANSI escape codes from the printed string if the terminal isn't a TTY. This may have a performance impact when not running from a TTY and printing lots of text though.

@Calinou
Calinou force-pushed the crash-handler-color-stacktrace branch from 8b20d4a to 95cbc6f Compare March 19, 2023 20:17
@Calinou

Calinou commented Mar 19, 2023

Copy link
Copy Markdown
Member Author

Rebased and tested again, it works as expected.

image

@fire

fire commented Nov 4, 2024

Copy link
Copy Markdown
Member

@Calinou Would you like to rebase this? I'm interested.

@Calinou
Calinou force-pushed the crash-handler-color-stacktrace branch from 95cbc6f to b76d3e3 Compare February 14, 2025 18:10
@Ivorforce
Ivorforce requested a review from Repiteo May 12, 2025 13:57
@ghost

ghost commented Dec 17, 2025

Copy link
Copy Markdown

I'm interested in this PR. Will it be merged after 4.6 release or is it still undetermined when this PR may be merged?

Comment thread platform/macos/crash_handler_macos.mm Outdated
// Dump the backtrace to stderr with a message to the user.
print_error("\n================================================================");
print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, sig));
if (isatty(fileno(stderr))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This can be changed to OS::get_singleton()->get_stderr_type() == OS::STD_HANDLE_CONSOLE and used on all platforms, including Windows.

@Calinou
Calinou force-pushed the crash-handler-color-stacktrace branch from b76d3e3 to 356d6f9 Compare January 8, 2026 23:41
TODO:

- Port to other crash handlers (including the other Linux crash handler
  below in that file).
@Calinou

Calinou commented Aug 19, 2026 •

Copy link
Copy Markdown
Member Author

Rebased and tested again, it works as expected. Putting this back to draft as I'm working on reorganizing colors based on recent changes to the various crash handlers. I'll also need to figure out a way to factor out as much code as possible to avoid duplication.

Currently, it looks like this on Linux with the second commit and https://github.com/gimli-rs/addr2line installed:

image

Godot functions are highlighted (as we're usually interested in those in crash handlers), and function/parameter names are distinguished.

@Calinou
Calinou force-pushed the crash-handler-color-stacktrace branch from 356d6f9 to 0e588e7 Compare August 19, 2026 23:04
@Calinou
Calinou marked this pull request as draft August 19, 2026 23:04
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.

7 participants