[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
|
|
|
//===-- LibCxxVector.cpp --------------------------------------------------===//
|
2014-10-22 20:34:38 +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
|
2014-10-22 20:34:38 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-09-04 21:01:18 +00:00
|
|
|
#include "LibCxx.h"
|
2014-10-22 20:34:38 +00:00
|
|
|
|
|
|
|
#include "lldb/Core/ValueObject.h"
|
2015-09-04 00:33:51 +00:00
|
|
|
#include "lldb/DataFormatters/FormattersHelpers.h"
|
2017-02-02 21:39:50 +00:00
|
|
|
#include "lldb/Utility/ConstString.h"
|
2014-10-22 20:34:38 +00:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
using namespace lldb_private::formatters;
|
|
|
|
|
|
|
|
namespace lldb_private {
|
|
|
|
namespace formatters {
|
|
|
|
class LibcxxStdVectorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
|
|
|
|
public:
|
|
|
|
LibcxxStdVectorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2015-10-20 01:10:59 +00:00
|
|
|
~LibcxxStdVectorSyntheticFrontEnd() override;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2015-10-20 01:10:59 +00:00
|
|
|
size_t CalculateNumChildren() override;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2015-10-20 01:10:59 +00:00
|
|
|
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2015-10-20 01:10:59 +00:00
|
|
|
bool Update() override;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2015-10-20 01:10:59 +00:00
|
|
|
bool MightHaveChildren() override;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2019-03-06 21:22:25 +00:00
|
|
|
size_t GetIndexOfChildWithName(ConstString name) override;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2014-10-22 20:34:38 +00:00
|
|
|
private:
|
2022-03-14 13:32:03 -07:00
|
|
|
ValueObject *m_start = nullptr;
|
|
|
|
ValueObject *m_finish = nullptr;
|
2015-08-11 22:53:00 +00:00
|
|
|
CompilerType m_element_type;
|
2022-03-14 13:32:03 -07:00
|
|
|
uint32_t m_element_size = 0;
|
2014-10-22 20:34:38 +00:00
|
|
|
};
|
2017-04-12 10:59:24 +00:00
|
|
|
|
|
|
|
class LibcxxVectorBoolSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
|
|
|
|
public:
|
|
|
|
LibcxxVectorBoolSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
|
|
|
|
|
|
|
|
size_t CalculateNumChildren() override;
|
|
|
|
|
|
|
|
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
|
|
|
|
|
|
|
|
bool Update() override;
|
|
|
|
|
|
|
|
bool MightHaveChildren() override { return true; }
|
|
|
|
|
2019-03-06 21:22:25 +00:00
|
|
|
size_t GetIndexOfChildWithName(ConstString name) override;
|
2017-04-12 10:59:24 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
CompilerType m_bool_type;
|
|
|
|
ExecutionContextRef m_exe_ctx_ref;
|
2022-03-14 13:32:03 -07:00
|
|
|
uint64_t m_count = 0;
|
|
|
|
lldb::addr_t m_base_data_address = 0;
|
2017-04-12 10:59:24 +00:00
|
|
|
std::map<size_t, lldb::ValueObjectSP> m_children;
|
|
|
|
};
|
|
|
|
|
2015-10-20 01:10:59 +00:00
|
|
|
} // namespace formatters
|
|
|
|
} // namespace lldb_private
|
2014-10-22 20:34:38 +00:00
|
|
|
|
|
|
|
lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::
|
|
|
|
LibcxxStdVectorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
|
2022-03-14 13:32:03 -07:00
|
|
|
: SyntheticChildrenFrontEnd(*valobj_sp), m_element_type() {
|
2014-10-22 20:34:38 +00:00
|
|
|
if (valobj_sp)
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
2015-10-20 01:10:59 +00:00
|
|
|
lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::
|
|
|
|
~LibcxxStdVectorSyntheticFrontEnd() {
|
|
|
|
// these need to stay around because they are child objects who will follow
|
|
|
|
// their parent's life cycle
|
|
|
|
// delete m_start;
|
|
|
|
// delete m_finish;
|
|
|
|
}
|
|
|
|
|
2014-10-22 20:34:38 +00:00
|
|
|
size_t lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::
|
|
|
|
CalculateNumChildren() {
|
|
|
|
if (!m_start || !m_finish)
|
|
|
|
return 0;
|
|
|
|
uint64_t start_val = m_start->GetValueAsUnsigned(0);
|
|
|
|
uint64_t finish_val = m_finish->GetValueAsUnsigned(0);
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2014-10-22 20:34:38 +00:00
|
|
|
if (start_val == 0 || finish_val == 0)
|
|
|
|
return 0;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2014-10-22 20:34:38 +00:00
|
|
|
if (start_val >= finish_val)
|
|
|
|
return 0;
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2014-10-22 20:34:38 +00:00
|
|
|
size_t num_children = (finish_val - start_val);
|
|
|
|
if (num_children % m_element_size)
|
|
|
|
return 0;
|
|
|
|
return num_children / m_element_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
lldb::ValueObjectSP
|
|
|
|
lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::GetChildAtIndex(
|
|
|
|
size_t idx) {
|
|
|
|
if (!m_start || !m_finish)
|
|
|
|
return lldb::ValueObjectSP();
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2014-10-22 20:34:38 +00:00
|
|
|
uint64_t offset = idx * m_element_size;
|
|
|
|
offset = offset + m_start->GetValueAsUnsigned(0);
|
|
|
|
StreamString name;
|
|
|
|
name.Printf("[%" PRIu64 "]", (uint64_t)idx);
|
2016-11-16 21:15:24 +00:00
|
|
|
return CreateValueObjectFromAddress(name.GetString(), offset,
|
Because of our lifetime rules w.r.t. ValueObjects and ClusterManagers, synthetic children caching is a tricky area:
- if a synthetic child comes from the same hierarchy as its parent object, then it can't be cached by SharedPointer inside the synthetic provider, or it will cause a reference loop;
- but, if a synthetic child is made from whole cloth (e.g. from an expression, a memory region, ...), then it better be cached by SharedPointer, or it will be cleared out and cause an assert() to fail if used at a later point
For most cases of self-rooted synthetic children, we have a flag we set "IsSyntheticChildrenGenerated", but we were not using it to track caching. So, what ended up happening is each provider would set up its own cache, and if it got it wrong, a hard to diagnose crash would ensue
This patch fixes that by centralizing caching in ValueObjectSynthetic - if a provider returns a self-rooted child (as per the flag), then it gets cached centrally by the ValueObject itself
This cache is used only for lifetime management and not later retrieval of child values - a different cache handles that (because we might have a mix of self-rooted and properly nested child values for the same parent, we can't trivially use this lifetime cache for retrieval)
Fixes rdar://26480007
llvm-svn: 274683
2016-07-06 21:24:28 +00:00
|
|
|
m_backend.GetExecutionContextRef(),
|
|
|
|
m_element_type);
|
2014-10-22 20:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::Update() {
|
2016-02-29 19:41:30 +00:00
|
|
|
m_start = m_finish = nullptr;
|
2014-10-22 20:34:38 +00:00
|
|
|
ValueObjectSP data_type_finder_sp(
|
|
|
|
m_backend.GetChildMemberWithName(ConstString("__end_cap_"), true));
|
|
|
|
if (!data_type_finder_sp)
|
|
|
|
return false;
|
2017-04-26 23:29:59 +00:00
|
|
|
|
|
|
|
switch (data_type_finder_sp->GetCompilerType().GetNumDirectBaseClasses()) {
|
|
|
|
case 1:
|
|
|
|
// Assume a pre llvm r300140 __compressed_pair implementation:
|
|
|
|
data_type_finder_sp = data_type_finder_sp->GetChildMemberWithName(
|
2014-10-22 20:34:38 +00:00
|
|
|
ConstString("__first_"), true);
|
2017-04-26 23:29:59 +00:00
|
|
|
break;
|
|
|
|
case 2: {
|
|
|
|
// Assume a post llvm r300140 __compressed_pair implementation:
|
|
|
|
ValueObjectSP first_elem_parent_sp =
|
|
|
|
data_type_finder_sp->GetChildAtIndex(0, true);
|
|
|
|
data_type_finder_sp = first_elem_parent_sp->GetChildMemberWithName(
|
|
|
|
ConstString("__value_"), true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-22 20:34:38 +00:00
|
|
|
if (!data_type_finder_sp)
|
|
|
|
return false;
|
2015-08-24 23:46:31 +00:00
|
|
|
m_element_type = data_type_finder_sp->GetCompilerType().GetPointeeType();
|
2019-01-15 20:33:58 +00:00
|
|
|
if (llvm::Optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) {
|
2019-01-15 18:07:52 +00:00
|
|
|
m_element_size = *size;
|
|
|
|
|
|
|
|
if (m_element_size > 0) {
|
|
|
|
// store raw pointers or end up with a circular dependency
|
|
|
|
m_start =
|
|
|
|
m_backend.GetChildMemberWithName(ConstString("__begin_"), true).get();
|
|
|
|
m_finish =
|
|
|
|
m_backend.GetChildMemberWithName(ConstString("__end_"), true).get();
|
|
|
|
}
|
2016-09-06 20:57:50 +00:00
|
|
|
}
|
2014-10-22 20:34:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::
|
|
|
|
MightHaveChildren() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::
|
2019-03-06 21:22:25 +00:00
|
|
|
GetIndexOfChildWithName(ConstString name) {
|
2014-10-22 20:34:38 +00:00
|
|
|
if (!m_start || !m_finish)
|
|
|
|
return UINT32_MAX;
|
|
|
|
return ExtractIndexFromString(name.GetCString());
|
|
|
|
}
|
|
|
|
|
2017-04-12 10:59:24 +00:00
|
|
|
lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::
|
|
|
|
LibcxxVectorBoolSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
|
|
|
|
: SyntheticChildrenFrontEnd(*valobj_sp), m_bool_type(), m_exe_ctx_ref(),
|
2022-03-14 13:32:03 -07:00
|
|
|
m_children() {
|
2017-04-12 10:59:24 +00:00
|
|
|
if (valobj_sp) {
|
|
|
|
Update();
|
|
|
|
m_bool_type =
|
|
|
|
valobj_sp->GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeBool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::
|
|
|
|
CalculateNumChildren() {
|
|
|
|
return m_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
lldb::ValueObjectSP
|
|
|
|
lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::GetChildAtIndex(
|
|
|
|
size_t idx) {
|
|
|
|
auto iter = m_children.find(idx), end = m_children.end();
|
|
|
|
if (iter != end)
|
|
|
|
return iter->second;
|
|
|
|
if (idx >= m_count)
|
2019-01-15 18:07:52 +00:00
|
|
|
return {};
|
2017-04-12 10:59:24 +00:00
|
|
|
if (m_base_data_address == 0 || m_count == 0)
|
2019-01-15 18:07:52 +00:00
|
|
|
return {};
|
2017-04-12 10:59:24 +00:00
|
|
|
if (!m_bool_type)
|
2019-01-15 18:07:52 +00:00
|
|
|
return {};
|
2017-04-12 10:59:24 +00:00
|
|
|
size_t byte_idx = (idx >> 3); // divide by 8 to get byte index
|
|
|
|
size_t bit_index = (idx & 7); // efficient idx % 8 for bit index
|
|
|
|
lldb::addr_t byte_location = m_base_data_address + byte_idx;
|
|
|
|
ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
|
|
|
|
if (!process_sp)
|
2019-01-15 18:07:52 +00:00
|
|
|
return {};
|
2017-04-12 10:59:24 +00:00
|
|
|
uint8_t byte = 0;
|
|
|
|
uint8_t mask = 0;
|
2017-05-12 04:51:55 +00:00
|
|
|
Status err;
|
2017-04-12 10:59:24 +00:00
|
|
|
size_t bytes_read = process_sp->ReadMemory(byte_location, &byte, 1, err);
|
|
|
|
if (err.Fail() || bytes_read == 0)
|
2019-01-15 18:07:52 +00:00
|
|
|
return {};
|
2017-04-12 10:59:24 +00:00
|
|
|
mask = 1 << bit_index;
|
|
|
|
bool bit_set = ((byte & mask) != 0);
|
2019-01-15 20:33:58 +00:00
|
|
|
llvm::Optional<uint64_t> size = m_bool_type.GetByteSize(nullptr);
|
2019-01-15 18:07:52 +00:00
|
|
|
if (!size)
|
|
|
|
return {};
|
|
|
|
DataBufferSP buffer_sp(new DataBufferHeap(*size, 0));
|
2017-04-12 10:59:24 +00:00
|
|
|
if (bit_set && buffer_sp && buffer_sp->GetBytes()) {
|
|
|
|
// regardless of endianness, anything non-zero is true
|
|
|
|
*(buffer_sp->GetBytes()) = 1;
|
|
|
|
}
|
|
|
|
StreamString name;
|
|
|
|
name.Printf("[%" PRIu64 "]", (uint64_t)idx);
|
|
|
|
ValueObjectSP retval_sp(CreateValueObjectFromData(
|
|
|
|
name.GetString(),
|
|
|
|
DataExtractor(buffer_sp, process_sp->GetByteOrder(),
|
|
|
|
process_sp->GetAddressByteSize()),
|
|
|
|
m_exe_ctx_ref, m_bool_type));
|
|
|
|
if (retval_sp)
|
|
|
|
m_children[idx] = retval_sp;
|
|
|
|
return retval_sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*(std::__1::vector<std::__1::allocator<bool> >) vBool = {
|
|
|
|
__begin_ = 0x00000001001000e0
|
|
|
|
__size_ = 56
|
|
|
|
__cap_alloc_ = {
|
|
|
|
std::__1::__libcpp_compressed_pair_imp<unsigned long,
|
|
|
|
std::__1::allocator<unsigned long> > = {
|
|
|
|
__first_ = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
|
|
bool lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::Update() {
|
|
|
|
m_children.clear();
|
|
|
|
ValueObjectSP valobj_sp = m_backend.GetSP();
|
|
|
|
if (!valobj_sp)
|
|
|
|
return false;
|
|
|
|
m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
|
|
|
|
ValueObjectSP size_sp(
|
|
|
|
valobj_sp->GetChildMemberWithName(ConstString("__size_"), true));
|
|
|
|
if (!size_sp)
|
|
|
|
return false;
|
|
|
|
m_count = size_sp->GetValueAsUnsigned(0);
|
|
|
|
if (!m_count)
|
|
|
|
return true;
|
|
|
|
ValueObjectSP begin_sp(
|
|
|
|
valobj_sp->GetChildMemberWithName(ConstString("__begin_"), true));
|
|
|
|
if (!begin_sp) {
|
|
|
|
m_count = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_base_data_address = begin_sp->GetValueAsUnsigned(0);
|
|
|
|
if (!m_base_data_address) {
|
|
|
|
m_count = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::
|
2019-03-06 21:22:25 +00:00
|
|
|
GetIndexOfChildWithName(ConstString name) {
|
2017-04-12 10:59:24 +00:00
|
|
|
if (!m_count || !m_base_data_address)
|
|
|
|
return UINT32_MAX;
|
|
|
|
const char *item_name = name.GetCString();
|
|
|
|
uint32_t idx = ExtractIndexFromString(item_name);
|
|
|
|
if (idx < UINT32_MAX && idx >= CalculateNumChildren())
|
|
|
|
return UINT32_MAX;
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2014-10-22 20:34:38 +00:00
|
|
|
lldb_private::SyntheticChildrenFrontEnd *
|
|
|
|
lldb_private::formatters::LibcxxStdVectorSyntheticFrontEndCreator(
|
|
|
|
CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
|
2017-04-12 10:59:24 +00:00
|
|
|
if (!valobj_sp)
|
|
|
|
return nullptr;
|
|
|
|
CompilerType type = valobj_sp->GetCompilerType();
|
|
|
|
if (!type.IsValid() || type.GetNumTemplateArguments() == 0)
|
|
|
|
return nullptr;
|
2017-11-13 14:26:21 +00:00
|
|
|
CompilerType arg_type = type.GetTypeTemplateArgument(0);
|
Allow direct comparison of ConstString against StringRef
Summary:
When we want to compare a ConstString against a string literal (or any other non-ConstString),
we currently have to explicitly turn the other string into a ConstString. This makes sense as
comparing ConstStrings against each other is only a fast pointer comparison.
However, currently we (rather incorrectly) use in several places in LLDB temporary ConstStrings when
we just want to compare a given ConstString against a hardcoded value, for example like this:
```
if (extension != ConstString(".oat") && extension != ConstString(".odex"))
```
Obviously this kind of defeats the point of ConstStrings. In the comparison above we would
construct two temporary ConstStrings every time we hit the given code. Constructing a
ConstString is relatively expensive: we need to go to the StringPool, take a read and possibly
an exclusive write-lock and then look up our temporary string in the string map of the pool.
So we do a lot of heavy work for essentially just comparing a <6 characters in two strings.
I initially wanted to just fix these issues by turning the temporary ConstString in static variables/
members, but that made the code much less readable. Instead I propose to add a new overload
for the ConstString comparison operator that takes a StringRef. This comparison operator directly
compares the ConstString content against the given StringRef without turning the StringRef into
a ConstString.
This means that the example above can look like this now:
```
if (extension != ".oat" && extension != ".odex")
```
It also no longer has to unlock/lock two locks and call multiple functions in other TUs for constructing
the temporary ConstString instances. Instead this should end up just being a direct string comparison
of the two given strings on most compilers.
This patch also directly updates all uses of temporary and short ConstStrings in LLDB to use this new
comparison operator. It also adds a some unit tests for the new and old comparison operator.
Reviewers: #lldb, JDevlieghere, espindola, amccarth
Reviewed By: JDevlieghere, amccarth
Subscribers: amccarth, clayborg, JDevlieghere, emaste, arichardson, MaskRay, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D60667
llvm-svn: 359281
2019-04-26 07:21:36 +00:00
|
|
|
if (arg_type.GetTypeName() == "bool")
|
2017-04-12 10:59:24 +00:00
|
|
|
return new LibcxxVectorBoolSyntheticFrontEnd(valobj_sp);
|
|
|
|
return new LibcxxStdVectorSyntheticFrontEnd(valobj_sp);
|
2014-10-22 20:34:38 +00:00
|
|
|
}
|