Skip to content

Commit 268c599

Browse files
committed
Cache debugger presence and late_attach feature
1 parent e693a05 commit 268c599

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ documentation = "https://docs.rs/unbug"
88
license = "MIT OR Apache-2.0"
99
keywords = ["debug", "debugging", "ensure", "assert", "breakpoint"]
1010
authors = [
11-
"Brian Jesse <brian@greymattergames.net>",
12-
"Scott Girton <scott@greymattergames.net>"
11+
"Brian Jesse <brian@greymattergames.net>",
12+
"Scott Girton <scott@greymattergames.net>",
1313
]
1414
categories = ["development-tools::debugging"]
1515
exclude = [".vscode/", "assets/"]
1616

1717
[dependencies]
1818
tracing = "0.1"
19-
dbg_breakpoint = "0.1.0"
19+
dbg_breakpoint = "0.1.1"
20+
21+
[features]
22+
default = []
23+
no_cache_debugger = []
2024

2125
[[example]]
2226
name = "basic"

src/lib.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,26 @@ pub mod _internal {
2626
#[doc(hidden)]
2727
pub use super::_internal_once as _once;
2828

29+
#[cfg(not(feature = "no_cache_debugger"))]
2930
pub fn _is_debugger_present() -> bool {
30-
pub use ::dbg_breakpoint::{ is_debugger_present, DebuggerPresence };
31+
use ::core::sync::atomic::{AtomicBool, Ordering};
32+
33+
static DEBUGGER_CHECKED: AtomicBool = AtomicBool::new(false);
34+
static DEBUGGER_ATTACHED: AtomicBool = AtomicBool::new(false);
35+
36+
if !DEBUGGER_CHECKED.swap(true, Ordering::Relaxed) {
37+
DEBUGGER_ATTACHED.swap(check_debugger(), Ordering::Relaxed);
38+
}
39+
40+
DEBUGGER_ATTACHED.load(Ordering::Relaxed)
41+
}
42+
#[cfg(feature = "no_cache_debugger")]
43+
pub fn _is_debugger_present() -> bool {
44+
check_debugger()
45+
}
46+
47+
fn check_debugger() -> bool {
48+
use ::dbg_breakpoint::{is_debugger_present, DebuggerPresence};
3149

3250
let Some(DebuggerPresence::Detected) = is_debugger_present() else {
3351
return false;
@@ -52,10 +70,7 @@ macro_rules! breakpoint {
5270
() => {};
5371
}
5472
#[macro_export]
55-
#[cfg(all(
56-
any(target_arch = "x86", target_arch = "x86_64"),
57-
debug_assertions
58-
))]
73+
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), debug_assertions))]
5974
macro_rules! breakpoint {
6075
() => {
6176
if $crate::_internal::_is_debugger_present() {
@@ -66,10 +81,7 @@ macro_rules! breakpoint {
6681
};
6782
}
6883
#[macro_export]
69-
#[cfg(all(
70-
target_arch = "aarch64",
71-
debug_assertions
72-
))]
84+
#[cfg(all(target_arch = "aarch64", debug_assertions))]
7385
macro_rules! breakpoint {
7486
() => {
7587
if $crate::_internal::_is_debugger_present() {
@@ -81,11 +93,7 @@ macro_rules! breakpoint {
8193
}
8294
#[macro_export]
8395
#[cfg(all(
84-
not(any(
85-
target_arch = "x86",
86-
target_arch = "x86_64",
87-
target_arch = "aarch64",
88-
)),
96+
not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64",)),
8997
debug_assertions
9098
))]
9199
macro_rules! breakpoint {

0 commit comments

Comments
 (0)