180814287SRaphael Isemann //===-- SBStream.cpp ------------------------------------------------------===// 2e2ae97f2SGreg Clayton // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e2ae97f2SGreg Clayton // 7e2ae97f2SGreg Clayton //===----------------------------------------------------------------------===// 8e2ae97f2SGreg Clayton 9e2ae97f2SGreg Clayton #include "lldb/API/SBStream.h" 10e2ae97f2SGreg Clayton 11baf5664fSJonas Devlieghere #include "SBReproducerPrivate.h" 1230cf6095SLawrence D'Anna #include "lldb/API/SBFile.h" 13e2ae97f2SGreg Clayton #include "lldb/Core/StreamFile.h" 1450bc1ed2SJonas Devlieghere #include "lldb/Host/FileSystem.h" 1597206d57SZachary Turner #include "lldb/Utility/Status.h" 16bf9a7730SZachary Turner #include "lldb/Utility/Stream.h" 17bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 18e2ae97f2SGreg Clayton 19e2ae97f2SGreg Clayton using namespace lldb; 20e2ae97f2SGreg Clayton using namespace lldb_private; 21e2ae97f2SGreg Clayton 229494c510SJonas Devlieghere SBStream::SBStream() : m_opaque_up(new StreamString()) { 23baf5664fSJonas Devlieghere LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStream); 24baf5664fSJonas Devlieghere } 25e2ae97f2SGreg Clayton 26b9c1b51eSKate Stone SBStream::SBStream(SBStream &&rhs) 27d5b44036SJonas Devlieghere : m_opaque_up(std::move(rhs.m_opaque_up)), m_is_file(rhs.m_is_file) {} 280817da88SGreg Clayton 29866b7a65SJonas Devlieghere SBStream::~SBStream() = default; 300817da88SGreg Clayton 31baf5664fSJonas Devlieghere bool SBStream::IsValid() const { 32baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, IsValid); 337f5237bcSPavel Labath return this->operator bool(); 347f5237bcSPavel Labath } 357f5237bcSPavel Labath SBStream::operator bool() const { 367f5237bcSPavel Labath LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, operator bool); 37baf5664fSJonas Devlieghere 38248a1305SKonrad Kleine return (m_opaque_up != nullptr); 39baf5664fSJonas Devlieghere } 40e2ae97f2SGreg Clayton 4105097246SAdrian Prantl // If this stream is not redirected to a file, it will maintain a local cache 4205097246SAdrian Prantl // for the stream data which can be accessed using this accessor. 43b9c1b51eSKate Stone const char *SBStream::GetData() { 44baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBStream, GetData); 45baf5664fSJonas Devlieghere 46248a1305SKonrad Kleine if (m_is_file || m_opaque_up == nullptr) 47248a1305SKonrad Kleine return nullptr; 48e2ae97f2SGreg Clayton 49d5b44036SJonas Devlieghere return static_cast<StreamString *>(m_opaque_up.get())->GetData(); 50e2ae97f2SGreg Clayton } 51e2ae97f2SGreg Clayton 5205097246SAdrian Prantl // If this stream is not redirected to a file, it will maintain a local cache 5305097246SAdrian Prantl // for the stream output whose length can be accessed using this accessor. 54b9c1b51eSKate Stone size_t SBStream::GetSize() { 55baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(size_t, SBStream, GetSize); 56baf5664fSJonas Devlieghere 57248a1305SKonrad Kleine if (m_is_file || m_opaque_up == nullptr) 5860fd91ffSBill Wendling return 0; 59e2ae97f2SGreg Clayton 60d5b44036SJonas Devlieghere return static_cast<StreamString *>(m_opaque_up.get())->GetSize(); 61e2ae97f2SGreg Clayton } 62e2ae97f2SGreg Clayton 63bfb27837SJonas Devlieghere void SBStream::Print(const char *str) { 64bfb27837SJonas Devlieghere LLDB_RECORD_METHOD(void, SBStream, Print, (const char *), str); 65bfb27837SJonas Devlieghere 66bfb27837SJonas Devlieghere Printf("%s", str); 67bfb27837SJonas Devlieghere } 68bfb27837SJonas Devlieghere 69b9c1b51eSKate Stone void SBStream::Printf(const char *format, ...) { 704f8189bcSJohnny Chen if (!format) 714f8189bcSJohnny Chen return; 72e2ae97f2SGreg Clayton va_list args; 73e2ae97f2SGreg Clayton va_start(args, format); 74e2ae97f2SGreg Clayton ref().PrintfVarArg(format, args); 75e2ae97f2SGreg Clayton va_end(args); 76e2ae97f2SGreg Clayton } 77e2ae97f2SGreg Clayton 78b9c1b51eSKate Stone void SBStream::RedirectToFile(const char *path, bool append) { 79baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (const char *, bool), path, 80baf5664fSJonas Devlieghere append); 81baf5664fSJonas Devlieghere 82e2dcbd00SZachary Turner if (path == nullptr) 83e2dcbd00SZachary Turner return; 84e2dcbd00SZachary Turner 85e2ae97f2SGreg Clayton std::string local_data; 86d5b44036SJonas Devlieghere if (m_opaque_up) { 87e2ae97f2SGreg Clayton // See if we have any locally backed data. If so, copy it so we can then 88e2ae97f2SGreg Clayton // redirect it to the file so we don't lose the data 89e2ae97f2SGreg Clayton if (!m_is_file) 90adcd0268SBenjamin Kramer local_data = std::string( 91adcd0268SBenjamin Kramer static_cast<StreamString *>(m_opaque_up.get())->GetString()); 92e2ae97f2SGreg Clayton } 93*14735cabSMichał Górny auto open_options = File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate; 9451b1e2d2SGreg Clayton if (append) 9551b1e2d2SGreg Clayton open_options |= File::eOpenOptionAppend; 9606357c93SGreg Clayton else 9706357c93SGreg Clayton open_options |= File::eOpenOptionTruncate; 9851b1e2d2SGreg Clayton 992fce1137SLawrence D'Anna llvm::Expected<FileUP> file = 1002fce1137SLawrence D'Anna FileSystem::Instance().Open(FileSpec(path), open_options); 1012fce1137SLawrence D'Anna if (!file) { 1022fce1137SLawrence D'Anna LLDB_LOG_ERROR(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API), file.takeError(), 1032fce1137SLawrence D'Anna "Cannot open {1}: {0}", path); 1042fce1137SLawrence D'Anna return; 1052fce1137SLawrence D'Anna } 106e2ae97f2SGreg Clayton 1072fce1137SLawrence D'Anna m_opaque_up = std::make_unique<StreamFile>(std::move(file.get())); 108e2ae97f2SGreg Clayton m_is_file = true; 109e2ae97f2SGreg Clayton 110e2ae97f2SGreg Clayton // If we had any data locally in our StreamString, then pass that along to 111e2ae97f2SGreg Clayton // the to new file we are redirecting to. 112e2ae97f2SGreg Clayton if (!local_data.empty()) 113d5b44036SJonas Devlieghere m_opaque_up->Write(&local_data[0], local_data.size()); 114e2ae97f2SGreg Clayton } 115e2ae97f2SGreg Clayton 116b9c1b51eSKate Stone void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) { 117baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool), fh, 118baf5664fSJonas Devlieghere transfer_fh_ownership); 11930cf6095SLawrence D'Anna FileSP file = std::make_unique<NativeFile>(fh, transfer_fh_ownership); 12030cf6095SLawrence D'Anna return RedirectToFile(file); 12130cf6095SLawrence D'Anna } 122baf5664fSJonas Devlieghere 12330cf6095SLawrence D'Anna void SBStream::RedirectToFile(SBFile file) { 12430cf6095SLawrence D'Anna LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (SBFile), file) 12530cf6095SLawrence D'Anna RedirectToFile(file.GetFile()); 12630cf6095SLawrence D'Anna } 12730cf6095SLawrence D'Anna 12830cf6095SLawrence D'Anna void SBStream::RedirectToFile(FileSP file_sp) { 12930cf6095SLawrence D'Anna LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (FileSP), file_sp); 13030cf6095SLawrence D'Anna 13130cf6095SLawrence D'Anna if (!file_sp || !file_sp->IsValid()) 132e2dcbd00SZachary Turner return; 133e2dcbd00SZachary Turner 134e2ae97f2SGreg Clayton std::string local_data; 135d5b44036SJonas Devlieghere if (m_opaque_up) { 136e2ae97f2SGreg Clayton // See if we have any locally backed data. If so, copy it so we can then 137e2ae97f2SGreg Clayton // redirect it to the file so we don't lose the data 138e2ae97f2SGreg Clayton if (!m_is_file) 139adcd0268SBenjamin Kramer local_data = std::string( 140adcd0268SBenjamin Kramer static_cast<StreamString *>(m_opaque_up.get())->GetString()); 141e2ae97f2SGreg Clayton } 142e2ae97f2SGreg Clayton 14330cf6095SLawrence D'Anna m_opaque_up = std::make_unique<StreamFile>(file_sp); 144e2ae97f2SGreg Clayton m_is_file = true; 145e2ae97f2SGreg Clayton 146e2ae97f2SGreg Clayton // If we had any data locally in our StreamString, then pass that along to 147e2ae97f2SGreg Clayton // the to new file we are redirecting to. 148e2ae97f2SGreg Clayton if (!local_data.empty()) 149d5b44036SJonas Devlieghere m_opaque_up->Write(&local_data[0], local_data.size()); 150e2ae97f2SGreg Clayton } 151e2ae97f2SGreg Clayton 152b9c1b51eSKate Stone void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) { 153baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool), fd, 154baf5664fSJonas Devlieghere transfer_fh_ownership); 155baf5664fSJonas Devlieghere 156e2ae97f2SGreg Clayton std::string local_data; 157d5b44036SJonas Devlieghere if (m_opaque_up) { 158e2ae97f2SGreg Clayton // See if we have any locally backed data. If so, copy it so we can then 159e2ae97f2SGreg Clayton // redirect it to the file so we don't lose the data 160e2ae97f2SGreg Clayton if (!m_is_file) 161adcd0268SBenjamin Kramer local_data = std::string( 162adcd0268SBenjamin Kramer static_cast<StreamString *>(m_opaque_up.get())->GetString()); 163e2ae97f2SGreg Clayton } 164e2ae97f2SGreg Clayton 1652fce1137SLawrence D'Anna m_opaque_up = std::make_unique<StreamFile>(fd, transfer_fh_ownership); 166e2ae97f2SGreg Clayton m_is_file = true; 167e2ae97f2SGreg Clayton 168e2ae97f2SGreg Clayton // If we had any data locally in our StreamString, then pass that along to 169e2ae97f2SGreg Clayton // the to new file we are redirecting to. 170e2ae97f2SGreg Clayton if (!local_data.empty()) 171d5b44036SJonas Devlieghere m_opaque_up->Write(&local_data[0], local_data.size()); 172e2ae97f2SGreg Clayton } 173e2ae97f2SGreg Clayton 174d5b44036SJonas Devlieghere lldb_private::Stream *SBStream::operator->() { return m_opaque_up.get(); } 175e2ae97f2SGreg Clayton 176d5b44036SJonas Devlieghere lldb_private::Stream *SBStream::get() { return m_opaque_up.get(); } 177e2ae97f2SGreg Clayton 178b9c1b51eSKate Stone lldb_private::Stream &SBStream::ref() { 179248a1305SKonrad Kleine if (m_opaque_up == nullptr) 1801c0bbe43SJonas Devlieghere m_opaque_up = std::make_unique<StreamString>(); 181d5b44036SJonas Devlieghere return *m_opaque_up; 182e2ae97f2SGreg Clayton } 183e2ae97f2SGreg Clayton 184b9c1b51eSKate Stone void SBStream::Clear() { 185baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(void, SBStream, Clear); 186baf5664fSJonas Devlieghere 187d5b44036SJonas Devlieghere if (m_opaque_up) { 188e2ae97f2SGreg Clayton // See if we have any locally backed data. If so, copy it so we can then 189e2ae97f2SGreg Clayton // redirect it to the file so we don't lose the data 190e2ae97f2SGreg Clayton if (m_is_file) 191d5b44036SJonas Devlieghere m_opaque_up.reset(); 192e2ae97f2SGreg Clayton else 193d5b44036SJonas Devlieghere static_cast<StreamString *>(m_opaque_up.get())->Clear(); 194e2ae97f2SGreg Clayton } 195e2ae97f2SGreg Clayton } 196ae211eceSMichal Gorny 197ae211eceSMichal Gorny namespace lldb_private { 198ae211eceSMichal Gorny namespace repro { 199ae211eceSMichal Gorny 200ae211eceSMichal Gorny template <> 201ae211eceSMichal Gorny void RegisterMethods<SBStream>(Registry &R) { 202ae211eceSMichal Gorny LLDB_REGISTER_CONSTRUCTOR(SBStream, ()); 203ae211eceSMichal Gorny LLDB_REGISTER_METHOD_CONST(bool, SBStream, IsValid, ()); 204ae211eceSMichal Gorny LLDB_REGISTER_METHOD_CONST(bool, SBStream, operator bool, ()); 205ae211eceSMichal Gorny LLDB_REGISTER_METHOD(const char *, SBStream, GetData, ()); 206ae211eceSMichal Gorny LLDB_REGISTER_METHOD(size_t, SBStream, GetSize, ()); 207ae211eceSMichal Gorny LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (const char *, bool)); 20830cf6095SLawrence D'Anna LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (FileSP)); 20930cf6095SLawrence D'Anna LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (SBFile)); 210ae211eceSMichal Gorny LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool)); 211ae211eceSMichal Gorny LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool)); 212ae211eceSMichal Gorny LLDB_REGISTER_METHOD(void, SBStream, Clear, ()); 213bfb27837SJonas Devlieghere LLDB_REGISTER_METHOD(void, SBStream, Print, (const char *)); 214ae211eceSMichal Gorny } 215ae211eceSMichal Gorny 216ae211eceSMichal Gorny } 217ae211eceSMichal Gorny } 218