Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"args": [
"build",
"--bin=unbug_basic_example",
"--package=unbug_basic_example",
"--features=dev_debug"
"--package=unbug_basic_example"
],
"filter": {
"name": "unbug_basic_example",
Expand Down
3 changes: 1 addition & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"command": "build",
"args": [
"--bin=unbug_basic_example",
"--package=unbug_basic_example",
"--features=dev_debug"
"--package=unbug_basic_example"
],
"problemMatcher": [
"$rustc"
Expand Down
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ documentation = "https://docs.rs/unbug"
license = "MIT OR Apache-2.0"
keywords = ["debug", "debugging", "ensure", "assert", "breakpoint"]
authors = [
"Brian Jesse <brian@greymattergames.net>",
"Brian Jesse <brian@greymattergames.net>",
"Scott Girton <scott@greymattergames.net>",
]
categories = ["development-tools::debugging"]
exclude = [".vscode/", "assets/"]

[features]
default = []
enable = []

[dependencies]
tracing = "0.1"
dbg_breakpoint = "0.1.1"

[features]
default = []
no_cache_debugger = []

[[example]]
name = "basic"
Expand Down
3 changes: 1 addition & 2 deletions examples/basic/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"args": [
"build",
"--bin=unbug_basic_example",
"--package=unbug_basic_example",
"--features=dev_debug"
"--package=unbug_basic_example"
],
"filter": {
"name": "unbug_basic_example",
Expand Down
3 changes: 1 addition & 2 deletions examples/basic/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"command": "build",
"args": [
"--bin=unbug_basic_example",
"--package=unbug_basic_example",
"--features=dev_debug"
"--package=unbug_basic_example"
],
"problemMatcher": [
"$rustc"
Expand Down
6 changes: 0 additions & 6 deletions examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ version = "1.0.0"
edition = "2021"
publish = false

[features]
default = []
dev_debug = [
"unbug/enable"
]

[dependencies]
unbug = { path = "../../" }
tracing-subscriber = "0.3"
83 changes: 53 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![doc = include_str!("../README.md")]

// mod debugger;

#[doc(hidden)]
pub mod _internal {

Expand All @@ -23,6 +25,34 @@ pub mod _internal {
// macro_export will move the macro to the root module, thus it is necessary to use `super::` here
#[doc(hidden)]
pub use super::_internal_once as _once;

#[cfg(not(feature = "no_cache_debugger"))]
pub fn _is_debugger_present() -> bool {
use ::core::sync::atomic::{AtomicBool, Ordering};

static DEBUGGER_CHECKED: AtomicBool = AtomicBool::new(false);
static DEBUGGER_ATTACHED: AtomicBool = AtomicBool::new(false);

if !DEBUGGER_CHECKED.swap(true, Ordering::Relaxed) {
DEBUGGER_ATTACHED.swap(check_debugger(), Ordering::Relaxed);
}

DEBUGGER_ATTACHED.load(Ordering::Relaxed)
}
#[cfg(feature = "no_cache_debugger")]
pub fn _is_debugger_present() -> bool {
check_debugger()
}

fn check_debugger() -> bool {
use ::dbg_breakpoint::{is_debugger_present, DebuggerPresence};

let Some(DebuggerPresence::Detected) = is_debugger_present() else {
return false;
};

true
}
}

/// When enabled, will pause execution with a break point
Expand All @@ -35,50 +65,43 @@ pub mod _internal {
/// unbug::breakpoint!();
/// ```
#[macro_export]
#[cfg(not(all(debug_assertions, feature = "enable")))]
#[cfg(not(debug_assertions))]
macro_rules! breakpoint {
() => {};
}
#[macro_export]
#[cfg(all(
any(target_arch = "x86", target_arch = "x86_64"),
debug_assertions,
feature = "enable"
))]
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), debug_assertions))]
macro_rules! breakpoint {
() => {
unsafe {
::core::arch::asm!("int3;nop");
if $crate::_internal::_is_debugger_present() {
unsafe {
::core::arch::asm!("int3;nop");
}
}
};
}
#[macro_export]
#[cfg(all(
target_arch = "aarch64",
debug_assertions,
feature = "enable"
))]
#[cfg(all(target_arch = "aarch64", debug_assertions))]
macro_rules! breakpoint {
() => {
unsafe {
::core::arch::asm!("brk#0xF000\nnop");
if $crate::_internal::_is_debugger_present() {
unsafe {
::core::arch::asm!("brk#0xF000\nnop");
}
}
};
}
#[macro_export]
#[cfg(all(
not(any(
target_arch = "x86",
target_arch = "x86_64",
target_arch = "aarch64",
)),
debug_assertions,
feature = "enable"
not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64",)),
debug_assertions
))]
macro_rules! breakpoint {
() => {
unsafe {
::core::arch::breakpoint();
if $crate::_internal::_is_debugger_present() {
unsafe {
::core::arch::breakpoint();
}
}
};
}
Expand All @@ -99,13 +122,13 @@ macro_rules! breakpoint {
/// unbug::ensure_always!(false, "a formatted message to log {:?}", some_var);
/// ```
#[macro_export]
#[cfg(not(all(debug_assertions, feature = "enable")))]
#[cfg(not(debug_assertions))]
macro_rules! ensure_always {
($expression: expr) => {};
($expression: expr, $($argument: tt),+ $(,)?) => {};
}
#[macro_export]
#[cfg(all(debug_assertions, feature = "enable"))]
#[cfg(debug_assertions)]
macro_rules! ensure_always {
($expression: expr) => {
if !$expression {
Expand Down Expand Up @@ -136,13 +159,13 @@ macro_rules! ensure_always {
/// unbug::ensure!(false, "a formatted message to log {:?}", some_var);
/// ```
#[macro_export]
#[cfg(not(all(debug_assertions, feature = "enable")))]
#[cfg(not(debug_assertions))]
macro_rules! ensure {
($expression: expr) => {};
($expression: expr, $($argument: tt),+ $(,)?) => {};
}
#[macro_export]
#[cfg(all(debug_assertions, feature = "enable"))]
#[cfg(debug_assertions)]
macro_rules! ensure {
($expression: expr) => {
if !$expression {
Expand Down Expand Up @@ -178,14 +201,14 @@ macro_rules! ensure {
/// unbug::fail_always!("failed to do something: {:?}", some_var);
/// ```
#[macro_export]
#[cfg(not(all(debug_assertions, feature = "enable")))]
#[cfg(not(debug_assertions))]
macro_rules! fail_always {
($($argument: tt),+ $(,)?) => {
$crate::_internal::_error!($($argument),+);
};
}
#[macro_export]
#[cfg(all(debug_assertions, feature = "enable"))]
#[cfg(debug_assertions)]
macro_rules! fail_always {
($($argument: tt),+ $(,)?) => {
$crate::_internal::_error!($($argument),+);
Expand Down