1#[cfg(target_os = "windows")]
2pub fn is_debugger_present() -> bool {
3 let ret = unsafe { windows_sys::Win32::System::Diagnostics::Debug::IsDebuggerPresent() };
4 ret != 0
5}
6
7#[cfg(target_os = "linux")]
8pub fn is_debugger_present() -> bool {
9 let proc = std::fs::read_to_string("/proc/self/status");
10 let Ok(proc) = proc else { return true; };
11 let pid = proc.lines().find(|line| line.starts_with("TracerPid:"));
12 let Some(pid) = pid else { return true; };
13 !pid.ends_with("\t0") && !pid.ends_with(" 0")
14}
15
16#[cfg(target_os = "macos")]
17pub fn is_debugger_present() -> bool {
18 let ret = unsafe { libc::ptrace(libc::PT_DENY_ATTACH, 0, std::ptr::null_mut(), 0) };
19 ret == -1
20}
21
22#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
23pub fn is_debugger_present() -> bool {
24 false
25}
26
27#[cfg(test)]
28mod tests {
29 #[test]
30 fn test_is_debugger_present() {
31 assert!(!super::is_debugger_present());
32 assert!(!super::is_debugger_present());
33 assert!(!super::is_debugger_present());
34 }
35}