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