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 "SBReproducerPrivate.h"
12 #include "lldb/API/SBFile.h"
13 #include "lldb/Core/StreamFile.h"
14 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/Utility/Stream.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 SBStream::SBStream() : m_opaque_up(new StreamString()) {
24 }
25 
27  : m_opaque_up(std::move(rhs.m_opaque_up)), m_is_file(rhs.m_is_file) {}
28 
29 SBStream::~SBStream() = default;
30 
31 bool SBStream::IsValid() const {
33  return this->operator bool();
34 }
35 SBStream::operator bool() const {
36  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, operator bool);
37 
38  return (m_opaque_up != nullptr);
39 }
40 
41 // If this stream is not redirected to a file, it will maintain a local cache
42 // for the stream data which can be accessed using this accessor.
43 const char *SBStream::GetData() {
45 
46  if (m_is_file || m_opaque_up == nullptr)
47  return nullptr;
48 
49  return static_cast<StreamString *>(m_opaque_up.get())->GetData();
50 }
51 
52 // If this stream is not redirected to a file, it will maintain a local cache
53 // for the stream output whose length can be accessed using this accessor.
56 
57  if (m_is_file || m_opaque_up == nullptr)
58  return 0;
59 
60  return static_cast<StreamString *>(m_opaque_up.get())->GetSize();
61 }
62 
63 void SBStream::Print(const char *str) {
64  LLDB_RECORD_METHOD(void, SBStream, Print, (const char *), str);
65 
66  Printf("%s", str);
67 }
68 
69 void SBStream::Printf(const char *format, ...) {
70  if (!format)
71  return;
72  va_list args;
73  va_start(args, format);
74  ref().PrintfVarArg(format, args);
75  va_end(args);
76 }
77 
78 void SBStream::RedirectToFile(const char *path, bool append) {
79  LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (const char *, bool), path,
80  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) {
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_RECORD_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool), fh,
118  transfer_fh_ownership);
119  FileSP file = std::make_unique<NativeFile>(fh, transfer_fh_ownership);
120  return RedirectToFile(file);
121 }
122 
125  RedirectToFile(file.GetFile());
126 }
127 
128 void SBStream::RedirectToFile(FileSP file_sp) {
129  LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (FileSP), file_sp);
130 
131  if (!file_sp || !file_sp->IsValid())
132  return;
133 
134  std::string local_data;
135  if (m_opaque_up) {
136  // See if we have any locally backed data. If so, copy it so we can then
137  // redirect it to the file so we don't lose the data
138  if (!m_is_file)
139  local_data = std::string(
140  static_cast<StreamString *>(m_opaque_up.get())->GetString());
141  }
142 
143  m_opaque_up = std::make_unique<StreamFile>(file_sp);
144  m_is_file = true;
145 
146  // If we had any data locally in our StreamString, then pass that along to
147  // the to new file we are redirecting to.
148  if (!local_data.empty())
149  m_opaque_up->Write(&local_data[0], local_data.size());
150 }
151 
152 void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) {
153  LLDB_RECORD_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool), fd,
154  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 
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 }
196 
197 namespace lldb_private {
198 namespace repro {
199 
200 template <>
203  LLDB_REGISTER_METHOD_CONST(bool, SBStream, IsValid, ());
204  LLDB_REGISTER_METHOD_CONST(bool, SBStream, operator bool, ());
205  LLDB_REGISTER_METHOD(const char *, SBStream, GetData, ());
206  LLDB_REGISTER_METHOD(size_t, SBStream, GetSize, ());
207  LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (const char *, bool));
208  LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (FileSP));
209  LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (SBFile));
210  LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool));
211  LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool));
212  LLDB_REGISTER_METHOD(void, SBStream, Clear, ());
213  LLDB_REGISTER_METHOD(void, SBStream, Print, (const char *));
214 }
215 
216 }
217 }
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:178
FileSystem.h
lldb::SBStream::Printf
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:69
LLDB_RECORD_METHOD_CONST_NO_ARGS
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:168
lldb::SBStream::GetSize
size_t GetSize()
Definition: SBStream.cpp:54
LIBLLDB_LOG_API
#define LIBLLDB_LOG_API
Definition: Logging.h:30
LLDB_RECORD_METHOD_NO_ARGS
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:165
lldb::SBStream::m_is_file
bool m_is_file
Definition: SBStream.h:108
LLDB_RECORD_CONSTRUCTOR_NO_ARGS
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
Definition: ReproducerInstrumentation.h:136
LLDB_REGISTER_METHOD_CONST
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:98
lldb::SBStream::operator->
lldb_private::Stream * operator->()
Definition: SBStream.cpp:174
lldb::SBStream::Print
void void Print(const char *str)
Definition: SBStream.cpp:63
lldb_private::Stream
Definition: Stream.h:28
SBReproducerPrivate.h
lldb::SBStream::m_opaque_up
std::unique_ptr< lldb_private::Stream > m_opaque_up
Definition: SBStream.h:107
LLDB_REGISTER_CONSTRUCTOR
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
Definition: ReproducerInstrumentation.h:89
lldb_private::StreamString::GetString
llvm::StringRef GetString() const
Definition: StreamString.cpp:51
lldb::SBStream::RedirectToFileHandle
void RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership)
Definition: SBStream.cpp:116
lldb_private::FileSpec
Definition: FileSpec.h:56
SBStream.h
lldb::SBFile
Definition: SBFile.h:16
lldb::SBStream::get
lldb_private::Stream * get()
Definition: SBStream.cpp:176
lldb::SBStream::RedirectToFileDescriptor
void RedirectToFileDescriptor(int fd, bool transfer_fh_ownership)
Definition: SBStream.cpp:152
LLDB_RECORD_METHOD
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:158
StreamString.h
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb_private::Stream::PrintfVarArg
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition: Stream.cpp:116
lldb::SBStream::IsValid
bool IsValid() const
Definition: SBStream.cpp:31
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
StreamFile.h
lldb_private::repro::Registry
The registry contains a unique mapping between functions and their ID.
Definition: ReproducerInstrumentation.h:517
SBFile.h
lldb_private::repro::RegisterMethods< SBStream >
void RegisterMethods< SBStream >(Registry &R)
Definition: SBStream.cpp:201
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Stream.h
lldb::SBStream
Definition: SBStream.h:18
lldb::SBStream::SBStream
SBStream()
Definition: SBStream.cpp:22
lldb::SBStream::RedirectToFile
void RedirectToFile(const char *path, bool append)
Definition: SBStream.cpp:78
lldb
Definition: SBAddress.h:15
lldb::SBFile::GetFile
FileSP GetFile() const
Definition: SBFile.cpp:131
LLDB_LOG_ERROR
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:265
lldb::SBStream::GetData
const char * GetData()
Definition: SBStream.cpp:43
lldb::SBStream::Clear
void Clear()
Definition: SBStream.cpp:184
lldb::SBStream::~SBStream
~SBStream()
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93