-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[lldb] Expose the Target API mutex through the SB API #133295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| %extend lldb::SBMutex { | ||
| #ifdef SWIGPYTHON | ||
| %pythoncode %{ | ||
| def __enter__(self): | ||
| self.lock() | ||
| return self | ||
|
|
||
| def __exit__(self, exc_type, exc_value, traceback): | ||
| self.unlock() | ||
| %} | ||
| #endif | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| //===-- SBMutex.h | ||
| //----------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLDB_API_SBLOCK_H | ||
| #define LLDB_API_SBLOCK_H | ||
JDevlieghere marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #include "lldb/API/SBDefines.h" | ||
| #include "lldb/lldb-forward.h" | ||
| #include <mutex> | ||
|
|
||
| namespace lldb { | ||
|
|
||
| /// A general-purpose lock in the SB API. The lock can be locked and unlocked. | ||
| /// The default constructed lock is unlocked, but generally the lock is locked | ||
| /// when it is returned from a class. | ||
JDevlieghere marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| class LLDB_API SBMutex { | ||
| public: | ||
| SBMutex(); | ||
| SBMutex(const SBMutex &rhs); | ||
| const SBMutex &operator=(const SBMutex &rhs); | ||
| ~SBMutex(); | ||
|
|
||
| /// Returns true if this lock has ownership of the underlying mutex. | ||
| bool IsValid() const; | ||
|
|
||
| /// Blocking operation that takes ownership of this lock. | ||
| void lock() const; | ||
|
|
||
| /// Releases ownership of this lock. | ||
| void unlock() const; | ||
|
|
||
| private: | ||
| // Private constructor used by SBTarget to create the Target API mutex. | ||
| // Requires a friend declaration. | ||
| SBMutex(lldb::TargetSP target_sp); | ||
| friend class SBTarget; | ||
|
|
||
| std::shared_ptr<std::recursive_mutex> m_opaque_sp; | ||
| }; | ||
| #endif | ||
JDevlieghere marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| } // namespace lldb | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //===-- SBMutex.cpp | ||
JDevlieghere marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //--------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "lldb/API/SBMutex.h" | ||
| #include "lldb/Target/Target.h" | ||
| #include "lldb/Utility/Instrumentation.h" | ||
| #include "lldb/lldb-forward.h" | ||
| #include <memory> | ||
| #include <mutex> | ||
|
|
||
| using namespace lldb; | ||
| using namespace lldb_private; | ||
|
|
||
| SBMutex::SBMutex() { LLDB_INSTRUMENT_VA(this); } | ||
|
|
||
| SBMutex::SBMutex(const SBMutex &rhs) : m_opaque_sp(rhs.m_opaque_sp) { | ||
bulbazord marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| LLDB_INSTRUMENT_VA(this); | ||
| } | ||
|
|
||
| const SBMutex &SBMutex::operator=(const SBMutex &rhs) { | ||
| LLDB_INSTRUMENT_VA(this); | ||
|
|
||
| m_opaque_sp = rhs.m_opaque_sp; | ||
| return *this; | ||
| } | ||
|
|
||
| SBMutex::SBMutex(lldb::TargetSP target_sp) | ||
| : m_opaque_sp(std::shared_ptr<std::recursive_mutex>( | ||
bulbazord marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| target_sp, &target_sp->GetAPIMutex())) { | ||
| LLDB_INSTRUMENT_VA(this, target_sp); | ||
| } | ||
|
|
||
| SBMutex::~SBMutex() { LLDB_INSTRUMENT_VA(this); } | ||
|
|
||
| bool SBMutex::IsValid() const { | ||
| LLDB_INSTRUMENT_VA(this); | ||
|
|
||
| return static_cast<bool>(m_opaque_sp); | ||
| } | ||
|
|
||
| void SBMutex::lock() const { | ||
| LLDB_INSTRUMENT_VA(this); | ||
|
|
||
| if (m_opaque_sp) | ||
| m_opaque_sp->lock(); | ||
| } | ||
|
|
||
| void SBMutex::unlock() const { | ||
| LLDB_INSTRUMENT_VA(this); | ||
|
|
||
| if (m_opaque_sp) | ||
| m_opaque_sp->unlock(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| add_lldb_unittest(APITests | ||
| SBCommandInterpreterTest.cpp | ||
| SBLineEntryTest.cpp | ||
| SBMutexTest.cpp | ||
|
|
||
| LINK_LIBS | ||
| liblldb | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,60 @@ | ||||||||||||||||||
| //===-- SBMutexTest.cpp | ||||||||||||||||||
JDevlieghere marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||
| //----------------------------------------------------===// | ||||||||||||||||||
| // | ||||||||||||||||||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||||||||||||
| // See https://llvm.org/LICENSE.txt for license information. | ||||||||||||||||||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||||||||||
| // | ||||||||||||||||||
| //===----------------------------------------------------------------------===/ | ||||||||||||||||||
|
|
||||||||||||||||||
| // Use the umbrella header for -Wdocumentation. | ||||||||||||||||||
| #include "lldb/API/LLDB.h" | ||||||||||||||||||
|
|
||||||||||||||||||
| #include "TestingSupport/SubsystemRAII.h" | ||||||||||||||||||
| #include "lldb/API/SBDebugger.h" | ||||||||||||||||||
| #include "lldb/API/SBTarget.h" | ||||||||||||||||||
| #include "gtest/gtest.h" | ||||||||||||||||||
| #include <atomic> | ||||||||||||||||||
| #include <chrono> | ||||||||||||||||||
| #include <future> | ||||||||||||||||||
| #include <mutex> | ||||||||||||||||||
|
|
||||||||||||||||||
| using namespace lldb; | ||||||||||||||||||
| using namespace lldb_private; | ||||||||||||||||||
|
|
||||||||||||||||||
| class SBMutexTest : public testing::Test { | ||||||||||||||||||
| protected: | ||||||||||||||||||
| void SetUp() override { debugger = SBDebugger::Create(); } | ||||||||||||||||||
| void TearDown() override { SBDebugger::Destroy(debugger); } | ||||||||||||||||||
|
|
||||||||||||||||||
| SubsystemRAII<lldb::SBDebugger> subsystems; | ||||||||||||||||||
| SBDebugger debugger; | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| TEST_F(SBMutexTest, LockTest) { | ||||||||||||||||||
| lldb::SBTarget target = debugger.GetDummyTarget(); | ||||||||||||||||||
|
|
||||||||||||||||||
| std::future<void> f; | ||||||||||||||||||
| { | ||||||||||||||||||
| std::atomic<bool> locked = false; | ||||||||||||||||||
| lldb::SBMutex lock = target.GetAPIMutex(); | ||||||||||||||||||
| std::lock_guard<lldb::SBMutex> lock_guard(lock); | ||||||||||||||||||
| ASSERT_FALSE(locked.exchange(true)); | ||||||||||||||||||
|
|
||||||||||||||||||
| f = std::async(std::launch::async, [&]() { | ||||||||||||||||||
| { | ||||||||||||||||||
| ASSERT_TRUE(locked); | ||||||||||||||||||
| target.BreakpointCreateByName("foo", "bar"); | ||||||||||||||||||
| ASSERT_FALSE(locked); | ||||||||||||||||||
| } | ||||||||||||||||||
|
||||||||||||||||||
| { | |
| ASSERT_TRUE(locked); | |
| target.BreakpointCreateByName("foo", "bar"); | |
| ASSERT_FALSE(locked); | |
| } | |
| ASSERT_TRUE(locked); | |
| target.BreakpointCreateByName("foo", "bar"); | |
| ASSERT_FALSE(locked); |
bulbazord marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm seeing use-after-scope asan failures with this test. I think the problem is that locked can be accessed asynchronously at any time up util the point where f.wait() is called, but locked is declared inside these braces so it can be destroyed at the closing brace before the call to f.wait(). locked should outlive the wait operation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm building with ASan, but based on your description this should fix it?
diff --git a/lldb/unittests/API/SBMutexTest.cpp b/lldb/unittests/API/SBMutexTest.cpp
index 0b888c2725aa..aafad59d58c1 100644
--- a/lldb/unittests/API/SBMutexTest.cpp
+++ b/lldb/unittests/API/SBMutexTest.cpp
@@ -32,10 +32,9 @@ protected:
TEST_F(SBMutexTest, LockTest) {
lldb::SBTarget target = debugger.GetDummyTarget();
-
+ std::atomic<bool> locked = false;
std::future<void> f;
{
- std::atomic<bool> locked = false;
lldb::SBMutex lock = target.GetAPIMutex();
std::lock_guard<lldb::SBMutex> lock_guard(lock);
ASSERT_FALSE(locked.exchange(true));
If you have a local build to confirm let me know, otherwise I'll put up a PR once my build finishes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that fix, and I agree it looks like the right fix to me. It seems to solve the issue.
However after applying it I get some memory leaks reported by LeakSanitizer (our internal asan config enables it) that I haven't been able to debug yet. I'm working through the report and so far all the reported indirect leaks I've looked into seem to be owned by shared pointers, so it's weird and I haven't yet discarded the possibility that the leaks are a problem on my end. All this goes to say I don't have a fully successful run to prove the fix works, but I think the fix is right :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.