xref: /freebsd-src/contrib/llvm-project/lldb/source/API/SBPlatform.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
1 //===-- SBPlatform.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 
9 #include "lldb/API/SBPlatform.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBEnvironment.h"
12 #include "lldb/API/SBError.h"
13 #include "lldb/API/SBFileSpec.h"
14 #include "lldb/API/SBLaunchInfo.h"
15 #include "lldb/API/SBPlatform.h"
16 #include "lldb/API/SBUnixSignals.h"
17 #include "lldb/Host/File.h"
18 #include "lldb/Target/Platform.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Utility/ArchSpec.h"
21 #include "lldb/Utility/Args.h"
22 #include "lldb/Utility/Status.h"
23 
24 #include "llvm/Support/FileSystem.h"
25 
26 #include <functional>
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 // PlatformConnectOptions
32 struct PlatformConnectOptions {
33   PlatformConnectOptions(const char *url = nullptr)
34       : m_url(), m_rsync_options(), m_rsync_remote_path_prefix(),
35 
36         m_local_cache_directory() {
37     if (url && url[0])
38       m_url = url;
39   }
40 
41   ~PlatformConnectOptions() = default;
42 
43   std::string m_url;
44   std::string m_rsync_options;
45   std::string m_rsync_remote_path_prefix;
46   bool m_rsync_enabled = false;
47   bool m_rsync_omit_hostname_from_remote_path = false;
48   ConstString m_local_cache_directory;
49 };
50 
51 // PlatformShellCommand
52 struct PlatformShellCommand {
53   PlatformShellCommand(llvm::StringRef shell_interpreter,
54                        llvm::StringRef shell_command)
55       : m_command(), m_working_dir(), m_status(0), m_signo(0) {
56     if (!shell_interpreter.empty())
57       m_shell = shell_interpreter.str();
58 
59     if (!m_shell.empty() && !shell_command.empty())
60       m_command = shell_command.str();
61   }
62 
63   PlatformShellCommand(llvm::StringRef shell_command = llvm::StringRef())
64       : m_shell(), m_command(), m_working_dir() {
65     if (!shell_command.empty())
66       m_command = shell_command.str();
67   }
68 
69   ~PlatformShellCommand() = default;
70 
71   std::string m_shell;
72   std::string m_command;
73   std::string m_working_dir;
74   std::string m_output;
75   int m_status = 0;
76   int m_signo = 0;
77   Timeout<std::ratio<1>> m_timeout = llvm::None;
78 };
79 // SBPlatformConnectOptions
80 SBPlatformConnectOptions::SBPlatformConnectOptions(const char *url)
81     : m_opaque_ptr(new PlatformConnectOptions(url)) {
82   LLDB_RECORD_CONSTRUCTOR(SBPlatformConnectOptions, (const char *), url);
83 }
84 
85 SBPlatformConnectOptions::SBPlatformConnectOptions(
86     const SBPlatformConnectOptions &rhs)
87     : m_opaque_ptr(new PlatformConnectOptions()) {
88   LLDB_RECORD_CONSTRUCTOR(SBPlatformConnectOptions,
89                           (const lldb::SBPlatformConnectOptions &), rhs);
90 
91   *m_opaque_ptr = *rhs.m_opaque_ptr;
92 }
93 
94 SBPlatformConnectOptions::~SBPlatformConnectOptions() { delete m_opaque_ptr; }
95 
96 SBPlatformConnectOptions &
97 SBPlatformConnectOptions::operator=(const SBPlatformConnectOptions &rhs) {
98   LLDB_RECORD_METHOD(
99       SBPlatformConnectOptions &,
100       SBPlatformConnectOptions, operator=,(
101                                     const lldb::SBPlatformConnectOptions &),
102       rhs);
103 
104   *m_opaque_ptr = *rhs.m_opaque_ptr;
105   return LLDB_RECORD_RESULT(*this);
106 }
107 
108 const char *SBPlatformConnectOptions::GetURL() {
109   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformConnectOptions, GetURL);
110 
111   if (m_opaque_ptr->m_url.empty())
112     return nullptr;
113   return m_opaque_ptr->m_url.c_str();
114 }
115 
116 void SBPlatformConnectOptions::SetURL(const char *url) {
117   LLDB_RECORD_METHOD(void, SBPlatformConnectOptions, SetURL, (const char *),
118                      url);
119 
120   if (url && url[0])
121     m_opaque_ptr->m_url = url;
122   else
123     m_opaque_ptr->m_url.clear();
124 }
125 
126 bool SBPlatformConnectOptions::GetRsyncEnabled() {
127   LLDB_RECORD_METHOD_NO_ARGS(bool, SBPlatformConnectOptions, GetRsyncEnabled);
128 
129   return m_opaque_ptr->m_rsync_enabled;
130 }
131 
132 void SBPlatformConnectOptions::EnableRsync(
133     const char *options, const char *remote_path_prefix,
134     bool omit_hostname_from_remote_path) {
135   LLDB_RECORD_METHOD(void, SBPlatformConnectOptions, EnableRsync,
136                      (const char *, const char *, bool), options,
137                      remote_path_prefix, omit_hostname_from_remote_path);
138 
139   m_opaque_ptr->m_rsync_enabled = true;
140   m_opaque_ptr->m_rsync_omit_hostname_from_remote_path =
141       omit_hostname_from_remote_path;
142   if (remote_path_prefix && remote_path_prefix[0])
143     m_opaque_ptr->m_rsync_remote_path_prefix = remote_path_prefix;
144   else
145     m_opaque_ptr->m_rsync_remote_path_prefix.clear();
146 
147   if (options && options[0])
148     m_opaque_ptr->m_rsync_options = options;
149   else
150     m_opaque_ptr->m_rsync_options.clear();
151 }
152 
153 void SBPlatformConnectOptions::DisableRsync() {
154   LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatformConnectOptions, DisableRsync);
155 
156   m_opaque_ptr->m_rsync_enabled = false;
157 }
158 
159 const char *SBPlatformConnectOptions::GetLocalCacheDirectory() {
160   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformConnectOptions,
161                              GetLocalCacheDirectory);
162 
163   return m_opaque_ptr->m_local_cache_directory.GetCString();
164 }
165 
166 void SBPlatformConnectOptions::SetLocalCacheDirectory(const char *path) {
167   LLDB_RECORD_METHOD(void, SBPlatformConnectOptions, SetLocalCacheDirectory,
168                      (const char *), path);
169 
170   if (path && path[0])
171     m_opaque_ptr->m_local_cache_directory.SetCString(path);
172   else
173     m_opaque_ptr->m_local_cache_directory = ConstString();
174 }
175 
176 // SBPlatformShellCommand
177 SBPlatformShellCommand::SBPlatformShellCommand(const char *shell_interpreter,
178                                                const char *shell_command)
179     : m_opaque_ptr(new PlatformShellCommand(shell_interpreter, shell_command)) {
180   LLDB_RECORD_CONSTRUCTOR(SBPlatformShellCommand, (const char *, const char *),
181                           shell_interpreter, shell_command);
182 }
183 
184 SBPlatformShellCommand::SBPlatformShellCommand(const char *shell_command)
185     : m_opaque_ptr(new PlatformShellCommand(shell_command)) {
186   LLDB_RECORD_CONSTRUCTOR(SBPlatformShellCommand, (const char *),
187                           shell_command);
188 }
189 
190 SBPlatformShellCommand::SBPlatformShellCommand(
191     const SBPlatformShellCommand &rhs)
192     : m_opaque_ptr(new PlatformShellCommand()) {
193   LLDB_RECORD_CONSTRUCTOR(SBPlatformShellCommand,
194                           (const lldb::SBPlatformShellCommand &), rhs);
195 
196   *m_opaque_ptr = *rhs.m_opaque_ptr;
197 }
198 
199 SBPlatformShellCommand &
200 SBPlatformShellCommand::operator=(const SBPlatformShellCommand &rhs) {
201 
202   LLDB_RECORD_METHOD(
203       SBPlatformShellCommand &,
204       SBPlatformShellCommand, operator=,(const lldb::SBPlatformShellCommand &),
205       rhs);
206 
207   *m_opaque_ptr = *rhs.m_opaque_ptr;
208   return LLDB_RECORD_RESULT(*this);
209 }
210 
211 SBPlatformShellCommand::~SBPlatformShellCommand() { delete m_opaque_ptr; }
212 
213 void SBPlatformShellCommand::Clear() {
214   LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatformShellCommand, Clear);
215 
216   m_opaque_ptr->m_output = std::string();
217   m_opaque_ptr->m_status = 0;
218   m_opaque_ptr->m_signo = 0;
219 }
220 
221 const char *SBPlatformShellCommand::GetShell() {
222   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand, GetShell);
223 
224   if (m_opaque_ptr->m_shell.empty())
225     return nullptr;
226   return m_opaque_ptr->m_shell.c_str();
227 }
228 
229 void SBPlatformShellCommand::SetShell(const char *shell_interpreter) {
230   LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetShell, (const char *),
231                      shell_interpreter);
232 
233   if (shell_interpreter && shell_interpreter[0])
234     m_opaque_ptr->m_shell = shell_interpreter;
235   else
236     m_opaque_ptr->m_shell.clear();
237 }
238 
239 const char *SBPlatformShellCommand::GetCommand() {
240   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand, GetCommand);
241 
242   if (m_opaque_ptr->m_command.empty())
243     return nullptr;
244   return m_opaque_ptr->m_command.c_str();
245 }
246 
247 void SBPlatformShellCommand::SetCommand(const char *shell_command) {
248   LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetCommand, (const char *),
249                      shell_command);
250 
251   if (shell_command && shell_command[0])
252     m_opaque_ptr->m_command = shell_command;
253   else
254     m_opaque_ptr->m_command.clear();
255 }
256 
257 const char *SBPlatformShellCommand::GetWorkingDirectory() {
258   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand,
259                              GetWorkingDirectory);
260 
261   if (m_opaque_ptr->m_working_dir.empty())
262     return nullptr;
263   return m_opaque_ptr->m_working_dir.c_str();
264 }
265 
266 void SBPlatformShellCommand::SetWorkingDirectory(const char *path) {
267   LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetWorkingDirectory,
268                      (const char *), path);
269 
270   if (path && path[0])
271     m_opaque_ptr->m_working_dir = path;
272   else
273     m_opaque_ptr->m_working_dir.clear();
274 }
275 
276 uint32_t SBPlatformShellCommand::GetTimeoutSeconds() {
277   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatformShellCommand,
278                              GetTimeoutSeconds);
279 
280   if (m_opaque_ptr->m_timeout)
281     return m_opaque_ptr->m_timeout->count();
282   return UINT32_MAX;
283 }
284 
285 void SBPlatformShellCommand::SetTimeoutSeconds(uint32_t sec) {
286   LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetTimeoutSeconds,
287                      (uint32_t), sec);
288 
289   if (sec == UINT32_MAX)
290     m_opaque_ptr->m_timeout = llvm::None;
291   else
292     m_opaque_ptr->m_timeout = std::chrono::seconds(sec);
293 }
294 
295 int SBPlatformShellCommand::GetSignal() {
296   LLDB_RECORD_METHOD_NO_ARGS(int, SBPlatformShellCommand, GetSignal);
297 
298   return m_opaque_ptr->m_signo;
299 }
300 
301 int SBPlatformShellCommand::GetStatus() {
302   LLDB_RECORD_METHOD_NO_ARGS(int, SBPlatformShellCommand, GetStatus);
303 
304   return m_opaque_ptr->m_status;
305 }
306 
307 const char *SBPlatformShellCommand::GetOutput() {
308   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand, GetOutput);
309 
310   if (m_opaque_ptr->m_output.empty())
311     return nullptr;
312   return m_opaque_ptr->m_output.c_str();
313 }
314 
315 // SBPlatform
316 SBPlatform::SBPlatform() : m_opaque_sp() {
317   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBPlatform);
318 }
319 
320 SBPlatform::SBPlatform(const char *platform_name) : m_opaque_sp() {
321   LLDB_RECORD_CONSTRUCTOR(SBPlatform, (const char *), platform_name);
322 
323   Status error;
324   if (platform_name && platform_name[0])
325     m_opaque_sp = Platform::Create(ConstString(platform_name), error);
326 }
327 
328 SBPlatform::SBPlatform(const SBPlatform &rhs) {
329   LLDB_RECORD_CONSTRUCTOR(SBPlatform, (const lldb::SBPlatform &), rhs);
330 
331   m_opaque_sp = rhs.m_opaque_sp;
332 }
333 
334 SBPlatform &SBPlatform::operator=(const SBPlatform &rhs) {
335   LLDB_RECORD_METHOD(SBPlatform &,
336                      SBPlatform, operator=,(const lldb::SBPlatform &), rhs);
337 
338   m_opaque_sp = rhs.m_opaque_sp;
339   return LLDB_RECORD_RESULT(*this);
340 }
341 
342 SBPlatform::~SBPlatform() = default;
343 
344 SBPlatform SBPlatform::GetHostPlatform() {
345   LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBPlatform, SBPlatform,
346                                     GetHostPlatform);
347 
348   SBPlatform host_platform;
349   host_platform.m_opaque_sp = Platform::GetHostPlatform();
350   return LLDB_RECORD_RESULT(host_platform);
351 }
352 
353 bool SBPlatform::IsValid() const {
354   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBPlatform, IsValid);
355   return this->operator bool();
356 }
357 SBPlatform::operator bool() const {
358   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBPlatform, operator bool);
359 
360   return m_opaque_sp.get() != nullptr;
361 }
362 
363 void SBPlatform::Clear() {
364   LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatform, Clear);
365 
366   m_opaque_sp.reset();
367 }
368 
369 const char *SBPlatform::GetName() {
370   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetName);
371 
372   PlatformSP platform_sp(GetSP());
373   if (platform_sp)
374     return platform_sp->GetName().GetCString();
375   return nullptr;
376 }
377 
378 lldb::PlatformSP SBPlatform::GetSP() const { return m_opaque_sp; }
379 
380 void SBPlatform::SetSP(const lldb::PlatformSP &platform_sp) {
381   m_opaque_sp = platform_sp;
382 }
383 
384 const char *SBPlatform::GetWorkingDirectory() {
385   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetWorkingDirectory);
386 
387   PlatformSP platform_sp(GetSP());
388   if (platform_sp)
389     return platform_sp->GetWorkingDirectory().GetCString();
390   return nullptr;
391 }
392 
393 bool SBPlatform::SetWorkingDirectory(const char *path) {
394   LLDB_RECORD_METHOD(bool, SBPlatform, SetWorkingDirectory, (const char *),
395                      path);
396 
397   PlatformSP platform_sp(GetSP());
398   if (platform_sp) {
399     if (path)
400       platform_sp->SetWorkingDirectory(FileSpec(path));
401     else
402       platform_sp->SetWorkingDirectory(FileSpec());
403     return true;
404   }
405   return false;
406 }
407 
408 SBError SBPlatform::ConnectRemote(SBPlatformConnectOptions &connect_options) {
409   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, ConnectRemote,
410                      (lldb::SBPlatformConnectOptions &), connect_options);
411 
412   SBError sb_error;
413   PlatformSP platform_sp(GetSP());
414   if (platform_sp && connect_options.GetURL()) {
415     Args args;
416     args.AppendArgument(connect_options.GetURL());
417     sb_error.ref() = platform_sp->ConnectRemote(args);
418   } else {
419     sb_error.SetErrorString("invalid platform");
420   }
421   return LLDB_RECORD_RESULT(sb_error);
422 }
423 
424 void SBPlatform::DisconnectRemote() {
425   LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatform, DisconnectRemote);
426 
427   PlatformSP platform_sp(GetSP());
428   if (platform_sp)
429     platform_sp->DisconnectRemote();
430 }
431 
432 bool SBPlatform::IsConnected() {
433   LLDB_RECORD_METHOD_NO_ARGS(bool, SBPlatform, IsConnected);
434 
435   PlatformSP platform_sp(GetSP());
436   if (platform_sp)
437     return platform_sp->IsConnected();
438   return false;
439 }
440 
441 const char *SBPlatform::GetTriple() {
442   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetTriple);
443 
444   PlatformSP platform_sp(GetSP());
445   if (platform_sp) {
446     ArchSpec arch(platform_sp->GetSystemArchitecture());
447     if (arch.IsValid()) {
448       // Const-ify the string so we don't need to worry about the lifetime of
449       // the string
450       return ConstString(arch.GetTriple().getTriple().c_str()).GetCString();
451     }
452   }
453   return nullptr;
454 }
455 
456 const char *SBPlatform::GetOSBuild() {
457   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetOSBuild);
458 
459   PlatformSP platform_sp(GetSP());
460   if (platform_sp) {
461     std::string s = platform_sp->GetOSBuildString().getValueOr("");
462     if (!s.empty()) {
463       // Const-ify the string so we don't need to worry about the lifetime of
464       // the string
465       return ConstString(s).GetCString();
466     }
467   }
468   return nullptr;
469 }
470 
471 const char *SBPlatform::GetOSDescription() {
472   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetOSDescription);
473 
474   PlatformSP platform_sp(GetSP());
475   if (platform_sp) {
476     std::string s = platform_sp->GetOSKernelDescription().getValueOr("");
477     if (!s.empty()) {
478       // Const-ify the string so we don't need to worry about the lifetime of
479       // the string
480       return ConstString(s.c_str()).GetCString();
481     }
482   }
483   return nullptr;
484 }
485 
486 const char *SBPlatform::GetHostname() {
487   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetHostname);
488 
489   PlatformSP platform_sp(GetSP());
490   if (platform_sp)
491     return platform_sp->GetHostname();
492   return nullptr;
493 }
494 
495 uint32_t SBPlatform::GetOSMajorVersion() {
496   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSMajorVersion);
497 
498   llvm::VersionTuple version;
499   if (PlatformSP platform_sp = GetSP())
500     version = platform_sp->GetOSVersion();
501   return version.empty() ? UINT32_MAX : version.getMajor();
502 }
503 
504 uint32_t SBPlatform::GetOSMinorVersion() {
505   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSMinorVersion);
506 
507   llvm::VersionTuple version;
508   if (PlatformSP platform_sp = GetSP())
509     version = platform_sp->GetOSVersion();
510   return version.getMinor().getValueOr(UINT32_MAX);
511 }
512 
513 uint32_t SBPlatform::GetOSUpdateVersion() {
514   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSUpdateVersion);
515 
516   llvm::VersionTuple version;
517   if (PlatformSP platform_sp = GetSP())
518     version = platform_sp->GetOSVersion();
519   return version.getSubminor().getValueOr(UINT32_MAX);
520 }
521 
522 SBError SBPlatform::Get(SBFileSpec &src, SBFileSpec &dst) {
523   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Get,
524                      (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst);
525 
526   SBError sb_error;
527   PlatformSP platform_sp(GetSP());
528   if (platform_sp) {
529     sb_error.ref() = platform_sp->GetFile(src.ref(), dst.ref());
530   } else {
531     sb_error.SetErrorString("invalid platform");
532   }
533   return LLDB_RECORD_RESULT(sb_error);
534 }
535 
536 SBError SBPlatform::Put(SBFileSpec &src, SBFileSpec &dst) {
537   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Put,
538                      (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst);
539   return LLDB_RECORD_RESULT(
540       ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
541         if (src.Exists()) {
542           uint32_t permissions =
543               FileSystem::Instance().GetPermissions(src.ref());
544           if (permissions == 0) {
545             if (FileSystem::Instance().IsDirectory(src.ref()))
546               permissions = eFilePermissionsDirectoryDefault;
547             else
548               permissions = eFilePermissionsFileDefault;
549           }
550 
551           return platform_sp->PutFile(src.ref(), dst.ref(), permissions);
552         }
553 
554         Status error;
555         error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'",
556                                        src.ref().GetPath().c_str());
557         return error;
558       }));
559 }
560 
561 SBError SBPlatform::Install(SBFileSpec &src, SBFileSpec &dst) {
562   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Install,
563                      (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst);
564   return LLDB_RECORD_RESULT(
565       ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
566         if (src.Exists())
567           return platform_sp->Install(src.ref(), dst.ref());
568 
569         Status error;
570         error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'",
571                                        src.ref().GetPath().c_str());
572         return error;
573       }));
574 }
575 
576 SBError SBPlatform::Run(SBPlatformShellCommand &shell_command) {
577   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Run,
578                      (lldb::SBPlatformShellCommand &), shell_command);
579   return LLDB_RECORD_RESULT(
580       ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
581         const char *command = shell_command.GetCommand();
582         if (!command)
583           return Status("invalid shell command (empty)");
584 
585         const char *working_dir = shell_command.GetWorkingDirectory();
586         if (working_dir == nullptr) {
587           working_dir = platform_sp->GetWorkingDirectory().GetCString();
588           if (working_dir)
589             shell_command.SetWorkingDirectory(working_dir);
590         }
591         return platform_sp->RunShellCommand(
592             shell_command.m_opaque_ptr->m_shell, command, FileSpec(working_dir),
593             &shell_command.m_opaque_ptr->m_status,
594             &shell_command.m_opaque_ptr->m_signo,
595             &shell_command.m_opaque_ptr->m_output,
596             shell_command.m_opaque_ptr->m_timeout);
597       }));
598 }
599 
600 SBError SBPlatform::Launch(SBLaunchInfo &launch_info) {
601   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Launch, (lldb::SBLaunchInfo &),
602                      launch_info);
603   return LLDB_RECORD_RESULT(
604       ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
605         ProcessLaunchInfo info = launch_info.ref();
606         Status error = platform_sp->LaunchProcess(info);
607         launch_info.set_ref(info);
608         return error;
609       }));
610 }
611 
612 SBError SBPlatform::Kill(const lldb::pid_t pid) {
613   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Kill, (const lldb::pid_t), pid);
614   return LLDB_RECORD_RESULT(
615       ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
616         return platform_sp->KillProcess(pid);
617       }));
618 }
619 
620 SBError SBPlatform::ExecuteConnected(
621     const std::function<Status(const lldb::PlatformSP &)> &func) {
622   SBError sb_error;
623   const auto platform_sp(GetSP());
624   if (platform_sp) {
625     if (platform_sp->IsConnected())
626       sb_error.ref() = func(platform_sp);
627     else
628       sb_error.SetErrorString("not connected");
629   } else
630     sb_error.SetErrorString("invalid platform");
631 
632   return sb_error;
633 }
634 
635 SBError SBPlatform::MakeDirectory(const char *path, uint32_t file_permissions) {
636   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, MakeDirectory,
637                      (const char *, uint32_t), path, file_permissions);
638 
639   SBError sb_error;
640   PlatformSP platform_sp(GetSP());
641   if (platform_sp) {
642     sb_error.ref() =
643         platform_sp->MakeDirectory(FileSpec(path), file_permissions);
644   } else {
645     sb_error.SetErrorString("invalid platform");
646   }
647   return LLDB_RECORD_RESULT(sb_error);
648 }
649 
650 uint32_t SBPlatform::GetFilePermissions(const char *path) {
651   LLDB_RECORD_METHOD(uint32_t, SBPlatform, GetFilePermissions, (const char *),
652                      path);
653 
654   PlatformSP platform_sp(GetSP());
655   if (platform_sp) {
656     uint32_t file_permissions = 0;
657     platform_sp->GetFilePermissions(FileSpec(path), file_permissions);
658     return file_permissions;
659   }
660   return 0;
661 }
662 
663 SBError SBPlatform::SetFilePermissions(const char *path,
664                                        uint32_t file_permissions) {
665   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, SetFilePermissions,
666                      (const char *, uint32_t), path, file_permissions);
667 
668   SBError sb_error;
669   PlatformSP platform_sp(GetSP());
670   if (platform_sp) {
671     sb_error.ref() =
672         platform_sp->SetFilePermissions(FileSpec(path), file_permissions);
673   } else {
674     sb_error.SetErrorString("invalid platform");
675   }
676   return LLDB_RECORD_RESULT(sb_error);
677 }
678 
679 SBUnixSignals SBPlatform::GetUnixSignals() const {
680   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBUnixSignals, SBPlatform,
681                                    GetUnixSignals);
682 
683   if (auto platform_sp = GetSP())
684     return LLDB_RECORD_RESULT(SBUnixSignals{platform_sp});
685 
686   return LLDB_RECORD_RESULT(SBUnixSignals());
687 }
688 
689 SBEnvironment SBPlatform::GetEnvironment() {
690   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBEnvironment, SBPlatform, GetEnvironment);
691   PlatformSP platform_sp(GetSP());
692 
693   if (platform_sp) {
694     return LLDB_RECORD_RESULT(SBEnvironment(platform_sp->GetEnvironment()));
695   }
696 
697   return LLDB_RECORD_RESULT(SBEnvironment());
698 }
699 
700 namespace lldb_private {
701 namespace repro {
702 
703 template <> void RegisterMethods<SBPlatformConnectOptions>(Registry &R) {
704   LLDB_REGISTER_CONSTRUCTOR(SBPlatformConnectOptions, (const char *));
705   LLDB_REGISTER_CONSTRUCTOR(SBPlatformConnectOptions,
706                             (const lldb::SBPlatformConnectOptions &));
707   LLDB_REGISTER_METHOD(
708       SBPlatformConnectOptions &,
709       SBPlatformConnectOptions, operator=,(
710                                     const lldb::SBPlatformConnectOptions &));
711   LLDB_REGISTER_METHOD(const char *, SBPlatformConnectOptions, GetURL, ());
712   LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, SetURL, (const char *));
713   LLDB_REGISTER_METHOD(bool, SBPlatformConnectOptions, GetRsyncEnabled, ());
714   LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, EnableRsync,
715                        (const char *, const char *, bool));
716   LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, DisableRsync, ());
717   LLDB_REGISTER_METHOD(const char *, SBPlatformConnectOptions,
718                        GetLocalCacheDirectory, ());
719   LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, SetLocalCacheDirectory,
720                        (const char *));
721 }
722 
723 template <> void RegisterMethods<SBPlatformShellCommand>(Registry &R) {
724   LLDB_REGISTER_CONSTRUCTOR(SBPlatformShellCommand, (const char *));
725   LLDB_REGISTER_CONSTRUCTOR(SBPlatformShellCommand,
726                             (const lldb::SBPlatformShellCommand &));
727   LLDB_REGISTER_METHOD(
728       SBPlatformShellCommand &,
729       SBPlatformShellCommand, operator=,(const lldb::SBPlatformShellCommand &));
730   LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, Clear, ());
731   LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand, GetShell, ());
732   LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetShell, (const char *));
733   LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand, GetCommand, ());
734   LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetCommand,
735                        (const char *));
736   LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand,
737                        GetWorkingDirectory, ());
738   LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetWorkingDirectory,
739                        (const char *));
740   LLDB_REGISTER_METHOD(uint32_t, SBPlatformShellCommand, GetTimeoutSeconds, ());
741   LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetTimeoutSeconds,
742                        (uint32_t));
743   LLDB_REGISTER_METHOD(int, SBPlatformShellCommand, GetSignal, ());
744   LLDB_REGISTER_METHOD(int, SBPlatformShellCommand, GetStatus, ());
745   LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand, GetOutput, ());
746 }
747 
748 template <> void RegisterMethods<SBPlatform>(Registry &R) {
749   LLDB_REGISTER_CONSTRUCTOR(SBPlatform, ());
750   LLDB_REGISTER_CONSTRUCTOR(SBPlatform, (const char *));
751   LLDB_REGISTER_CONSTRUCTOR(SBPlatform, (const lldb::SBPlatform &));
752   LLDB_REGISTER_CONSTRUCTOR(SBPlatformShellCommand,
753                             (const char *, const char *));
754   LLDB_REGISTER_METHOD(SBPlatform &,
755                        SBPlatform, operator=,(const lldb::SBPlatform &));
756   LLDB_REGISTER_METHOD_CONST(bool, SBPlatform, IsValid, ());
757   LLDB_REGISTER_METHOD_CONST(bool, SBPlatform, operator bool,());
758   LLDB_REGISTER_METHOD(void, SBPlatform, Clear, ());
759   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetName, ());
760   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetWorkingDirectory, ());
761   LLDB_REGISTER_METHOD(bool, SBPlatform, SetWorkingDirectory, (const char *));
762   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, ConnectRemote,
763                        (lldb::SBPlatformConnectOptions &));
764   LLDB_REGISTER_METHOD(void, SBPlatform, DisconnectRemote, ());
765   LLDB_REGISTER_METHOD(bool, SBPlatform, IsConnected, ());
766   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetTriple, ());
767   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetOSBuild, ());
768   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetOSDescription, ());
769   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetHostname, ());
770   LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetOSMajorVersion, ());
771   LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetOSMinorVersion, ());
772   LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetOSUpdateVersion, ());
773   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Get,
774                        (lldb::SBFileSpec &, lldb::SBFileSpec &));
775   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Put,
776                        (lldb::SBFileSpec &, lldb::SBFileSpec &));
777   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Install,
778                        (lldb::SBFileSpec &, lldb::SBFileSpec &));
779   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Run,
780                        (lldb::SBPlatformShellCommand &));
781   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Launch,
782                        (lldb::SBLaunchInfo &));
783   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Kill, (const lldb::pid_t));
784   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, MakeDirectory,
785                        (const char *, uint32_t));
786   LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetFilePermissions,
787                        (const char *));
788   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, SetFilePermissions,
789                        (const char *, uint32_t));
790   LLDB_REGISTER_METHOD(lldb::SBEnvironment, SBPlatform, GetEnvironment, ());
791   LLDB_REGISTER_METHOD_CONST(lldb::SBUnixSignals, SBPlatform, GetUnixSignals,
792                              ());
793   LLDB_REGISTER_STATIC_METHOD(lldb::SBPlatform, SBPlatform, GetHostPlatform,
794                               ());
795 }
796 
797 } // namespace repro
798 } // namespace lldb_private
799