[Go to site: main page, start]

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