2010-09-18 01:42:16 +08:00
|
|
|
//===-- SBStream.cpp ----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-09-18 01:42:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/API/SBStream.h"
|
|
|
|
|
|
|
|
#include "lldb/Core/StreamFile.h"
|
2018-11-03 06:34:51 +08:00
|
|
|
#include "lldb/Host/FileSystem.h"
|
2017-05-12 12:51:55 +08:00
|
|
|
#include "lldb/Utility/Status.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/Stream.h"
|
|
|
|
#include "lldb/Utility/StreamString.h"
|
2010-09-18 01:42:16 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
SBStream::SBStream() : m_opaque_up(new StreamString()), m_is_file(false) {}
|
2010-09-18 01:42:16 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBStream::SBStream(SBStream &&rhs)
|
2019-02-13 14:25:41 +08:00
|
|
|
: m_opaque_up(std::move(rhs.m_opaque_up)), m_is_file(rhs.m_is_file) {}
|
2015-12-16 07:03:22 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBStream::~SBStream() {}
|
2015-12-16 07:03:22 +08:00
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
bool SBStream::IsValid() const { return (m_opaque_up != NULL); }
|
2010-09-18 01:42:16 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// If this stream is not redirected to a file, it will maintain a local cache
|
|
|
|
// for the stream data which can be accessed using this accessor.
|
2016-09-07 04:57:50 +08:00
|
|
|
const char *SBStream::GetData() {
|
2019-02-13 14:25:41 +08:00
|
|
|
if (m_is_file || m_opaque_up == NULL)
|
2016-09-07 04:57:50 +08:00
|
|
|
return NULL;
|
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
return static_cast<StreamString *>(m_opaque_up.get())->GetData();
|
2010-09-18 01:42:16 +08:00
|
|
|
}
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// If this stream is not redirected to a file, it will maintain a local cache
|
|
|
|
// for the stream output whose length can be accessed using this accessor.
|
2016-09-07 04:57:50 +08:00
|
|
|
size_t SBStream::GetSize() {
|
2019-02-13 14:25:41 +08:00
|
|
|
if (m_is_file || m_opaque_up == NULL)
|
2016-09-07 04:57:50 +08:00
|
|
|
return 0;
|
2010-09-18 01:42:16 +08:00
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
return static_cast<StreamString *>(m_opaque_up.get())->GetSize();
|
2010-09-18 01:42:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SBStream::Printf(const char *format, ...) {
|
|
|
|
if (!format)
|
|
|
|
return;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
ref().PrintfVarArg(format, args);
|
|
|
|
va_end(args);
|
2010-09-18 01:42:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SBStream::RedirectToFile(const char *path, bool append) {
|
|
|
|
if (path == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string local_data;
|
2019-02-13 14:25:41 +08:00
|
|
|
if (m_opaque_up) {
|
2016-09-07 04:57:50 +08:00
|
|
|
// See if we have any locally backed data. If so, copy it so we can then
|
|
|
|
// redirect it to the file so we don't lose the data
|
|
|
|
if (!m_is_file)
|
2019-02-13 14:25:41 +08:00
|
|
|
local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
StreamFile *stream_file = new StreamFile;
|
|
|
|
uint32_t open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
|
|
|
|
if (append)
|
|
|
|
open_options |= File::eOpenOptionAppend;
|
|
|
|
else
|
|
|
|
open_options |= File::eOpenOptionTruncate;
|
|
|
|
|
2018-11-03 06:34:51 +08:00
|
|
|
FileSystem::Instance().Open(stream_file->GetFile(), FileSpec(path),
|
|
|
|
open_options);
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up.reset(stream_file);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
if (m_opaque_up) {
|
2016-09-07 04:57:50 +08:00
|
|
|
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())
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up->Write(&local_data[0], local_data.size());
|
2016-09-07 04:57:50 +08:00
|
|
|
} else
|
|
|
|
m_is_file = false;
|
2010-09-18 01:42:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
|
|
|
|
if (fh == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string local_data;
|
2019-02-13 14:25:41 +08:00
|
|
|
if (m_opaque_up) {
|
2016-09-07 04:57:50 +08:00
|
|
|
// See if we have any locally backed data. If so, copy it so we can then
|
|
|
|
// redirect it to the file so we don't lose the data
|
|
|
|
if (!m_is_file)
|
2019-02-13 14:25:41 +08:00
|
|
|
local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up.reset(new StreamFile(fh, transfer_fh_ownership));
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
if (m_opaque_up) {
|
2016-09-07 04:57:50 +08:00
|
|
|
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())
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up->Write(&local_data[0], local_data.size());
|
2016-09-07 04:57:50 +08:00
|
|
|
} else
|
|
|
|
m_is_file = false;
|
2010-09-18 01:42:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) {
|
|
|
|
std::string local_data;
|
2019-02-13 14:25:41 +08:00
|
|
|
if (m_opaque_up) {
|
2016-09-07 04:57:50 +08:00
|
|
|
// See if we have any locally backed data. If so, copy it so we can then
|
|
|
|
// redirect it to the file so we don't lose the data
|
|
|
|
if (!m_is_file)
|
2019-02-13 14:25:41 +08:00
|
|
|
local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up.reset(new StreamFile(::fdopen(fd, "w"), transfer_fh_ownership));
|
|
|
|
if (m_opaque_up) {
|
2016-09-07 04:57:50 +08:00
|
|
|
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())
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up->Write(&local_data[0], local_data.size());
|
2016-09-07 04:57:50 +08:00
|
|
|
} else
|
|
|
|
m_is_file = false;
|
2010-09-18 01:42:16 +08:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
lldb_private::Stream *SBStream::operator->() { return m_opaque_up.get(); }
|
2010-09-18 01:42:16 +08:00
|
|
|
|
2019-02-13 14:25:41 +08:00
|
|
|
lldb_private::Stream *SBStream::get() { return m_opaque_up.get(); }
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
lldb_private::Stream &SBStream::ref() {
|
2019-02-13 14:25:41 +08:00
|
|
|
if (m_opaque_up == NULL)
|
|
|
|
m_opaque_up.reset(new StreamString());
|
|
|
|
return *m_opaque_up;
|
2010-09-18 01:42:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SBStream::Clear() {
|
2019-02-13 14:25:41 +08:00
|
|
|
if (m_opaque_up) {
|
2016-09-07 04:57:50 +08:00
|
|
|
// See if we have any locally backed data. If so, copy it so we can then
|
|
|
|
// redirect it to the file so we don't lose the data
|
|
|
|
if (m_is_file)
|
2019-02-13 14:25:41 +08:00
|
|
|
m_opaque_up.reset();
|
2016-09-07 04:57:50 +08:00
|
|
|
else
|
2019-02-13 14:25:41 +08:00
|
|
|
static_cast<StreamString *>(m_opaque_up.get())->Clear();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-09-18 01:42:16 +08:00
|
|
|
}
|