1 //===-- SBStream.cpp ------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBStream.h"
11 #include "SBReproducerPrivate.h"
12 #include "lldb/API/SBFile.h"
13 #include "lldb/Core/StreamFile.h"
14 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/Utility/Stream.h"
17 #include "lldb/Utility/StreamString.h"
20 using namespace lldb_private;
22 SBStream::SBStream() : m_opaque_up(new StreamString()), m_is_file(false) {
23 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStream);
26 SBStream::SBStream(SBStream &&rhs)
27 : m_opaque_up(std::move(rhs.m_opaque_up)), m_is_file(rhs.m_is_file) {}
29 SBStream::~SBStream() = default;
31 bool SBStream::IsValid() const {
32 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, IsValid);
33 return this->operator bool();
35 SBStream::operator bool() const {
36 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, operator bool);
38 return (m_opaque_up != nullptr);
41 // If this stream is not redirected to a file, it will maintain a local cache
42 // for the stream data which can be accessed using this accessor.
43 const char *SBStream::GetData() {
44 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBStream, GetData);
46 if (m_is_file || m_opaque_up == nullptr)
49 return static_cast<StreamString *>(m_opaque_up.get())->GetData();
52 // If this stream is not redirected to a file, it will maintain a local cache
53 // for the stream output whose length can be accessed using this accessor.
54 size_t SBStream::GetSize() {
55 LLDB_RECORD_METHOD_NO_ARGS(size_t, SBStream, GetSize);
57 if (m_is_file || m_opaque_up == nullptr)
60 return static_cast<StreamString *>(m_opaque_up.get())->GetSize();
63 void SBStream::Print(const char *str) {
64 LLDB_RECORD_METHOD(void, SBStream, Print, (const char *), str);
69 void SBStream::Printf(const char *format, ...) {
73 va_start(args, format);
74 ref().PrintfVarArg(format, args);
78 void SBStream::RedirectToFile(const char *path, bool append) {
79 LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (const char *, bool), path,
85 std::string local_data;
87 // See if we have any locally backed data. If so, copy it so we can then
88 // redirect it to the file so we don't lose the data
90 local_data = std::string(
91 static_cast<StreamString *>(m_opaque_up.get())->GetString());
93 auto open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
95 open_options |= File::eOpenOptionAppend;
97 open_options |= File::eOpenOptionTruncate;
99 llvm::Expected<FileUP> file =
100 FileSystem::Instance().Open(FileSpec(path), open_options);
102 LLDB_LOG_ERROR(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API), file.takeError(),
103 "Cannot open {1}: {0}", path);
107 m_opaque_up = std::make_unique<StreamFile>(std::move(file.get()));
110 // If we had any data locally in our StreamString, then pass that along to
111 // the to new file we are redirecting to.
112 if (!local_data.empty())
113 m_opaque_up->Write(&local_data[0], local_data.size());
116 void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
117 LLDB_RECORD_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool), fh,
118 transfer_fh_ownership);
119 FileSP file = std::make_unique<NativeFile>(fh, transfer_fh_ownership);
120 return RedirectToFile(file);
123 void SBStream::RedirectToFile(SBFile file) {
124 LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (SBFile), file)
125 RedirectToFile(file.GetFile());
128 void SBStream::RedirectToFile(FileSP file_sp) {
129 LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (FileSP), file_sp);
131 if (!file_sp || !file_sp->IsValid())
134 std::string local_data;
136 // See if we have any locally backed data. If so, copy it so we can then
137 // redirect it to the file so we don't lose the data
139 local_data = std::string(
140 static_cast<StreamString *>(m_opaque_up.get())->GetString());
143 m_opaque_up = std::make_unique<StreamFile>(file_sp);
146 // If we had any data locally in our StreamString, then pass that along to
147 // the to new file we are redirecting to.
148 if (!local_data.empty())
149 m_opaque_up->Write(&local_data[0], local_data.size());
152 void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) {
153 LLDB_RECORD_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool), fd,
154 transfer_fh_ownership);
156 std::string local_data;
158 // See if we have any locally backed data. If so, copy it so we can then
159 // redirect it to the file so we don't lose the data
161 local_data = std::string(
162 static_cast<StreamString *>(m_opaque_up.get())->GetString());
165 m_opaque_up = std::make_unique<StreamFile>(fd, transfer_fh_ownership);
168 // If we had any data locally in our StreamString, then pass that along to
169 // the to new file we are redirecting to.
170 if (!local_data.empty())
171 m_opaque_up->Write(&local_data[0], local_data.size());
174 lldb_private::Stream *SBStream::operator->() { return m_opaque_up.get(); }
176 lldb_private::Stream *SBStream::get() { return m_opaque_up.get(); }
178 lldb_private::Stream &SBStream::ref() {
179 if (m_opaque_up == nullptr)
180 m_opaque_up = std::make_unique<StreamString>();
184 void SBStream::Clear() {
185 LLDB_RECORD_METHOD_NO_ARGS(void, SBStream, Clear);
188 // See if we have any locally backed data. If so, copy it so we can then
189 // redirect it to the file so we don't lose the data
193 static_cast<StreamString *>(m_opaque_up.get())->Clear();
197 namespace lldb_private {
201 void RegisterMethods<SBStream>(Registry &R) {
202 LLDB_REGISTER_CONSTRUCTOR(SBStream, ());
203 LLDB_REGISTER_METHOD_CONST(bool, SBStream, IsValid, ());
204 LLDB_REGISTER_METHOD_CONST(bool, SBStream, operator bool, ());
205 LLDB_REGISTER_METHOD(const char *, SBStream, GetData, ());
206 LLDB_REGISTER_METHOD(size_t, SBStream, GetSize, ());
207 LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (const char *, bool));
208 LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (FileSP));
209 LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (SBFile));
210 LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool));
211 LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool));
212 LLDB_REGISTER_METHOD(void, SBStream, Clear, ());
213 LLDB_REGISTER_METHOD(void, SBStream, Print, (const char *));