Skip to content

Commit

Permalink
Fix gdb-qemu (#2773)
Browse files Browse the repository at this point in the history
Co-authored-by: WorksButNotTested
  • Loading branch information
WorksButNotTested authored Dec 17, 2024
1 parent 61e3f0b commit f9b643e
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 66 deletions.
2 changes: 1 addition & 1 deletion utils/gdb_qemu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
resolver = "2"
members = ["gdb_qemu", "demo"]
members = ["gdb_qemu", "gdb_demo"]
10 changes: 5 additions & 5 deletions utils/gdb_qemu/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ command = "cargo"
args = [
"build",
"-p",
"demo",
"gdb_demo",
"--profile",
"${PROFILE}",
"--target",
Expand All @@ -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"]
Expand All @@ -65,7 +65,7 @@ args = [
"/usr/powerpc-linux-gnu",
"-g",
"1234",
"${DEMO_DIR}/demo",
"${DEMO_DIR}/gdb_demo",
]

[tasks.gdb]
Expand All @@ -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]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use {std::error::Error, vergen::EmitBuilder};
use std::error::Error;

use vergen::EmitBuilder;

fn main() -> Result<(), Box<dyn Error>> {
EmitBuilder::builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Write;

use clap::{builder::Str, Parser};

#[derive(Default)]
Expand All @@ -18,8 +20,10 @@ impl From<Version> for Str {
("Cargo Target Triple", env!("VERGEN_CARGO_TARGET_TRIPLE")),
]
.iter()
.map(|(k, v)| format!("{k:25}: {v}\n"))
.collect::<String>();
.fold(String::new(), |mut output, (k, v)| {
writeln!(output, "{k:25}: {v}").unwrap();
output
});

format!("\n{version:}").into()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion utils/gdb_qemu/gdb_qemu/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use {std::error::Error, vergen::EmitBuilder};
use std::error::Error;

use vergen::EmitBuilder;

fn main() -> Result<(), Box<dyn Error>> {
EmitBuilder::builder()
Expand Down
3 changes: 2 additions & 1 deletion utils/gdb_qemu/gdb_qemu/src/args/level.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use {clap::ValueEnum, simplelog::LevelFilter};
use clap::ValueEnum;
use simplelog::LevelFilter;

#[derive(ValueEnum, Debug, Clone, Copy)]
pub enum Level {
Expand Down
10 changes: 5 additions & 5 deletions utils/gdb_qemu/gdb_qemu/src/args/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 6 additions & 2 deletions utils/gdb_qemu/gdb_qemu/src/args/version.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Write;

use clap::builder::Str;

#[derive(Default)]
Expand All @@ -18,8 +20,10 @@ impl From<Version> for Str {
("Cargo Target Triple", env!("VERGEN_CARGO_TARGET_TRIPLE")),
]
.iter()
.map(|(k, v)| format!("{k:25}: {v}\n"))
.collect::<String>();
.fold(String::new(), |mut output, (k, v)| {
writeln!(output, "{k:25}: {v}").unwrap();
output
});

format!("\n{version:}").into()
}
Expand Down
14 changes: 8 additions & 6 deletions utils/gdb_qemu/gdb_qemu/src/child.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
fd1: RawFd,
Expand Down
20 changes: 9 additions & 11 deletions utils/gdb_qemu/gdb_qemu/src/exit.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions utils/gdb_qemu/gdb_qemu/src/logger.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
19 changes: 11 additions & 8 deletions utils/gdb_qemu/gdb_qemu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(())
Expand Down
24 changes: 12 additions & 12 deletions utils/gdb_qemu/gdb_qemu/src/parent.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit f9b643e

Please sign in to comment.