Detecting debuggers with asm int 3 (c++ to rust)

c++ code

bool IsDebugged()
{
    __try
    {
        __asm int 3;
        return true;
    }
    __except(EXCEPTION_EXECUTE_HANDLER)
    {
        return false;
    }
}

Rust attempt:

use std::arch::asm;

fn is_debugged() -> bool {
    std::panic::catch_unwind(|| {
        unsafe { asm!("int 3"); }
    }).is_err()
}

fn main() {
    println!("{}", is_debugged());
}

error: process didn't exit successfully: target\debug\p.exe (exit code: 0x80000003)

tried core::intrinsics::breakpoint()

am I doing something wrong? am I not understanding anything correctly?

__try/__except isn't standard C++; it's a compiler extension to support a feature unique to Windows. I'd be surprised if rustc ever added a direct equivalent since it breaks portability.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.