Skip to content

Commit 79241d2

Browse files
committed
[lldb] Expose the Target API mutex through the SB API
Expose u target API mutex through the SB API. This is motivated by lldb-dap, which is built on top of the SB API and needs a way to execute a series of SB API calls in an atomic manner (see llvm#131242). We can solve this problem by either introducing an additional layer of locking at the DAP level or by exposing the existing locking at the SB API level. This patch implements the second approach. This was discussed in an RFC on Discourse [0]. The original implementation exposed a move-only lock rather than a mutex [1] which doesn't work well with SWIG 4.0 [2]. This implement the alternative solution of exposing the mutex rather than the lock. The SBMutex conforms to the BasicLockable requirement [3] (which is why the methods are called `lock` and `unlock` rather than Lock and Unlock) so it can be used as `std::lock_guard<lldb::SBMutex>` and `std::unique_lock<lldb::SBMutex>`. [0]: https://discourse.llvm.org/t/rfc-exposing-the-target-api-lock-through-the-sb-api/85215/6 [1]: llvm#131404 [2]: https://discourse.llvm.org/t/rfc-bumping-the-minimum-swig-version-to-4-1-0/85377/9 [3]: https://en.cppreference.com/w/cpp/named_req/BasicLockable
1 parent 59d0607 commit 79241d2

File tree

13 files changed

+241
-6
lines changed

13 files changed

+241
-6
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
%extend lldb::SBMutex {
2+
#ifdef SWIGPYTHON
3+
%pythoncode %{
4+
def __enter__(self):
5+
self.lock()
6+
return self
7+
8+
def __exit__(self, exc_type, exc_value, traceback):
9+
self.unlock()
10+
%}
11+
#endif
12+
}

lldb/bindings/interfaces.swig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
%include "./interface/SBMemoryRegionInfoListDocstrings.i"
5252
%include "./interface/SBModuleDocstrings.i"
5353
%include "./interface/SBModuleSpecDocstrings.i"
54+
%include "./interface/SBMutexExtensions.i"
5455
%include "./interface/SBPlatformDocstrings.i"
5556
%include "./interface/SBProcessDocstrings.i"
5657
%include "./interface/SBProcessInfoDocstrings.i"
@@ -121,15 +122,16 @@
121122
%include "lldb/API/SBHostOS.h"
122123
%include "lldb/API/SBInstruction.h"
123124
%include "lldb/API/SBInstructionList.h"
124-
%include "lldb/API/SBLanguages.h"
125125
%include "lldb/API/SBLanguageRuntime.h"
126+
%include "lldb/API/SBLanguages.h"
126127
%include "lldb/API/SBLaunchInfo.h"
127128
%include "lldb/API/SBLineEntry.h"
128129
%include "lldb/API/SBListener.h"
129130
%include "lldb/API/SBMemoryRegionInfo.h"
130131
%include "lldb/API/SBMemoryRegionInfoList.h"
131132
%include "lldb/API/SBModule.h"
132133
%include "lldb/API/SBModuleSpec.h"
134+
%include "lldb/API/SBMutex.h"
133135
%include "lldb/API/SBPlatform.h"
134136
%include "lldb/API/SBProcess.h"
135137
%include "lldb/API/SBProcessInfo.h"

lldb/include/lldb/API/LLDB.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "lldb/API/SBMemoryRegionInfoList.h"
5151
#include "lldb/API/SBModule.h"
5252
#include "lldb/API/SBModuleSpec.h"
53+
#include "lldb/API/SBMutex.h"
5354
#include "lldb/API/SBPlatform.h"
5455
#include "lldb/API/SBProcess.h"
5556
#include "lldb/API/SBProcessInfo.h"

lldb/include/lldb/API/SBDefines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class LLDB_API SBMemoryRegionInfoList;
8989
class LLDB_API SBModule;
9090
class LLDB_API SBModuleSpec;
9191
class LLDB_API SBModuleSpecList;
92+
class LLDB_API SBMutex;
9293
class LLDB_API SBPlatform;
9394
class LLDB_API SBPlatformConnectOptions;
9495
class LLDB_API SBPlatformShellCommand;

lldb/include/lldb/API/SBMutex.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===-- SBMutex.h
2+
//----------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLDB_API_SBLOCK_H
11+
#define LLDB_API_SBLOCK_H
12+
13+
#include "lldb/API/SBDefines.h"
14+
#include "lldb/lldb-forward.h"
15+
#include <mutex>
16+
17+
namespace lldb {
18+
19+
/// A general-purpose lock in the SB API. The lock can be locked and unlocked.
20+
/// The default constructed lock is unlocked, but generally the lock is locked
21+
/// when it is returned from a class.
22+
class LLDB_API SBMutex {
23+
public:
24+
SBMutex();
25+
SBMutex(const SBMutex &rhs);
26+
const SBMutex &operator=(const SBMutex &rhs);
27+
~SBMutex();
28+
29+
/// Returns true if this lock has ownership of the underlying mutex.
30+
bool IsValid() const;
31+
32+
/// Blocking operation that takes ownership of this lock.
33+
void lock() const;
34+
35+
/// Releases ownership of this lock.
36+
void unlock() const;
37+
38+
private:
39+
// Private constructor used by SBTarget to create the Target API mutex.
40+
// Requires a friend declaration.
41+
SBMutex(lldb::TargetSP target_sp);
42+
friend class SBTarget;
43+
44+
std::shared_ptr<std::recursive_mutex> m_opaque_sp;
45+
};
46+
#endif
47+
48+
} // namespace lldb

lldb/include/lldb/API/SBTarget.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ class LLDB_API SBTarget {
342342
uint32_t GetAddressByteSize();
343343

344344
const char *GetTriple();
345-
345+
346346
const char *GetABIName();
347347

348348
const char *GetLabel() const;
@@ -946,6 +946,8 @@ class LLDB_API SBTarget {
946946
/// An error if a Trace already exists or the trace couldn't be created.
947947
lldb::SBTrace CreateTrace(SBError &error);
948948

949+
lldb::SBMutex GetAPIMutex() const;
950+
949951
protected:
950952
friend class SBAddress;
951953
friend class SBAddressRange;

lldb/include/lldb/Target/Target.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,22 @@ class Target : public std::enable_shared_from_this<Target>,
16921692
}
16931693
};
16941694

1695+
/// The private implementation backing SBLock.
1696+
class APILock {
1697+
public:
1698+
APILock(std::shared_ptr<std::recursive_mutex> mutex_sp)
1699+
: m_mutex(std::move(mutex_sp)), m_lock(*m_mutex) {}
1700+
1701+
void Lock() { m_lock.lock(); }
1702+
void Unlock() { m_lock.unlock(); }
1703+
1704+
operator bool() const { return static_cast<bool>(m_lock); }
1705+
1706+
private:
1707+
std::shared_ptr<std::recursive_mutex> m_mutex;
1708+
std::unique_lock<std::recursive_mutex> m_lock;
1709+
};
1710+
16951711
} // namespace lldb_private
16961712

16971713
#endif // LLDB_TARGET_TARGET_H

lldb/source/API/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ add_lldb_library(liblldb SHARED ${option_framework}
8181
SBMemoryRegionInfoList.cpp
8282
SBModule.cpp
8383
SBModuleSpec.cpp
84+
SBMutex.cpp
8485
SBPlatform.cpp
8586
SBProcess.cpp
8687
SBProcessInfo.cpp

lldb/source/API/SBMutex.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===-- SBMutex.cpp
2+
//--------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "lldb/API/SBMutex.h"
11+
#include "lldb/Target/Target.h"
12+
#include "lldb/Utility/Instrumentation.h"
13+
#include "lldb/lldb-forward.h"
14+
#include <memory>
15+
#include <mutex>
16+
17+
using namespace lldb;
18+
using namespace lldb_private;
19+
20+
SBMutex::SBMutex() { LLDB_INSTRUMENT_VA(this); }
21+
22+
SBMutex::SBMutex(const SBMutex &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
23+
LLDB_INSTRUMENT_VA(this);
24+
}
25+
26+
const SBMutex &SBMutex::operator=(const SBMutex &rhs) {
27+
LLDB_INSTRUMENT_VA(this);
28+
29+
m_opaque_sp = rhs.m_opaque_sp;
30+
return *this;
31+
}
32+
33+
SBMutex::SBMutex(lldb::TargetSP target_sp)
34+
: m_opaque_sp(std::shared_ptr<std::recursive_mutex>(
35+
target_sp, &target_sp->GetAPIMutex())) {
36+
LLDB_INSTRUMENT_VA(this, target_sp);
37+
}
38+
39+
SBMutex::~SBMutex() { LLDB_INSTRUMENT_VA(this); }
40+
41+
bool SBMutex::IsValid() const {
42+
LLDB_INSTRUMENT_VA(this);
43+
44+
return static_cast<bool>(m_opaque_sp);
45+
}
46+
47+
void SBMutex::lock() const {
48+
LLDB_INSTRUMENT_VA(this);
49+
50+
if (m_opaque_sp)
51+
m_opaque_sp->lock();
52+
}
53+
54+
void SBMutex::unlock() const {
55+
LLDB_INSTRUMENT_VA(this);
56+
57+
if (m_opaque_sp)
58+
m_opaque_sp->unlock();
59+
}

lldb/source/API/SBTarget.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBTarget.h"
10-
#include "lldb/Utility/Instrumentation.h"
11-
#include "lldb/Utility/LLDBLog.h"
12-
#include "lldb/lldb-public.h"
13-
1410
#include "lldb/API/SBBreakpoint.h"
1511
#include "lldb/API/SBDebugger.h"
1612
#include "lldb/API/SBEnvironment.h"
@@ -20,6 +16,7 @@
2016
#include "lldb/API/SBListener.h"
2117
#include "lldb/API/SBModule.h"
2218
#include "lldb/API/SBModuleSpec.h"
19+
#include "lldb/API/SBMutex.h"
2320
#include "lldb/API/SBProcess.h"
2421
#include "lldb/API/SBSourceManager.h"
2522
#include "lldb/API/SBStream.h"
@@ -58,11 +55,14 @@
5855
#include "lldb/Utility/ArchSpec.h"
5956
#include "lldb/Utility/Args.h"
6057
#include "lldb/Utility/FileSpec.h"
58+
#include "lldb/Utility/Instrumentation.h"
59+
#include "lldb/Utility/LLDBLog.h"
6160
#include "lldb/Utility/ProcessInfo.h"
6261
#include "lldb/Utility/RegularExpression.h"
6362
#include "lldb/ValueObject/ValueObjectConstResult.h"
6463
#include "lldb/ValueObject/ValueObjectList.h"
6564
#include "lldb/ValueObject/ValueObjectVariable.h"
65+
#include "lldb/lldb-public.h"
6666

6767
#include "Commands/CommandObjectBreakpoint.h"
6868
#include "lldb/Interpreter/CommandReturnObject.h"
@@ -2439,3 +2439,11 @@ lldb::SBTrace SBTarget::CreateTrace(lldb::SBError &error) {
24392439
}
24402440
return SBTrace();
24412441
}
2442+
2443+
lldb::SBMutex SBTarget::GetAPIMutex() const {
2444+
LLDB_INSTRUMENT_VA(this);
2445+
2446+
if (TargetSP target_sp = GetSP())
2447+
return lldb::SBMutex(target_sp);
2448+
return lldb::SBMutex();
2449+
}

0 commit comments

Comments
 (0)