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