aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/lldb/source/API/SBStream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm-project/lldb/source/API/SBStream.cpp')
-rw-r--r--contrib/llvm-project/lldb/source/API/SBStream.cpp73
1 files changed, 41 insertions, 32 deletions
diff --git a/contrib/llvm-project/lldb/source/API/SBStream.cpp b/contrib/llvm-project/lldb/source/API/SBStream.cpp
index ae652338e1ea..d57634d2947c 100644
--- a/contrib/llvm-project/lldb/source/API/SBStream.cpp
+++ b/contrib/llvm-project/lldb/source/API/SBStream.cpp
@@ -9,6 +9,7 @@
#include "lldb/API/SBStream.h"
#include "SBReproducerPrivate.h"
+#include "lldb/API/SBFile.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Utility/Status.h"
@@ -82,33 +83,45 @@ void SBStream::RedirectToFile(const char *path, bool append) {
if (!m_is_file)
local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString();
}
- StreamFile *stream_file = new StreamFile;
- uint32_t open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
+ auto open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
if (append)
open_options |= File::eOpenOptionAppend;
else
open_options |= File::eOpenOptionTruncate;
- FileSystem::Instance().Open(stream_file->GetFile(), FileSpec(path),
- open_options);
- m_opaque_up.reset(stream_file);
+ llvm::Expected<FileUP> file =
+ FileSystem::Instance().Open(FileSpec(path), open_options);
+ if (!file) {
+ LLDB_LOG_ERROR(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API), file.takeError(),
+ "Cannot open {1}: {0}", path);
+ return;
+ }
- if (m_opaque_up) {
- m_is_file = true;
+ m_opaque_up = std::make_unique<StreamFile>(std::move(file.get()));
+ m_is_file = true;
- // If we had any data locally in our StreamString, then pass that along to
- // the to new file we are redirecting to.
- if (!local_data.empty())
- m_opaque_up->Write(&local_data[0], local_data.size());
- } else
- m_is_file = false;
+ // If we had any data locally in our StreamString, then pass that along to
+ // the to new file we are redirecting to.
+ if (!local_data.empty())
+ m_opaque_up->Write(&local_data[0], local_data.size());
}
void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
LLDB_RECORD_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool), fh,
transfer_fh_ownership);
+ FileSP file = std::make_unique<NativeFile>(fh, transfer_fh_ownership);
+ return RedirectToFile(file);
+}
- if (fh == nullptr)
+void SBStream::RedirectToFile(SBFile file) {
+ LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (SBFile), file)
+ RedirectToFile(file.GetFile());
+}
+
+void SBStream::RedirectToFile(FileSP file_sp) {
+ LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (FileSP), file_sp);
+
+ if (!file_sp || !file_sp->IsValid())
return;
std::string local_data;
@@ -118,17 +131,14 @@ void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
if (!m_is_file)
local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString();
}
- m_opaque_up.reset(new StreamFile(fh, transfer_fh_ownership));
- if (m_opaque_up) {
- m_is_file = true;
+ m_opaque_up = std::make_unique<StreamFile>(file_sp);
+ m_is_file = true;
- // If we had any data locally in our StreamString, then pass that along to
- // the to new file we are redirecting to.
- if (!local_data.empty())
- m_opaque_up->Write(&local_data[0], local_data.size());
- } else
- m_is_file = false;
+ // If we had any data locally in our StreamString, then pass that along to
+ // the to new file we are redirecting to.
+ if (!local_data.empty())
+ m_opaque_up->Write(&local_data[0], local_data.size());
}
void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) {
@@ -143,16 +153,13 @@ void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) {
local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString();
}
- m_opaque_up.reset(new StreamFile(::fdopen(fd, "w"), transfer_fh_ownership));
- if (m_opaque_up) {
- m_is_file = true;
+ m_opaque_up = std::make_unique<StreamFile>(fd, transfer_fh_ownership);
+ m_is_file = true;
- // If we had any data locally in our StreamString, then pass that along to
- // the to new file we are redirecting to.
- if (!local_data.empty())
- m_opaque_up->Write(&local_data[0], local_data.size());
- } else
- m_is_file = false;
+ // If we had any data locally in our StreamString, then pass that along to
+ // the to new file we are redirecting to.
+ if (!local_data.empty())
+ m_opaque_up->Write(&local_data[0], local_data.size());
}
lldb_private::Stream *SBStream::operator->() { return m_opaque_up.get(); }
@@ -189,6 +196,8 @@ void RegisterMethods<SBStream>(Registry &R) {
LLDB_REGISTER_METHOD(const char *, SBStream, GetData, ());
LLDB_REGISTER_METHOD(size_t, SBStream, GetSize, ());
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (const char *, bool));
+ LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (FileSP));
+ LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (SBFile));
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool));
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool));
LLDB_REGISTER_METHOD(void, SBStream, Clear, ());