159 lines · cpp
1//===-- SBSaveCoreOptions.cpp -----------------------------------*- C++ -*-===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/API/SBSaveCoreOptions.h"10#include "lldb/API/SBMemoryRegionInfo.h"11#include "lldb/Host/FileSystem.h"12#include "lldb/Symbol/SaveCoreOptions.h"13#include "lldb/Target/ThreadCollection.h"14#include "lldb/Utility/Instrumentation.h"15 16#include "Utils.h"17 18using namespace lldb;19 20SBSaveCoreOptions::SBSaveCoreOptions() {21 LLDB_INSTRUMENT_VA(this)22 23 m_opaque_up = std::make_unique<lldb_private::SaveCoreOptions>();24}25 26SBSaveCoreOptions::SBSaveCoreOptions(const SBSaveCoreOptions &rhs) {27 LLDB_INSTRUMENT_VA(this, rhs);28 29 m_opaque_up = clone(rhs.m_opaque_up);30}31 32SBSaveCoreOptions::~SBSaveCoreOptions() = default;33 34const SBSaveCoreOptions &35SBSaveCoreOptions::operator=(const SBSaveCoreOptions &rhs) {36 LLDB_INSTRUMENT_VA(this, rhs);37 38 if (this != &rhs)39 m_opaque_up = clone(rhs.m_opaque_up);40 return *this;41}42 43SBError SBSaveCoreOptions::SetPluginName(const char *name) {44 LLDB_INSTRUMENT_VA(this, name);45 return SBError(m_opaque_up->SetPluginName(name));46}47 48void SBSaveCoreOptions::SetStyle(lldb::SaveCoreStyle style) {49 LLDB_INSTRUMENT_VA(this, style);50 m_opaque_up->SetStyle(style);51}52 53void SBSaveCoreOptions::SetOutputFile(lldb::SBFileSpec file_spec) {54 LLDB_INSTRUMENT_VA(this, file_spec);55 m_opaque_up->SetOutputFile(file_spec.ref());56}57 58const char *SBSaveCoreOptions::GetPluginName() const {59 LLDB_INSTRUMENT_VA(this);60 const auto name = m_opaque_up->GetPluginName();61 if (!name)62 return nullptr;63 return lldb_private::ConstString(name.value()).GetCString();64}65 66SBFileSpec SBSaveCoreOptions::GetOutputFile() const {67 LLDB_INSTRUMENT_VA(this);68 const auto file_spec = m_opaque_up->GetOutputFile();69 if (file_spec)70 return SBFileSpec(file_spec.value());71 return SBFileSpec();72}73 74lldb::SaveCoreStyle SBSaveCoreOptions::GetStyle() const {75 LLDB_INSTRUMENT_VA(this);76 return m_opaque_up->GetStyle();77}78 79SBError SBSaveCoreOptions::SetProcess(lldb::SBProcess process) {80 LLDB_INSTRUMENT_VA(this, process);81 return m_opaque_up->SetProcess(process.GetSP());82}83 84SBProcess SBSaveCoreOptions::GetProcess() {85 LLDB_INSTRUMENT_VA(this);86 return SBProcess(m_opaque_up->GetProcess());87}88 89SBError SBSaveCoreOptions::AddThread(lldb::SBThread thread) {90 LLDB_INSTRUMENT_VA(this, thread);91 return m_opaque_up->AddThread(thread.GetSP());92}93 94bool SBSaveCoreOptions::RemoveThread(lldb::SBThread thread) {95 LLDB_INSTRUMENT_VA(this, thread);96 return m_opaque_up->RemoveThread(thread.GetSP());97}98 99lldb::SBError100SBSaveCoreOptions::AddMemoryRegionToSave(const SBMemoryRegionInfo ®ion) {101 LLDB_INSTRUMENT_VA(this, region);102 // Currently add memory region can't fail, so we always return a success103 // SBerror, but because these API's live forever, this is the most future104 // proof thing to do.105 m_opaque_up->AddMemoryRegionToSave(region.ref());106 return SBError();107}108 109lldb::SBThreadCollection SBSaveCoreOptions::GetThreadsToSave() const {110 LLDB_INSTRUMENT_VA(this);111 lldb::ThreadCollectionSP threadcollection_sp =112 std::make_shared<lldb_private::ThreadCollection>(113 m_opaque_up->GetThreadsToSave());114 return SBThreadCollection(threadcollection_sp);115}116 117void SBSaveCoreOptions::Clear() {118 LLDB_INSTRUMENT_VA(this);119 m_opaque_up->Clear();120}121 122uint64_t SBSaveCoreOptions::GetCurrentSizeInBytes(SBError &error) {123 LLDB_INSTRUMENT_VA(this, error);124 llvm::Expected<uint64_t> expected_bytes =125 m_opaque_up->GetCurrentSizeInBytes();126 if (!expected_bytes) {127 error =128 SBError(lldb_private::Status::FromError(expected_bytes.takeError()));129 return 0;130 }131 // Clear the error, so if the clearer uses it we set it to success.132 error.Clear();133 return *expected_bytes;134}135 136lldb::SBMemoryRegionInfoList SBSaveCoreOptions::GetMemoryRegionsToSave() {137 LLDB_INSTRUMENT_VA(this);138 llvm::Expected<lldb_private::CoreFileMemoryRanges> memory_ranges =139 m_opaque_up->GetMemoryRegionsToSave();140 if (!memory_ranges) {141 llvm::consumeError(memory_ranges.takeError());142 return SBMemoryRegionInfoList();143 }144 145 SBMemoryRegionInfoList memory_region_infos;146 for (const auto &range : *memory_ranges) {147 SBMemoryRegionInfo region_info(148 nullptr, range.GetRangeBase(), range.GetRangeEnd(),149 range.data.lldb_permissions, /*mapped=*/true);150 memory_region_infos.Append(region_info);151 }152 153 return memory_region_infos;154}155 156lldb_private::SaveCoreOptions &SBSaveCoreOptions::ref() const {157 return *m_opaque_up;158}159