2020-01-24 08:23:27 +01:00
|
|
|
//===-- SBStream.cpp ------------------------------------------------------===//
|
2010-09-17 17:42:16 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00: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-17 17:42:16 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "lldb/API/SBStream.h"
|
|
|
|
|
|
2019-03-06 00:06:00 +00:00
|
|
|
#include "SBReproducerPrivate.h"
|
2019-10-15 17:41:40 +00:00
|
|
|
#include "lldb/API/SBFile.h"
|
2010-09-17 17:42:16 +00:00
|
|
|
#include "lldb/Core/StreamFile.h"
|
2018-11-02 22:34:51 +00:00
|
|
|
#include "lldb/Host/FileSystem.h"
|
2017-05-12 04:51:55 +00:00
|
|
|
#include "lldb/Utility/Status.h"
|
2017-02-02 21:39:50 +00:00
|
|
|
#include "lldb/Utility/Stream.h"
|
|
|
|
|
#include "lldb/Utility/StreamString.h"
|
2010-09-17 17:42:16 +00:00
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
2019-03-06 00:06:00 +00:00
|
|
|
SBStream::SBStream() : m_opaque_up(new StreamString()), m_is_file(false) {
|
|
|
|
|
LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStream);
|
|
|
|
|
}
|
2010-09-17 17:42:16 +00:00
|
|
|
|
2015-12-15 23:03:22 +00:00
|
|
|
SBStream::SBStream(SBStream &&rhs)
|
2019-02-13 06:25:41 +00:00
|
|
|
: m_opaque_up(std::move(rhs.m_opaque_up)), m_is_file(rhs.m_is_file) {}
|
2015-12-15 23:03:22 +00:00
|
|
|
|
2020-02-17 22:57:06 -08:00
|
|
|
SBStream::~SBStream() = default;
|
2015-12-15 23:03:22 +00:00
|
|
|
|
2019-03-06 00:06:00 +00:00
|
|
|
bool SBStream::IsValid() const {
|
|
|
|
|
LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, IsValid);
|
2019-03-11 13:58:46 +00:00
|
|
|
return this->operator bool();
|
|
|
|
|
}
|
|
|
|
|
SBStream::operator bool() const {
|
|
|
|
|
LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, operator bool);
|
2019-03-06 00:06:00 +00:00
|
|
|
|
2019-05-23 11:14:47 +00:00
|
|
|
return (m_opaque_up != nullptr);
|
2019-03-06 00:06:00 +00:00
|
|
|
}
|
2010-09-17 17:42:16 +00:00
|
|
|
|
2018-04-30 16:49:04 +00: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.
|
2010-09-17 17:42:16 +00:00
|
|
|
const char *SBStream::GetData() {
|
2019-03-06 00:06:00 +00:00
|
|
|
LLDB_RECORD_METHOD_NO_ARGS(const char *, SBStream, GetData);
|
|
|
|
|
|
2019-05-23 11:14:47 +00:00
|
|
|
if (m_is_file || m_opaque_up == nullptr)
|
|
|
|
|
return nullptr;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2019-02-13 06:25:41 +00:00
|
|
|
return static_cast<StreamString *>(m_opaque_up.get())->GetData();
|
2010-09-17 17:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
2018-04-30 16:49:04 +00: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.
|
2010-09-17 17:42:16 +00:00
|
|
|
size_t SBStream::GetSize() {
|
2019-03-06 00:06:00 +00:00
|
|
|
LLDB_RECORD_METHOD_NO_ARGS(size_t, SBStream, GetSize);
|
|
|
|
|
|
2019-05-23 11:14:47 +00:00
|
|
|
if (m_is_file || m_opaque_up == nullptr)
|
2012-04-03 04:14:31 +00:00
|
|
|
return 0;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2019-02-13 06:25:41 +00:00
|
|
|
return static_cast<StreamString *>(m_opaque_up.get())->GetSize();
|
2010-09-17 17:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-20 10:32:51 -07:00
|
|
|
void SBStream::Print(const char *str) {
|
|
|
|
|
LLDB_RECORD_METHOD(void, SBStream, Print, (const char *), str);
|
|
|
|
|
|
|
|
|
|
Printf("%s", str);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-17 17:42:16 +00:00
|
|
|
void SBStream::Printf(const char *format, ...) {
|
2011-12-20 00:41:28 +00:00
|
|
|
if (!format)
|
|
|
|
|
return;
|
2010-09-17 17:42:16 +00:00
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
ref().PrintfVarArg(format, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SBStream::RedirectToFile(const char *path, bool append) {
|
2019-03-06 00:06:00 +00:00
|
|
|
LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (const char *, bool), path,
|
|
|
|
|
append);
|
|
|
|
|
|
2015-01-14 18:34:35 +00:00
|
|
|
if (path == nullptr)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-09-17 17:42:16 +00:00
|
|
|
std::string local_data;
|
2019-02-13 06:25:41 +00:00
|
|
|
if (m_opaque_up) {
|
2010-09-17 17:42:16 +00: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)
|
2020-01-28 20:23:46 +01:00
|
|
|
local_data = std::string(
|
|
|
|
|
static_cast<StreamString *>(m_opaque_up.get())->GetString());
|
2016-09-06 20:57:50 +00:00
|
|
|
}
|
2019-10-14 20:15:34 +00:00
|
|
|
auto open_options = File::eOpenOptionWrite | File::eOpenOptionCanCreate;
|
2016-09-06 20:57:50 +00:00
|
|
|
if (append)
|
2014-07-30 17:38:47 +00:00
|
|
|
open_options |= File::eOpenOptionAppend;
|
2016-09-06 20:57:50 +00:00
|
|
|
else
|
2014-07-30 17:38:47 +00:00
|
|
|
open_options |= File::eOpenOptionTruncate;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2019-09-26 17:54:59 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2019-09-26 17:54:59 +00:00
|
|
|
m_opaque_up = std::make_unique<StreamFile>(std::move(file.get()));
|
|
|
|
|
m_is_file = true;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2019-09-26 17:54:59 +00:00
|
|
|
// 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());
|
2016-09-06 20:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
2010-09-17 17:42:16 +00:00
|
|
|
void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
|
2019-03-06 00:06:00 +00:00
|
|
|
LLDB_RECORD_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool), fh,
|
|
|
|
|
transfer_fh_ownership);
|
2019-10-15 17:41:40 +00:00
|
|
|
FileSP file = std::make_unique<NativeFile>(fh, transfer_fh_ownership);
|
|
|
|
|
return RedirectToFile(file);
|
|
|
|
|
}
|
2019-03-06 00:06:00 +00:00
|
|
|
|
2019-10-15 17:41:40 +00:00
|
|
|
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())
|
2016-09-06 20:57:50 +00:00
|
|
|
return;
|
|
|
|
|
|
2010-09-17 17:42:16 +00:00
|
|
|
std::string local_data;
|
2019-02-13 06:25:41 +00:00
|
|
|
if (m_opaque_up) {
|
2010-09-17 17:42:16 +00: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)
|
2020-01-28 20:23:46 +01:00
|
|
|
local_data = std::string(
|
|
|
|
|
static_cast<StreamString *>(m_opaque_up.get())->GetString());
|
2010-09-17 17:42:16 +00:00
|
|
|
}
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2019-10-15 17:41:40 +00:00
|
|
|
m_opaque_up = std::make_unique<StreamFile>(file_sp);
|
2019-09-26 17:54:59 +00:00
|
|
|
m_is_file = true;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2019-09-26 17:54:59 +00:00
|
|
|
// 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());
|
2016-09-06 20:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-09 01:08:52 +00:00
|
|
|
void SBStream::RedirectToFileDescriptor(int fd, bool transfer_fh_ownership) {
|
2019-03-06 00:06:00 +00:00
|
|
|
LLDB_RECORD_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool), fd,
|
|
|
|
|
transfer_fh_ownership);
|
|
|
|
|
|
2011-02-09 01:08:52 +00:00
|
|
|
std::string local_data;
|
2019-02-13 06:25:41 +00:00
|
|
|
if (m_opaque_up) {
|
2011-02-09 01:08:52 +00: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
|
2010-09-17 17:42:16 +00:00
|
|
|
if (!m_is_file)
|
2020-01-28 20:23:46 +01:00
|
|
|
local_data = std::string(
|
|
|
|
|
static_cast<StreamString *>(m_opaque_up.get())->GetString());
|
2016-09-06 20:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-26 17:54:59 +00:00
|
|
|
m_opaque_up = std::make_unique<StreamFile>(fd, transfer_fh_ownership);
|
|
|
|
|
m_is_file = true;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2019-09-26 17:54:59 +00:00
|
|
|
// 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());
|
2016-09-06 20:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-13 06:25:41 +00:00
|
|
|
lldb_private::Stream *SBStream::operator->() { return m_opaque_up.get(); }
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2019-02-13 06:25:41 +00:00
|
|
|
lldb_private::Stream *SBStream::get() { return m_opaque_up.get(); }
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2010-09-17 17:42:16 +00:00
|
|
|
lldb_private::Stream &SBStream::ref() {
|
2019-05-23 11:14:47 +00:00
|
|
|
if (m_opaque_up == nullptr)
|
2020-06-24 16:25:05 -07:00
|
|
|
m_opaque_up = std::make_unique<StreamString>();
|
2019-02-13 06:25:41 +00:00
|
|
|
return *m_opaque_up;
|
2016-09-06 20:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
2010-09-17 17:42:16 +00:00
|
|
|
void SBStream::Clear() {
|
2019-03-06 00:06:00 +00:00
|
|
|
LLDB_RECORD_METHOD_NO_ARGS(void, SBStream, Clear);
|
|
|
|
|
|
2019-02-13 06:25:41 +00:00
|
|
|
if (m_opaque_up) {
|
2010-09-17 17:42:16 +00: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 06:25:41 +00:00
|
|
|
m_opaque_up.reset();
|
2014-07-30 17:38:47 +00:00
|
|
|
else
|
2019-02-13 06:25:41 +00:00
|
|
|
static_cast<StreamString *>(m_opaque_up.get())->Clear();
|
2010-09-17 17:42:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-03-19 17:13:13 +00:00
|
|
|
|
|
|
|
|
namespace lldb_private {
|
|
|
|
|
namespace repro {
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void RegisterMethods<SBStream>(Registry &R) {
|
|
|
|
|
LLDB_REGISTER_CONSTRUCTOR(SBStream, ());
|
|
|
|
|
LLDB_REGISTER_METHOD_CONST(bool, SBStream, IsValid, ());
|
|
|
|
|
LLDB_REGISTER_METHOD_CONST(bool, SBStream, operator bool, ());
|
|
|
|
|
LLDB_REGISTER_METHOD(const char *, SBStream, GetData, ());
|
|
|
|
|
LLDB_REGISTER_METHOD(size_t, SBStream, GetSize, ());
|
|
|
|
|
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (const char *, bool));
|
2019-10-15 17:41:40 +00:00
|
|
|
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (FileSP));
|
|
|
|
|
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (SBFile));
|
2019-03-19 17:13:13 +00:00
|
|
|
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool));
|
|
|
|
|
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool));
|
|
|
|
|
LLDB_REGISTER_METHOD(void, SBStream, Clear, ());
|
2020-05-20 10:32:51 -07:00
|
|
|
LLDB_REGISTER_METHOD(void, SBStream, Print, (const char *));
|
2019-03-19 17:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|