Skip to content

Commit 0438c38

Browse files
authored
Merge pull request #4147 from apple/jdevlieghere/main/rdar/82390061
rdar://90360204
2 parents 387a378 + 60e53b1 commit 0438c38

File tree

17 files changed

+299
-107
lines changed

17 files changed

+299
-107
lines changed

lldb/include/lldb/Core/ModuleSpec.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "lldb/Target/PathMappingList.h"
1414
#include "lldb/Utility/ArchSpec.h"
1515
#include "lldb/Utility/FileSpec.h"
16+
#include "lldb/Utility/Iterable.h"
1617
#include "lldb/Utility/Stream.h"
1718
#include "lldb/Utility/UUID.h"
1819

@@ -293,7 +294,7 @@ class ModuleSpecList {
293294
if (this != &rhs) {
294295
std::lock(m_mutex, rhs.m_mutex);
295296
std::lock_guard<std::recursive_mutex> lhs_guard(m_mutex, std::adopt_lock);
296-
std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_mutex,
297+
std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_mutex,
297298
std::adopt_lock);
298299
m_specs = rhs.m_specs;
299300
}
@@ -393,8 +394,16 @@ class ModuleSpecList {
393394
}
394395
}
395396

397+
typedef std::vector<ModuleSpec> collection;
398+
typedef LockingAdaptedIterable<collection, ModuleSpec, vector_adapter,
399+
std::recursive_mutex>
400+
ModuleSpecIterable;
401+
402+
ModuleSpecIterable ModuleSpecs() {
403+
return ModuleSpecIterable(m_specs, m_mutex);
404+
}
405+
396406
protected:
397-
typedef std::vector<ModuleSpec> collection; ///< The module collection type.
398407
collection m_specs; ///< The collection of modules.
399408
mutable std::recursive_mutex m_mutex;
400409
};

lldb/include/lldb/Target/Platform.h

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ class Platform : public PluginInterface {
9999
const ArchSpec &process_host_arch = {},
100100
ArchSpec *platform_arch_ptr = nullptr);
101101

102+
/// Get the platform for the given list of architectures.
103+
///
104+
/// The algorithm works a follows:
105+
///
106+
/// 1. Returns the selected platform if it matches any of the architectures.
107+
/// 2. Returns the host platform if it matches any of the architectures.
108+
/// 3. Returns the platform that matches all the architectures.
109+
///
110+
/// If none of the above apply, this function returns a default platform. The
111+
/// candidates output argument differentiates between either no platforms
112+
/// supporting the given architecture or multiple platforms supporting the
113+
/// given architecture.
114+
static lldb::PlatformSP
115+
GetPlatformForArchitectures(std::vector<ArchSpec> archs,
116+
const ArchSpec &process_host_arch,
117+
lldb::PlatformSP selected_platform_sp,
118+
std::vector<lldb::PlatformSP> &candidates);
119+
102120
static const char *GetHostPlatformName();
103121

104122
static void SetHostPlatform(const lldb::PlatformSP &platform_sp);
@@ -220,7 +238,7 @@ class Platform : public PluginInterface {
220238
bool GetOSKernelDescription(std::string &s);
221239

222240
// Returns the name of the platform
223-
ConstString GetName();
241+
llvm::StringRef GetName() { return GetPluginName(); }
224242

225243
virtual const char *GetHostname();
226244

@@ -514,17 +532,17 @@ class Platform : public PluginInterface {
514532

515533
virtual uint64_t ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst,
516534
uint64_t dst_len, Status &error) {
517-
error.SetErrorStringWithFormat(
518-
"Platform::ReadFile() is not supported in the %s platform",
519-
GetName().GetCString());
535+
error.SetErrorStringWithFormatv(
536+
"Platform::ReadFile() is not supported in the {0} platform",
537+
GetPluginName());
520538
return -1;
521539
}
522540

523541
virtual uint64_t WriteFile(lldb::user_id_t fd, uint64_t offset,
524542
const void *src, uint64_t src_len, Status &error) {
525-
error.SetErrorStringWithFormat(
526-
"Platform::WriteFile() is not supported in the %s platform",
527-
GetName().GetCString());
543+
error.SetErrorStringWithFormatv(
544+
"Platform::WriteFile() is not supported in the {0} platform",
545+
GetPluginName());
528546
return -1;
529547
}
530548

@@ -852,6 +870,9 @@ class Platform : public PluginInterface {
852870
}
853871

854872
protected:
873+
/// For unit testing purposes only.
874+
static void Clear();
875+
855876
/// Create a list of ArchSpecs with the given OS and a architectures. The
856877
/// vendor field is left as an "unspecified unknown".
857878
static std::vector<ArchSpec>

lldb/source/API/SBPlatform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ const char *SBPlatform::GetName() {
342342

343343
PlatformSP platform_sp(GetSP());
344344
if (platform_sp)
345-
return platform_sp->GetName().GetCString();
345+
return ConstString(platform_sp->GetName()).AsCString();
346346
return nullptr;
347347
}
348348

lldb/source/Commands/CommandObjectPlatform.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,16 +1289,15 @@ class CommandObjectPlatformProcessList : public CommandObjectParsed {
12891289
result.AppendErrorWithFormatv(
12901290
"no processes were found that {0} \"{1}\" on the \"{2}\" "
12911291
"platform\n",
1292-
match_desc, match_name, platform_sp->GetPluginName());
1292+
match_desc, match_name, platform_sp->GetName());
12931293
else
12941294
result.AppendErrorWithFormatv(
12951295
"no processes were found on the \"{0}\" platform\n",
1296-
platform_sp->GetPluginName());
1296+
platform_sp->GetName());
12971297
} else {
1298-
result.AppendMessageWithFormat(
1299-
"%u matching process%s found on \"%s\"", matches,
1300-
matches > 1 ? "es were" : " was",
1301-
platform_sp->GetName().GetCString());
1298+
result.AppendMessageWithFormatv(
1299+
"{0} matching process{1} found on \"{2}\"", matches,
1300+
matches > 1 ? "es were" : " was", platform_sp->GetName());
13021301
if (match_desc)
13031302
result.AppendMessageWithFormat(" whose name %s \"%s\"",
13041303
match_desc, match_name);

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ static void DumpTargetInfo(uint32_t target_idx, Target *target,
8484
}
8585
PlatformSP platform_sp(target->GetPlatform());
8686
if (platform_sp)
87-
strm.Printf("%splatform=%s", properties++ > 0 ? ", " : " ( ",
88-
platform_sp->GetName().GetCString());
87+
strm.Format("{0}platform={1}", properties++ > 0 ? ", " : " ( ",
88+
platform_sp->GetName());
8989

9090
ProcessSP process_sp(target->GetProcessSP());
9191
bool show_process_status = false;

lldb/source/Core/IOHandlerCursesGUI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,7 @@ class ChoicesFieldDelegate : public FieldDelegate {
16111611
// Returns the index of the choice.
16121612
int GetChoice() { return m_choice; }
16131613

1614-
void SetChoice(const std::string &choice) {
1614+
void SetChoice(llvm::StringRef choice) {
16151615
for (int i = 0; i < GetNumberOfChoices(); i++) {
16161616
if (choice == m_choices[i]) {
16171617
m_choice = i;
@@ -1636,7 +1636,7 @@ class PlatformPluginFieldDelegate : public ChoicesFieldDelegate {
16361636
: ChoicesFieldDelegate("Platform Plugin", 3, GetPossiblePluginNames()) {
16371637
PlatformSP platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
16381638
if (platform_sp)
1639-
SetChoice(platform_sp->GetName().AsCString());
1639+
SetChoice(platform_sp->GetPluginName());
16401640
}
16411641

16421642
std::vector<std::string> GetPossiblePluginNames() {

lldb/source/Interpreter/OptionGroupPlatform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ bool OptionGroupPlatform::PlatformMatches(
122122
const lldb::PlatformSP &platform_sp) const {
123123
if (platform_sp) {
124124
if (!m_platform_name.empty()) {
125-
if (platform_sp->GetName() != ConstString(m_platform_name.c_str()))
125+
if (platform_sp->GetName() != m_platform_name)
126126
return false;
127127
}
128128

lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ static Status FindUnusedPort(uint16_t &port) {
7474
return error;
7575
}
7676

77-
PlatformAndroidRemoteGDBServer::PlatformAndroidRemoteGDBServer() = default;
78-
7977
PlatformAndroidRemoteGDBServer::~PlatformAndroidRemoteGDBServer() {
8078
for (const auto &it : m_port_forwards)
8179
DeleteForwardPortWithAdb(it.second, m_device_id);

lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace platform_android {
2424
class PlatformAndroidRemoteGDBServer
2525
: public platform_gdb_server::PlatformRemoteGDBServer {
2626
public:
27-
PlatformAndroidRemoteGDBServer();
27+
using PlatformRemoteGDBServer::PlatformRemoteGDBServer;
2828

2929
~PlatformAndroidRemoteGDBServer() override;
3030

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@
4848
using namespace lldb;
4949
using namespace lldb_private;
5050

51-
/// Default Constructor
52-
PlatformDarwin::PlatformDarwin(bool is_host) : PlatformPOSIX(is_host) {}
53-
5451
/// Destructor.
5552
///
5653
/// The destructor is virtual since this class is designed to be

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
class PlatformDarwin : public PlatformPOSIX {
2626
public:
27-
PlatformDarwin(bool is_host);
27+
using PlatformPOSIX::PlatformPOSIX;
2828

2929
~PlatformDarwin() override;
3030

lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,9 +2434,8 @@ Status ProcessGDBRemote::DoDestroy() {
24342434
m_public_state.GetValue() != eStateRunning) {
24352435
PlatformSP platform_sp = GetTarget().GetPlatform();
24362436

2437-
if (platform_sp && platform_sp->GetName() &&
2438-
platform_sp->GetName().GetStringRef() ==
2439-
PlatformRemoteiOS::GetPluginNameStatic()) {
2437+
if (platform_sp && platform_sp->GetPluginName() ==
2438+
PlatformRemoteiOS::GetPluginNameStatic()) {
24402439
if (m_destroy_tried_resuming) {
24412440
if (log)
24422441
log->PutCString("ProcessGDBRemote::DoDestroy() - Tried resuming to "

lldb/source/Target/Platform.cpp

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "lldb/Utility/Log.h"
3939
#include "lldb/Utility/Status.h"
4040
#include "lldb/Utility/StructuredData.h"
41+
#include "llvm/ADT/STLExtras.h"
4142
#include "llvm/Support/FileSystem.h"
4243
#include "llvm/Support/Path.h"
4344

@@ -156,6 +157,8 @@ void Platform::Terminate() {
156157
}
157158
}
158159

160+
void Platform::Clear() { GetPlatformList().clear(); }
161+
159162
PlatformProperties &Platform::GetGlobalPlatformProperties() {
160163
static PlatformProperties g_settings;
161164
return g_settings;
@@ -280,7 +283,7 @@ PlatformSP Platform::Find(ConstString name) {
280283

281284
std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
282285
for (const auto &platform_sp : GetPlatformList()) {
283-
if (platform_sp->GetName() == name)
286+
if (platform_sp->GetName() == name.GetStringRef())
284287
return platform_sp;
285288
}
286289
}
@@ -786,8 +789,6 @@ Status Platform::SetFilePermissions(const FileSpec &file_spec,
786789
}
787790
}
788791

789-
ConstString Platform::GetName() { return ConstString(GetPluginName()); }
790-
791792
const char *Platform::GetHostname() {
792793
if (IsHost())
793794
return "127.0.0.1";
@@ -1231,6 +1232,61 @@ Platform::GetPlatformForArchitecture(const ArchSpec &arch,
12311232
return platform_sp;
12321233
}
12331234

1235+
lldb::PlatformSP Platform::GetPlatformForArchitectures(
1236+
std::vector<ArchSpec> archs, const ArchSpec &process_host_arch,
1237+
lldb::PlatformSP selected_platform_sp,
1238+
std::vector<lldb::PlatformSP> &candidates) {
1239+
candidates.clear();
1240+
candidates.reserve(archs.size());
1241+
1242+
if (archs.empty())
1243+
return nullptr;
1244+
1245+
PlatformSP host_platform_sp = Platform::GetHostPlatform();
1246+
1247+
// Prefer the selected platform if it matches at least one architecture.
1248+
if (selected_platform_sp) {
1249+
for (const ArchSpec &arch : archs) {
1250+
if (selected_platform_sp->IsCompatibleArchitecture(
1251+
arch, process_host_arch, false, nullptr))
1252+
return selected_platform_sp;
1253+
}
1254+
}
1255+
1256+
// Prefer the host platform if it matches at least one architecture.
1257+
if (host_platform_sp) {
1258+
for (const ArchSpec &arch : archs) {
1259+
if (host_platform_sp->IsCompatibleArchitecture(arch, process_host_arch,
1260+
false, nullptr))
1261+
return host_platform_sp;
1262+
}
1263+
}
1264+
1265+
// Collect a list of candidate platforms for the architectures.
1266+
for (const ArchSpec &arch : archs) {
1267+
if (PlatformSP platform =
1268+
Platform::GetPlatformForArchitecture(arch, process_host_arch))
1269+
candidates.push_back(platform);
1270+
}
1271+
1272+
// The selected or host platform didn't match any of the architectures. If
1273+
// the same platform supports all architectures then that's the obvious next
1274+
// best thing.
1275+
if (candidates.size() == archs.size()) {
1276+
if (std::all_of(candidates.begin(), candidates.end(),
1277+
[&](const PlatformSP &p) -> bool {
1278+
return p->GetName() == candidates.front()->GetName();
1279+
})) {
1280+
return candidates.front();
1281+
}
1282+
}
1283+
1284+
// At this point we either have no platforms that match the given
1285+
// architectures or multiple platforms with no good way to disambiguate
1286+
// between them.
1287+
return nullptr;
1288+
}
1289+
12341290
std::vector<ArchSpec>
12351291
Platform::CreateArchList(llvm::ArrayRef<llvm::Triple::ArchType> archs,
12361292
llvm::Triple::OSType os) {
@@ -1759,7 +1815,7 @@ Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
17591815

17601816
FileSpec Platform::GetModuleCacheRoot() {
17611817
auto dir_spec = GetGlobalPlatformProperties().GetModuleCacheDirectory();
1762-
dir_spec.AppendPathComponent(GetName().AsCString());
1818+
dir_spec.AppendPathComponent(GetPluginName());
17631819
return dir_spec;
17641820
}
17651821

lldb/source/Target/Process.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,11 +3058,10 @@ void Process::CompleteAttach() {
30583058
if (platform_sp) {
30593059
GetTarget().SetPlatform(platform_sp);
30603060
GetTarget().SetArchitecture(platform_arch);
3061-
LLDB_LOGF(log,
3062-
"Process::%s switching platform to %s and architecture "
3063-
"to %s based on info from attach",
3064-
__FUNCTION__, platform_sp->GetName().AsCString(""),
3065-
platform_arch.GetTriple().getTriple().c_str());
3061+
LLDB_LOG(log,
3062+
"switching platform to {0} and architecture to {1} based on "
3063+
"info from attach",
3064+
platform_sp->GetName(), platform_arch.GetTriple().getTriple());
30663065
}
30673066
} else if (!process_arch.IsValid()) {
30683067
ProcessInstanceInfo process_info;

0 commit comments

Comments
 (0)