LLDB  mainline
OptionGroupPlatform.cpp
Go to the documentation of this file.
1 //===-- OptionGroupPlatform.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 #include "lldb/Host/OptionParser.h"
13 #include "lldb/Target/Platform.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 PlatformSP OptionGroupPlatform::CreatePlatformWithOptions(
19  CommandInterpreter &interpreter, const ArchSpec &arch, bool make_selected,
20  Status &error, ArchSpec &platform_arch) const {
21  PlatformList &platforms = interpreter.GetDebugger().GetPlatformList();
22 
23  PlatformSP platform_sp;
24 
25  if (!m_platform_name.empty()) {
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  }
32  if (platform_sp) {
33  if (platform_arch.IsValid() &&
34  !platform_sp->IsCompatibleArchitecture(
35  arch, {}, ArchSpec::CompatibleMatch, &platform_arch)) {
36  error.SetErrorStringWithFormatv("platform '{0}' doesn't support '{1}'",
37  platform_sp->GetPluginName(),
38  arch.GetTriple().getTriple());
39  platform_sp.reset();
40  return platform_sp;
41  }
42  }
43  } else if (arch.IsValid()) {
44  platform_sp = platforms.GetOrCreate(arch, {}, &platform_arch, error);
45  }
46 
47  if (platform_sp) {
48  if (make_selected)
49  platforms.SetSelectedPlatform(platform_sp);
50  if (!m_os_version.empty())
51  platform_sp->SetOSVersion(m_os_version);
52 
53  if (m_sdk_sysroot)
54  platform_sp->SetSDKRootDirectory(m_sdk_sysroot);
55 
56  if (m_sdk_build)
57  platform_sp->SetSDKBuild(m_sdk_build);
58  }
59 
60  return platform_sp;
61 }
62 
63 void OptionGroupPlatform::OptionParsingStarting(
64  ExecutionContext *execution_context) {
65  m_platform_name.clear();
66  m_sdk_sysroot.Clear();
67  m_sdk_build.Clear();
68  m_os_version = llvm::VersionTuple();
69 }
70 
71 static constexpr OptionDefinition g_option_table[] = {
72  {LLDB_OPT_SET_ALL, false, "platform", 'p', OptionParser::eRequiredArgument,
73  nullptr, {}, 0, eArgTypePlatform, "Specify name of the platform to "
74  "use for this target, creating the "
75  "platform if necessary."},
76  {LLDB_OPT_SET_ALL, false, "version", 'v', OptionParser::eRequiredArgument,
77  nullptr, {}, 0, eArgTypeNone,
78  "Specify the initial SDK version to use prior to connecting."},
79  {LLDB_OPT_SET_ALL, false, "build", 'b', OptionParser::eRequiredArgument,
80  nullptr, {}, 0, eArgTypeNone,
81  "Specify the initial SDK build number."},
82  {LLDB_OPT_SET_ALL, false, "sysroot", 'S', OptionParser::eRequiredArgument,
83  nullptr, {}, 0, eArgTypeFilename, "Specify the SDK root directory "
84  "that contains a root of all "
85  "remote system files."}};
86 
87 llvm::ArrayRef<OptionDefinition> OptionGroupPlatform::GetDefinitions() {
88  llvm::ArrayRef<OptionDefinition> result(g_option_table);
89  if (m_include_platform_option)
90  return result;
91  return result.drop_front();
92 }
93 
94 Status
95 OptionGroupPlatform::SetOptionValue(uint32_t option_idx,
96  llvm::StringRef option_arg,
97  ExecutionContext *execution_context) {
98  Status error;
99  if (!m_include_platform_option)
100  ++option_idx;
101 
102  const int short_option = g_option_table[option_idx].short_option;
103 
104  switch (short_option) {
105  case 'p':
106  m_platform_name.assign(std::string(option_arg));
107  break;
108 
109  case 'v':
110  if (m_os_version.tryParse(option_arg))
111  error.SetErrorStringWithFormatv("invalid version string '{0}'",
112  option_arg);
113  break;
114 
115  case 'b':
116  m_sdk_build.SetString(option_arg);
117  break;
118 
119  case 'S':
120  m_sdk_sysroot.SetString(option_arg);
121  break;
122 
123  default:
124  llvm_unreachable("Unimplemented option");
125  }
126  return error;
127 }
128 
129 bool OptionGroupPlatform::PlatformMatches(
130  const lldb::PlatformSP &platform_sp) const {
131  if (platform_sp) {
132  if (!m_platform_name.empty()) {
133  if (platform_sp->GetName() != m_platform_name)
134  return false;
135  }
136 
137  if (m_sdk_build && m_sdk_build != platform_sp->GetSDKBuild())
138  return false;
139 
140  if (m_sdk_sysroot && m_sdk_sysroot != platform_sp->GetSDKRootDirectory())
141  return false;
142 
143  if (!m_os_version.empty() && m_os_version != platform_sp->GetOSVersion())
144  return false;
145  return true;
146  }
147  return false;
148 }
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::PlatformList::Create
lldb::PlatformSP Create(llvm::StringRef name)
Definition: Platform.cpp:2077
lldb::eArgTypeFilename
@ eArgTypeFilename
Definition: lldb-enumerations.h:537
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:455
Platform.h
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb::eArgTypePlatform
@ eArgTypePlatform
Definition: lldb-enumerations.h:596
LLDB_OPT_SET_ALL
#define LLDB_OPT_SET_ALL
Definition: lldb-defines.h:101
OptionGroupPlatform.h
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:214
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::PlatformList
Definition: Platform.h:946
lldb_private::ArchSpec::IsValid
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:353
lldb_private::PlatformList::SetSelectedPlatform
void SetSelectedPlatform(const lldb::PlatformSP &platform_sp)
Definition: Platform.h:988
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb_private::CommandInterpreter::GetDebugger
Debugger & GetDebugger()
Definition: CommandInterpreter.h:437
lldb_private::Debugger::GetPlatformList
PlatformList & GetPlatformList()
Definition: Debugger.h:204
lldb::eArgTypeNone
@ eArgTypeNone
Definition: lldb-enumerations.h:595
OptionParser.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::OptionDefinition
Definition: OptionDefinition.h:20
CommandInterpreter.h
lldb_private::PlatformList::GetOrCreate
lldb::PlatformSP GetOrCreate(llvm::StringRef name)
Definition: Platform.cpp:1955
lldb_private::OptionDefinition::short_option
int short_option
Single character for this option.
Definition: OptionDefinition.h:32
lldb
Definition: SBAddress.h:15
g_option_table
static constexpr OptionDefinition g_option_table[]
Definition: OptionGroupPlatform.cpp:71