2015-10-20 09:10:59 +08:00
|
|
|
//===-- LibCxxUnorderedMap.cpp ----------------------------------*- C++ -*-===//
|
2013-09-12 08:48:47 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-10-20 09:10:59 +08:00
|
|
|
// C Includes
|
|
|
|
// C++ Includes
|
|
|
|
// Other libraries and framework includes
|
|
|
|
// Project includes
|
2015-09-05 05:01:18 +08:00
|
|
|
#include "LibCxx.h"
|
2013-09-12 08:48:47 +08:00
|
|
|
|
|
|
|
#include "lldb/Core/DataBufferHeap.h"
|
|
|
|
#include "lldb/Core/Error.h"
|
|
|
|
#include "lldb/Core/Stream.h"
|
|
|
|
#include "lldb/Core/ValueObject.h"
|
|
|
|
#include "lldb/Core/ValueObjectConstResult.h"
|
2015-09-04 08:33:51 +08:00
|
|
|
#include "lldb/DataFormatters/FormattersHelpers.h"
|
2013-09-12 08:48:47 +08:00
|
|
|
#include "lldb/Host/Endian.h"
|
|
|
|
#include "lldb/Symbol/ClangASTContext.h"
|
|
|
|
#include "lldb/Target/Target.h"
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
using namespace lldb_private::formatters;
|
|
|
|
|
2014-10-23 04:34:38 +08:00
|
|
|
namespace lldb_private {
|
|
|
|
namespace formatters {
|
|
|
|
class LibcxxStdUnorderedMapSyntheticFrontEnd : public SyntheticChildrenFrontEnd
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LibcxxStdUnorderedMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp);
|
2015-10-20 09:10:59 +08:00
|
|
|
|
|
|
|
~LibcxxStdUnorderedMapSyntheticFrontEnd() override = default;
|
|
|
|
|
|
|
|
size_t
|
|
|
|
CalculateNumChildren() override;
|
2014-10-23 04:34:38 +08:00
|
|
|
|
2015-10-20 09:10:59 +08:00
|
|
|
lldb::ValueObjectSP
|
|
|
|
GetChildAtIndex(size_t idx) override;
|
2014-10-23 04:34:38 +08:00
|
|
|
|
2015-10-20 09:10:59 +08:00
|
|
|
bool
|
|
|
|
Update() override;
|
2014-10-23 04:34:38 +08:00
|
|
|
|
2015-10-20 09:10:59 +08:00
|
|
|
bool
|
|
|
|
MightHaveChildren() override;
|
2014-10-23 04:34:38 +08:00
|
|
|
|
2015-10-20 09:10:59 +08:00
|
|
|
size_t
|
|
|
|
GetIndexOfChildWithName(const ConstString &name) override;
|
|
|
|
|
2014-10-23 04:34:38 +08:00
|
|
|
private:
|
|
|
|
ValueObject* m_tree;
|
|
|
|
size_t m_num_elements;
|
|
|
|
ValueObject* m_next_element;
|
|
|
|
std::vector<std::pair<ValueObject*, uint64_t> > m_elements_cache;
|
|
|
|
};
|
2015-10-20 09:10:59 +08:00
|
|
|
} // namespace formatters
|
|
|
|
} // namespace lldb_private
|
2014-10-23 04:34:38 +08:00
|
|
|
|
2013-09-12 08:48:47 +08:00
|
|
|
lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::LibcxxStdUnorderedMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
|
2016-03-01 03:41:30 +08:00
|
|
|
SyntheticChildrenFrontEnd(*valobj_sp),
|
|
|
|
m_tree(nullptr),
|
|
|
|
m_num_elements(0),
|
|
|
|
m_next_element(nullptr),
|
|
|
|
m_elements_cache()
|
2013-09-12 08:48:47 +08:00
|
|
|
{
|
|
|
|
if (valobj_sp)
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::CalculateNumChildren ()
|
|
|
|
{
|
|
|
|
if (m_num_elements != UINT32_MAX)
|
|
|
|
return m_num_elements;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lldb::ValueObjectSP
|
|
|
|
lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::GetChildAtIndex (size_t idx)
|
|
|
|
{
|
|
|
|
if (idx >= CalculateNumChildren())
|
|
|
|
return lldb::ValueObjectSP();
|
2016-03-01 03:41:30 +08:00
|
|
|
if (m_tree == nullptr)
|
2013-09-12 08:48:47 +08:00
|
|
|
return lldb::ValueObjectSP();
|
|
|
|
|
|
|
|
while (idx >= m_elements_cache.size())
|
|
|
|
{
|
|
|
|
if (m_next_element == nullptr)
|
|
|
|
return lldb::ValueObjectSP();
|
|
|
|
|
|
|
|
Error error;
|
|
|
|
ValueObjectSP node_sp = m_next_element->Dereference(error);
|
|
|
|
if (!node_sp || error.Fail())
|
|
|
|
return lldb::ValueObjectSP();
|
|
|
|
|
|
|
|
ValueObjectSP value_sp = node_sp->GetChildMemberWithName(ConstString("__value_"), true);
|
|
|
|
ValueObjectSP hash_sp = node_sp->GetChildMemberWithName(ConstString("__hash_"), true);
|
|
|
|
if (!hash_sp || !value_sp)
|
|
|
|
return lldb::ValueObjectSP();
|
|
|
|
m_elements_cache.push_back({value_sp.get(),hash_sp->GetValueAsUnsigned(0)});
|
|
|
|
m_next_element = node_sp->GetChildMemberWithName(ConstString("__next_"),true).get();
|
|
|
|
if (!m_next_element || m_next_element->GetValueAsUnsigned(0) == 0)
|
|
|
|
m_next_element = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<ValueObject*, uint64_t> val_hash = m_elements_cache[idx];
|
|
|
|
if (!val_hash.first)
|
|
|
|
return lldb::ValueObjectSP();
|
|
|
|
StreamString stream;
|
2014-03-03 23:39:47 +08:00
|
|
|
stream.Printf("[%" PRIu64 "]", (uint64_t)idx);
|
2013-09-12 08:48:47 +08:00
|
|
|
DataExtractor data;
|
2014-03-01 06:27:53 +08:00
|
|
|
Error error;
|
|
|
|
val_hash.first->GetData(data, error);
|
|
|
|
if (error.Fail())
|
|
|
|
return lldb::ValueObjectSP();
|
2014-01-28 07:43:24 +08:00
|
|
|
const bool thread_and_frame_only_if_stopped = true;
|
|
|
|
ExecutionContext exe_ctx = val_hash.first->GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped);
|
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-07 05:24:28 +08:00
|
|
|
return CreateValueObjectFromData(stream.GetData(),
|
|
|
|
data,
|
|
|
|
exe_ctx,
|
|
|
|
val_hash.first->GetCompilerType());
|
2013-09-12 08:48:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::Update()
|
|
|
|
{
|
|
|
|
m_num_elements = UINT32_MAX;
|
|
|
|
m_next_element = nullptr;
|
|
|
|
m_elements_cache.clear();
|
|
|
|
ValueObjectSP table_sp = m_backend.GetChildMemberWithName(ConstString("__table_"), true);
|
|
|
|
if (!table_sp)
|
|
|
|
return false;
|
|
|
|
ValueObjectSP num_elements_sp = table_sp->GetChildAtNamePath({ConstString("__p2_"),ConstString("__first_")});
|
|
|
|
if (!num_elements_sp)
|
|
|
|
return false;
|
|
|
|
m_num_elements = num_elements_sp->GetValueAsUnsigned(0);
|
|
|
|
m_tree = table_sp->GetChildAtNamePath({ConstString("__p1_"),ConstString("__first_"),ConstString("__next_")}).get();
|
|
|
|
if (m_num_elements > 0)
|
|
|
|
m_next_element = table_sp->GetChildAtNamePath({ConstString("__p1_"),ConstString("__first_"),ConstString("__next_")}).get();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::MightHaveChildren ()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
|
|
|
|
{
|
|
|
|
return ExtractIndexFromString(name.GetCString());
|
|
|
|
}
|
|
|
|
|
|
|
|
SyntheticChildrenFrontEnd*
|
|
|
|
lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
|
|
|
|
{
|
2016-03-01 03:41:30 +08:00
|
|
|
return (valobj_sp ? new LibcxxStdUnorderedMapSyntheticFrontEnd(valobj_sp) : nullptr);
|
2013-09-12 08:48:47 +08:00
|
|
|
}
|