Skip to content

Commit 47030d3

Browse files
committed
std: <ExitStatus as Display>::fmt name the signal it died from
1 parent 2d5a21f commit 47030d3

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

library/std/src/sys/unix/process/process_unix.rs

+63
Original file line numberDiff line numberDiff line change
@@ -695,17 +695,80 @@ impl From<c_int> for ExitStatus {
695695
}
696696
}
697697

698+
/// Convert a signal number to a readable, searchable name.
699+
fn signal_string(signal: i32) -> String {
700+
(match signal {
701+
libc::SIGHUP => "SIGHUP",
702+
libc::SIGINT => "SIGINT",
703+
libc::SIGQUIT => "SIGQUIT",
704+
libc::SIGILL => "SIGILL",
705+
libc::SIGTRAP => "SIGTRAP",
706+
libc::SIGABRT => "SIGABRT",
707+
libc::SIGBUS => "SIGBUS",
708+
libc::SIGFPE => "SIGFPE",
709+
libc::SIGKILL => "SIGKILL",
710+
libc::SIGUSR1 => "SIGUSR1",
711+
libc::SIGSEGV => "SIGSEGV",
712+
libc::SIGUSR2 => "SIGUSR2",
713+
libc::SIGPIPE => "SIGPIPE",
714+
libc::SIGALRM => "SIGALRM",
715+
libc::SIGTERM => "SIGTERM",
716+
libc::SIGCHLD => "SIGCHLD",
717+
libc::SIGCONT => "SIGCONT",
718+
libc::SIGSTOP => "SIGSTOP",
719+
libc::SIGTSTP => "SIGTSTP",
720+
libc::SIGTTIN => "SIGTTIN",
721+
libc::SIGTTOU => "SIGTTOU",
722+
libc::SIGURG => "SIGURG",
723+
libc::SIGXCPU => "SIGXCPU",
724+
libc::SIGXFSZ => "SIGXFSZ",
725+
libc::SIGVTALRM => "SIGVTALRM",
726+
libc::SIGPROF => "SIGPROF",
727+
libc::SIGWINCH => "SIGWINCH",
728+
libc::SIGIO => "SIGIO",
729+
libc::SIGSYS => "SIGSYS",
730+
#[cfg(target_os = "linux")]
731+
libc::SIGSTKFLT => "SIGSTKFLT",
732+
#[cfg(target_os = "linux")]
733+
libc::SIGPWR => "SIGPWR",
734+
#[cfg(any(
735+
target_os = "macos",
736+
target_os = "ios",
737+
target_os = "tvos",
738+
target_os = "freebsd",
739+
target_os = "netbsd",
740+
target_os = "openbsd",
741+
target_os = "dragonfly"
742+
))]
743+
libc::SIGEMT => "SIGEMT",
744+
#[cfg(any(
745+
target_os = "macos",
746+
target_os = "ios",
747+
target_os = "tvos",
748+
target_os = "freebsd",
749+
target_os = "netbsd",
750+
target_os = "openbsd",
751+
target_os = "dragonfly"
752+
))]
753+
libc::SIGINFO => "SIGINFO",
754+
_ => return format!("{signal}"),
755+
})
756+
.to_string()
757+
}
758+
698759
impl fmt::Display for ExitStatus {
699760
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
700761
if let Some(code) = self.code() {
701762
write!(f, "exit status: {code}")
702763
} else if let Some(signal) = self.signal() {
764+
let signal = signal_string(signal);
703765
if self.core_dumped() {
704766
write!(f, "signal: {signal} (core dumped)")
705767
} else {
706768
write!(f, "signal: {signal}")
707769
}
708770
} else if let Some(signal) = self.stopped_signal() {
771+
let signal = signal_string(signal);
709772
write!(f, "stopped (not terminated) by signal: {signal}")
710773
} else if self.continued() {
711774
write!(f, "continued (WIFCONTINUED)")

library/std/src/sys/unix/process/process_unix/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ fn exitstatus_display_tests() {
1414

1515
let t = |v, s| assert_eq!(s, format!("{}", <ExitStatus as ExitStatusExt>::from_raw(v)));
1616

17-
t(0x0000f, "signal: 15");
18-
t(0x0008b, "signal: 11 (core dumped)");
17+
t(0x0000f, "signal: SIGTERM");
18+
t(0x0008b, "signal: SIGSEGV (core dumped)");
1919
t(0x00000, "exit status: 0");
2020
t(0x0ff00, "exit status: 255");
2121

@@ -24,7 +24,7 @@ fn exitstatus_display_tests() {
2424
// The purpose of this test is to test our string formatting, not our understanding of the wait
2525
// status magic numbers. So restrict these to Linux.
2626
if cfg!(target_os = "linux") {
27-
t(0x0137f, "stopped (not terminated) by signal: 19");
27+
t(0x0137f, "stopped (not terminated) by signal: SIGSTOP");
2828
t(0x0ffff, "continued (WIFCONTINUED)");
2929
}
3030

0 commit comments

Comments
 (0)