use crate::*;
use crate::debug::DebugEvent;
use abistr::AsCStr;
use winapi::shared::minwindef::FALSE;
use winapi::um::debugapi::*;
use winapi::um::winbase::INFINITE;
use core::time::Duration;
pub fn check_remote_debugger_present<'a>(process: impl AsRef<process::PsuedoHandle<'a>>) -> Result<bool, Error> {
let mut result = 0;
Error::get_last_if(FALSE == unsafe { CheckRemoteDebuggerPresent(process.as_ref().as_handle(), &mut result) })?;
Ok(result != FALSE)
}
pub fn continue_debug_event(process_id: process::Id, thread_id: thread::Id, continue_status: u32) -> Result<(), Error> {
Error::get_last_if(FALSE == unsafe { ContinueDebugEvent(process_id, thread_id, continue_status) })
}
pub fn debug_active_process(process_id: process::Id) -> Result<(), Error> {
Error::get_last_if(FALSE == unsafe { DebugActiveProcess(process_id) })
}
pub fn debug_active_process_stop(process_id: process::Id) -> Result<(), Error> {
Error::get_last_if(FALSE == unsafe { DebugActiveProcessStop(process_id) })
}
pub fn debug_break() {
unsafe { DebugBreak() }
}
pub fn is_debugger_present() -> bool {
FALSE != unsafe { IsDebuggerPresent() }
}
pub fn output_debug_string_a(output_string: impl AsCStr) {
unsafe { OutputDebugStringA(output_string.as_cstr()) }
}
pub fn output_debug_string_w(output_string: impl AsCStr<u16>) {
unsafe { OutputDebugStringW(output_string.as_cstr()) }
}
pub fn wait_for_debug_event(timeout: impl Into<Option<Duration>>) -> Result<DebugEvent, Error> {
let timeout_ms = timeout.into().map_or(INFINITE, |d| u32::try_from(d.as_millis()).unwrap_or(INFINITE).max(INFINITE-1));
let mut de = Default::default();
Error::get_last_if(FALSE == unsafe { WaitForDebugEvent(&mut de, timeout_ms) })?;
Ok(unsafe { DebugEvent::from_raw(de) })
}
pub fn wait_for_debug_event_ex(timeout: impl Into<Option<Duration>>) -> Result<DebugEvent, Error> {
let timeout_ms = timeout.into().map_or(INFINITE, |d| u32::try_from(d.as_millis()).unwrap_or(INFINITE).max(INFINITE-1));
let mut de = Default::default();
Error::get_last_if(FALSE == unsafe { WaitForDebugEventEx(&mut de, timeout_ms) })?;
Ok(unsafe { DebugEvent::from_raw(de) })
}