From f9b643e7b3e4c55b87e0e6e6975374b5226b3833 Mon Sep 17 00:00:00 2001 From: WorksButNotTested <62701594+WorksButNotTested@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:34:26 +0000 Subject: [PATCH] Fix gdb-qemu (#2773) Co-authored-by: WorksButNotTested --- utils/gdb_qemu/Cargo.toml | 2 +- utils/gdb_qemu/Makefile.toml | 10 ++++---- utils/gdb_qemu/{demo => gdb_demo}/Cargo.toml | 0 utils/gdb_qemu/{demo => gdb_demo}/build.rs | 4 +++- utils/gdb_qemu/{demo => gdb_demo}/src/args.rs | 8 +++++-- utils/gdb_qemu/{demo => gdb_demo}/src/main.rs | 10 ++++---- utils/gdb_qemu/gdb_qemu/build.rs | 4 +++- utils/gdb_qemu/gdb_qemu/src/args/level.rs | 3 ++- utils/gdb_qemu/gdb_qemu/src/args/mod.rs | 10 ++++---- utils/gdb_qemu/gdb_qemu/src/args/version.rs | 8 +++++-- utils/gdb_qemu/gdb_qemu/src/child.rs | 14 ++++++----- utils/gdb_qemu/gdb_qemu/src/exit.rs | 20 +++++++--------- utils/gdb_qemu/gdb_qemu/src/logger.rs | 12 +++++----- utils/gdb_qemu/gdb_qemu/src/main.rs | 19 ++++++++------- utils/gdb_qemu/gdb_qemu/src/parent.rs | 24 +++++++++---------- 15 files changed, 82 insertions(+), 66 deletions(-) rename utils/gdb_qemu/{demo => gdb_demo}/Cargo.toml (100%) rename utils/gdb_qemu/{demo => gdb_demo}/build.rs (80%) rename utils/gdb_qemu/{demo => gdb_demo}/src/args.rs (87%) rename utils/gdb_qemu/{demo => gdb_demo}/src/main.rs (82%) diff --git a/utils/gdb_qemu/Cargo.toml b/utils/gdb_qemu/Cargo.toml index 5691c03f0e..673b584464 100644 --- a/utils/gdb_qemu/Cargo.toml +++ b/utils/gdb_qemu/Cargo.toml @@ -1,3 +1,3 @@ [workspace] resolver = "2" -members = ["gdb_qemu", "demo"] +members = ["gdb_qemu", "gdb_demo"] diff --git a/utils/gdb_qemu/Makefile.toml b/utils/gdb_qemu/Makefile.toml index 313490dbda..f2a7fd5f46 100644 --- a/utils/gdb_qemu/Makefile.toml +++ b/utils/gdb_qemu/Makefile.toml @@ -28,7 +28,7 @@ command = "cargo" args = [ "build", "-p", - "demo", + "gdb_demo", "--profile", "${PROFILE}", "--target", @@ -38,7 +38,7 @@ args = [ [tasks.run_demo] dependencies = ["demo"] command = "cargo" -args = ["run", "-p", "demo", "--target", "powerpc-unknown-linux-gnu"] +args = ["run", "-p", "gdb_demo", "--target", "powerpc-unknown-linux-gnu"] [tasks.build] dependencies = ["format", "clippy"] @@ -65,7 +65,7 @@ args = [ "/usr/powerpc-linux-gnu", "-g", "1234", - "${DEMO_DIR}/demo", + "${DEMO_DIR}/gdb_demo", ] [tasks.gdb] @@ -79,9 +79,9 @@ args = [ "-ex", "set confirm off", "-ex", - "file ${DEMO_DIR}/demo", + "file ${DEMO_DIR}/gdb_demo", "-ex", - "target remote | ${TARGET_DIR}/gdb_qemu -p 1234 -L trace qemu-ppc -- -L /usr/powerpc-linux-gnu -g 1234 ${DEMO_DIR}/demo", + "target remote | ${TARGET_DIR}/gdb_qemu -p 1234 -L trace qemu-ppc -- -L /usr/powerpc-linux-gnu -g 1234 ${DEMO_DIR}/gdb_demo", ] [tasks.all] diff --git a/utils/gdb_qemu/demo/Cargo.toml b/utils/gdb_qemu/gdb_demo/Cargo.toml similarity index 100% rename from utils/gdb_qemu/demo/Cargo.toml rename to utils/gdb_qemu/gdb_demo/Cargo.toml diff --git a/utils/gdb_qemu/demo/build.rs b/utils/gdb_qemu/gdb_demo/build.rs similarity index 80% rename from utils/gdb_qemu/demo/build.rs rename to utils/gdb_qemu/gdb_demo/build.rs index b8c56e8d82..03565e532e 100644 --- a/utils/gdb_qemu/demo/build.rs +++ b/utils/gdb_qemu/gdb_demo/build.rs @@ -1,4 +1,6 @@ -use {std::error::Error, vergen::EmitBuilder}; +use std::error::Error; + +use vergen::EmitBuilder; fn main() -> Result<(), Box> { EmitBuilder::builder() diff --git a/utils/gdb_qemu/demo/src/args.rs b/utils/gdb_qemu/gdb_demo/src/args.rs similarity index 87% rename from utils/gdb_qemu/demo/src/args.rs rename to utils/gdb_qemu/gdb_demo/src/args.rs index 4895944b88..5576a283b6 100644 --- a/utils/gdb_qemu/demo/src/args.rs +++ b/utils/gdb_qemu/gdb_demo/src/args.rs @@ -1,3 +1,5 @@ +use std::fmt::Write; + use clap::{builder::Str, Parser}; #[derive(Default)] @@ -18,8 +20,10 @@ impl From for Str { ("Cargo Target Triple", env!("VERGEN_CARGO_TARGET_TRIPLE")), ] .iter() - .map(|(k, v)| format!("{k:25}: {v}\n")) - .collect::(); + .fold(String::new(), |mut output, (k, v)| { + writeln!(output, "{k:25}: {v}").unwrap(); + output + }); format!("\n{version:}").into() } diff --git a/utils/gdb_qemu/demo/src/main.rs b/utils/gdb_qemu/gdb_demo/src/main.rs similarity index 82% rename from utils/gdb_qemu/demo/src/main.rs rename to utils/gdb_qemu/gdb_demo/src/main.rs index 1ea296a3cb..445dbe62c3 100644 --- a/utils/gdb_qemu/demo/src/main.rs +++ b/utils/gdb_qemu/gdb_demo/src/main.rs @@ -1,10 +1,10 @@ mod args; -use { - crate::args::Args, - clap::Parser, - std::{thread::sleep, time::Duration}, -}; +use std::{thread::sleep, time::Duration}; + +use clap::Parser; + +use crate::args::Args; #[no_mangle] extern "C" fn run_test(num: usize) { diff --git a/utils/gdb_qemu/gdb_qemu/build.rs b/utils/gdb_qemu/gdb_qemu/build.rs index b8c56e8d82..03565e532e 100644 --- a/utils/gdb_qemu/gdb_qemu/build.rs +++ b/utils/gdb_qemu/gdb_qemu/build.rs @@ -1,4 +1,6 @@ -use {std::error::Error, vergen::EmitBuilder}; +use std::error::Error; + +use vergen::EmitBuilder; fn main() -> Result<(), Box> { EmitBuilder::builder() diff --git a/utils/gdb_qemu/gdb_qemu/src/args/level.rs b/utils/gdb_qemu/gdb_qemu/src/args/level.rs index e516f47ceb..696e428920 100644 --- a/utils/gdb_qemu/gdb_qemu/src/args/level.rs +++ b/utils/gdb_qemu/gdb_qemu/src/args/level.rs @@ -1,4 +1,5 @@ -use {clap::ValueEnum, simplelog::LevelFilter}; +use clap::ValueEnum; +use simplelog::LevelFilter; #[derive(ValueEnum, Debug, Clone, Copy)] pub enum Level { diff --git a/utils/gdb_qemu/gdb_qemu/src/args/mod.rs b/utils/gdb_qemu/gdb_qemu/src/args/mod.rs index dc7ce27b66..d92cac14d9 100644 --- a/utils/gdb_qemu/gdb_qemu/src/args/mod.rs +++ b/utils/gdb_qemu/gdb_qemu/src/args/mod.rs @@ -1,11 +1,11 @@ pub mod level; mod version; -use { - crate::args::{level::Level, version::Version}, - clap::Parser, - std::iter, -}; +use std::iter; + +use clap::Parser; + +use crate::args::{level::Level, version::Version}; pub trait ParentArgs { fn port(&self) -> u16; diff --git a/utils/gdb_qemu/gdb_qemu/src/args/version.rs b/utils/gdb_qemu/gdb_qemu/src/args/version.rs index b664e960b3..84f41727d3 100644 --- a/utils/gdb_qemu/gdb_qemu/src/args/version.rs +++ b/utils/gdb_qemu/gdb_qemu/src/args/version.rs @@ -1,3 +1,5 @@ +use std::fmt::Write; + use clap::builder::Str; #[derive(Default)] @@ -18,8 +20,10 @@ impl From for Str { ("Cargo Target Triple", env!("VERGEN_CARGO_TARGET_TRIPLE")), ] .iter() - .map(|(k, v)| format!("{k:25}: {v}\n")) - .collect::(); + .fold(String::new(), |mut output, (k, v)| { + writeln!(output, "{k:25}: {v}").unwrap(); + output + }); format!("\n{version:}").into() } diff --git a/utils/gdb_qemu/gdb_qemu/src/child.rs b/utils/gdb_qemu/gdb_qemu/src/child.rs index 9ceacd6a46..cd2a64f492 100644 --- a/utils/gdb_qemu/gdb_qemu/src/child.rs +++ b/utils/gdb_qemu/gdb_qemu/src/child.rs @@ -1,11 +1,13 @@ -use { - crate::{args::ChildArgs, exit::Exit}, - anyhow::{anyhow, Result}, - nix::unistd::{dup2, execvp}, - std::ffi::CString, - std::os::fd::{AsRawFd, RawFd}, +use std::{ + ffi::CString, + os::fd::{AsRawFd, RawFd}, }; +use anyhow::{anyhow, Result}; +use nix::unistd::{dup2, execvp}; + +use crate::{args::ChildArgs, exit::Exit}; + pub struct Child { argv: Vec, fd1: RawFd, diff --git a/utils/gdb_qemu/gdb_qemu/src/exit.rs b/utils/gdb_qemu/gdb_qemu/src/exit.rs index c961039f2f..6cddc4cefd 100644 --- a/utils/gdb_qemu/gdb_qemu/src/exit.rs +++ b/utils/gdb_qemu/gdb_qemu/src/exit.rs @@ -1,17 +1,15 @@ -use { - crate::errno::errno, - anyhow::{anyhow, Result}, - libc::{_exit, prctl, PR_SET_PDEATHSIG}, - nix::sys::signal::SIGKILL, - nix::{ - sys::{ - signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, SIGCHLD}, - wait::{waitpid, WaitStatus::Exited}, - }, - unistd::Pid, +use anyhow::{anyhow, Result}; +use libc::{_exit, prctl, PR_SET_PDEATHSIG}; +use nix::{ + sys::{ + signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, SIGCHLD, SIGKILL}, + wait::{waitpid, WaitStatus::Exited}, }, + unistd::Pid, }; +use crate::errno::errno; + pub struct Exit; impl Exit { diff --git a/utils/gdb_qemu/gdb_qemu/src/logger.rs b/utils/gdb_qemu/gdb_qemu/src/logger.rs index 6e1ae877dd..4eb00a50cc 100644 --- a/utils/gdb_qemu/gdb_qemu/src/logger.rs +++ b/utils/gdb_qemu/gdb_qemu/src/logger.rs @@ -1,9 +1,9 @@ -use { - crate::args::LogArgs, - anyhow::{anyhow, Result}, - simplelog::{Config, LevelFilter, WriteLogger}, - std::fs::File, -}; +use std::fs::File; + +use anyhow::{anyhow, Result}; +use simplelog::{Config, LevelFilter, WriteLogger}; + +use crate::args::LogArgs; pub struct Logger; diff --git a/utils/gdb_qemu/gdb_qemu/src/main.rs b/utils/gdb_qemu/gdb_qemu/src/main.rs index c5234044a2..4c832be553 100644 --- a/utils/gdb_qemu/gdb_qemu/src/main.rs +++ b/utils/gdb_qemu/gdb_qemu/src/main.rs @@ -9,12 +9,13 @@ mod parent; extern crate log; extern crate simplelog; -use { - crate::{args::Args, child::Child, exit::Exit, logger::Logger, parent::Parent}, - anyhow::{anyhow, Result}, - clap::Parser, - nix::unistd::{fork, pipe, ForkResult}, -}; +use std::os::fd::AsRawFd; + +use anyhow::{anyhow, Result}; +use clap::Parser; +use nix::unistd::{fork, pipe, ForkResult}; + +use crate::{args::Args, child::Child, exit::Exit, logger::Logger, parent::Parent}; fn main() -> Result<()> { let args = Args::parse(); @@ -30,8 +31,10 @@ fn main() -> Result<()> { let (a2, b2) = pipe().map_err(|e| anyhow!("Failed to create pipe #2: {e:}"))?; match unsafe { fork() } { - Ok(ForkResult::Parent { child: _, .. }) => Parent::new(&args, a1, a2).run()?, - Ok(ForkResult::Child) => Child::new(&args, b1, b2).run()?, + Ok(ForkResult::Parent { child: _, .. }) => { + Parent::new(&args, a1.as_raw_fd(), a2.as_raw_fd()).run()? + } + Ok(ForkResult::Child) => Child::new(&args, b1.as_raw_fd(), b2.as_raw_fd()).run()?, Err(e) => Err(anyhow!("main: fork failed: {e:}"))?, }; Ok(()) diff --git a/utils/gdb_qemu/gdb_qemu/src/parent.rs b/utils/gdb_qemu/gdb_qemu/src/parent.rs index 30648ac377..d7758fbd15 100644 --- a/utils/gdb_qemu/gdb_qemu/src/parent.rs +++ b/utils/gdb_qemu/gdb_qemu/src/parent.rs @@ -1,17 +1,17 @@ -use { - crate::{args::ParentArgs, exit::Exit}, - anyhow::{anyhow, Result}, - nix::unistd::read, - std::{ - fmt, - io::{Read, Write}, - net::{SocketAddr, TcpStream}, - os::fd::RawFd, - thread::spawn, - time::{Duration, SystemTime}, - }, +use std::{ + fmt, + io::{Read, Write}, + net::{SocketAddr, TcpStream}, + os::fd::RawFd, + thread::spawn, + time::{Duration, SystemTime}, }; +use anyhow::{anyhow, Result}; +use nix::unistd::read; + +use crate::{args::ParentArgs, exit::Exit}; + enum Direction { GdbToTarget, TargetToGdb,