LLDB mainline
SBStream.cpp
Go to the documentation of this file.
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"
16#include "lldb/Utility/Status.h"
17#include "lldb/Utility/Stream.h"
19
20using namespace lldb;
21using namespace lldb_private;
22
26
29
30SBStream::~SBStream() = default;
31
32bool SBStream::IsValid() const {
34 return this->operator bool();
35}
36SBStream::operator bool() const {
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.
44const char *SBStream::GetData() {
46
47 if (m_is_file || m_opaque_up == nullptr)
48 return nullptr;
49
50 return ConstString(static_cast<StreamString *>(m_opaque_up.get())->GetData())
51 .GetCString();
52}
53
54// If this stream is not redirected to a file, it will maintain a local cache
55// for the stream output whose length can be accessed using this accessor.
58
59 if (m_is_file || m_opaque_up == nullptr)
60 return 0;
61
62 return static_cast<StreamString *>(m_opaque_up.get())->GetSize();
63}
64
65void SBStream::Print(const char *str) {
66 LLDB_INSTRUMENT_VA(this, str);
67
68 Printf("%s", str);
69}
70
71void SBStream::Printf(const char *format, ...) {
72 if (!format)
73 return;
74 va_list args;
75 va_start(args, format);
76 ref().PrintfVarArg(format, args);
77 va_end(args);
78}
79
80void SBStream::RedirectToFile(const char *path, bool append) {
81 LLDB_INSTRUMENT_VA(this, path, append);
82
83 if (path == nullptr)
84 return;
85
86 std::string local_data;
87 if (m_opaque_up) {
88 // See if we have any locally backed data. If so, copy it so we can then
89 // redirect it to the file so we don't lose the data
90 if (!m_is_file)
91 local_data = std::string(
92 static_cast<StreamString *>(m_opaque_up.get())->GetString());
93 }
95 if (append)
96 open_options |= File::eOpenOptionAppend;
97 else
98 open_options |= File::eOpenOptionTruncate;
99
100 llvm::Expected<FileUP> file =
101 FileSystem::Instance().Open(FileSpec(path), open_options);
102 if (!file) {
103 LLDB_LOG_ERROR(GetLog(LLDBLog::API), file.takeError(),
104 "Cannot open {1}: {0}", path);
105 return;
106 }
107
108 m_opaque_up = std::make_unique<StreamFile>(std::move(file.get()));
109 m_is_file = true;
110
111 // If we had any data locally in our StreamString, then pass that along to
112 // the to new file we are redirecting to.
113 if (!local_data.empty())
114 m_opaque_up->Write(&local_data[0], local_data.size());
115}
116
117void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
118 LLDB_INSTRUMENT_VA(this, fh, transfer_fh_ownership);
119 FileSP file = std::make_unique<NativeFile>(fh, File::eOpenOptionReadWrite,
120 transfer_fh_ownership);
121 return RedirectToFile(file);
122}
123
125 LLDB_INSTRUMENT_VA(this, file)
126 RedirectToFile(file.GetFile());
127}
128
130 LLDB_INSTRUMENT_VA(this, file_sp);
131
132 if (!file_sp || !file_sp->IsValid())
133 return;
134
135 std::string local_data;
136 if (m_opaque_up) {
137 // See if we have any locally backed data. If so, copy it so we can then
138 // redirect it to the file so we don't lose the data
139 if (!m_is_file)
140 local_data = std::string(
141 static_cast<StreamString *>(m_opaque_up.get())->GetString());
142 }
143
144 m_opaque_up = std::make_unique<StreamFile>(file_sp);
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
153void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) {
154 LLDB_INSTRUMENT_VA(this, fd, transfer_fh_ownership);
155
156 std::string local_data;
157 if (m_opaque_up) {
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
160 if (!m_is_file)
161 local_data = std::string(
162 static_cast<StreamString *>(m_opaque_up.get())->GetString());
163 }
164
165 m_opaque_up = std::make_unique<StreamFile>(fd, transfer_fh_ownership);
166 m_is_file = true;
167
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());
172}
173
175
177
179 if (m_opaque_up == nullptr)
180 m_opaque_up = std::make_unique<StreamString>();
181 return *m_opaque_up;
182}
183
185 LLDB_INSTRUMENT_VA(this);
186
187 if (m_opaque_up) {
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
190 if (m_is_file)
191 m_opaque_up.reset();
192 else
193 static_cast<StreamString *>(m_opaque_up.get())->Clear();
194 }
195}
#define LLDB_INSTRUMENT_VA(...)
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:392
FileSP GetFile() const
Definition SBFile.cpp:136
void Print(const char *str)
Definition SBStream.cpp:65
size_t GetSize()
Definition SBStream.cpp:56
void RedirectToFileDescriptor(int fd, bool transfer_fh_ownership)
Definition SBStream.cpp:153
void RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership)
Definition SBStream.cpp:117
lldb_private::Stream & ref()
Definition SBStream.cpp:178
std::unique_ptr< lldb_private::Stream > m_opaque_up
Definition SBStream.h:122
void RedirectToFile(const char *path, bool append)
Definition SBStream.cpp:80
const char * GetData()
Definition SBStream.cpp:44
lldb_private::Stream * operator->()
Definition SBStream.cpp:174
bool IsValid() const
Definition SBStream.cpp:32
lldb_private::Stream * get()
Definition SBStream.cpp:176
A uniqued constant string class.
Definition ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
A file utility class.
Definition FileSpec.h:57
int Open(const char *path, int flags, int mode=0600)
Wraps open in a platform-independent way.
static FileSystem & Instance()
@ eOpenOptionReadWrite
Definition File.h:53
@ eOpenOptionWriteOnly
Definition File.h:52
@ eOpenOptionCanCreate
Definition File.h:56
@ eOpenOptionTruncate
Definition File.h:57
const char * GetData() const
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition Stream.cpp:143
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::File > FileSP