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
23SBStream::SBStream() : m_opaque_up(new StreamString()) {
25}
26
28 : m_opaque_up(std::move(rhs.m_opaque_up)), m_is_file(rhs.m_is_file) {}
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 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.
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
64void SBStream::Print(const char *str) {
65 LLDB_INSTRUMENT_VA(this, str);
66
67 Printf("%s", str);
68}
69
70void 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
79void 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 }
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
116void 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
123 LLDB_INSTRUMENT_VA(this, file)
124 RedirectToFile(file.GetFile());
125}
126
127void 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
151void 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
173
175
177 if (m_opaque_up == nullptr)
178 m_opaque_up = std::make_unique<StreamString>();
179 return *m_opaque_up;
180}
181
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}
#define LLDB_INSTRUMENT_VA(...)
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:360
FileSP GetFile() const
Definition: SBFile.cpp:126
bool m_is_file
Definition: SBStream.h:108
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:70
void void Print(const char *str)
Definition: SBStream.cpp:64
size_t GetSize()
Definition: SBStream.cpp:55
void RedirectToFileDescriptor(int fd, bool transfer_fh_ownership)
Definition: SBStream.cpp:151
void RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership)
Definition: SBStream.cpp:116
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
std::unique_ptr< lldb_private::Stream > m_opaque_up
Definition: SBStream.h:107
void RedirectToFile(const char *path, bool append)
Definition: SBStream.cpp:79
const char * GetData()
Definition: SBStream.cpp:44
lldb_private::Stream * operator->()
Definition: SBStream.cpp:172
bool IsValid() const
Definition: SBStream.cpp:32
lldb_private::Stream * get()
Definition: SBStream.cpp:174
A file utility class.
Definition: FileSpec.h:56
int Open(const char *path, int flags, int mode)
Wraps ::open in a platform-independent way.
static FileSystem & Instance()
@ eOpenOptionWriteOnly
Definition: File.h:52
@ eOpenOptionAppend
Definition: File.h:54
@ eOpenOptionCanCreate
Definition: File.h:56
@ eOpenOptionTruncate
Definition: File.h:57
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:116
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:309
Definition: SBAddress.h:15