[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 08:23:27 +01:00
|
|
|
//===-- ReproducerInstrumentation.cpp -------------------------------------===//
|
2019-02-05 18:46:36 +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
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "lldb/Utility/ReproducerInstrumentation.h"
|
|
|
|
|
#include "lldb/Utility/Reproducer.h"
|
2021-05-26 12:19:37 +02:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
2020-12-10 09:35:12 -08:00
|
|
|
#include <limits>
|
2020-04-20 09:37:07 -07:00
|
|
|
#include <thread>
|
2019-02-05 18:46:36 +00:00
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
using namespace lldb_private::repro;
|
|
|
|
|
|
2021-10-08 11:41:02 +03:00
|
|
|
// Whether we're currently across the API boundary.
|
|
|
|
|
static thread_local bool g_global_boundary = false;
|
|
|
|
|
|
2019-02-05 18:46:36 +00:00
|
|
|
void *IndexToObject::GetObjectForIndexImpl(unsigned idx) {
|
|
|
|
|
return m_mapping.lookup(idx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexToObject::AddObjectForIndexImpl(unsigned idx, void *object) {
|
|
|
|
|
assert(idx != 0 && "Cannot add object for sentinel");
|
|
|
|
|
m_mapping[idx] = object;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 15:42:28 -07:00
|
|
|
std::vector<void *> IndexToObject::GetAllObjects() const {
|
|
|
|
|
std::vector<std::pair<unsigned, void *>> pairs;
|
|
|
|
|
for (auto &e : m_mapping) {
|
|
|
|
|
pairs.emplace_back(e.first, e.second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort based on index.
|
|
|
|
|
std::sort(pairs.begin(), pairs.end(),
|
|
|
|
|
[](auto &lhs, auto &rhs) { return lhs.first < rhs.first; });
|
|
|
|
|
|
|
|
|
|
std::vector<void *> objects;
|
|
|
|
|
objects.reserve(pairs.size());
|
|
|
|
|
for (auto &p : pairs) {
|
|
|
|
|
objects.push_back(p.second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return objects;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 15:09:53 -08:00
|
|
|
template <> const uint8_t *Deserializer::Deserialize<const uint8_t *>() {
|
|
|
|
|
return Deserialize<uint8_t *>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 19:00:10 -08:00
|
|
|
template <> void *Deserializer::Deserialize<void *>() {
|
|
|
|
|
return const_cast<void *>(Deserialize<const void *>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <> const void *Deserializer::Deserialize<const void *>() {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 18:46:36 +00:00
|
|
|
template <> char *Deserializer::Deserialize<char *>() {
|
|
|
|
|
return const_cast<char *>(Deserialize<const char *>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <> const char *Deserializer::Deserialize<const char *>() {
|
2020-01-31 16:17:28 -08:00
|
|
|
const size_t size = Deserialize<size_t>();
|
|
|
|
|
if (size == std::numeric_limits<size_t>::max())
|
2019-02-05 18:46:36 +00:00
|
|
|
return nullptr;
|
2020-01-31 16:17:28 -08:00
|
|
|
assert(HasData(size + 1));
|
2019-02-05 18:46:36 +00:00
|
|
|
const char *str = m_buffer.data();
|
2020-01-31 16:17:28 -08:00
|
|
|
m_buffer = m_buffer.drop_front(size + 1);
|
2020-01-29 09:15:13 -08:00
|
|
|
#ifdef LLDB_REPRO_INSTR_TRACE
|
2020-01-29 21:21:02 -08:00
|
|
|
llvm::errs() << "Deserializing with " << LLVM_PRETTY_FUNCTION << " -> \""
|
|
|
|
|
<< str << "\"\n";
|
2020-01-29 09:15:13 -08:00
|
|
|
#endif
|
2019-02-05 18:46:36 +00:00
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-29 14:06:20 -08:00
|
|
|
template <> const char **Deserializer::Deserialize<const char **>() {
|
2020-01-31 16:17:28 -08:00
|
|
|
const size_t size = Deserialize<size_t>();
|
2020-01-29 16:32:41 -08:00
|
|
|
if (size == 0)
|
|
|
|
|
return nullptr;
|
2020-01-29 14:06:20 -08:00
|
|
|
const char **r =
|
|
|
|
|
reinterpret_cast<const char **>(calloc(size + 1, sizeof(char *)));
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
r[i] = Deserialize<const char *>();
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 09:35:12 -08:00
|
|
|
void Deserializer::CheckSequence(unsigned sequence) {
|
|
|
|
|
if (m_expected_sequence && *m_expected_sequence != sequence)
|
|
|
|
|
llvm::report_fatal_error(
|
|
|
|
|
"The result does not match the preceding "
|
|
|
|
|
"function. This is probably the result of concurrent "
|
|
|
|
|
"use of the SB API during capture, which is currently not "
|
|
|
|
|
"supported.");
|
|
|
|
|
m_expected_sequence.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 18:57:42 +00:00
|
|
|
bool Registry::Replay(const FileSpec &file) {
|
|
|
|
|
auto error_or_file = llvm::MemoryBuffer::getFile(file.GetPath());
|
|
|
|
|
if (auto err = error_or_file.getError())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return Replay((*error_or_file)->getBuffer());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Registry::Replay(llvm::StringRef buffer) {
|
2020-04-10 15:42:28 -07:00
|
|
|
Deserializer deserializer(buffer);
|
|
|
|
|
return Replay(deserializer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Registry::Replay(Deserializer &deserializer) {
|
2019-02-27 16:40:08 +00:00
|
|
|
#ifndef LLDB_REPRO_INSTR_TRACE
|
2019-02-06 18:57:42 +00:00
|
|
|
Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_API);
|
2019-02-27 16:40:08 +00:00
|
|
|
#endif
|
2019-02-06 18:57:42 +00:00
|
|
|
|
2020-01-22 16:15:06 -08:00
|
|
|
// Disable buffering stdout so that we approximate the way things get flushed
|
|
|
|
|
// during an interactive session.
|
|
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
|
|
|
|
|
2019-02-06 18:57:42 +00:00
|
|
|
while (deserializer.HasData(1)) {
|
2020-12-10 09:35:12 -08:00
|
|
|
unsigned sequence = deserializer.Deserialize<unsigned>();
|
2019-02-06 18:57:42 +00:00
|
|
|
unsigned id = deserializer.Deserialize<unsigned>();
|
2019-02-27 16:40:08 +00:00
|
|
|
|
|
|
|
|
#ifndef LLDB_REPRO_INSTR_TRACE
|
|
|
|
|
LLDB_LOG(log, "Replaying {0}: {1}", id, GetSignature(id));
|
|
|
|
|
#else
|
|
|
|
|
llvm::errs() << "Replaying " << id << ": " << GetSignature(id) << "\n";
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-12-10 09:35:12 -08:00
|
|
|
deserializer.SetExpectedSequence(sequence);
|
2019-02-27 16:40:08 +00:00
|
|
|
GetReplayer(id)->operator()(deserializer);
|
2019-02-06 18:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 11:01:33 -07:00
|
|
|
// Add a small artificial delay to ensure that all asynchronous events have
|
|
|
|
|
// completed before we exit.
|
2020-04-20 09:37:07 -07:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
2020-04-09 11:01:33 -07:00
|
|
|
|
2019-02-06 18:57:42 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 16:40:08 +00:00
|
|
|
void Registry::DoRegister(uintptr_t RunID, std::unique_ptr<Replayer> replayer,
|
|
|
|
|
SignatureStr signature) {
|
2019-02-06 18:57:42 +00:00
|
|
|
const unsigned id = m_replayers.size() + 1;
|
|
|
|
|
assert(m_replayers.find(RunID) == m_replayers.end());
|
|
|
|
|
m_replayers[RunID] = std::make_pair(std::move(replayer), id);
|
2019-02-27 16:40:08 +00:00
|
|
|
m_ids[id] =
|
|
|
|
|
std::make_pair(m_replayers[RunID].first.get(), std::move(signature));
|
2019-02-06 18:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned Registry::GetID(uintptr_t addr) {
|
|
|
|
|
unsigned id = m_replayers[addr].second;
|
|
|
|
|
assert(id != 0 && "Forgot to add function to registry?");
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 16:40:08 +00:00
|
|
|
std::string Registry::GetSignature(unsigned id) {
|
|
|
|
|
assert(m_ids.count(id) != 0 && "ID not in registry");
|
|
|
|
|
return m_ids[id].second.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 09:37:07 -07:00
|
|
|
void Registry::CheckID(unsigned expected, unsigned actual) {
|
|
|
|
|
if (expected != actual) {
|
|
|
|
|
llvm::errs() << "Reproducer expected signature " << expected << ": '"
|
|
|
|
|
<< GetSignature(expected) << "'\n";
|
|
|
|
|
llvm::errs() << "Reproducer actual signature " << actual << ": '"
|
|
|
|
|
<< GetSignature(actual) << "'\n";
|
|
|
|
|
llvm::report_fatal_error(
|
|
|
|
|
"Detected reproducer replay divergence. Refusing to continue.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef LLDB_REPRO_INSTR_TRACE
|
|
|
|
|
llvm::errs() << "Replaying " << actual << ": " << GetSignature(actual)
|
|
|
|
|
<< "\n";
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 16:40:08 +00:00
|
|
|
Replayer *Registry::GetReplayer(unsigned id) {
|
|
|
|
|
assert(m_ids.count(id) != 0 && "ID not in registry");
|
|
|
|
|
return m_ids[id].first;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Registry::SignatureStr::ToString() const {
|
|
|
|
|
return (result + (result.empty() ? "" : " ") + scope + "::" + name + args)
|
|
|
|
|
.str();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-07 13:51:38 +00:00
|
|
|
unsigned ObjectToIndex::GetIndexForObjectImpl(const void *object) {
|
2019-02-05 18:46:36 +00:00
|
|
|
unsigned index = m_mapping.size() + 1;
|
|
|
|
|
auto it = m_mapping.find(object);
|
|
|
|
|
if (it == m_mapping.end())
|
|
|
|
|
m_mapping[object] = index;
|
|
|
|
|
return m_mapping[object];
|
|
|
|
|
}
|
2019-02-06 18:57:42 +00:00
|
|
|
|
2020-05-27 10:27:44 -07:00
|
|
|
Recorder::Recorder()
|
|
|
|
|
: m_pretty_func(), m_pretty_args(),
|
2021-06-09 09:25:29 -07:00
|
|
|
|
2020-12-10 09:35:12 -08:00
|
|
|
m_sequence(std::numeric_limits<unsigned>::max()) {
|
2020-05-27 10:27:44 -07:00
|
|
|
if (!g_global_boundary) {
|
|
|
|
|
g_global_boundary = true;
|
|
|
|
|
m_local_boundary = true;
|
2020-12-10 09:35:12 -08:00
|
|
|
m_sequence = GetNextSequenceNumber();
|
2020-05-27 10:27:44 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 17:44:40 +00:00
|
|
|
Recorder::Recorder(llvm::StringRef pretty_func, std::string &&pretty_args)
|
|
|
|
|
: m_serializer(nullptr), m_pretty_func(pretty_func),
|
|
|
|
|
m_pretty_args(pretty_args), m_local_boundary(false),
|
2020-12-10 09:35:12 -08:00
|
|
|
m_result_recorded(true),
|
|
|
|
|
m_sequence(std::numeric_limits<unsigned>::max()) {
|
2019-02-06 18:57:42 +00:00
|
|
|
if (!g_global_boundary) {
|
|
|
|
|
g_global_boundary = true;
|
|
|
|
|
m_local_boundary = true;
|
2020-12-10 09:35:12 -08:00
|
|
|
m_sequence = GetNextSequenceNumber();
|
2019-04-23 17:44:40 +00:00
|
|
|
LLDB_LOG(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API), "{0} ({1})",
|
|
|
|
|
m_pretty_func, m_pretty_args);
|
2019-02-06 18:57:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Recorder::~Recorder() {
|
|
|
|
|
assert(m_result_recorded && "Did you forget LLDB_RECORD_RESULT?");
|
|
|
|
|
UpdateBoundary();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 09:35:12 -08:00
|
|
|
unsigned Recorder::GetSequenceNumber() const {
|
|
|
|
|
assert(m_sequence != std::numeric_limits<unsigned>::max());
|
|
|
|
|
return m_sequence;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-08 11:41:02 +03:00
|
|
|
void Recorder::PrivateThread() { g_global_boundary = true; }
|
|
|
|
|
|
|
|
|
|
void Recorder::UpdateBoundary() {
|
|
|
|
|
if (m_local_boundary)
|
|
|
|
|
g_global_boundary = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 09:37:07 -07:00
|
|
|
void InstrumentationData::Initialize(Serializer &serializer,
|
|
|
|
|
Registry ®istry) {
|
|
|
|
|
InstanceImpl().emplace(serializer, registry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InstrumentationData::Initialize(Deserializer &deserializer,
|
|
|
|
|
Registry ®istry) {
|
|
|
|
|
InstanceImpl().emplace(deserializer, registry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InstrumentationData &InstrumentationData::Instance() {
|
|
|
|
|
if (!InstanceImpl())
|
|
|
|
|
InstanceImpl().emplace();
|
|
|
|
|
return *InstanceImpl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
llvm::Optional<InstrumentationData> &InstrumentationData::InstanceImpl() {
|
|
|
|
|
static llvm::Optional<InstrumentationData> g_instrumentation_data;
|
|
|
|
|
return g_instrumentation_data;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 09:35:12 -08:00
|
|
|
std::atomic<unsigned> lldb_private::repro::Recorder::g_sequence;
|
|
|
|
|
std::mutex lldb_private::repro::Recorder::g_mutex;
|