diff --git a/AK/Debug.h b/AK/Debug.h index fa4297948a..5358103df1 100644 --- a/AK/Debug.h +++ b/AK/Debug.h @@ -573,3 +573,15 @@ constexpr bool debug_lock_restore = true; #else constexpr bool debug_lock_restore = false; #endif + +#ifdef FUTEXQUEUE_DEBUG +constexpr bool debug_futex_queue = true; +#else +constexpr bool debug_futex_queue = false; +#endif + +#ifdef FUTEX_DEBUG +constexpr bool debug_futex = true; +#else +constexpr bool debug_futex = false; +#endif diff --git a/AK/LogStream.cpp b/AK/LogStream.cpp index 416e3ce2b3..f5108f0b08 100644 --- a/AK/LogStream.cpp +++ b/AK/LogStream.cpp @@ -149,7 +149,10 @@ KernelLogStream klog() #else DebugLogStream klog() { +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" return dbg(); +# pragma GCC diagnostic pop } #endif diff --git a/AK/LogStream.h b/AK/LogStream.h index c5e80c97b9..9d6a0900bf 100644 --- a/AK/LogStream.h +++ b/AK/LogStream.h @@ -179,7 +179,7 @@ inline const LogStream& operator<<(const LogStream& stream, bool value) return stream << (value ? "true" : "false"); } -DebugLogStream dbg(); +[[deprecated("Plase use dbgln in AK/Format.h instead.")]] DebugLogStream dbg(); #ifdef KERNEL KernelLogStream klog(); diff --git a/Kernel/FutexQueue.cpp b/Kernel/FutexQueue.cpp index e5aa42454b..d12435782a 100644 --- a/Kernel/FutexQueue.cpp +++ b/Kernel/FutexQueue.cpp @@ -24,11 +24,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include -//#define FUTEXQUEUE_DEBUG - namespace Kernel { bool FutexQueue::should_add_blocker(Thread::Blocker& b, void* data) @@ -36,9 +35,9 @@ bool FutexQueue::should_add_blocker(Thread::Blocker& b, void* data) ASSERT(data != nullptr); // Thread that is requesting to be blocked ASSERT(m_lock.is_locked()); ASSERT(b.blocker_type() == Thread::Blocker::Type::Futex); -#ifdef FUTEXQUEUE_DEBUG - dbg() << "FutexQueue @ " << this << ": should block thread " << *static_cast(data); -#endif + + dbgln("FutexQueue @ {}: should block thread {}", this, *static_cast(data)); + return true; } @@ -46,17 +45,16 @@ u32 FutexQueue::wake_n_requeue(u32 wake_count, const Function& ge { is_empty_target = false; ScopedSpinLock lock(m_lock); -#ifdef FUTEXQUEUE_DEBUG - dbg() << "FutexQueue @ " << this << ": wake_n_requeue(" << wake_count << ", " << requeue_count << ")"; -#endif + + dbgln("FutexQueue @ {}: wake_n_requeue({}, {})", this, wake_count, requeue_count); + u32 did_wake = 0, did_requeue = 0; do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) { ASSERT(data); ASSERT(b.blocker_type() == Thread::Blocker::Type::Futex); auto& blocker = static_cast(b); -#ifdef FUTEXQUEUE_DEBUG - dbg() << "FutexQueue @ " << this << ": wake_n_requeue unblocking " << *static_cast(data); -#endif + + dbgln("FutexQueue @ {}: wake_n_requeue unblocking {}", this, *static_cast(data)); ASSERT(did_wake < wake_count); if (blocker.unblock()) { if (++did_wake >= wake_count) @@ -70,9 +68,8 @@ u32 FutexQueue::wake_n_requeue(u32 wake_count, const Function& ge auto blockers_to_requeue = do_take_blockers(requeue_count); if (!blockers_to_requeue.is_empty()) { if (auto* target_futex_queue = get_target_queue()) { -#ifdef FUTEXQUEUE_DEBUG - dbg() << "FutexQueue @ " << this << ": wake_n_requeue requeueing " << blockers_to_requeue.size() << " blockers to " << target_futex_queue; -#endif + dbgln("FutexQueue @ {}: wake_n_requeue requeueing {} blockers to {}", this, blockers_to_requeue.size(), target_futex_queue); + // While still holding m_lock, notify each blocker for (auto& info : blockers_to_requeue) { ASSERT(info.blocker->blocker_type() == Thread::Blocker::Type::Futex); @@ -94,9 +91,7 @@ u32 FutexQueue::wake_n_requeue(u32 wake_count, const Function& ge target_futex_queue->do_append_blockers(move(blockers_to_requeue)); is_empty_target = target_futex_queue->is_empty_locked(); } else { -#ifdef FUTEXQUEUE_DEBUG - dbg() << "FutexQueue @ " << this << ": wake_n_requeue could not get target queue to requeueing " << blockers_to_requeue.size() << " blockers"; -#endif + dbgln("FutexQueue @ {}: wake_n_requeue could not get target queue to requeue {} blockers", this, blockers_to_requeue.size()); do_append_blockers(move(blockers_to_requeue)); } } @@ -109,17 +104,14 @@ u32 FutexQueue::wake_n(u32 wake_count, const Optional& bitset, bool& is_emp if (wake_count == 0) return 0; // should we assert instead? ScopedSpinLock lock(m_lock); -#ifdef FUTEXQUEUE_DEBUG - dbg() << "FutexQueue @ " << this << ": wake_n(" << wake_count << ")"; -#endif + dbgln("FutexQueue @ {}: wake_n({})", this, wake_count); u32 did_wake = 0; do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) { ASSERT(data); ASSERT(b.blocker_type() == Thread::Blocker::Type::Futex); auto& blocker = static_cast(b); -#ifdef FUTEXQUEUE_DEBUG - dbg() << "FutexQueue @ " << this << ": wake_n unblocking " << *static_cast(data); -#endif + + dbgln("FutexQueue @ {}: wake_n unblocking {}", this, *static_cast(data)); ASSERT(did_wake < wake_count); if (bitset.has_value() ? blocker.unblock_bitset(bitset.value()) : blocker.unblock()) { if (++did_wake >= wake_count) @@ -135,17 +127,13 @@ u32 FutexQueue::wake_n(u32 wake_count, const Optional& bitset, bool& is_emp u32 FutexQueue::wake_all(bool& is_empty) { ScopedSpinLock lock(m_lock); -#ifdef FUTEXQUEUE_DEBUG - dbg() << "FutexQueue @ " << this << ": wake_all"; -#endif + dbgln("FutexQueue @ {}: wake_all", this); u32 did_wake = 0; do_unblock([&](Thread::Blocker& b, void* data, bool&) { ASSERT(data); ASSERT(b.blocker_type() == Thread::Blocker::Type::Futex); auto& blocker = static_cast(b); -#ifdef FUTEXQUEUE_DEBUG - dbg() << "FutexQueue @ " << this << ": wake_all unblocking " << *static_cast(data); -#endif + dbgln("FutexQueue @ {}: wake_all unblocking", this, *static_cast(data)); if (blocker.unblock(true)) { did_wake++; return true; diff --git a/Kernel/Syscalls/futex.cpp b/Kernel/Syscalls/futex.cpp index 6057d93766..2851bced4e 100644 --- a/Kernel/Syscalls/futex.cpp +++ b/Kernel/Syscalls/futex.cpp @@ -24,13 +24,12 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include #include -//#define FUTEX_DEBUG - namespace Kernel { static SpinLock g_global_futex_lock; @@ -40,9 +39,10 @@ FutexQueue::FutexQueue(FlatPtr user_address_or_offset, VMObject* vmobject) : m_user_address_or_offset(user_address_or_offset) , m_is_global(vmobject != nullptr) { -#ifdef FUTEX_DEBUG - dbg() << "Futex @ " << this << (m_is_global ? " (global)" : "(local)"); -#endif + dbgln("Futex @ {}{}", + this, + m_is_global ? " (global)" : " (local)"); + if (m_is_global) { // Only register for global futexes m_vmobject = vmobject->make_weak_ptr(); @@ -56,9 +56,9 @@ FutexQueue::~FutexQueue() if (auto vmobject = m_vmobject.strong_ref()) vmobject->unregister_on_deleted_handler(*this); } -#ifdef FUTEX_DEBUG - dbg() << "~Futex @ " << this << (m_is_global ? " (global)" : "(local)"); -#endif + dbgln("~Futex @ {}{}", + this, + m_is_global ? " (global)" : " (local)"); } void FutexQueue::vmobject_deleted(VMObject& vmobject) @@ -68,9 +68,9 @@ void FutexQueue::vmobject_deleted(VMObject& vmobject) // to make sure we have at last a reference until we're done NonnullRefPtr own_ref(*this); -#ifdef FUTEX_DEBUG - dbg() << "Futex::vmobject_deleted @ " << this << (m_is_global ? " (global)" : "(local)"); -#endif + dbgln("Futex::vmobject_deleted @ {}{}", + this, + m_is_global ? " (global)" : " (local)"); // Because this is called from the VMObject's destructor, getting a // strong_ref in this function is unsafe! @@ -83,12 +83,12 @@ void FutexQueue::vmobject_deleted(VMObject& vmobject) bool did_wake_all; auto wake_count = wake_all(did_wake_all); -#ifdef FUTEX_DEBUG - if (wake_count > 0) - dbg() << "Futex: @ " << this << " unblocked " << wake_count << " waiters due to vmobject free"; -#else - (void)wake_count; -#endif + + if constexpr (debug_futex) { + if (wake_count > 0) + dbgln("Futex @ {} unblocked {} waiters due to vmobject free", this, wake_count); + } + ASSERT(did_wake_all); // No one should be left behind... } @@ -233,7 +233,7 @@ int Process::sys$futex(Userspace user_params) if (!user_value.has_value()) return -EFAULT; if (user_value.value() != params.val) { - dbg() << "futex wait: EAGAIN. user value: " << (void*)user_value.value() << " @ " << (void*)params.userspace_address << " != val: " << params.val; + dbgln("futex wait: EAGAIN. user value: {:p} @ {:p} != val: {}", user_value.value(), params.userspace_address, params.val); return -EAGAIN; } atomic_thread_fence(AK::MemoryOrder::memory_order_acquire);