SBPlatform.cpp 27 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
//===-- SBPlatform.cpp ----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/API/SBPlatform.h"
#include "SBReproducerPrivate.h"
#include "lldb/API/SBEnvironment.h"
#include "lldb/API/SBError.h"
#include "lldb/API/SBFileSpec.h"
#include "lldb/API/SBLaunchInfo.h"
#include "lldb/API/SBPlatform.h"
#include "lldb/API/SBUnixSignals.h"
#include "lldb/Host/File.h"
#include "lldb/Target/Platform.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/Args.h"
#include "lldb/Utility/Status.h"

#include "llvm/Support/FileSystem.h"

#include <functional>

using namespace lldb;
using namespace lldb_private;

// PlatformConnectOptions
struct PlatformConnectOptions {
  PlatformConnectOptions(const char *url = nullptr)
      : m_url(), m_rsync_options(), m_rsync_remote_path_prefix(),
        m_rsync_enabled(false), m_rsync_omit_hostname_from_remote_path(false),
        m_local_cache_directory() {
    if (url && url[0])
      m_url = url;
  }

  ~PlatformConnectOptions() = default;

  std::string m_url;
  std::string m_rsync_options;
  std::string m_rsync_remote_path_prefix;
  bool m_rsync_enabled;
  bool m_rsync_omit_hostname_from_remote_path;
  ConstString m_local_cache_directory;
};

// PlatformShellCommand
struct PlatformShellCommand {
  PlatformShellCommand(llvm::StringRef shell_interpreter,
                       llvm::StringRef shell_command)
      : m_command(), m_working_dir(), m_status(0), m_signo(0) {
    if (!shell_interpreter.empty())
      m_shell = shell_interpreter.str();

    if (!m_shell.empty() && !shell_command.empty())
      m_command = shell_command.str();
  }

  PlatformShellCommand(llvm::StringRef shell_command = llvm::StringRef())
      : m_shell(), m_command(), m_working_dir(), m_status(0), m_signo(0) {
    if (!shell_command.empty())
      m_command = shell_command.str();
  }

  ~PlatformShellCommand() = default;

  std::string m_shell;
  std::string m_command;
  std::string m_working_dir;
  std::string m_output;
  int m_status;
  int m_signo;
  Timeout<std::ratio<1>> m_timeout = llvm::None;
};
// SBPlatformConnectOptions
SBPlatformConnectOptions::SBPlatformConnectOptions(const char *url)
    : m_opaque_ptr(new PlatformConnectOptions(url)) {
  LLDB_RECORD_CONSTRUCTOR(SBPlatformConnectOptions, (const char *), url);
}

SBPlatformConnectOptions::SBPlatformConnectOptions(
    const SBPlatformConnectOptions &rhs)
    : m_opaque_ptr(new PlatformConnectOptions()) {
  LLDB_RECORD_CONSTRUCTOR(SBPlatformConnectOptions,
                          (const lldb::SBPlatformConnectOptions &), rhs);

  *m_opaque_ptr = *rhs.m_opaque_ptr;
}

SBPlatformConnectOptions::~SBPlatformConnectOptions() { delete m_opaque_ptr; }

SBPlatformConnectOptions &
SBPlatformConnectOptions::operator=(const SBPlatformConnectOptions &rhs) {
  LLDB_RECORD_METHOD(
      SBPlatformConnectOptions &,
      SBPlatformConnectOptions, operator=,(
                                    const lldb::SBPlatformConnectOptions &),
      rhs);

  *m_opaque_ptr = *rhs.m_opaque_ptr;
  return LLDB_RECORD_RESULT(*this);
}

const char *SBPlatformConnectOptions::GetURL() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformConnectOptions, GetURL);

  if (m_opaque_ptr->m_url.empty())
    return nullptr;
  return m_opaque_ptr->m_url.c_str();
}

void SBPlatformConnectOptions::SetURL(const char *url) {
  LLDB_RECORD_METHOD(void, SBPlatformConnectOptions, SetURL, (const char *),
                     url);

  if (url && url[0])
    m_opaque_ptr->m_url = url;
  else
    m_opaque_ptr->m_url.clear();
}

bool SBPlatformConnectOptions::GetRsyncEnabled() {
  LLDB_RECORD_METHOD_NO_ARGS(bool, SBPlatformConnectOptions, GetRsyncEnabled);

  return m_opaque_ptr->m_rsync_enabled;
}

void SBPlatformConnectOptions::EnableRsync(
    const char *options, const char *remote_path_prefix,
    bool omit_hostname_from_remote_path) {
  LLDB_RECORD_METHOD(void, SBPlatformConnectOptions, EnableRsync,
                     (const char *, const char *, bool), options,
                     remote_path_prefix, omit_hostname_from_remote_path);

  m_opaque_ptr->m_rsync_enabled = true;
  m_opaque_ptr->m_rsync_omit_hostname_from_remote_path =
      omit_hostname_from_remote_path;
  if (remote_path_prefix && remote_path_prefix[0])
    m_opaque_ptr->m_rsync_remote_path_prefix = remote_path_prefix;
  else
    m_opaque_ptr->m_rsync_remote_path_prefix.clear();

  if (options && options[0])
    m_opaque_ptr->m_rsync_options = options;
  else
    m_opaque_ptr->m_rsync_options.clear();
}

void SBPlatformConnectOptions::DisableRsync() {
  LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatformConnectOptions, DisableRsync);

  m_opaque_ptr->m_rsync_enabled = false;
}

const char *SBPlatformConnectOptions::GetLocalCacheDirectory() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformConnectOptions,
                             GetLocalCacheDirectory);

  return m_opaque_ptr->m_local_cache_directory.GetCString();
}

void SBPlatformConnectOptions::SetLocalCacheDirectory(const char *path) {
  LLDB_RECORD_METHOD(void, SBPlatformConnectOptions, SetLocalCacheDirectory,
                     (const char *), path);

  if (path && path[0])
    m_opaque_ptr->m_local_cache_directory.SetCString(path);
  else
    m_opaque_ptr->m_local_cache_directory = ConstString();
}

// SBPlatformShellCommand
SBPlatformShellCommand::SBPlatformShellCommand(const char *shell_interpreter,
                                               const char *shell_command)
    : m_opaque_ptr(new PlatformShellCommand(shell_interpreter, shell_command)) {
  LLDB_RECORD_CONSTRUCTOR(SBPlatformShellCommand, (const char *, const char *),
                          shell_interpreter, shell_command);
}

SBPlatformShellCommand::SBPlatformShellCommand(const char *shell_command)
    : m_opaque_ptr(new PlatformShellCommand(shell_command)) {
  LLDB_RECORD_CONSTRUCTOR(SBPlatformShellCommand, (const char *),
                          shell_command);
}

SBPlatformShellCommand::SBPlatformShellCommand(
    const SBPlatformShellCommand &rhs)
    : m_opaque_ptr(new PlatformShellCommand()) {
  LLDB_RECORD_CONSTRUCTOR(SBPlatformShellCommand,
                          (const lldb::SBPlatformShellCommand &), rhs);

  *m_opaque_ptr = *rhs.m_opaque_ptr;
}

SBPlatformShellCommand &
SBPlatformShellCommand::operator=(const SBPlatformShellCommand &rhs) {

  LLDB_RECORD_METHOD(
      SBPlatformShellCommand &,
      SBPlatformShellCommand, operator=,(const lldb::SBPlatformShellCommand &),
      rhs);

  *m_opaque_ptr = *rhs.m_opaque_ptr;
  return LLDB_RECORD_RESULT(*this);
}

SBPlatformShellCommand::~SBPlatformShellCommand() { delete m_opaque_ptr; }

void SBPlatformShellCommand::Clear() {
  LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatformShellCommand, Clear);

  m_opaque_ptr->m_output = std::string();
  m_opaque_ptr->m_status = 0;
  m_opaque_ptr->m_signo = 0;
}

const char *SBPlatformShellCommand::GetShell() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand, GetShell);

  if (m_opaque_ptr->m_shell.empty())
    return nullptr;
  return m_opaque_ptr->m_shell.c_str();
}

void SBPlatformShellCommand::SetShell(const char *shell_interpreter) {
  LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetShell, (const char *),
                     shell_interpreter);

  if (shell_interpreter && shell_interpreter[0])
    m_opaque_ptr->m_shell = shell_interpreter;
  else
    m_opaque_ptr->m_shell.clear();
}

const char *SBPlatformShellCommand::GetCommand() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand, GetCommand);

  if (m_opaque_ptr->m_command.empty())
    return nullptr;
  return m_opaque_ptr->m_command.c_str();
}

void SBPlatformShellCommand::SetCommand(const char *shell_command) {
  LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetCommand, (const char *),
                     shell_command);

  if (shell_command && shell_command[0])
    m_opaque_ptr->m_command = shell_command;
  else
    m_opaque_ptr->m_command.clear();
}

const char *SBPlatformShellCommand::GetWorkingDirectory() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand,
                             GetWorkingDirectory);

  if (m_opaque_ptr->m_working_dir.empty())
    return nullptr;
  return m_opaque_ptr->m_working_dir.c_str();
}

void SBPlatformShellCommand::SetWorkingDirectory(const char *path) {
  LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetWorkingDirectory,
                     (const char *), path);

  if (path && path[0])
    m_opaque_ptr->m_working_dir = path;
  else
    m_opaque_ptr->m_working_dir.clear();
}

uint32_t SBPlatformShellCommand::GetTimeoutSeconds() {
  LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatformShellCommand,
                             GetTimeoutSeconds);

  if (m_opaque_ptr->m_timeout)
    return m_opaque_ptr->m_timeout->count();
  return UINT32_MAX;
}

void SBPlatformShellCommand::SetTimeoutSeconds(uint32_t sec) {
  LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetTimeoutSeconds,
                     (uint32_t), sec);

  if (sec == UINT32_MAX)
    m_opaque_ptr->m_timeout = llvm::None;
  else
    m_opaque_ptr->m_timeout = std::chrono::seconds(sec);
}

int SBPlatformShellCommand::GetSignal() {
  LLDB_RECORD_METHOD_NO_ARGS(int, SBPlatformShellCommand, GetSignal);

  return m_opaque_ptr->m_signo;
}

int SBPlatformShellCommand::GetStatus() {
  LLDB_RECORD_METHOD_NO_ARGS(int, SBPlatformShellCommand, GetStatus);

  return m_opaque_ptr->m_status;
}

const char *SBPlatformShellCommand::GetOutput() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand, GetOutput);

  if (m_opaque_ptr->m_output.empty())
    return nullptr;
  return m_opaque_ptr->m_output.c_str();
}

// SBPlatform
SBPlatform::SBPlatform() : m_opaque_sp() {
  LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBPlatform);
}

SBPlatform::SBPlatform(const char *platform_name) : m_opaque_sp() {
  LLDB_RECORD_CONSTRUCTOR(SBPlatform, (const char *), platform_name);

  Status error;
  if (platform_name && platform_name[0])
    m_opaque_sp = Platform::Create(ConstString(platform_name), error);
}

SBPlatform::SBPlatform(const SBPlatform &rhs) {
  LLDB_RECORD_CONSTRUCTOR(SBPlatform, (const lldb::SBPlatform &), rhs);

  m_opaque_sp = rhs.m_opaque_sp;
}

SBPlatform &SBPlatform::operator=(const SBPlatform &rhs) {
  LLDB_RECORD_METHOD(SBPlatform &,
                     SBPlatform, operator=,(const lldb::SBPlatform &), rhs);

  m_opaque_sp = rhs.m_opaque_sp;
  return LLDB_RECORD_RESULT(*this);
}

SBPlatform::~SBPlatform() = default;

SBPlatform SBPlatform::GetHostPlatform() {
  LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBPlatform, SBPlatform,
                                    GetHostPlatform);

  SBPlatform host_platform;
  host_platform.m_opaque_sp = Platform::GetHostPlatform();
  return LLDB_RECORD_RESULT(host_platform);
}

bool SBPlatform::IsValid() const {
  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBPlatform, IsValid);
  return this->operator bool();
}
SBPlatform::operator bool() const {
  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBPlatform, operator bool);

  return m_opaque_sp.get() != nullptr;
}

void SBPlatform::Clear() {
  LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatform, Clear);

  m_opaque_sp.reset();
}

const char *SBPlatform::GetName() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetName);

  PlatformSP platform_sp(GetSP());
  if (platform_sp)
    return platform_sp->GetName().GetCString();
  return nullptr;
}

lldb::PlatformSP SBPlatform::GetSP() const { return m_opaque_sp; }

void SBPlatform::SetSP(const lldb::PlatformSP &platform_sp) {
  m_opaque_sp = platform_sp;
}

const char *SBPlatform::GetWorkingDirectory() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetWorkingDirectory);

  PlatformSP platform_sp(GetSP());
  if (platform_sp)
    return platform_sp->GetWorkingDirectory().GetCString();
  return nullptr;
}

bool SBPlatform::SetWorkingDirectory(const char *path) {
  LLDB_RECORD_METHOD(bool, SBPlatform, SetWorkingDirectory, (const char *),
                     path);

  PlatformSP platform_sp(GetSP());
  if (platform_sp) {
    if (path)
      platform_sp->SetWorkingDirectory(FileSpec(path));
    else
      platform_sp->SetWorkingDirectory(FileSpec());
    return true;
  }
  return false;
}

SBError SBPlatform::ConnectRemote(SBPlatformConnectOptions &connect_options) {
  LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, ConnectRemote,
                     (lldb::SBPlatformConnectOptions &), connect_options);

  SBError sb_error;
  PlatformSP platform_sp(GetSP());
  if (platform_sp && connect_options.GetURL()) {
    Args args;
    args.AppendArgument(
        llvm::StringRef::withNullAsEmpty(connect_options.GetURL()));
    sb_error.ref() = platform_sp->ConnectRemote(args);
  } else {
    sb_error.SetErrorString("invalid platform");
  }
  return LLDB_RECORD_RESULT(sb_error);
}

void SBPlatform::DisconnectRemote() {
  LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatform, DisconnectRemote);

  PlatformSP platform_sp(GetSP());
  if (platform_sp)
    platform_sp->DisconnectRemote();
}

bool SBPlatform::IsConnected() {
  LLDB_RECORD_METHOD_NO_ARGS(bool, SBPlatform, IsConnected);

  PlatformSP platform_sp(GetSP());
  if (platform_sp)
    return platform_sp->IsConnected();
  return false;
}

const char *SBPlatform::GetTriple() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetTriple);

  PlatformSP platform_sp(GetSP());
  if (platform_sp) {
    ArchSpec arch(platform_sp->GetSystemArchitecture());
    if (arch.IsValid()) {
      // Const-ify the string so we don't need to worry about the lifetime of
      // the string
      return ConstString(arch.GetTriple().getTriple().c_str()).GetCString();
    }
  }
  return nullptr;
}

const char *SBPlatform::GetOSBuild() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetOSBuild);

  PlatformSP platform_sp(GetSP());
  if (platform_sp) {
    std::string s;
    if (platform_sp->GetOSBuildString(s)) {
      if (!s.empty()) {
        // Const-ify the string so we don't need to worry about the lifetime of
        // the string
        return ConstString(s.c_str()).GetCString();
      }
    }
  }
  return nullptr;
}

const char *SBPlatform::GetOSDescription() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetOSDescription);

  PlatformSP platform_sp(GetSP());
  if (platform_sp) {
    std::string s;
    if (platform_sp->GetOSKernelDescription(s)) {
      if (!s.empty()) {
        // Const-ify the string so we don't need to worry about the lifetime of
        // the string
        return ConstString(s.c_str()).GetCString();
      }
    }
  }
  return nullptr;
}

const char *SBPlatform::GetHostname() {
  LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetHostname);

  PlatformSP platform_sp(GetSP());
  if (platform_sp)
    return platform_sp->GetHostname();
  return nullptr;
}

uint32_t SBPlatform::GetOSMajorVersion() {
  LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSMajorVersion);

  llvm::VersionTuple version;
  if (PlatformSP platform_sp = GetSP())
    version = platform_sp->GetOSVersion();
  return version.empty() ? UINT32_MAX : version.getMajor();
}

uint32_t SBPlatform::GetOSMinorVersion() {
  LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSMinorVersion);

  llvm::VersionTuple version;
  if (PlatformSP platform_sp = GetSP())
    version = platform_sp->GetOSVersion();
  return version.getMinor().getValueOr(UINT32_MAX);
}

uint32_t SBPlatform::GetOSUpdateVersion() {
  LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSUpdateVersion);

  llvm::VersionTuple version;
  if (PlatformSP platform_sp = GetSP())
    version = platform_sp->GetOSVersion();
  return version.getSubminor().getValueOr(UINT32_MAX);
}

SBError SBPlatform::Get(SBFileSpec &src, SBFileSpec &dst) {
  LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Get,
                     (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst);

  SBError sb_error;
  PlatformSP platform_sp(GetSP());
  if (platform_sp) {
    sb_error.ref() = platform_sp->GetFile(src.ref(), dst.ref());
  } else {
    sb_error.SetErrorString("invalid platform");
  }
  return LLDB_RECORD_RESULT(sb_error);
}

SBError SBPlatform::Put(SBFileSpec &src, SBFileSpec &dst) {
  LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Put,
                     (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst);
  return LLDB_RECORD_RESULT(
      ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
        if (src.Exists()) {
          uint32_t permissions =
              FileSystem::Instance().GetPermissions(src.ref());
          if (permissions == 0) {
            if (FileSystem::Instance().IsDirectory(src.ref()))
              permissions = eFilePermissionsDirectoryDefault;
            else
              permissions = eFilePermissionsFileDefault;
          }

          return platform_sp->PutFile(src.ref(), dst.ref(), permissions);
        }

        Status error;
        error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'",
                                       src.ref().GetPath().c_str());
        return error;
      }));
}

SBError SBPlatform::Install(SBFileSpec &src, SBFileSpec &dst) {
  LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Install,
                     (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst);
  return LLDB_RECORD_RESULT(
      ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
        if (src.Exists())
          return platform_sp->Install(src.ref(), dst.ref());

        Status error;
        error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'",
                                       src.ref().GetPath().c_str());
        return error;
      }));
}

SBError SBPlatform::Run(SBPlatformShellCommand &shell_command) {
  LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Run,
                     (lldb::SBPlatformShellCommand &), shell_command);
  return LLDB_RECORD_RESULT(
      ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
        const char *command = shell_command.GetCommand();
        if (!command)
          return Status("invalid shell command (empty)");

        const char *working_dir = shell_command.GetWorkingDirectory();
        if (working_dir == nullptr) {
          working_dir = platform_sp->GetWorkingDirectory().GetCString();
          if (working_dir)
            shell_command.SetWorkingDirectory(working_dir);
        }
        return platform_sp->RunShellCommand(
            shell_command.m_opaque_ptr->m_shell, command, FileSpec(working_dir),
            &shell_command.m_opaque_ptr->m_status,
            &shell_command.m_opaque_ptr->m_signo,
            &shell_command.m_opaque_ptr->m_output,
            shell_command.m_opaque_ptr->m_timeout);
      }));
}

SBError SBPlatform::Launch(SBLaunchInfo &launch_info) {
  LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Launch, (lldb::SBLaunchInfo &),
                     launch_info);
  return LLDB_RECORD_RESULT(
      ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
        ProcessLaunchInfo info = launch_info.ref();
        Status error = platform_sp->LaunchProcess(info);
        launch_info.set_ref(info);
        return error;
      }));
}

SBError SBPlatform::Kill(const lldb::pid_t pid) {
  LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Kill, (const lldb::pid_t), pid);
  return LLDB_RECORD_RESULT(
      ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
        return platform_sp->KillProcess(pid);
      }));
}

SBError SBPlatform::ExecuteConnected(
    const std::function<Status(const lldb::PlatformSP &)> &func) {
  SBError sb_error;
  const auto platform_sp(GetSP());
  if (platform_sp) {
    if (platform_sp->IsConnected())
      sb_error.ref() = func(platform_sp);
    else
      sb_error.SetErrorString("not connected");
  } else
    sb_error.SetErrorString("invalid platform");

  return sb_error;
}

SBError SBPlatform::MakeDirectory(const char *path, uint32_t file_permissions) {
  LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, MakeDirectory,
                     (const char *, uint32_t), path, file_permissions);

  SBError sb_error;
  PlatformSP platform_sp(GetSP());
  if (platform_sp) {
    sb_error.ref() =
        platform_sp->MakeDirectory(FileSpec(path), file_permissions);
  } else {
    sb_error.SetErrorString("invalid platform");
  }
  return LLDB_RECORD_RESULT(sb_error);
}

uint32_t SBPlatform::GetFilePermissions(const char *path) {
  LLDB_RECORD_METHOD(uint32_t, SBPlatform, GetFilePermissions, (const char *),
                     path);

  PlatformSP platform_sp(GetSP());
  if (platform_sp) {
    uint32_t file_permissions = 0;
    platform_sp->GetFilePermissions(FileSpec(path), file_permissions);
    return file_permissions;
  }
  return 0;
}

SBError SBPlatform::SetFilePermissions(const char *path,
                                       uint32_t file_permissions) {
  LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, SetFilePermissions,
                     (const char *, uint32_t), path, file_permissions);

  SBError sb_error;
  PlatformSP platform_sp(GetSP());
  if (platform_sp) {
    sb_error.ref() =
        platform_sp->SetFilePermissions(FileSpec(path), file_permissions);
  } else {
    sb_error.SetErrorString("invalid platform");
  }
  return LLDB_RECORD_RESULT(sb_error);
}

SBUnixSignals SBPlatform::GetUnixSignals() const {
  LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBUnixSignals, SBPlatform,
                                   GetUnixSignals);

  if (auto platform_sp = GetSP())
    return LLDB_RECORD_RESULT(SBUnixSignals{platform_sp});

  return LLDB_RECORD_RESULT(SBUnixSignals());
}

SBEnvironment SBPlatform::GetEnvironment() {
  LLDB_RECORD_METHOD_NO_ARGS(lldb::SBEnvironment, SBPlatform, GetEnvironment);
  PlatformSP platform_sp(GetSP());

  if (platform_sp) {
    return LLDB_RECORD_RESULT(SBEnvironment(platform_sp->GetEnvironment()));
  }

  return LLDB_RECORD_RESULT(SBEnvironment());
}

namespace lldb_private {
namespace repro {

template <> void RegisterMethods<SBPlatformConnectOptions>(Registry &R) {
  LLDB_REGISTER_CONSTRUCTOR(SBPlatformConnectOptions, (const char *));
  LLDB_REGISTER_CONSTRUCTOR(SBPlatformConnectOptions,
                            (const lldb::SBPlatformConnectOptions &));
  LLDB_REGISTER_METHOD(
      SBPlatformConnectOptions &,
      SBPlatformConnectOptions, operator=,(
                                    const lldb::SBPlatformConnectOptions &));
  LLDB_REGISTER_METHOD(const char *, SBPlatformConnectOptions, GetURL, ());
  LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, SetURL, (const char *));
  LLDB_REGISTER_METHOD(bool, SBPlatformConnectOptions, GetRsyncEnabled, ());
  LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, EnableRsync,
                       (const char *, const char *, bool));
  LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, DisableRsync, ());
  LLDB_REGISTER_METHOD(const char *, SBPlatformConnectOptions,
                       GetLocalCacheDirectory, ());
  LLDB_REGISTER_METHOD(void, SBPlatformConnectOptions, SetLocalCacheDirectory,
                       (const char *));
}

template <> void RegisterMethods<SBPlatformShellCommand>(Registry &R) {
  LLDB_REGISTER_CONSTRUCTOR(SBPlatformShellCommand, (const char *));
  LLDB_REGISTER_CONSTRUCTOR(SBPlatformShellCommand,
                            (const lldb::SBPlatformShellCommand &));
  LLDB_REGISTER_METHOD(
      SBPlatformShellCommand &,
      SBPlatformShellCommand, operator=,(const lldb::SBPlatformShellCommand &));
  LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, Clear, ());
  LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand, GetShell, ());
  LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetShell, (const char *));
  LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand, GetCommand, ());
  LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetCommand,
                       (const char *));
  LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand,
                       GetWorkingDirectory, ());
  LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetWorkingDirectory,
                       (const char *));
  LLDB_REGISTER_METHOD(uint32_t, SBPlatformShellCommand, GetTimeoutSeconds, ());
  LLDB_REGISTER_METHOD(void, SBPlatformShellCommand, SetTimeoutSeconds,
                       (uint32_t));
  LLDB_REGISTER_METHOD(int, SBPlatformShellCommand, GetSignal, ());
  LLDB_REGISTER_METHOD(int, SBPlatformShellCommand, GetStatus, ());
  LLDB_REGISTER_METHOD(const char *, SBPlatformShellCommand, GetOutput, ());
}

template <> void RegisterMethods<SBPlatform>(Registry &R) {
  LLDB_REGISTER_CONSTRUCTOR(SBPlatform, ());
  LLDB_REGISTER_CONSTRUCTOR(SBPlatform, (const char *));
  LLDB_REGISTER_CONSTRUCTOR(SBPlatform, (const lldb::SBPlatform &));
  LLDB_REGISTER_CONSTRUCTOR(SBPlatformShellCommand,
                            (const char *, const char *));
  LLDB_REGISTER_METHOD(SBPlatform &,
                       SBPlatform, operator=,(const lldb::SBPlatform &));
  LLDB_REGISTER_METHOD_CONST(bool, SBPlatform, IsValid, ());
  LLDB_REGISTER_METHOD_CONST(bool, SBPlatform, operator bool,());
  LLDB_REGISTER_METHOD(void, SBPlatform, Clear, ());
  LLDB_REGISTER_METHOD(const char *, SBPlatform, GetName, ());
  LLDB_REGISTER_METHOD(const char *, SBPlatform, GetWorkingDirectory, ());
  LLDB_REGISTER_METHOD(bool, SBPlatform, SetWorkingDirectory, (const char *));
  LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, ConnectRemote,
                       (lldb::SBPlatformConnectOptions &));
  LLDB_REGISTER_METHOD(void, SBPlatform, DisconnectRemote, ());
  LLDB_REGISTER_METHOD(bool, SBPlatform, IsConnected, ());
  LLDB_REGISTER_METHOD(const char *, SBPlatform, GetTriple, ());
  LLDB_REGISTER_METHOD(const char *, SBPlatform, GetOSBuild, ());
  LLDB_REGISTER_METHOD(const char *, SBPlatform, GetOSDescription, ());
  LLDB_REGISTER_METHOD(const char *, SBPlatform, GetHostname, ());
  LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetOSMajorVersion, ());
  LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetOSMinorVersion, ());
  LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetOSUpdateVersion, ());
  LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Get,
                       (lldb::SBFileSpec &, lldb::SBFileSpec &));
  LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Put,
                       (lldb::SBFileSpec &, lldb::SBFileSpec &));
  LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Install,
                       (lldb::SBFileSpec &, lldb::SBFileSpec &));
  LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Run,
                       (lldb::SBPlatformShellCommand &));
  LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Launch,
                       (lldb::SBLaunchInfo &));
  LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, Kill, (const lldb::pid_t));
  LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, MakeDirectory,
                       (const char *, uint32_t));
  LLDB_REGISTER_METHOD(uint32_t, SBPlatform, GetFilePermissions,
                       (const char *));
  LLDB_REGISTER_METHOD(lldb::SBError, SBPlatform, SetFilePermissions,
                       (const char *, uint32_t));
  LLDB_REGISTER_METHOD(lldb::SBEnvironment, SBPlatform, GetEnvironment, ());
  LLDB_REGISTER_METHOD_CONST(lldb::SBUnixSignals, SBPlatform, GetUnixSignals,
                             ());
  LLDB_REGISTER_STATIC_METHOD(lldb::SBPlatform, SBPlatform, GetHostPlatform,
                              ());
}

} // namespace repro
} // namespace lldb_private