Detailed message during core dumped (#7737)

# Description

In bash when a program crashes, it prints the reason for what happened:
```
$ ./division_by_zero
Floating point exception (core dumped)
$ ./segfault
Segmentation fault (core dumped)
```

Nushell always prints the same thing in this case:
```
> ./division_by_zero
nushell: oops, process './division_by_zero' core dumped
Error: nu:🐚:external_command (link)
# etc..
```

This PR adds more detailed error printing, like in bash:
```
> ./division_by_zero
Floating point exception: oops, process './division_by_zero' core dumped
Error: nu:🐚:external_command (link)
# etc..
```

I made this message format as an example:
```
Floating point exception: oops, process './division_by_zero' core dumped
```
Instead of `nushell:` it writes a meaningful message, but I can change
this format as per the suggestions.

I tested the change only on linux, but it should work on other unix
systems.

# User-Facing Changes

The error message only.

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.

Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>
This commit is contained in:
Nano 2023-01-13 21:22:11 +12:00 committed by GitHub
parent 54dd65cfe1
commit 2ee2370a71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -418,19 +418,35 @@ impl ExternalCommand {
#[cfg(unix)]
{
use nu_ansi_term::{Color, Style};
use std::ffi::CStr;
use std::os::unix::process::ExitStatusExt;
if x.core_dumped() {
let cause = x.signal().and_then(|sig| unsafe {
// SAFETY: We should be the first to call `char * strsignal(int sig)`
let sigstr_ptr = libc::strsignal(sig);
if sigstr_ptr.is_null() {
return None;
}
// SAFETY: The pointer points to a valid non-null string
let sigstr = CStr::from_ptr(sigstr_ptr);
sigstr.to_str().map(String::from).ok()
});
let cause = cause.as_deref().unwrap_or("Something went wrong");
let style = Style::new().bold().on(Color::Red);
eprintln!(
"{}",
style.paint(format!(
"nushell: oops, process '{commandname}' core dumped"
"{cause}: oops, process '{commandname}' core dumped"
))
);
let _ = exit_code_tx.send(Value::Error {
error: ShellError::ExternalCommand(
"core dumped".to_string(),
format!("Child process '{commandname}' core dumped"),
format!("{cause}: child process '{commandname}' core dumped"),
head,
),
});