Greg Clayton | e2ae97f | 2010-09-17 17:42:16 +0000 | [diff] [blame] | 1 | //===-- SBStream.cpp ----------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Greg Clayton | e2ae97f | 2010-09-17 17:42:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/API/SBStream.h" |
| 10 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 11 | #include "SBReproducerPrivate.h" |
Greg Clayton | e2ae97f | 2010-09-17 17:42:16 +0000 | [diff] [blame] | 12 | #include "lldb/Core/StreamFile.h" |
Jonas Devlieghere | 50bc1ed | 2018-11-02 22:34:51 +0000 | [diff] [blame] | 13 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 14 | #include "lldb/Utility/Status.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/Stream.h" |
| 16 | #include "lldb/Utility/StreamString.h" |
Greg Clayton | e2ae97f | 2010-09-17 17:42:16 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace lldb; |
| 19 | using namespace lldb_private; |
| 20 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 21 | SBStream::SBStream() : m_opaque_up(new StreamString()), m_is_file(false) { |
| 22 | LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStream); |
| 23 | } |
Greg Clayton | e2ae97f | 2010-09-17 17:42:16 +0000 | [diff] [blame] | 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | SBStream::SBStream(SBStream &&rhs) |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 26 | : m_opaque_up(std::move(rhs.m_opaque_up)), m_is_file(rhs.m_is_file) {} |
Greg Clayton | 0817da8 | 2015-12-15 23:03:22 +0000 | [diff] [blame] | 27 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | SBStream::~SBStream() {} |
Greg Clayton | 0817da8 | 2015-12-15 23:03:22 +0000 | [diff] [blame] | 29 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 30 | bool SBStream::IsValid() const { |
| 31 | LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, IsValid); |
| 32 | |
| 33 | return (m_opaque_up != NULL); |
| 34 | } |
Greg Clayton | e2ae97f | 2010-09-17 17:42:16 +0000 | [diff] [blame] | 35 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 36 | // If this stream is not redirected to a file, it will maintain a local cache |
| 37 | // for the stream data which can be accessed using this accessor. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | const char *SBStream::GetData() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 39 | LLDB_RECORD_METHOD_NO_ARGS(const char *, SBStream, GetData); |
| 40 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 41 | if (m_is_file || m_opaque_up == NULL) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | return NULL; |
| 43 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 44 | return static_cast<StreamString *>(m_opaque_up.get())->GetData(); |
Greg Clayton | e2ae97f | 2010-09-17 17:42:16 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 47 | // If this stream is not redirected to a file, it will maintain a local cache |
| 48 | // for the stream output whose length can be accessed using this accessor. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | size_t SBStream::GetSize() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 50 | LLDB_RECORD_METHOD_NO_ARGS(size_t, SBStream, GetSize); |
| 51 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 52 | if (m_is_file || m_opaque_up == NULL) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | return 0; |
| 54 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 55 | return static_cast<StreamString *>(m_opaque_up.get())->GetSize(); |
Greg Clayton | e2ae97f | 2010-09-17 17:42:16 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | void SBStream::Printf(const char *format, ...) { |
| 59 | if (!format) |
| 60 | return; |
| 61 | va_list args; |
| 62 | va_start(args, format); |
| 63 | ref().PrintfVarArg(format, args); |
| 64 | va_end(args); |
Greg Clayton | e2ae97f | 2010-09-17 17:42:16 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | void SBStream::RedirectToFile(const char *path, bool append) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 68 | LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (const char *, bool), path, |
| 69 | append); |
| 70 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | if (path == nullptr) |
| 72 | return; |
Zachary Turner | e2dcbd0 | 2015-01-14 18:34:35 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | std::string local_data; |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 75 | if (m_opaque_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | // See if we have any locally backed data. If so, copy it so we can then |
| 77 | // redirect it to the file so we don't lose the data |
| 78 | if (!m_is_file) |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 79 | local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | } |
| 81 | StreamFile *stream_file = new StreamFile; |
| 82 | uint32_t open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate; |
| 83 | if (append) |
| 84 | open_options |= File::eOpenOptionAppend; |
| 85 | else |
| 86 | open_options |= File::eOpenOptionTruncate; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | |
Jonas Devlieghere | 50bc1ed | 2018-11-02 22:34:51 +0000 | [diff] [blame] | 88 | FileSystem::Instance().Open(stream_file->GetFile(), FileSpec(path), |
| 89 | open_options); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 90 | m_opaque_up.reset(stream_file); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 92 | if (m_opaque_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | m_is_file = true; |
| 94 | |
| 95 | // If we had any data locally in our StreamString, then pass that along to |
| 96 | // the to new file we are redirecting to. |
| 97 | if (!local_data.empty()) |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 98 | m_opaque_up->Write(&local_data[0], local_data.size()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | } else |
| 100 | m_is_file = false; |
| 101 | } |
| 102 | |
| 103 | void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 104 | LLDB_RECORD_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool), fh, |
| 105 | transfer_fh_ownership); |
| 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | if (fh == nullptr) |
| 108 | return; |
| 109 | |
| 110 | std::string local_data; |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 111 | if (m_opaque_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | // See if we have any locally backed data. If so, copy it so we can then |
| 113 | // redirect it to the file so we don't lose the data |
| 114 | if (!m_is_file) |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 115 | local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | } |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 117 | m_opaque_up.reset(new StreamFile(fh, transfer_fh_ownership)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 119 | if (m_opaque_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | m_is_file = true; |
| 121 | |
| 122 | // If we had any data locally in our StreamString, then pass that along to |
| 123 | // the to new file we are redirecting to. |
| 124 | if (!local_data.empty()) |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 125 | m_opaque_up->Write(&local_data[0], local_data.size()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | } else |
| 127 | m_is_file = false; |
| 128 | } |
| 129 | |
| 130 | void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 131 | LLDB_RECORD_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool), fd, |
| 132 | transfer_fh_ownership); |
| 133 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | std::string local_data; |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 135 | if (m_opaque_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 139 | local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 142 | m_opaque_up.reset(new StreamFile(::fdopen(fd, "w"), transfer_fh_ownership)); |
| 143 | if (m_opaque_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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()) |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 149 | m_opaque_up->Write(&local_data[0], local_data.size()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | } else |
| 151 | m_is_file = false; |
| 152 | } |
| 153 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 154 | lldb_private::Stream *SBStream::operator->() { return m_opaque_up.get(); } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 156 | lldb_private::Stream *SBStream::get() { return m_opaque_up.get(); } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | |
| 158 | lldb_private::Stream &SBStream::ref() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 159 | if (m_opaque_up == NULL) |
| 160 | m_opaque_up.reset(new StreamString()); |
| 161 | return *m_opaque_up; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void SBStream::Clear() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 165 | LLDB_RECORD_METHOD_NO_ARGS(void, SBStream, Clear); |
| 166 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 167 | if (m_opaque_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | // See if we have any locally backed data. If so, copy it so we can then |
| 169 | // redirect it to the file so we don't lose the data |
| 170 | if (m_is_file) |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 171 | m_opaque_up.reset(); |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 172 | else |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 173 | static_cast<StreamString *>(m_opaque_up.get())->Clear(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | } |
Greg Clayton | e2ae97f | 2010-09-17 17:42:16 +0000 | [diff] [blame] | 175 | } |