-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[lldb] Expose the Target API lock through the SB API #131404
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
%extend lldb::SBLock { | ||
#ifdef SWIGPYTHON | ||
%pythoncode %{ | ||
def __enter__(self): | ||
if not self.IsValid(): | ||
self.Lock() | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
self.Unlock() | ||
%} | ||
#endif | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//===-- SBLock.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 | ||
|
||
#include "lldb/API/SBDefines.h" | ||
#include "lldb/lldb-forward.h" | ||
#include <mutex> | ||
|
||
namespace lldb_private { | ||
class APILock; | ||
} | ||
|
||
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. | ||
class LLDB_API SBLock { | ||
public: | ||
SBLock(); | ||
SBLock(SBLock &&rhs); | ||
SBLock &operator=(SBLock &&rhs); | ||
~SBLock(); | ||
|
||
/// 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 lock. | ||
// Requires a friend declaration. | ||
SBLock(lldb::TargetSP target_sp); | ||
friend class SBTarget; | ||
|
||
SBLock(const SBLock &rhs) = delete; | ||
const SBLock &operator=(const SBLock &rhs) = delete; | ||
|
||
std::unique_ptr<lldb_private::APILock> m_opaque_up; | ||
}; | ||
#endif | ||
|
||
} // namespace lldb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//===-- SBLock.cpp --------------------------------------------------------===// | ||
// | ||
// 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/SBLock.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; | ||
|
||
SBLock::SBLock() { LLDB_INSTRUMENT_VA(this); } | ||
|
||
SBLock::SBLock(SBLock &&rhs) : m_opaque_up(std::move(rhs.m_opaque_up)) { | ||
LLDB_INSTRUMENT_VA(this); | ||
} | ||
|
||
SBLock &SBLock::operator=(SBLock &&rhs) { | ||
LLDB_INSTRUMENT_VA(this); | ||
|
||
m_opaque_up = std::move(rhs.m_opaque_up); | ||
return *this; | ||
} | ||
|
||
SBLock::SBLock(lldb::TargetSP target_sp) | ||
: m_opaque_up( | ||
std::make_unique<APILock>(std::shared_ptr<std::recursive_mutex>( | ||
target_sp, &target_sp->GetAPIMutex()))) { | ||
LLDB_INSTRUMENT_VA(this, target_sp); | ||
} | ||
|
||
SBLock::~SBLock() { LLDB_INSTRUMENT_VA(this); } | ||
|
||
bool SBLock::IsValid() const { | ||
LLDB_INSTRUMENT_VA(this); | ||
|
||
return static_cast<bool>(m_opaque_up) && static_cast<bool>(*m_opaque_up); | ||
} | ||
|
||
void SBLock::Lock() const { | ||
LLDB_INSTRUMENT_VA(this); | ||
|
||
if (m_opaque_up) | ||
m_opaque_up->Lock(); | ||
} | ||
|
||
void SBLock::Unlock() const { | ||
LLDB_INSTRUMENT_VA(this); | ||
|
||
if (m_opaque_up) | ||
m_opaque_up->Unlock(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
add_lldb_unittest(APITests | ||
SBCommandInterpreterTest.cpp | ||
SBLineEntryTest.cpp | ||
SBLockTest.cpp | ||
|
||
LINK_LIBS | ||
liblldb | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//===-- SBLockTest.cpp ----------------------------------------------------===// | ||
// | ||
// 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> | ||
|
||
using namespace lldb; | ||
using namespace lldb_private; | ||
|
||
class SBLockTest : public testing::Test { | ||
protected: | ||
void SetUp() override { debugger = SBDebugger::Create(); } | ||
void TearDown() override { SBDebugger::Destroy(debugger); } | ||
|
||
SubsystemRAII<lldb::SBDebugger> subsystems; | ||
SBDebugger debugger; | ||
}; | ||
|
||
TEST_F(SBLockTest, LockTest) { | ||
lldb::SBTarget target = debugger.GetDummyTarget(); | ||
|
||
std::future<void> f; | ||
{ | ||
std::atomic<bool> locked = false; | ||
lldb::SBLock lock = target.AcquireAPILock(); | ||
ASSERT_FALSE(locked.exchange(true)); | ||
|
||
f = std::async(std::launch::async, [&]() { | ||
{ | ||
ASSERT_TRUE(locked); | ||
target.BreakpointCreateByName("foo", "bar"); | ||
ASSERT_FALSE(locked); | ||
} | ||
}); | ||
ASSERT_TRUE(f.valid()); | ||
|
||
// Wait 500ms to confirm the thread is blocked. | ||
auto status = f.wait_for(std::chrono::milliseconds(500)); | ||
ASSERT_EQ(status, std::future_status::timeout); | ||
|
||
ASSERT_TRUE(locked.exchange(false)); | ||
} | ||
f.wait(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What would you say to removing these, and having SBLock::Unlock do its work by clearing the unique_ptr ?