Skip to content

Commit af92100

Browse files
committed
[lldb] Remove the global platform list
This patch moves the platform creation and selection logic into the per-debugger platform lists. I've tried to keep functional changes to a minimum -- the main (only) observable difference in this change is that APIs, which select a platform by name (e.g., Debugger::SetCurrentPlatform) will not automatically pick up a platform associated with another debugger (or no debugger at all). I've also added several tests for this functionality -- one of the pleasant consequences of the debugger isolation is that it is now possible to test the platform selection and creation logic. This is a product of the discussion at <https://discourse.llvm.org/t/multiple-platforms-with-the-same-name/59594>. Differential Revision: https://reviews.llvm.org/D120810
1 parent ba4537b commit af92100

File tree

20 files changed

+388
-345
lines changed

20 files changed

+388
-345
lines changed

lldb/include/lldb/Target/Platform.h

Lines changed: 27 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -94,79 +94,11 @@ class Platform : public PluginInterface {
9494
/// attaching to processes unless another platform is specified.
9595
static lldb::PlatformSP GetHostPlatform();
9696

97-
/// Get the platform for the given architecture. See Platform::Create for how
98-
/// a platform is chosen for a given architecture.
99-
///
100-
/// \param[in] arch
101-
/// The architecture for which we are getting a platform.
102-
///
103-
/// \param[in] process_host_arch
104-
/// The process host architecture if it's known. An invalid ArchSpec
105-
/// represents that the process host architecture is unknown.
106-
///
107-
/// \param[out] platform_arch_ptr
108-
/// The architecture which was used to pick the returned platform. This
109-
/// can be different from the input architecture if we're looking for a
110-
/// compatible match instead of an exact match.
111-
///
112-
/// \return
113-
/// Return the platform that matches the input architecture or the
114-
/// default (invalid) platform if none could be found.
115-
static lldb::PlatformSP
116-
GetPlatformForArchitecture(const ArchSpec &arch,
117-
const ArchSpec &process_host_arch = {},
118-
ArchSpec *platform_arch_ptr = nullptr);
119-
120-
/// Get the platform for the given list of architectures.
121-
///
122-
/// The algorithm works a follows:
123-
///
124-
/// 1. Returns the selected platform if it matches any of the architectures.
125-
/// 2. Returns the host platform if it matches any of the architectures.
126-
/// 3. Returns the platform that matches all the architectures.
127-
///
128-
/// If none of the above apply, this function returns a default platform. The
129-
/// candidates output argument differentiates between either no platforms
130-
/// supporting the given architecture or multiple platforms supporting the
131-
/// given architecture.
132-
///
133-
/// \param[in] arch
134-
/// The architecture for which we are getting a platform.
135-
///
136-
/// \param[in] process_host_arch
137-
/// The process host architecture if it's known. An invalid ArchSpec
138-
/// represents that the process host architecture is unknown.
139-
///
140-
/// \param[in] selected_platform_sp
141-
/// The currently selected platform.
142-
///
143-
/// \param[out] candidates
144-
/// A list of candidate platforms in case multiple platforms could
145-
/// support the given list of architectures. If no platforms match the
146-
/// given architecture the list will be empty.
147-
///
148-
/// \return
149-
/// Return the platform that matches the input architectures or the
150-
/// default (invalid) platform if no platforms support the given
151-
/// architectures or multiple platforms support the given architecture.
152-
static lldb::PlatformSP
153-
GetPlatformForArchitectures(std::vector<ArchSpec> archs,
154-
const ArchSpec &process_host_arch,
155-
lldb::PlatformSP selected_platform_sp,
156-
std::vector<lldb::PlatformSP> &candidates);
157-
15897
static const char *GetHostPlatformName();
15998

16099
static void SetHostPlatform(const lldb::PlatformSP &platform_sp);
161100

162-
// Find an existing platform plug-in by name
163-
static lldb::PlatformSP Find(ConstString name);
164-
165-
static lldb::PlatformSP Create(ConstString name, Status &error);
166-
167-
static lldb::PlatformSP Create(const ArchSpec &arch,
168-
const ArchSpec &process_host_arch,
169-
ArchSpec *platform_arch_ptr, Status &error);
101+
static lldb::PlatformSP Create(llvm::StringRef name);
170102

171103
/// Augments the triple either with information from platform or the host
172104
/// system (if platform is null).
@@ -917,9 +849,6 @@ class Platform : public PluginInterface {
917849
virtual CompilerType GetSiginfoType(const llvm::Triple &triple);
918850

919851
protected:
920-
/// For unit testing purposes only.
921-
static void Clear();
922-
923852
/// Create a list of ArchSpecs with the given OS and a architectures. The
924853
/// vendor field is left as an "unspecified unknown".
925854
static std::vector<ArchSpec>
@@ -1069,6 +998,32 @@ class PlatformList {
1069998
}
1070999
}
10711000

1001+
lldb::PlatformSP GetOrCreate(llvm::StringRef name);
1002+
lldb::PlatformSP GetOrCreate(const ArchSpec &arch,
1003+
const ArchSpec &process_host_arch,
1004+
ArchSpec *platform_arch_ptr, Status &error);
1005+
lldb::PlatformSP GetOrCreate(const ArchSpec &arch,
1006+
const ArchSpec &process_host_arch,
1007+
ArchSpec *platform_arch_ptr);
1008+
1009+
/// Get the platform for the given list of architectures.
1010+
///
1011+
/// The algorithm works a follows:
1012+
///
1013+
/// 1. Returns the selected platform if it matches any of the architectures.
1014+
/// 2. Returns the host platform if it matches any of the architectures.
1015+
/// 3. Returns the platform that matches all the architectures.
1016+
///
1017+
/// If none of the above apply, this function returns a default platform. The
1018+
/// candidates output argument differentiates between either no platforms
1019+
/// supporting the given architecture or multiple platforms supporting the
1020+
/// given architecture.
1021+
lldb::PlatformSP GetOrCreate(llvm::ArrayRef<ArchSpec> archs,
1022+
const ArchSpec &process_host_arch,
1023+
std::vector<lldb::PlatformSP> &candidates);
1024+
1025+
lldb::PlatformSP Create(llvm::StringRef name);
1026+
10721027
protected:
10731028
typedef std::vector<lldb::PlatformSP> collection;
10741029
mutable std::recursive_mutex m_mutex;

lldb/packages/Python/lldbsuite/test/dotest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@ def run_suite():
902902
(configuration.lldb_platform_name))
903903
lldb.remote_platform = lldb.SBPlatform(
904904
configuration.lldb_platform_name)
905+
lldb.selected_platform = lldb.remote_platform
905906
if not lldb.remote_platform.IsValid():
906907
print(
907908
"error: unable to create the LLDB platform named '%s'." %
@@ -918,7 +919,6 @@ def run_suite():
918919
err = lldb.remote_platform.ConnectRemote(platform_connect_options)
919920
if err.Success():
920921
print("Connected.")
921-
lldb.selected_platform = lldb.remote_platform
922922
else:
923923
print("error: failed to connect to remote platform using URL '%s': %s" % (
924924
configuration.lldb_platform_url, err))

lldb/source/API/SBDebugger.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,21 +1460,11 @@ SBError SBDebugger::SetCurrentPlatform(const char *platform_name_cstr) {
14601460
SBError sb_error;
14611461
if (m_opaque_sp) {
14621462
if (platform_name_cstr && platform_name_cstr[0]) {
1463-
ConstString platform_name(platform_name_cstr);
1464-
PlatformSP platform_sp(Platform::Find(platform_name));
1465-
1466-
if (platform_sp) {
1467-
// Already have a platform with this name, just select it
1468-
m_opaque_sp->GetPlatformList().SetSelectedPlatform(platform_sp);
1469-
} else {
1470-
// We don't have a platform by this name yet, create one
1471-
platform_sp = Platform::Create(platform_name, sb_error.ref());
1472-
if (platform_sp) {
1473-
// We created the platform, now append and select it
1474-
bool make_selected = true;
1475-
m_opaque_sp->GetPlatformList().Append(platform_sp, make_selected);
1476-
}
1477-
}
1463+
PlatformList &platforms = m_opaque_sp->GetPlatformList();
1464+
if (PlatformSP platform_sp = platforms.GetOrCreate(platform_name_cstr))
1465+
platforms.SetSelectedPlatform(platform_sp);
1466+
else
1467+
sb_error.ref().SetErrorString("platform not found");
14781468
} else {
14791469
sb_error.ref().SetErrorString("invalid platform name");
14801470
}

lldb/source/API/SBPlatform.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,7 @@ SBPlatform::SBPlatform() { LLDB_INSTRUMENT_VA(this); }
292292
SBPlatform::SBPlatform(const char *platform_name) {
293293
LLDB_INSTRUMENT_VA(this, platform_name);
294294

295-
Status error;
296-
if (platform_name && platform_name[0])
297-
m_opaque_sp = Platform::Create(ConstString(platform_name), error);
295+
m_opaque_sp = Platform::Create(platform_name);
298296
}
299297

300298
SBPlatform::SBPlatform(const SBPlatform &rhs) {

lldb/source/Interpreter/OptionGroupPlatform.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ using namespace lldb_private;
1818
PlatformSP OptionGroupPlatform::CreatePlatformWithOptions(
1919
CommandInterpreter &interpreter, const ArchSpec &arch, bool make_selected,
2020
Status &error, ArchSpec &platform_arch) const {
21+
PlatformList &platforms = interpreter.GetDebugger().GetPlatformList();
22+
2123
PlatformSP platform_sp;
2224

2325
if (!m_platform_name.empty()) {
24-
platform_sp = Platform::Create(ConstString(m_platform_name.c_str()), error);
26+
platform_sp = platforms.Create(m_platform_name);
27+
if (!platform_sp) {
28+
error.SetErrorStringWithFormatv(
29+
"unable to find a plug-in for the platform named \"{0}\"",
30+
m_platform_name);
31+
}
2532
if (platform_sp) {
2633
if (platform_arch.IsValid() && !platform_sp->IsCompatibleArchitecture(
2734
arch, {}, false, &platform_arch)) {
@@ -33,12 +40,12 @@ PlatformSP OptionGroupPlatform::CreatePlatformWithOptions(
3340
}
3441
}
3542
} else if (arch.IsValid()) {
36-
platform_sp = Platform::Create(arch, {}, &platform_arch, error);
43+
platform_sp = platforms.GetOrCreate(arch, {}, &platform_arch, error);
3744
}
3845

3946
if (platform_sp) {
40-
interpreter.GetDebugger().GetPlatformList().Append(platform_sp,
41-
make_selected);
47+
if (make_selected)
48+
platforms.SetSelectedPlatform(platform_sp);
4249
if (!m_os_version.empty())
4350
platform_sp->SetOSVersion(m_os_version);
4451

lldb/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,9 @@ DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel(Process *process,
511511
m_kext_summary_header(), m_known_kexts(), m_mutex(),
512512
m_break_id(LLDB_INVALID_BREAK_ID) {
513513
Status error;
514-
PlatformSP platform_sp(Platform::Create(
515-
ConstString(PlatformDarwinKernel::GetPluginNameStatic()), error));
514+
PlatformSP platform_sp =
515+
process->GetTarget().GetDebugger().GetPlatformList().Create(
516+
PlatformDarwinKernel::GetPluginNameStatic());
516517
if (platform_sp.get())
517518
process->GetTarget().SetPlatform(platform_sp);
518519
}

lldb/source/Plugins/Platform/POSIX/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ add_lldb_library(lldbPluginPlatformPOSIX
77
lldbHost
88
lldbInterpreter
99
lldbTarget
10+
lldbPluginPlatformGDB
1011
lldbPluginTypeSystemClang
1112
)

lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "PlatformPOSIX.h"
1010

11+
#include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
1112
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
1213
#include "lldb/Core/Debugger.h"
1314
#include "lldb/Core/Module.h"
@@ -307,7 +308,8 @@ Status PlatformPOSIX::ConnectRemote(Args &args) {
307308
} else {
308309
if (!m_remote_platform_sp)
309310
m_remote_platform_sp =
310-
Platform::Create(ConstString("remote-gdb-server"), error);
311+
platform_gdb_server::PlatformRemoteGDBServer::CreateInstance(
312+
/*force=*/true, nullptr);
311313

312314
if (m_remote_platform_sp && error.Success())
313315
error = m_remote_platform_sp->ConnectRemote(args);

lldb/source/Plugins/Platform/Windows/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_lldb_library(lldbPluginPlatformWindows PLUGIN
66
lldbCore
77
lldbHost
88
lldbTarget
9+
lldbPluginPlatformGDB
910

1011
LINK_COMPONENTS
1112
Support

lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <winsock2.h>
1515
#endif
1616

17+
#include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
18+
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
1719
#include "lldb/Breakpoint/BreakpointLocation.h"
1820
#include "lldb/Breakpoint/BreakpointSite.h"
1921
#include "lldb/Core/Debugger.h"
@@ -28,8 +30,6 @@
2830
#include "lldb/Target/Process.h"
2931
#include "lldb/Utility/Status.h"
3032

31-
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
32-
3333
#include "llvm/ADT/ScopeExit.h"
3434
#include "llvm/Support/ConvertUTF.h"
3535

@@ -140,7 +140,8 @@ Status PlatformWindows::ConnectRemote(Args &args) {
140140
} else {
141141
if (!m_remote_platform_sp)
142142
m_remote_platform_sp =
143-
Platform::Create(ConstString("remote-gdb-server"), error);
143+
platform_gdb_server::PlatformRemoteGDBServer::CreateInstance(
144+
/*force=*/true, nullptr);
144145

145146
if (m_remote_platform_sp) {
146147
if (error.Success()) {

lldb/source/Plugins/Platform/gdb-server/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ add_lldb_library(lldbPluginPlatformGDB PLUGIN
77
lldbHost
88
lldbTarget
99
lldbPluginProcessUtility
10+
lldbPluginProcessGDBRemote
1011
)

0 commit comments

Comments
 (0)