Skip to content

Commit bbb5448

Browse files
committed
std: Render large exit codes as hex on Windows
On Windows process exit codes are never signals but rather always 32-bit integers. Most faults like segfaults and such end up having large integers used to represent them, like STATUS_ACCESS_VIOLATION being 0xC0000005. Currently, however, when an `ExitStatus` is printed this ends up getting rendered as 3221225477 which is somewhat more difficult to debug. This commit adds a branch in `Display for ExitStatus` on Windows which handles exit statuses where the high bit is set and prints those exit statuses as hex instead of with decimals. This will hopefully preserve the current display for small exit statuses (like `exit code: 22`), but assist in quickly debugging segfaults/access violations/etc. I've found at least that the hex codes are easier to search for than decimal. I wasn't able to find any official documentation saying that all system exit codes have the high bit set, but I figure it's a good enough heuristic for now.
1 parent d22fa2d commit bbb5448

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/libstd/sys/windows/process.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,16 @@ impl From<c::DWORD> for ExitStatus {
393393

394394
impl fmt::Display for ExitStatus {
395395
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
396-
write!(f, "exit code: {}", self.0)
396+
// Windows exit codes with the high bit set typically mean some form of
397+
// unhandled exception or warning. In this scenario printing the exit
398+
// code in decimal doesn't always make sense because it's a very large
399+
// and somewhat gibberish number. The hex code is a bit more
400+
// recognizable and easier to search for, so print that.
401+
if self.0 & 0x80000000 != 0 {
402+
write!(f, "exit code: {:#x}", self.0)
403+
} else {
404+
write!(f, "exit code: {}", self.0)
405+
}
397406
}
398407
}
399408

0 commit comments

Comments
 (0)