Skip to content

Commit 267a6c8

Browse files
committed
std: show signal number along with name
1 parent 47030d3 commit 267a6c8

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

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

+47-43
Original file line numberDiff line numberDiff line change
@@ -696,41 +696,46 @@ impl From<c_int> for ExitStatus {
696696
}
697697

698698
/// 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",
699+
///
700+
/// This string should be displayed right after the signal number.
701+
/// If a signal is unrecognized, it returns the empty string, so that
702+
/// you just get the number like "0". If it is recognized, you'll get
703+
/// something like "9 (SIGKILL)".
704+
fn signal_string(signal: i32) -> &'static str {
705+
match signal {
706+
libc::SIGHUP => " (SIGHUP)",
707+
libc::SIGINT => " (SIGINT)",
708+
libc::SIGQUIT => " (SIGQUIT)",
709+
libc::SIGILL => " (SIGILL)",
710+
libc::SIGTRAP => " (SIGTRAP)",
711+
libc::SIGABRT => " (SIGABRT)",
712+
libc::SIGBUS => " (SIGBUS)",
713+
libc::SIGFPE => " (SIGFPE)",
714+
libc::SIGKILL => " (SIGKILL)",
715+
libc::SIGUSR1 => " (SIGUSR1)",
716+
libc::SIGSEGV => " (SIGSEGV)",
717+
libc::SIGUSR2 => " (SIGUSR2)",
718+
libc::SIGPIPE => " (SIGPIPE)",
719+
libc::SIGALRM => " (SIGALRM)",
720+
libc::SIGTERM => " (SIGTERM)",
721+
libc::SIGCHLD => " (SIGCHLD)",
722+
libc::SIGCONT => " (SIGCONT)",
723+
libc::SIGSTOP => " (SIGSTOP)",
724+
libc::SIGTSTP => " (SIGTSTP)",
725+
libc::SIGTTIN => " (SIGTTIN)",
726+
libc::SIGTTOU => " (SIGTTOU)",
727+
libc::SIGURG => " (SIGURG)",
728+
libc::SIGXCPU => " (SIGXCPU)",
729+
libc::SIGXFSZ => " (SIGXFSZ)",
730+
libc::SIGVTALRM => " (SIGVTALRM)",
731+
libc::SIGPROF => " (SIGPROF)",
732+
libc::SIGWINCH => " (SIGWINCH)",
733+
libc::SIGIO => " (SIGIO)",
734+
libc::SIGSYS => " (SIGSYS)",
730735
#[cfg(target_os = "linux")]
731-
libc::SIGSTKFLT => "SIGSTKFLT",
736+
libc::SIGSTKFLT => " (SIGSTKFLT)",
732737
#[cfg(target_os = "linux")]
733-
libc::SIGPWR => "SIGPWR",
738+
libc::SIGPWR => " (SIGPWR)",
734739
#[cfg(any(
735740
target_os = "macos",
736741
target_os = "ios",
@@ -740,7 +745,7 @@ fn signal_string(signal: i32) -> String {
740745
target_os = "openbsd",
741746
target_os = "dragonfly"
742747
))]
743-
libc::SIGEMT => "SIGEMT",
748+
libc::SIGEMT => " (SIGEMT)",
744749
#[cfg(any(
745750
target_os = "macos",
746751
target_os = "ios",
@@ -750,26 +755,25 @@ fn signal_string(signal: i32) -> String {
750755
target_os = "openbsd",
751756
target_os = "dragonfly"
752757
))]
753-
libc::SIGINFO => "SIGINFO",
754-
_ => return format!("{signal}"),
755-
})
756-
.to_string()
758+
libc::SIGINFO => " (SIGINFO)",
759+
_ => "",
760+
}
757761
}
758762

759763
impl fmt::Display for ExitStatus {
760764
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
761765
if let Some(code) = self.code() {
762766
write!(f, "exit status: {code}")
763767
} else if let Some(signal) = self.signal() {
764-
let signal = signal_string(signal);
768+
let signal_string = signal_string(signal);
765769
if self.core_dumped() {
766-
write!(f, "signal: {signal} (core dumped)")
770+
write!(f, "signal: {signal}{signal_string} (core dumped)")
767771
} else {
768-
write!(f, "signal: {signal}")
772+
write!(f, "signal: {signal}{signal_string}")
769773
}
770774
} else if let Some(signal) = self.stopped_signal() {
771-
let signal = signal_string(signal);
772-
write!(f, "stopped (not terminated) by signal: {signal}")
775+
let signal_string = signal_string(signal);
776+
write!(f, "stopped (not terminated) by signal: {signal}{signal_string}")
773777
} else if self.continued() {
774778
write!(f, "continued (WIFCONTINUED)")
775779
} else {

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: SIGTERM");
18-
t(0x0008b, "signal: SIGSEGV (core dumped)");
17+
t(0x0000f, "signal: 15 (SIGTERM)");
18+
t(0x0008b, "signal: 11 (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: SIGSTOP");
27+
t(0x0137f, "stopped (not terminated) by signal: 19 (SIGSTOP)");
2828
t(0x0ffff, "continued (WIFCONTINUED)");
2929
}
3030

0 commit comments

Comments
 (0)