Path: blob/main/contrib/llvm-project/lldb/source/API/SBFile.cpp
39587 views
//===-- SBFile.cpp --------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/API/SBFile.h"9#include "lldb/API/SBError.h"10#include "lldb/Host/File.h"11#include "lldb/Utility/Instrumentation.h"1213using namespace lldb;14using namespace lldb_private;1516SBFile::~SBFile() = default;1718SBFile::SBFile(FileSP file_sp) : m_opaque_sp(file_sp) {19// We have no way to capture the incoming FileSP as the class isn't20// instrumented, so pretend that it's always null.21LLDB_INSTRUMENT_VA(this, file_sp);22}2324SBFile::SBFile(const SBFile &rhs) : m_opaque_sp(rhs.m_opaque_sp) {25LLDB_INSTRUMENT_VA(this, rhs);26}2728SBFile &SBFile ::operator=(const SBFile &rhs) {29LLDB_INSTRUMENT_VA(this, rhs);3031if (this != &rhs)32m_opaque_sp = rhs.m_opaque_sp;33return *this;34}3536SBFile::SBFile() { LLDB_INSTRUMENT_VA(this); }3738SBFile::SBFile(FILE *file, bool transfer_ownership) {39LLDB_INSTRUMENT_VA(this, file, transfer_ownership);4041m_opaque_sp = std::make_shared<NativeFile>(file, transfer_ownership);42}4344SBFile::SBFile(int fd, const char *mode, bool transfer_owndership) {45LLDB_INSTRUMENT_VA(this, fd, mode, transfer_owndership);4647auto options = File::GetOptionsFromMode(mode);48if (!options) {49llvm::consumeError(options.takeError());50return;51}52m_opaque_sp =53std::make_shared<NativeFile>(fd, options.get(), transfer_owndership);54}5556SBError SBFile::Read(uint8_t *buf, size_t num_bytes, size_t *bytes_read) {57LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_read);5859SBError error;60if (!m_opaque_sp) {61error.SetErrorString("invalid SBFile");62*bytes_read = 0;63} else {64Status status = m_opaque_sp->Read(buf, num_bytes);65error.SetError(status);66*bytes_read = num_bytes;67}68return error;69}7071SBError SBFile::Write(const uint8_t *buf, size_t num_bytes,72size_t *bytes_written) {73LLDB_INSTRUMENT_VA(this, buf, num_bytes, bytes_written);7475SBError error;76if (!m_opaque_sp) {77error.SetErrorString("invalid SBFile");78*bytes_written = 0;79} else {80Status status = m_opaque_sp->Write(buf, num_bytes);81error.SetError(status);82*bytes_written = num_bytes;83}84return error;85}8687SBError SBFile::Flush() {88LLDB_INSTRUMENT_VA(this);8990SBError error;91if (!m_opaque_sp) {92error.SetErrorString("invalid SBFile");93} else {94Status status = m_opaque_sp->Flush();95error.SetError(status);96}97return error;98}99100bool SBFile::IsValid() const {101LLDB_INSTRUMENT_VA(this);102return m_opaque_sp && m_opaque_sp->IsValid();103}104105SBError SBFile::Close() {106LLDB_INSTRUMENT_VA(this);107SBError error;108if (m_opaque_sp) {109Status status = m_opaque_sp->Close();110error.SetError(status);111}112return error;113}114115SBFile::operator bool() const {116LLDB_INSTRUMENT_VA(this);117return IsValid();118}119120bool SBFile::operator!() const {121LLDB_INSTRUMENT_VA(this);122return !IsValid();123}124125FileSP SBFile::GetFile() const {126LLDB_INSTRUMENT_VA(this);127return m_opaque_sp;128}129130131