use crate::common::*;
use crate::target::Target;
use crate::target::TargetResult;
pub enum ShouldTerminate {
Yes,
No,
}
impl ShouldTerminate {
pub fn into_bool(self) -> bool {
match self {
ShouldTerminate::Yes => true,
ShouldTerminate::No => false,
}
}
}
pub enum AttachKind {
Attach,
Run,
}
impl AttachKind {
pub(crate) fn was_attached(self) -> bool {
match self {
AttachKind::Attach => true,
AttachKind::Run => false,
}
}
}
pub trait ExtendedMode: Target {
fn run(&mut self, filename: Option<&[u8]>, args: Args<'_, '_>) -> TargetResult<Pid, Self>;
fn attach(&mut self, pid: Pid) -> TargetResult<(), Self>;
fn query_if_attached(&mut self, pid: Pid) -> TargetResult<AttachKind, Self>;
fn kill(&mut self, pid: Option<Pid>) -> TargetResult<ShouldTerminate, Self>;
fn restart(&mut self) -> Result<(), Self::Error>;
fn on_start(&mut self) -> Result<(), Self::Error> {
Ok(())
}
#[inline(always)]
fn support_configure_aslr(&mut self) -> Option<ConfigureAslrOps<'_, Self>> {
None
}
#[inline(always)]
fn support_configure_env(&mut self) -> Option<ConfigureEnvOps<'_, Self>> {
None
}
#[inline(always)]
fn support_configure_startup_shell(&mut self) -> Option<ConfigureStartupShellOps<'_, Self>> {
None
}
#[inline(always)]
fn support_configure_working_dir(&mut self) -> Option<ConfigureWorkingDirOps<'_, Self>> {
None
}
#[inline(always)]
fn support_current_active_pid(&mut self) -> Option<CurrentActivePidOps<'_, Self>> {
None
}
}
define_ext!(ExtendedModeOps, ExtendedMode);
pub struct Args<'a, 'args> {
inner: &'a mut dyn Iterator<Item = &'args [u8]>,
}
impl core::fmt::Debug for Args<'_, '_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Args {{ .. }}")
}
}
impl<'a, 'b> Args<'a, 'b> {
pub(crate) fn new(inner: &'a mut dyn Iterator<Item = &'b [u8]>) -> Args<'a, 'b> {
Args { inner }
}
}
impl<'args> Iterator for Args<'_, 'args> {
type Item = &'args [u8];
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
pub trait ConfigureAslr: ExtendedMode {
fn cfg_aslr(&mut self, enabled: bool) -> TargetResult<(), Self>;
}
define_ext!(ConfigureAslrOps, ConfigureAslr);
pub trait ConfigureEnv: ExtendedMode {
fn set_env(&mut self, key: &[u8], val: Option<&[u8]>) -> TargetResult<(), Self>;
fn remove_env(&mut self, key: &[u8]) -> TargetResult<(), Self>;
fn reset_env(&mut self) -> TargetResult<(), Self>;
}
define_ext!(ConfigureEnvOps, ConfigureEnv);
pub trait ConfigureStartupShell: ExtendedMode {
fn cfg_startup_with_shell(&mut self, enabled: bool) -> TargetResult<(), Self>;
}
define_ext!(ConfigureStartupShellOps, ConfigureStartupShell);
pub trait ConfigureWorkingDir: ExtendedMode {
fn cfg_working_dir(&mut self, dir: Option<&[u8]>) -> TargetResult<(), Self>;
}
define_ext!(ConfigureWorkingDirOps, ConfigureWorkingDir);
pub trait CurrentActivePid: ExtendedMode {
fn current_active_pid(&mut self) -> Result<Pid, Self::Error>;
}
define_ext!(CurrentActivePidOps, CurrentActivePid);