Verified Commit 77d7417f authored by jD91mZM2's avatar jD91mZM2

Implement vFile

Finally, debugging from a container works! Redox OS support is soon:tm:
parent a40315f9
......@@ -21,13 +21,18 @@ name = "atty"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "1.1.0"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "byteorder"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
......@@ -42,7 +47,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -65,6 +70,9 @@ dependencies = [
name = "gdb-remote-protocol"
version = "0.1.0"
dependencies = [
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -78,7 +86,7 @@ version = "0.1.0"
dependencies = [
"env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"gdb-remote-protocol 0.1.0",
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -107,7 +115,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.62"
version = "0.2.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
......@@ -123,7 +131,7 @@ name = "memchr"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -328,14 +336,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum aho-corasick 0.7.10 (registry+https://github.com/rust-lang/crates.io-index)" = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada"
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90"
"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd"
"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
"checksum byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
"checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
"checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
"checksum humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba"
"checksum libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)" = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
"checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a"
"checksum memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
......
Subproject commit 74043ee893bf00d25f8b3af05b2e362ad328a690
Subproject commit e69cded56cea63c06f5b5391020e10a5a3ff7196
use std::{cmp::min, borrow::Cow, net::TcpListener, convert::TryFrom};
use gdb_remote_protocol::{
Error, Handler, Id, MemoryRegion, ProcessType, StopReason, ThreadId, VCont,
VContFeature,
Error, FileSystem, Handler, Id, LibcFS, MemoryRegion, ProcessType,
StopReason, ThreadId, VCont, VContFeature,
};
use log::debug;
use structopt::StructOpt;
......@@ -11,6 +11,9 @@ mod os;
use os::{Os, Registers, Target};
const ERROR_PARSE_STRING: u8 = 0;
const ERROR_GET_PATH: u8 = 1;
#[derive(Debug, StructOpt)]
struct Opt {
/// The address which to bind the server to
......@@ -26,6 +29,7 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
pub struct App {
tracee: Os,
fs: LibcFS,
}
impl Handler for App {
fn attached(&self, _pid: Option<u64>) -> Result<ProcessType> {
......@@ -59,6 +63,7 @@ impl Handler for App {
fn query_supported_features(&self) -> Vec<String> {
vec![
String::from("qXfer:features:read+"),
String::from("qXfer:exec-file:read+"),
]
}
fn query_supported_vcont(&self) -> Result<Cow<'static, [VContFeature]>> {
......@@ -115,22 +120,31 @@ impl Handler for App {
Ok(self.tracee.status())
}
fn read_bytes(&self, object: String, annex: String, offset: u64, length: u64) -> Result<(Vec<u8>, bool)> {
let transfer_bytes = |source: &[u8]| -> Result<(Vec<u8>, bool)> {
let start = usize::try_from(offset).expect("usize < u64");
let end = start.saturating_add(usize::try_from(length).expect("usize < u64"));
if start >= source.len() {
return Ok((Vec::new(), true));
}
let slice = &source[start..min(end, source.len())];
Ok((Vec::from(slice), false))
};
match (&*object, &*annex) {
("features", "target.xml") => {
let target_xml = include_str!("../target-desc.xml");
let start = usize::try_from(offset).expect("usize < u64");
let end = start.saturating_add(usize::try_from(length).expect("usize < u64"));
if start >= target_xml.len() {
return Ok((Vec::new(), true));
}
let slice = &target_xml[start..min(end, target_xml.len())];
dbg!(slice.len());
Ok((Vec::from(slice), false))
let target_xml = include_bytes!("../target-desc.xml");
transfer_bytes(&target_xml[..])
},
("exec-file", pid) => {
let pid = usize::from_str_radix(pid, 16).map_err(|_| Error::Error(ERROR_PARSE_STRING))?;
let path = self.tracee.path(pid)?;
transfer_bytes(&path[..])
},
_ => Err(Error::Unimplemented),
}
}
fn fs(&self) -> Result<&dyn FileSystem, ()> {
Ok(&self.fs)
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
......@@ -148,7 +162,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let tracee = Os::new(opt.program, opt.args)?;
gdb_remote_protocol::process_packets_from(&mut reader, &mut writer, App { tracee });
gdb_remote_protocol::process_packets_from(&mut reader, &mut writer, App {
tracee,
fs: LibcFS::default(),
});
Ok(())
}
use super::Registers;
use crate::Result;
use log::error;
use log::{error, warn};
use std::{
cell::Cell,
ffi::CString,
io, iter,
io,
iter,
mem::{self, MaybeUninit},
os::unix::ffi::OsStrExt,
path::PathBuf,
ptr,
};
......@@ -404,6 +407,18 @@ impl super::Target for Os {
}
Ok(())
}
fn path(&self, pid: usize) -> Result<Vec<u8>> {
let mut path = PathBuf::from("/proc");
path.push(pid.to_string());
path.push("exe");
let link = path.read_link().map_err(|err| {
warn!("failed to read link {}: {}", path.display(), err);
Error::Error(crate::ERROR_GET_PATH)
})?;
Ok(Vec::from(link.as_os_str().as_bytes()))
}
}
impl Drop for Os {
fn drop(&mut self) {
......
......@@ -55,4 +55,7 @@ pub trait Target: Sized {
/// Continue execution until signal or other breakpoint
fn cont(&self, signal: Option<u8>) -> Result<()>;
/// Return the executable that's being run for specified PID
fn path(&self, pid: usize) -> Result<Vec<u8>>;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment