1 //===-- SBStream.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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBStream.h"
10 
11 #include "SBReproducerPrivate.h"
12 #include "lldb/Core/StreamFile.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Utility/Status.h"
15 #include "lldb/Utility/Stream.h"
16 #include "lldb/Utility/StreamString.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 SBStream::SBStream() : m_opaque_up(new StreamString()), m_is_file(false) {
22   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStream);
23 }
24 
25 SBStream::SBStream(SBStream &&rhs)
26     : m_opaque_up(std::move(rhs.m_opaque_up)), m_is_file(rhs.m_is_file) {}
27 
28 SBStream::~SBStream() {}
29 
30 bool SBStream::IsValid() const {
31   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, IsValid);
32   return this->operator bool();
33 }
34 SBStream::operator bool() const {
35   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, operator bool);
36 
37   return (m_opaque_up != nullptr);
38 }
39 
40 // If this stream is not redirected to a file, it will maintain a local cache
41 // for the stream data which can be accessed using this accessor.
42 const char *SBStream::GetData() {
43   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBStream, GetData);
44 
45   if (m_is_file || m_opaque_up == nullptr)
46     return nullptr;
47 
48   return static_cast<StreamString *>(m_opaque_up.get())->GetData();
49 }
50 
51 // If this stream is not redirected to a file, it will maintain a local cache
52 // for the stream output whose length can be accessed using this accessor.
53 size_t SBStream::GetSize() {
54   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBStream, GetSize);
55 
56   if (m_is_file || m_opaque_up == nullptr)
57     return 0;
58 
59   return static_cast<StreamString *>(m_opaque_up.get())->GetSize();
60 }
61 
62 void SBStream::Printf(const char *format, ...) {
63   if (!format)
64     return;
65   va_list args;
66   va_start(args, format);
67   ref().PrintfVarArg(format, args);
68   va_end(args);
69 }
70 
71 void SBStream::RedirectToFile(const char *path, bool append) {
72   LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (const char *, bool), path,
73                      append);
74 
75   if (path == nullptr)
76     return;
77 
78   std::string local_data;
79   if (m_opaque_up) {
80     // See if we have any locally backed data. If so, copy it so we can then
81     // redirect it to the file so we don't lose the data
82     if (!m_is_file)
83       local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString();
84   }
85   uint32_t open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
86   if (append)
87     open_options |= File::eOpenOptionAppend;
88   else
89     open_options |= File::eOpenOptionTruncate;
90 
91   llvm::Expected<FileUP> file =
92       FileSystem::Instance().Open(FileSpec(path), open_options);
93   if (!file) {
94     LLDB_LOG_ERROR(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API), file.takeError(),
95                    "Cannot open {1}: {0}", path);
96     return;
97   }
98 
99   m_opaque_up = std::make_unique<StreamFile>(std::move(file.get()));
100   m_is_file = true;
101 
102   // If we had any data locally in our StreamString, then pass that along to
103   // the to new file we are redirecting to.
104   if (!local_data.empty())
105     m_opaque_up->Write(&local_data[0], local_data.size());
106 }
107 
108 void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
109   LLDB_RECORD_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool), fh,
110                      transfer_fh_ownership);
111 
112   if (fh == nullptr)
113     return;
114 
115   std::string local_data;
116   if (m_opaque_up) {
117     // See if we have any locally backed data. If so, copy it so we can then
118     // redirect it to the file so we don't lose the data
119     if (!m_is_file)
120       local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString();
121   }
122 
123   m_opaque_up = std::make_unique<StreamFile>(fh, transfer_fh_ownership);
124   m_is_file = true;
125 
126   // If we had any data locally in our StreamString, then pass that along to
127   // the to new file we are redirecting to.
128   if (!local_data.empty())
129     m_opaque_up->Write(&local_data[0], local_data.size());
130 }
131 
132 void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) {
133   LLDB_RECORD_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool), fd,
134                      transfer_fh_ownership);
135 
136   std::string local_data;
137   if (m_opaque_up) {
138     // See if we have any locally backed data. If so, copy it so we can then
139     // redirect it to the file so we don't lose the data
140     if (!m_is_file)
141       local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString();
142   }
143 
144   m_opaque_up = std::make_unique<StreamFile>(fd, transfer_fh_ownership);
145   m_is_file = true;
146 
147   // If we had any data locally in our StreamString, then pass that along to
148   // the to new file we are redirecting to.
149   if (!local_data.empty())
150     m_opaque_up->Write(&local_data[0], local_data.size());
151 }
152 
153 lldb_private::Stream *SBStream::operator->() { return m_opaque_up.get(); }
154 
155 lldb_private::Stream *SBStream::get() { return m_opaque_up.get(); }
156 
157 lldb_private::Stream &SBStream::ref() {
158   if (m_opaque_up == nullptr)
159     m_opaque_up.reset(new StreamString());
160   return *m_opaque_up;
161 }
162 
163 void SBStream::Clear() {
164   LLDB_RECORD_METHOD_NO_ARGS(void, SBStream, Clear);
165 
166   if (m_opaque_up) {
167     // See if we have any locally backed data. If so, copy it so we can then
168     // redirect it to the file so we don't lose the data
169     if (m_is_file)
170       m_opaque_up.reset();
171     else
172       static_cast<StreamString *>(m_opaque_up.get())->Clear();
173   }
174 }
175 
176 namespace lldb_private {
177 namespace repro {
178 
179 template <>
180 void RegisterMethods<SBStream>(Registry &R) {
181   LLDB_REGISTER_CONSTRUCTOR(SBStream, ());
182   LLDB_REGISTER_METHOD_CONST(bool, SBStream, IsValid, ());
183   LLDB_REGISTER_METHOD_CONST(bool, SBStream, operator bool, ());
184   LLDB_REGISTER_METHOD(const char *, SBStream, GetData, ());
185   LLDB_REGISTER_METHOD(size_t, SBStream, GetSize, ());
186   LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (const char *, bool));
187   LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool));
188   LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool));
189   LLDB_REGISTER_METHOD(void, SBStream, Clear, ());
190 }
191 
192 }
193 }
194