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         m_rsync_enabled(false), m_rsync_omit_hostname_from_remote_path(false),
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;
47   bool m_rsync_omit_hostname_from_remote_path;
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(), m_status(0), m_signo(0) {
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;
76   int m_signo;
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(
417         llvm::StringRef::withNullAsEmpty(connect_options.GetURL()));
418     sb_error.ref() = platform_sp->ConnectRemote(args);
419   } else {
420     sb_error.SetErrorString("invalid platform");
421   }
422   return LLDB_RECORD_RESULT(sb_error);
423 }
424 
425 void SBPlatform::DisconnectRemote() {
426   LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatform, DisconnectRemote);
427 
428   PlatformSP platform_sp(GetSP());
429   if (platform_sp)
430     platform_sp->DisconnectRemote();
431 }
432 
433 bool SBPlatform::IsConnected() {
434   LLDB_RECORD_METHOD_NO_ARGS(bool, SBPlatform, IsConnected);
435 
436   PlatformSP platform_sp(GetSP());
437   if (platform_sp)
438     return platform_sp->IsConnected();
439   return false;
440 }
441 
442 const char *SBPlatform::GetTriple() {
443   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetTriple);
444 
445   PlatformSP platform_sp(GetSP());
446   if (platform_sp) {
447     ArchSpec arch(platform_sp->GetSystemArchitecture());
448     if (arch.IsValid()) {
449       // Const-ify the string so we don't need to worry about the lifetime of
450       // the string
451       return ConstString(arch.GetTriple().getTriple().c_str()).GetCString();
452     }
453   }
454   return nullptr;
455 }
456 
457 const char *SBPlatform::GetOSBuild() {
458   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetOSBuild);
459 
460   PlatformSP platform_sp(GetSP());
461   if (platform_sp) {
462     std::string s;
463     if (platform_sp->GetOSBuildString(s)) {
464       if (!s.empty()) {
465         // Const-ify the string so we don't need to worry about the lifetime of
466         // the string
467         return ConstString(s.c_str()).GetCString();
468       }
469     }
470   }
471   return nullptr;
472 }
473 
474 const char *SBPlatform::GetOSDescription() {
475   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetOSDescription);
476 
477   PlatformSP platform_sp(GetSP());
478   if (platform_sp) {
479     std::string s;
480     if (platform_sp->GetOSKernelDescription(s)) {
481       if (!s.empty()) {
482         // Const-ify the string so we don't need to worry about the lifetime of
483         // the string
484         return ConstString(s.c_str()).GetCString();
485       }
486     }
487   }
488   return nullptr;
489 }
490 
491 const char *SBPlatform::GetHostname() {
492   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetHostname);
493 
494   PlatformSP platform_sp(GetSP());
495   if (platform_sp)
496     return platform_sp->GetHostname();
497   return nullptr;
498 }
499 
500 uint32_t SBPlatform::GetOSMajorVersion() {
501   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSMajorVersion);
502 
503   llvm::VersionTuple version;
504   if (PlatformSP platform_sp = GetSP())
505     version = platform_sp->GetOSVersion();
506   return version.empty() ? UINT32_MAX : version.getMajor();
507 }
508 
509 uint32_t SBPlatform::GetOSMinorVersion() {
510   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSMinorVersion);
511 
512   llvm::VersionTuple version;
513   if (PlatformSP platform_sp = GetSP())
514     version = platform_sp->GetOSVersion();
515   return version.getMinor().getValueOr(UINT32_MAX);
516 }
517 
518 uint32_t SBPlatform::GetOSUpdateVersion() {
519   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSUpdateVersion);
520 
521   llvm::VersionTuple version;
522   if (PlatformSP platform_sp = GetSP())
523     version = platform_sp->GetOSVersion();
524   return version.getSubminor().getValueOr(UINT32_MAX);
525 }
526 
527 SBError SBPlatform::Get(SBFileSpec &src, SBFileSpec &dst) {
528   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Get,
529                      (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst);
530 
531   SBError sb_error;
532   PlatformSP platform_sp(GetSP());
533   if (platform_sp) {
534     sb_error.ref() = platform_sp->GetFile(src.ref(), dst.ref());
535   } else {
536     sb_error.SetErrorString("invalid platform");
537   }
538   return LLDB_RECORD_RESULT(sb_error);
539 }
540 
541 SBError SBPlatform::Put(SBFileSpec &src, SBFileSpec &dst) {
542   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Put,
543                      (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst);
544   return LLDB_RECORD_RESULT(
545       ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
546         if (src.Exists()) {
547           uint32_t permissions =
548               FileSystem::Instance().GetPermissions(src.ref());
549           if (permissions == 0) {
550             if (FileSystem::Instance().IsDirectory(src.ref()))
551               permissions = eFilePermissionsDirectoryDefault;
552             else
553               permissions = eFilePermissionsFileDefault;
554           }
555 
556           return platform_sp->PutFile(src.ref(), dst.ref(), permissions);
557         }
558 
559         Status error;
560         error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'",
561                                        src.ref().GetPath().c_str());
562         return error;
563       }));
564 }
565 
566 SBError SBPlatform::Install(SBFileSpec &src, SBFileSpec &dst) {
567   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Install,
568                      (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst);
569   return LLDB_RECORD_RESULT(
570       ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
571         if (src.Exists())
572           return platform_sp->Install(src.ref(), dst.ref());
573 
574         Status error;
575         error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'",
576                                        src.ref().GetPath().c_str());
577         return error;
578       }));
579 }
580 
581 SBError SBPlatform::Run(SBPlatformShellCommand &shell_command) {
582   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Run,
583                      (lldb::SBPlatformShellCommand &), shell_command);
584   return LLDB_RECORD_RESULT(
585       ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
586         const char *command = shell_command.GetCommand();
587         if (!command)
588           return Status("invalid shell command (empty)");
589 
590         const char *working_dir = shell_command.GetWorkingDirectory();
591         if (working_dir == nullptr) {
592           working_dir = platform_sp->GetWorkingDirectory().GetCString();
593           if (working_dir)
594             shell_command.SetWorkingDirectory(working_dir);
595         }
596         return platform_sp->RunShellCommand(
597             shell_command.m_opaque_ptr->m_shell, command, FileSpec(working_dir),
598             &shell_command.m_opaque_ptr->m_status,
599             &shell_command.m_opaque_ptr->m_signo,
600             &shell_command.m_opaque_ptr->m_output,
601             shell_command.m_opaque_ptr->m_timeout);
602       }));
603 }
604 
605 SBError SBPlatform::Launch(SBLaunchInfo &launch_info) {
606   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Launch, (lldb::SBLaunchInfo &),
607                      launch_info);
608   return LLDB_RECORD_RESULT(
609       ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
610         ProcessLaunchInfo info = launch_info.ref();
611         Status error = platform_sp->LaunchProcess(info);
612         launch_info.set_ref(info);
613         return error;
614       }));
615 }
616 
617 SBError SBPlatform::Kill(const lldb::pid_t pid) {
618   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Kill, (const lldb::pid_t), pid);
619   return LLDB_RECORD_RESULT(
620       ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
621         return platform_sp->KillProcess(pid);
622       }));
623 }
624 
625 SBError SBPlatform::ExecuteConnected(
626     const std::function<Status(const lldb::PlatformSP &)> &func) {
627   SBError sb_error;
628   const auto platform_sp(GetSP());
629   if (platform_sp) {
630     if (platform_sp->IsConnected())
631       sb_error.ref() = func(platform_sp);
632     else
633       sb_error.SetErrorString("not connected");
634   } else
635     sb_error.SetErrorString("invalid platform");
636 
637   return sb_error;
638 }
639 
640 SBError SBPlatform::MakeDirectory(const char *path, uint32_t file_permissions) {
641   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, MakeDirectory,
642                      (const char *, uint32_t), path, file_permissions);
643 
644   SBError sb_error;
645   PlatformSP platform_sp(GetSP());
646   if (platform_sp) {
647     sb_error.ref() =
648         platform_sp->MakeDirectory(FileSpec(path), file_permissions);
649   } else {
650     sb_error.SetErrorString("invalid platform");
651   }
652   return LLDB_RECORD_RESULT(sb_error);
653 }
654 
655 uint32_t SBPlatform::GetFilePermissions(const char *path) {
656   LLDB_RECORD_METHOD(uint32_t, SBPlatform, GetFilePermissions, (const char *),
657                      path);
658 
659   PlatformSP platform_sp(GetSP());
660   if (platform_sp) {
661     uint32_t file_permissions = 0;
662     platform_sp->GetFilePermissions(FileSpec(path), file_permissions);
663     return file_permissions;
664   }
665   return 0;
666 }
667 
668 SBError SBPlatform::SetFilePermissions(const char *path,
669                                        uint32_t file_permissions) {
670   LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, SetFilePermissions,
671                      (const char *, uint32_t), path, file_permissions);
672 
673   SBError sb_error;
674   PlatformSP platform_sp(GetSP());
675   if (platform_sp) {
676     sb_error.ref() =
677         platform_sp->SetFilePermissions(FileSpec(path), file_permissions);
678   } else {
679     sb_error.SetErrorString("invalid platform");
680   }
681   return LLDB_RECORD_RESULT(sb_error);
682 }
683 
684 SBUnixSignals SBPlatform::GetUnixSignals() const {
685   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBUnixSignals, SBPlatform,
686                                    GetUnixSignals);
687 
688   if (auto platform_sp = GetSP())
689     return LLDB_RECORD_RESULT(SBUnixSignals{platform_sp});
690 
691   return LLDB_RECORD_RESULT(SBUnixSignals());
692 }
693 
694 SBEnvironment SBPlatform::GetEnvironment() {
695   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBEnvironment, SBPlatform, GetEnvironment);
696   PlatformSP platform_sp(GetSP());
697 
698   if (platform_sp) {
699     return LLDB_RECORD_RESULT(SBEnvironment(platform_sp->GetEnvironment()));
700   }
701 
702   return LLDB_RECORD_RESULT(SBEnvironment());
703 }
704 
705 namespace lldb_private {
706 namespace repro {
707 
708 template <> void RegisterMethods<SBPlatformConnectOptions>(Registry &R) {
709   LLDB_REGISTER_CONSTRUCTOR(SBPlatformConnectOptions, (const char *));
710   LLDB_REGISTER_CONSTRUCTOR(SBPlatformConnectOptions,
711                             (const lldb::SBPlatformConnectOptions &));
712   LLDB_REGISTER_METHOD(
713       SBPlatformConnectOptions &,
714       SBPlatformConnectOptions, operator=,(
715                                     const lldb::SBPlatformConnectOptions &));
716   LLDB_REGISTER_METHOD(const char *, SBPlatformConnectOptions, GetURL, ());
717   LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, SetURL, (const char *));
718   LLDB_REGISTER_METHOD(bool, SBPlatformConnectOptions, GetRsyncEnabled, ());
719   LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, EnableRsync,
720                        (const char *, const char *, bool));
721   LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, DisableRsync, ());
722   LLDB_REGISTER_METHOD(const char *, SBPlatformConnectOptions,
723                        GetLocalCacheDirectory, ());
724   LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, SetLocalCacheDirectory,
725                        (const char *));
726 }
727 
728 template <> void RegisterMethods<SBPlatformShellCommand>(Registry &R) {
729   LLDB_REGISTER_CONSTRUCTOR(SBPlatformShellCommand, (const char *));
730   LLDB_REGISTER_CONSTRUCTOR(SBPlatformShellCommand,
731                             (const lldb::SBPlatformShellCommand &));
732   LLDB_REGISTER_METHOD(
733       SBPlatformShellCommand &,
734       SBPlatformShellCommand, operator=,(const lldb::SBPlatformShellCommand &));
735   LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, Clear, ());
736   LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand, GetShell, ());
737   LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetShell, (const char *));
738   LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand, GetCommand, ());
739   LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetCommand,
740                        (const char *));
741   LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand,
742                        GetWorkingDirectory, ());
743   LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetWorkingDirectory,
744                        (const char *));
745   LLDB_REGISTER_METHOD(uint32_t, SBPlatformShellCommand, GetTimeoutSeconds, ());
746   LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetTimeoutSeconds,
747                        (uint32_t));
748   LLDB_REGISTER_METHOD(int, SBPlatformShellCommand, GetSignal, ());
749   LLDB_REGISTER_METHOD(int, SBPlatformShellCommand, GetStatus, ());
750   LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand, GetOutput, ());
751 }
752 
753 template <> void RegisterMethods<SBPlatform>(Registry &R) {
754   LLDB_REGISTER_CONSTRUCTOR(SBPlatform, ());
755   LLDB_REGISTER_CONSTRUCTOR(SBPlatform, (const char *));
756   LLDB_REGISTER_CONSTRUCTOR(SBPlatform, (const lldb::SBPlatform &));
757   LLDB_REGISTER_CONSTRUCTOR(SBPlatformShellCommand,
758                             (const char *, const char *));
759   LLDB_REGISTER_METHOD(SBPlatform &,
760                        SBPlatform, operator=,(const lldb::SBPlatform &));
761   LLDB_REGISTER_METHOD_CONST(bool, SBPlatform, IsValid, ());
762   LLDB_REGISTER_METHOD_CONST(bool, SBPlatform, operator bool,());
763   LLDB_REGISTER_METHOD(void, SBPlatform, Clear, ());
764   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetName, ());
765   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetWorkingDirectory, ());
766   LLDB_REGISTER_METHOD(bool, SBPlatform, SetWorkingDirectory, (const char *));
767   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, ConnectRemote,
768                        (lldb::SBPlatformConnectOptions &));
769   LLDB_REGISTER_METHOD(void, SBPlatform, DisconnectRemote, ());
770   LLDB_REGISTER_METHOD(bool, SBPlatform, IsConnected, ());
771   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetTriple, ());
772   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetOSBuild, ());
773   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetOSDescription, ());
774   LLDB_REGISTER_METHOD(const char *, SBPlatform, GetHostname, ());
775   LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetOSMajorVersion, ());
776   LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetOSMinorVersion, ());
777   LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetOSUpdateVersion, ());
778   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Get,
779                        (lldb::SBFileSpec &, lldb::SBFileSpec &));
780   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Put,
781                        (lldb::SBFileSpec &, lldb::SBFileSpec &));
782   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Install,
783                        (lldb::SBFileSpec &, lldb::SBFileSpec &));
784   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Run,
785                        (lldb::SBPlatformShellCommand &));
786   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Launch,
787                        (lldb::SBLaunchInfo &));
788   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Kill, (const lldb::pid_t));
789   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, MakeDirectory,
790                        (const char *, uint32_t));
791   LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetFilePermissions,
792                        (const char *));
793   LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, SetFilePermissions,
794                        (const char *, uint32_t));
795   LLDB_REGISTER_METHOD(lldb::SBEnvironment, SBPlatform, GetEnvironment, ());
796   LLDB_REGISTER_METHOD_CONST(lldb::SBUnixSignals, SBPlatform, GetUnixSignals,
797                              ());
798   LLDB_REGISTER_STATIC_METHOD(lldb::SBPlatform, SBPlatform, GetHostPlatform,
799                               ());
800 }
801 
802 } // namespace repro
803 } // namespace lldb_private
804