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