From 8a97cf48a7c18d599deeb4c7249a7f9b5cb66058 Mon Sep 17 00:00:00 2001 From: Elnath Date: Sat, 19 Apr 2025 18:07:39 +0200 Subject: [PATCH] Using color_eyre instead of anyhow in main --- Cargo.lock | 216 +++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 2 +- src/main.rs | 17 +++-- 3 files changed, 222 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a390d08..1456507 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,16 +3,40 @@ version = 4 [[package]] -name = "anyhow" -version = "1.0.98" +name = "addr2line" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "backtrace" +version = "0.3.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] [[package]] name = "bdb" version = "0.0.0" dependencies = [ - "anyhow", + "color-eyre", "nix", "thiserror", ] @@ -23,6 +47,15 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" +[[package]] +name = "cc" +version = "1.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e3a13707ac958681c13b39b458c073d0d9bc8a22cb1b2f4c8e55eb72c13f362" +dependencies = [ + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -35,12 +68,82 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "color-eyre" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2" +dependencies = [ + "once_cell", + "owo-colors", + "tracing-core", + "tracing-error", +] + +[[package]] +name = "eyre" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec" +dependencies = [ + "indenter", + "once_cell", +] + +[[package]] +name = "gimli" +version = "0.28.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" + +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.172" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "miniz_oxide" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +dependencies = [ + "adler", +] + [[package]] name = "nix" version = "0.29.0" @@ -53,6 +156,33 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.32.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "owo-colors" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + [[package]] name = "proc-macro2" version = "1.0.95" @@ -71,6 +201,27 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "syn" version = "2.0.100" @@ -102,8 +253,65 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-error" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b1581020d7a273442f5b45074a6a57d5757ad0a47dac0e9f0bd57b81936f3db" +dependencies = [ + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" +dependencies = [ + "sharded-slab", + "thread_local", + "tracing-core", +] + [[package]] name = "unicode-ident" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" diff --git a/Cargo.toml b/Cargo.toml index c7abdff..b6c93b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,4 @@ publish = false [dependencies] nix = { version = "0.29.0", features = ["process", "ptrace"] } thiserror = "2.0.12" -anyhow = "1.0.98" \ No newline at end of file +color-eyre = "0.6.3" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 379b760..f258813 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ mod child; mod debug_target; use crate::debug_target::DebugTarget; -use anyhow::{anyhow, bail}; +use color_eyre::eyre::eyre; use nix::libc::user_regs_struct; use nix::sys::ptrace::*; use nix::sys::signal::Signal::*; @@ -11,7 +11,7 @@ use nix::unistd::{fork, ForkResult, Pid}; use std::ffi::{c_long, c_void, CString}; #[allow(dead_code)] -fn single_step_all(child_pid: Pid) -> Result<(), anyhow::Error> { +fn single_step_all(child_pid: Pid) -> color_eyre::Result<()> { let mut instruction_number = 0; loop { let wait_status = waitpid(child_pid, None); @@ -27,14 +27,14 @@ fn single_step_all(child_pid: Pid) -> Result<(), anyhow::Error> { return Ok(()); } other => { - bail!("⚠️ Other (unexpected) wait status: {other:?}"); + return Err(eyre!("⚠️ Other (unexpected) wait status: {other:?}")) } } } } #[allow(dead_code)] -fn breakpoint_fun(child_pid: Pid) -> Result<(), anyhow::Error> { +fn breakpoint_fun(child_pid: Pid) -> color_eyre::Result<()> { println!("⏳️ Waiting for child to be ready"); waitid(Id::Pid(child_pid), WaitPidFlag::WSTOPPED)?; @@ -70,7 +70,7 @@ fn breakpoint_fun(child_pid: Pid) -> Result<(), anyhow::Error> { cont(child_pid, None)?; } other => { - bail!("⚠️ Other (unexpected) wait status: {other:?}"); + return Err(eyre!("⚠️ Other (unexpected) wait status: {other:?}")); } } @@ -80,12 +80,13 @@ fn breakpoint_fun(child_pid: Pid) -> Result<(), anyhow::Error> { Ok(()) } other => { - bail!("⚠️ Other (unexpected) wait status: {other:?}"); + Err(eyre!("⚠️ Other (unexpected) wait status: {other:?}")) } } } -fn main() -> Result<(), anyhow::Error> { +fn main() -> color_eyre::Result<()> { + color_eyre::install()?; let child_exec_path = CString::new(env!("ASM_PROG_PATH"))?; match unsafe { fork() } { @@ -110,7 +111,7 @@ fn main() -> Result<(), anyhow::Error> { } Err(e) => { println!("❌ Fork failed: {e}"); - Err(anyhow!("Fork failed")) + Err(eyre!("Fork failed")) } } }