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"
12 #include "lldb/Core/StreamFile.h"
13 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Utility/LLDBLog.h"
16 #include "lldb/Utility/Status.h"
17 #include "lldb/Utility/Stream.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 
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.
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 
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 
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 }
Instrumentation.h
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
FileSystem.h
lldb::SBStream::Printf
void Printf(const char *format,...) __attribute__((format(printf
Definition: SBStream.cpp:70
lldb::SBStream::GetSize
size_t GetSize()
Definition: SBStream.cpp:55
lldb::SBStream::m_is_file
bool m_is_file
Definition: SBStream.h:108
lldb::SBStream::operator->
lldb_private::Stream * operator->()
Definition: SBStream.cpp:172
lldb::SBStream::Print
void void Print(const char *str)
Definition: SBStream.cpp:64
lldb_private::Stream
Definition: Stream.h:28
lldb::SBStream::m_opaque_up
std::unique_ptr< lldb_private::Stream > m_opaque_up
Definition: SBStream.h:107
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:174
lldb::SBStream::RedirectToFileDescriptor
void RedirectToFileDescriptor(int fd, bool transfer_fh_ownership)
Definition: SBStream.cpp:151
StreamString.h
lldb_private::StreamString
Definition: StreamString.h:23
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:32
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
StreamFile.h
SBFile.h
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
Stream.h
lldb::SBStream
Definition: SBStream.h:18
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:235
lldb::SBStream::SBStream
SBStream()
Definition: SBStream.cpp:23
lldb::SBStream::RedirectToFile
void RedirectToFile(const char *path, bool append)
Definition: SBStream.cpp:79
lldb
Definition: SBAddress.h:15
lldb::SBFile::GetFile
FileSP GetFile() const
Definition: SBFile.cpp:126
LLDB_LOG_ERROR
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:286
LLDBLog.h
lldb::SBStream::GetData
const char * GetData()
Definition: SBStream.cpp:44
lldb::SBStream::Clear
void Clear()
Definition: SBStream.cpp:182
lldb::SBStream::~SBStream
~SBStream()