Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
#include <type_traits>
#include <utility> // for std::forward

// COMPRESSED_PAIR_REV versions:
// 0 -> Post-c88580c layout
// 1 -> Post-27c83382d83dc layout
// 2 -> Post-769c42f4a552a layout
// 3 -> Post-f5e687d7bf49c layout
// 4 -> padding-less no_unique_address-based layout (introduced in
// 27c83382d83dc)

namespace std {
namespace __lldb {

Expand All @@ -13,7 +21,50 @@ namespace __lldb {
#define _LLDB_NO_UNIQUE_ADDRESS [[__no_unique_address__]]
#endif

#if COMPRESSED_PAIR_REV == 0 // Post-c88580c layout
// From libc++ datasizeof.h
template <class _Tp> struct _FirstPaddingByte {
_LLDB_NO_UNIQUE_ADDRESS _Tp __v_;
char __first_padding_byte_;
};

template <class _Tp>
inline const size_t __datasizeof_v =
__builtin_offsetof(_FirstPaddingByte<_Tp>, __first_padding_byte_);

template <class _Tp>
struct __lldb_is_final : public integral_constant<bool, __is_final(_Tp)> {};

// The legacy layout has been patched, see
// https://github.com/llvm/llvm-project/pull/142516.
#if COMPRESSED_PAIR_REV == 1
template <class _ToPad> class __compressed_pair_padding {
char __padding_[((is_empty<_ToPad>::value &&
!__lldb_is_final<_ToPad>::value) ||
is_reference<_ToPad>::value)
? 0
: sizeof(_ToPad) - __datasizeof_v<_ToPad>];
};
#elif COMPRESSED_PAIR_REV > 1 && COMPRESSED_PAIR_REV < 4
template <class _ToPad>
inline const bool __is_reference_or_unpadded_object =
(std::is_empty<_ToPad>::value && !__lldb_is_final<_ToPad>::value) ||
sizeof(_ToPad) == __datasizeof_v<_ToPad>;

template <class _Tp>
inline const bool __is_reference_or_unpadded_object<_Tp &> = true;

template <class _Tp>
inline const bool __is_reference_or_unpadded_object<_Tp &&> = true;

template <class _ToPad, bool _Empty = __is_reference_or_unpadded_object<_ToPad>>
class __compressed_pair_padding {
char __padding_[sizeof(_ToPad) - __datasizeof_v<_ToPad>] = {};
};

template <class _ToPad> class __compressed_pair_padding<_ToPad, true> {};
#endif // COMPRESSED_PAIR_REV == 1

#if COMPRESSED_PAIR_REV == 0
struct __value_init_tag {};
struct __default_init_tag {};

Expand Down Expand Up @@ -58,28 +109,7 @@ class __compressed_pair : private __compressed_pair_elem<_T1, 0>,

_T1 &first() { return static_cast<_Base1 &>(*this).__get(); }
};
#elif COMPRESSED_PAIR_REV == 1
// From libc++ datasizeof.h
template <class _Tp> struct _FirstPaddingByte {
_LLDB_NO_UNIQUE_ADDRESS _Tp __v_;
char __first_padding_byte_;
};

template <class _Tp>
inline const size_t __datasizeof_v =
__builtin_offsetof(_FirstPaddingByte<_Tp>, __first_padding_byte_);

template <class _Tp>
struct __lldb_is_final : public integral_constant<bool, __is_final(_Tp)> {};

template <class _ToPad> class __compressed_pair_padding {
char __padding_[((is_empty<_ToPad>::value &&
!__lldb_is_final<_ToPad>::value) ||
is_reference<_ToPad>::value)
? 0
: sizeof(_ToPad) - __datasizeof_v<_ToPad>];
};

#elif COMPRESSED_PAIR_REV == 1 || COMPRESSED_PAIR_REV == 2
#define _LLDB_COMPRESSED_PAIR(T1, Initializer1, T2, Initializer2) \
[[__gnu__::__aligned__( \
alignof(T2))]] _LLDB_NO_UNIQUE_ADDRESS T1 Initializer1; \
Expand All @@ -96,7 +126,28 @@ template <class _ToPad> class __compressed_pair_padding {
_LLDB_NO_UNIQUE_ADDRESS __compressed_pair_padding<T2> __padding2_; \
_LLDB_NO_UNIQUE_ADDRESS T3 Initializer3; \
_LLDB_NO_UNIQUE_ADDRESS __compressed_pair_padding<T3> __padding3_;
#elif COMPRESSED_PAIR_REV == 2
#elif COMPRESSED_PAIR_REV == 3
#define _LLDB_COMPRESSED_PAIR(T1, Initializer1, T2, Initializer2) \
struct { \
[[__gnu__::__aligned__( \
alignof(T2))]] _LLDB_NO_UNIQUE_ADDRESS T1 Initializer1; \
_LLDB_NO_UNIQUE_ADDRESS __compressed_pair_padding<T1> __padding1_; \
_LLDB_NO_UNIQUE_ADDRESS T2 Initializer2; \
_LLDB_NO_UNIQUE_ADDRESS __compressed_pair_padding<T2> __padding2_; \
}

#define _LLDB_COMPRESSED_TRIPLE(T1, Initializer1, T2, Initializer2, T3, \
Initializer3) \
struct { \
[[using __gnu__: __aligned__(alignof(T2)), \
__aligned__(alignof(T3))]] _LLDB_NO_UNIQUE_ADDRESS T1 Initializer1; \
_LLDB_NO_UNIQUE_ADDRESS __compressed_pair_padding<T1> __padding1_; \
_LLDB_NO_UNIQUE_ADDRESS T2 Initializer2; \
_LLDB_NO_UNIQUE_ADDRESS __compressed_pair_padding<T2> __padding2_; \
_LLDB_NO_UNIQUE_ADDRESS T3 Initializer3; \
_LLDB_NO_UNIQUE_ADDRESS __compressed_pair_padding<T3> __padding3_; \
}
#elif COMPRESSED_PAIR_REV == 4
#define _LLDB_COMPRESSED_PAIR(T1, Name1, T2, Name2) \
_LLDB_NO_UNIQUE_ADDRESS T1 Name1; \
_LLDB_NO_UNIQUE_ADDRESS T2 Name2
Expand All @@ -105,7 +156,7 @@ template <class _ToPad> class __compressed_pair_padding {
_LLDB_NO_UNIQUE_ADDRESS T1 Name1; \
_LLDB_NO_UNIQUE_ADDRESS T2 Name2; \
_LLDB_NO_UNIQUE_ADDRESS T3 Name3
#endif
#endif // COMPRESSED_PAIR_REV == 3
} // namespace __lldb
} // namespace std

Expand Down
67 changes: 50 additions & 17 deletions lldb/source/Plugins/Language/CPlusPlus/LibCxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ static void consumeInlineNamespace(llvm::StringRef &name) {
}
}

bool lldb_private::formatters::isOldCompressedPairLayout(
ValueObject &pair_obj) {
return isStdTemplate(pair_obj.GetTypeName(), "__compressed_pair");
}

bool lldb_private::formatters::isStdTemplate(ConstString type_name,
llvm::StringRef type) {
llvm::StringRef name = type_name.GetStringRef();
Expand Down Expand Up @@ -105,6 +100,44 @@ lldb_private::formatters::GetSecondValueOfLibCXXCompressedPair(
return value;
}

std::pair<lldb::ValueObjectSP, bool>
lldb_private::formatters::GetValueOrOldCompressedPair(
ValueObject &obj, size_t anon_struct_idx, llvm::StringRef child_name,
llvm::StringRef compressed_pair_name) {
auto is_old_compressed_pair = [](ValueObject &pair_obj) -> bool {
return isStdTemplate(pair_obj.GetTypeName(), "__compressed_pair");
};

// Try searching the child member in an anonymous structure first.
if (auto unwrapped = obj.GetChildAtIndex(anon_struct_idx)) {
ValueObjectSP node_sp(obj.GetChildMemberWithName(child_name));
if (node_sp)
return {node_sp, is_old_compressed_pair(*node_sp)};
}

// Older versions of libc++ don't wrap the children in anonymous structures.
// Try that instead.
ValueObjectSP node_sp(obj.GetChildMemberWithName(child_name));
if (node_sp)
return {node_sp, is_old_compressed_pair(*node_sp)};

// Try the even older __compressed_pair layout.

assert(!compressed_pair_name.empty());

node_sp = obj.GetChildMemberWithName(compressed_pair_name);

// Unrecognized layout (possibly older than LLDB supports).
if (!node_sp)
return {nullptr, false};

// Expected old compressed_pair layout, but got something else.
if (!is_old_compressed_pair(*node_sp))
return {nullptr, false};

return {node_sp, true};
}

bool lldb_private::formatters::LibcxxFunctionSummaryProvider(
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {

Expand Down Expand Up @@ -205,11 +238,12 @@ bool lldb_private::formatters::LibcxxUniquePointerSummaryProvider(
if (!valobj_sp)
return false;

ValueObjectSP ptr_sp(valobj_sp->GetChildMemberWithName("__ptr_"));
auto [ptr_sp, is_compressed_pair] = GetValueOrOldCompressedPair(
*valobj_sp, /*anon_struct_idx=*/0, "__ptr_", "__ptr_");
if (!ptr_sp)
return false;

if (isOldCompressedPairLayout(*ptr_sp))
if (is_compressed_pair)
ptr_sp = GetFirstValueOfLibCXXCompressedPair(*ptr_sp);

if (!ptr_sp)
Expand Down Expand Up @@ -382,13 +416,14 @@ lldb_private::formatters::LibcxxUniquePtrSyntheticFrontEnd::Update() {
if (!valobj_sp)
return lldb::ChildCacheState::eRefetch;

ValueObjectSP ptr_sp(valobj_sp->GetChildMemberWithName("__ptr_"));
auto [ptr_sp, is_compressed_pair] = GetValueOrOldCompressedPair(
*valobj_sp, /*anon_struct_idx=*/0, "__ptr_", "__ptr_");
if (!ptr_sp)
return lldb::ChildCacheState::eRefetch;

// Retrieve the actual pointer and the deleter, and clone them to give them
// user-friendly names.
if (isOldCompressedPairLayout(*ptr_sp)) {
if (is_compressed_pair) {
if (ValueObjectSP value_pointer_sp =
GetFirstValueOfLibCXXCompressedPair(*ptr_sp))
m_value_ptr_sp = value_pointer_sp->Clone(ConstString("pointer"));
Expand Down Expand Up @@ -442,17 +477,15 @@ enum class StringLayout { CSD, DSC };
}

static ValueObjectSP ExtractLibCxxStringData(ValueObject &valobj) {
if (auto rep_sp = valobj.GetChildMemberWithName("__rep_"))
return rep_sp;

ValueObjectSP valobj_r_sp = valobj.GetChildMemberWithName("__r_");
if (!valobj_r_sp || !valobj_r_sp->GetError().Success())
auto [valobj_r_sp, is_compressed_pair] = GetValueOrOldCompressedPair(
valobj, /*anon_struct_idx=*/0, "__rep_", "__r_");
if (!valobj_r_sp)
return nullptr;

if (!isOldCompressedPairLayout(*valobj_r_sp))
return nullptr;
if (is_compressed_pair)
return GetFirstValueOfLibCXXCompressedPair(*valobj_r_sp);

return GetFirstValueOfLibCXXCompressedPair(*valobj_r_sp);
return valobj_r_sp;
}

/// Determine the size in bytes of \p valobj (a libc++ std::string object) and
Expand Down
17 changes: 16 additions & 1 deletion lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,22 @@ GetChildMemberWithName(ValueObject &obj,

lldb::ValueObjectSP GetFirstValueOfLibCXXCompressedPair(ValueObject &pair);
lldb::ValueObjectSP GetSecondValueOfLibCXXCompressedPair(ValueObject &pair);
bool isOldCompressedPairLayout(ValueObject &pair_obj);

/// Returns the ValueObjectSP of the child of \c obj. If \c obj has no
/// child named \c child_name, returns the __compressed_pair child instead
/// with \c compressed_pair_name, if one exists.
///
/// Latest libc++ wrap the compressed children in an anonymous structure.
/// The \c anon_struct_idx indicates the location of this struct.
///
/// The returned boolean is \c true if the returned child was has an old-style
/// libc++ __compressed_pair layout.
///
/// If no child was found returns a nullptr.
std::pair<lldb::ValueObjectSP, bool>
GetValueOrOldCompressedPair(ValueObject &obj, size_t anon_struct_idx,
llvm::StringRef child_name,
llvm::StringRef compressed_pair_name);
bool isStdTemplate(ConstString type_name, llvm::StringRef type);

bool LibcxxStringSummaryProviderASCII(
Expand Down
24 changes: 12 additions & 12 deletions lldb/source/Plugins/Language/CPlusPlus/LibCxxList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,18 @@ lldb::ChildCacheState ForwardListFrontEnd::Update() {
if (err.Fail() || !backend_addr)
return lldb::ChildCacheState::eRefetch;

ValueObjectSP impl_sp(m_backend.GetChildMemberWithName("__before_begin_"));
auto list_base_sp = m_backend.GetChildAtIndex(0);
if (!list_base_sp)
return lldb::ChildCacheState::eRefetch;

// Anonymous strucutre index is in base class at index 0.
auto [impl_sp, is_compressed_pair] =
GetValueOrOldCompressedPair(*list_base_sp, /*anon_struct_idx=*/0,
"__before_begin_", "__before_begin_");
if (!impl_sp)
return ChildCacheState::eRefetch;

if (isOldCompressedPairLayout(*impl_sp))
if (is_compressed_pair)
impl_sp = GetFirstValueOfLibCXXCompressedPair(*impl_sp);

if (!impl_sp)
Expand All @@ -320,17 +327,10 @@ llvm::Expected<uint32_t> ListFrontEnd::CalculateNumChildren() {
if (!m_head || !m_tail || m_node_address == 0)
return 0;

ValueObjectSP size_node_sp(m_backend.GetChildMemberWithName("__size_"));
if (!size_node_sp) {
size_node_sp = m_backend.GetChildMemberWithName(
"__size_alloc_"); // pre-compressed_pair rework

if (!isOldCompressedPairLayout(*size_node_sp))
return llvm::createStringError("Unexpected std::list layout: expected "
"old __compressed_pair layout.");

auto [size_node_sp, is_compressed_pair] = GetValueOrOldCompressedPair(
m_backend, /*anon_struct_idx=*/1, "__size_", "__size_alloc_");
if (is_compressed_pair)
size_node_sp = GetFirstValueOfLibCXXCompressedPair(*size_node_sp);
}

if (size_node_sp)
m_count = size_node_sp->GetValueAsUnsigned(UINT32_MAX);
Expand Down
29 changes: 13 additions & 16 deletions lldb/source/Plugins/Language/CPlusPlus/LibCxxMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ class LibcxxStdMapSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
size_t GetIndexOfChildWithName(ConstString name) override;

private:
llvm::Expected<uint32_t> CalculateNumChildrenForOldCompressedPairLayout();
llvm::Expected<uint32_t>
CalculateNumChildrenForOldCompressedPairLayout(ValueObject &pair);

/// Returns the ValueObject for the __tree_node type that
/// holds the key/value pair of the node at index \ref idx.
Expand Down Expand Up @@ -258,16 +259,8 @@ lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::

llvm::Expected<uint32_t>
lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
CalculateNumChildrenForOldCompressedPairLayout() {
ValueObjectSP node_sp(m_tree->GetChildMemberWithName("__pair3_"));
if (!node_sp)
return 0;

if (!isOldCompressedPairLayout(*node_sp))
return llvm::createStringError("Unexpected std::map layout: expected "
"old __compressed_pair layout.");

node_sp = GetFirstValueOfLibCXXCompressedPair(*node_sp);
CalculateNumChildrenForOldCompressedPairLayout(ValueObject &pair) {
auto node_sp = GetFirstValueOfLibCXXCompressedPair(pair);

if (!node_sp)
return 0;
Expand All @@ -285,12 +278,16 @@ llvm::Expected<uint32_t> lldb_private::formatters::
if (m_tree == nullptr)
return 0;

if (auto node_sp = m_tree->GetChildMemberWithName("__size_")) {
m_count = node_sp->GetValueAsUnsigned(0);
return m_count;
}
auto [size_sp, is_compressed_pair] = GetValueOrOldCompressedPair(
*m_tree, /*anon_struct_idx=*/2, "__size_", "__pair3_");
if (!size_sp)
return llvm::createStringError("Unexpected std::map layout");

return CalculateNumChildrenForOldCompressedPairLayout();
if (is_compressed_pair)
return CalculateNumChildrenForOldCompressedPairLayout(*size_sp);

m_count = size_sp->GetValueAsUnsigned(0);
return m_count;
}

ValueObjectSP
Expand Down
Loading