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"
|
|
|
|
|
2019-03-06 08:06:00 +08:00
|
|
|
#include "SBReproducerPrivate.h"
|
2010-09-18 01:42:16 +08:00
|
|
|
#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-03-06 08:06:00 +08:00
|
|
|
SBStream::SBStream() : m_opaque_up(new StreamString()), m_is_file(false) {
|
|
|
|
LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStream);
|
|
|
|
}
|
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-03-06 08:06:00 +08:00
|
|
|
bool SBStream::IsValid() const {
|
|
|
|
LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, IsValid);
|
Add "operator bool" to SB APIs
Summary:
Our python version of the SB API has (the python equivalent of)
operator bool, but the C++ version doesn't.
This is because our python operators are added by modify-python-lldb.py,
which performs postprocessing on the swig-generated interface files.
In this patch, I add the "operator bool" to all SB classes which have an
IsValid method (which is the same logic used by modify-python-lldb.py).
This way, we make the two interfaces more constent, and it allows us to
rely on swig's automatic syntesis of python __nonzero__ methods instead
of doing manual fixups.
Reviewers: zturner, jingham, clayborg, jfb, serge-sans-paille
Subscribers: jdoerfert, lldb-commits
Differential Revision: https://reviews.llvm.org/D58792
llvm-svn: 355824
2019-03-11 21:58:46 +08:00
|
|
|
return this->operator bool();
|
|
|
|
}
|
|
|
|
SBStream::operator bool() const {
|
|
|
|
LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStream, operator bool);
|
2019-03-06 08:06:00 +08:00
|
|
|
|
|
|
|
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-03-06 08:06:00 +08:00
|
|
|
LLDB_RECORD_METHOD_NO_ARGS(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-03-06 08:06:00 +08:00
|
|
|
LLDB_RECORD_METHOD_NO_ARGS(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) {
|
2019-03-06 08:06:00 +08:00
|
|
|
LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (const char *, bool), path,
|
|
|
|
append);
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
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) {
|
2019-03-06 08:06:00 +08:00
|
|
|
LLDB_RECORD_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool), fh,
|
|
|
|
transfer_fh_ownership);
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
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) {
|
2019-03-06 08:06:00 +08:00
|
|
|
LLDB_RECORD_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool), fd,
|
|
|
|
transfer_fh_ownership);
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
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-03-06 08:06:00 +08:00
|
|
|
LLDB_RECORD_METHOD_NO_ARGS(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
|
|
|
}
|