blob: 5dea45959101c8ba383b6bb1b04ae344528c1319 [file] [log] [blame]
Greg Claytone2ae97f2010-09-17 17:42:16 +00001//===-- SBStream.cpp ----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/API/SBStream.h"
11
Greg Claytone2ae97f2010-09-17 17:42:16 +000012#include "lldb/Core/StreamFile.h"
Jonas Devlieghere50bc1ed2018-11-02 22:34:51 +000013#include "lldb/Host/FileSystem.h"
Zachary Turner97206d52017-05-12 04:51:55 +000014#include "lldb/Utility/Status.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000015#include "lldb/Utility/Stream.h"
16#include "lldb/Utility/StreamString.h"
Greg Claytone2ae97f2010-09-17 17:42:16 +000017
18using namespace lldb;
19using namespace lldb_private;
20
Kate Stoneb9c1b512016-09-06 20:57:50 +000021SBStream::SBStream() : m_opaque_ap(new StreamString()), m_is_file(false) {}
Greg Claytone2ae97f2010-09-17 17:42:16 +000022
Kate Stoneb9c1b512016-09-06 20:57:50 +000023SBStream::SBStream(SBStream &&rhs)
24 : m_opaque_ap(std::move(rhs.m_opaque_ap)), m_is_file(rhs.m_is_file) {}
Greg Clayton0817da82015-12-15 23:03:22 +000025
Kate Stoneb9c1b512016-09-06 20:57:50 +000026SBStream::~SBStream() {}
Greg Clayton0817da82015-12-15 23:03:22 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028bool SBStream::IsValid() const { return (m_opaque_ap.get() != NULL); }
Greg Claytone2ae97f2010-09-17 17:42:16 +000029
Adrian Prantl05097242018-04-30 16:49:04 +000030// If this stream is not redirected to a file, it will maintain a local cache
31// for the stream data which can be accessed using this accessor.
Kate Stoneb9c1b512016-09-06 20:57:50 +000032const char *SBStream::GetData() {
33 if (m_is_file || m_opaque_ap.get() == NULL)
34 return NULL;
35
36 return static_cast<StreamString *>(m_opaque_ap.get())->GetData();
Greg Claytone2ae97f2010-09-17 17:42:16 +000037}
38
Adrian Prantl05097242018-04-30 16:49:04 +000039// If this stream is not redirected to a file, it will maintain a local cache
40// for the stream output whose length can be accessed using this accessor.
Kate Stoneb9c1b512016-09-06 20:57:50 +000041size_t SBStream::GetSize() {
42 if (m_is_file || m_opaque_ap.get() == NULL)
43 return 0;
44
45 return static_cast<StreamString *>(m_opaque_ap.get())->GetSize();
Greg Claytone2ae97f2010-09-17 17:42:16 +000046}
47
Kate Stoneb9c1b512016-09-06 20:57:50 +000048void SBStream::Printf(const char *format, ...) {
49 if (!format)
50 return;
51 va_list args;
52 va_start(args, format);
53 ref().PrintfVarArg(format, args);
54 va_end(args);
Greg Claytone2ae97f2010-09-17 17:42:16 +000055}
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057void SBStream::RedirectToFile(const char *path, bool append) {
58 if (path == nullptr)
59 return;
Zachary Turnere2dcbd02015-01-14 18:34:35 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 std::string local_data;
62 if (m_opaque_ap.get()) {
63 // See if we have any locally backed data. If so, copy it so we can then
64 // redirect it to the file so we don't lose the data
65 if (!m_is_file)
Zachary Turnerc1564272016-11-16 21:15:24 +000066 local_data = static_cast<StreamString *>(m_opaque_ap.get())->GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 }
68 StreamFile *stream_file = new StreamFile;
69 uint32_t open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
70 if (append)
71 open_options |= File::eOpenOptionAppend;
72 else
73 open_options |= File::eOpenOptionTruncate;
Kate Stoneb9c1b512016-09-06 20:57:50 +000074
Jonas Devlieghere50bc1ed2018-11-02 22:34:51 +000075 FileSystem::Instance().Open(stream_file->GetFile(), FileSpec(path),
76 open_options);
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 m_opaque_ap.reset(stream_file);
78
79 if (m_opaque_ap.get()) {
80 m_is_file = true;
81
82 // If we had any data locally in our StreamString, then pass that along to
83 // the to new file we are redirecting to.
84 if (!local_data.empty())
85 m_opaque_ap->Write(&local_data[0], local_data.size());
86 } else
87 m_is_file = false;
88}
89
90void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
91 if (fh == nullptr)
92 return;
93
94 std::string local_data;
95 if (m_opaque_ap.get()) {
96 // See if we have any locally backed data. If so, copy it so we can then
97 // redirect it to the file so we don't lose the data
98 if (!m_is_file)
Zachary Turnerc1564272016-11-16 21:15:24 +000099 local_data = static_cast<StreamString *>(m_opaque_ap.get())->GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 }
101 m_opaque_ap.reset(new StreamFile(fh, transfer_fh_ownership));
102
103 if (m_opaque_ap.get()) {
104 m_is_file = true;
105
106 // If we had any data locally in our StreamString, then pass that along to
107 // the to new file we are redirecting to.
108 if (!local_data.empty())
109 m_opaque_ap->Write(&local_data[0], local_data.size());
110 } else
111 m_is_file = false;
112}
113
114void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) {
115 std::string local_data;
116 if (m_opaque_ap.get()) {
117 // See if we have any locally backed data. If so, copy it so we can then
118 // redirect it to the file so we don't lose the data
119 if (!m_is_file)
Zachary Turnerc1564272016-11-16 21:15:24 +0000120 local_data = static_cast<StreamString *>(m_opaque_ap.get())->GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 }
122
123 m_opaque_ap.reset(new StreamFile(::fdopen(fd, "w"), transfer_fh_ownership));
124 if (m_opaque_ap.get()) {
125 m_is_file = true;
126
127 // If we had any data locally in our StreamString, then pass that along to
128 // the to new file we are redirecting to.
129 if (!local_data.empty())
130 m_opaque_ap->Write(&local_data[0], local_data.size());
131 } else
132 m_is_file = false;
133}
134
135lldb_private::Stream *SBStream::operator->() { return m_opaque_ap.get(); }
136
137lldb_private::Stream *SBStream::get() { return m_opaque_ap.get(); }
138
139lldb_private::Stream &SBStream::ref() {
140 if (m_opaque_ap.get() == NULL)
141 m_opaque_ap.reset(new StreamString());
142 return *m_opaque_ap.get();
143}
144
145void SBStream::Clear() {
146 if (m_opaque_ap.get()) {
147 // See if we have any locally backed data. If so, copy it so we can then
148 // redirect it to the file so we don't lose the data
149 if (m_is_file)
150 m_opaque_ap.reset();
Greg Clayton06357c92014-07-30 17:38:47 +0000151 else
Zachary Turnerc1564272016-11-16 21:15:24 +0000152 static_cast<StreamString *>(m_opaque_ap.get())->Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 }
Greg Claytone2ae97f2010-09-17 17:42:16 +0000154}