Data formatter support for libc++ std::atomic<T>

On libc++ std::atomic is a fairly simple data type (layout wise, at least), wrapping actual contents in a member variable named "__a_"

All the formatters are doing is "peel away" this intermediate layer and exposing user data as direct children or values of the std::atomic root variable

Fixes rdar://24329405

llvm-svn: 260752
This commit is contained in:
Enrico Granata 2016-02-12 22:18:24 +00:00
parent 1617d5ab15
commit 75995b5e86
9 changed files with 245 additions and 5 deletions

View File

@ -745,6 +745,7 @@
942612F81B952C9B00EF842E /* ObjCLanguage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 94B6385E1B8FB7A2004FE1E4 /* ObjCLanguage.cpp */; };
942829561A89614C00521B30 /* JSON.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 942829551A89614C00521B30 /* JSON.cpp */; };
942829CC1A89839300521B30 /* liblldb-core.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 2689FFCA13353D7A00698AC0 /* liblldb-core.a */; };
9428BC2C1C6E64E4002A24D7 /* LibCxxAtomic.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9428BC291C6E64DC002A24D7 /* LibCxxAtomic.cpp */; };
94380B8219940B0A00BFE4A8 /* StringLexer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 94380B8119940B0A00BFE4A8 /* StringLexer.cpp */; };
943BDEFE1AA7B2F800789CE8 /* LLDBAssert.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 943BDEFD1AA7B2F800789CE8 /* LLDBAssert.cpp */; };
944372DC171F6B4300E57C32 /* RegisterContextDummy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 944372DA171F6B4300E57C32 /* RegisterContextDummy.cpp */; };
@ -2484,6 +2485,8 @@
942829541A89614000521B30 /* JSON.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = JSON.h; path = include/lldb/Utility/JSON.h; sourceTree = "<group>"; };
942829551A89614C00521B30 /* JSON.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = JSON.cpp; path = source/Utility/JSON.cpp; sourceTree = "<group>"; };
942829C01A89835300521B30 /* lldb-argdumper */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "lldb-argdumper"; sourceTree = BUILT_PRODUCTS_DIR; };
9428BC291C6E64DC002A24D7 /* LibCxxAtomic.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = LibCxxAtomic.cpp; path = Language/CPlusPlus/LibCxxAtomic.cpp; sourceTree = "<group>"; };
9428BC2A1C6E64DC002A24D7 /* LibCxxAtomic.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = LibCxxAtomic.h; path = Language/CPlusPlus/LibCxxAtomic.h; sourceTree = "<group>"; };
94380B8019940B0300BFE4A8 /* StringLexer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = StringLexer.h; path = include/lldb/Utility/StringLexer.h; sourceTree = "<group>"; };
94380B8119940B0A00BFE4A8 /* StringLexer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = StringLexer.cpp; path = source/Utility/StringLexer.cpp; sourceTree = "<group>"; };
943B90FC1B991586007BA499 /* VectorIterator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = VectorIterator.h; path = include/lldb/DataFormatters/VectorIterator.h; sourceTree = "<group>"; };
@ -5510,6 +5513,8 @@
945261B31B9A11E800BF138D /* CxxStringTypes.cpp */,
945261B61B9A11E800BF138D /* LibCxx.h */,
945261B51B9A11E800BF138D /* LibCxx.cpp */,
9428BC2A1C6E64DC002A24D7 /* LibCxxAtomic.h */,
9428BC291C6E64DC002A24D7 /* LibCxxAtomic.cpp */,
945261B71B9A11E800BF138D /* LibCxxInitializerList.cpp */,
945261B81B9A11E800BF138D /* LibCxxList.cpp */,
945261B91B9A11E800BF138D /* LibCxxMap.cpp */,
@ -6638,6 +6643,7 @@
3FDFE53519A29327009756A7 /* HostInfoBase.cpp in Sources */,
26474CBE18D0CB2D0073DEBA /* RegisterContextMach_i386.cpp in Sources */,
2689008613353E2200698AC0 /* Options.cpp in Sources */,
9428BC2C1C6E64E4002A24D7 /* LibCxxAtomic.cpp in Sources */,
2689008713353E2200698AC0 /* ScriptInterpreter.cpp in Sources */,
260A63191861009E00FECF8E /* IOHandler.cpp in Sources */,
2689008D13353E4200698AC0 /* DynamicLoaderMacOSXDYLD.cpp in Sources */,

View File

@ -0,0 +1,4 @@
LEVEL = ../../../../../make
CXX_SOURCES := main.cpp
CXXFLAGS += -std=c++11
include $(LEVEL)/Makefile.rules

View File

@ -0,0 +1,51 @@
"""
Test lldb data formatter subsystem.
"""
from __future__ import print_function
import os, time
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
class LibCxxAtomicTestCase(TestBase):
mydir = TestBase.compute_mydir(__file__)
def get_variable(self, name):
var = self.frame().FindVariable(name)
var.SetPreferDynamicValue(lldb.eDynamicCanRunTarget)
var.SetPreferSyntheticValue(True)
return var
@skipIfWindows # libc++ not ported to Windows yet
def test(self):
"""Test that std::atomic as defined by libc++ is correctly printed by LLDB"""
self.build()
self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
bkpt = self.target().FindBreakpointByID(lldbutil.run_break_set_by_source_regexp (self, "Set break point at this line."))
self.runCmd("run", RUN_SUCCEEDED)
lldbutil.skip_if_library_missing(self, self.target(), lldbutil.PrintableRegex("libc\+\+"))
# The stop reason of the thread should be breakpoint.
self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
substrs = ['stopped',
'stop reason = breakpoint'])
s = self.get_variable('s')
i = self.get_variable('i')
if self.TraceOn(): print(s)
if self.TraceOn(): print(i)
self.assertTrue(i.GetValueAsUnsigned(0) == 5, "i == 5")
self.assertTrue(s.GetNumChildren() == 2, "s has two children")
self.assertTrue(s.GetChildAtIndex(0).GetValueAsUnsigned(0) == 1, "s.x == 1")
self.assertTrue(s.GetChildAtIndex(1).GetValueAsUnsigned(0) == 2, "s.y == 2")

View File

@ -0,0 +1,26 @@
//===-- main.cpp --------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <atomic>
struct S {
int x = 1;
int y = 2;
};
int main ()
{
std::atomic<S> s;
s.store(S());
std::atomic<int> i;
i.store(5);
return 0; // Set break point at this line.
}

View File

@ -231,7 +231,7 @@ TypeCategoryMap::GetFormat (FormattersMatchData& match_data)
{
for (auto match : match_data.GetMatchesVector())
{
log->Printf("[CategoryMap::GetSummaryFormat] candidate match = %s %s %s %s reason = %" PRIu32,
log->Printf("[CategoryMap::GetFormat] candidate match = %s %s %s %s reason = %" PRIu32,
match.GetTypeName().GetCString(),
match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
match.DidStripReference() ? "strip-reference" : "no-strip-reference",
@ -309,7 +309,7 @@ TypeCategoryMap::GetSyntheticChildren (FormattersMatchData& match_data)
{
for (auto match : match_data.GetMatchesVector())
{
log->Printf("[CategoryMap::GetSummaryFormat] candidate match = %s %s %s %s reason = %" PRIu32,
log->Printf("[CategoryMap::GetSyntheticChildren] candidate match = %s %s %s %s reason = %" PRIu32,
match.GetTypeName().GetCString(),
match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
match.DidStripReference() ? "strip-reference" : "no-strip-reference",

View File

@ -2,6 +2,7 @@ add_lldb_library(lldbPluginCPlusPlusLanguage
CPlusPlusLanguage.cpp
CxxStringTypes.cpp
LibCxx.cpp
LibCxxAtomic.cpp
LibCxxInitializerList.cpp
LibCxxList.cpp
LibCxxMap.cpp

View File

@ -23,6 +23,7 @@
#include "CxxStringTypes.h"
#include "LibCxx.h"
#include "LibCxxAtomic.h"
#include "LibStdcpp.h"
#include <cstring>
@ -529,7 +530,8 @@ LoadLibCxxFormatters (lldb::TypeCategoryImplSP cpp_category_sp)
AddCXXSynthetic(cpp_category_sp, lldb_private::formatters::LibcxxStdMapSyntheticFrontEndCreator, "libc++ std::multimap synthetic children", ConstString("^std::__1::multimap<.+> >(( )?&)?$"), stl_synth_flags, true);
AddCXXSynthetic(cpp_category_sp, lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEndCreator, "libc++ std::unordered containers synthetic children", ConstString("^(std::__1::)unordered_(multi)?(map|set)<.+> >$"), stl_synth_flags, true);
AddCXXSynthetic(cpp_category_sp, lldb_private::formatters::LibcxxInitializerListSyntheticFrontEndCreator, "libc++ std::initializer_list synthetic children", ConstString("^std::initializer_list<.+>(( )?&)?$"), stl_synth_flags, true);
AddCXXSynthetic(cpp_category_sp, lldb_private::formatters::LibcxxAtomicSyntheticFrontEndCreator, "libc++ std::atomic synthetic children", ConstString("^std::__1::atomic<.+>$"), stl_synth_flags, true);
cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add(RegularExpressionSP(new RegularExpression("^(std::__1::)deque<.+>(( )?&)?$")),
SyntheticChildrenSP(new ScriptedSyntheticChildren(stl_synth_flags,
"lldb.formatters.cpp.libcxx.stddeque_SynthProvider")));
@ -550,6 +552,7 @@ LoadLibCxxFormatters (lldb::TypeCategoryImplSP cpp_category_sp)
AddCXXSummary(cpp_category_sp, lldb_private::formatters::LibcxxContainerSummaryProvider, "libc++ std::multiset summary provider", ConstString("^std::__1::multiset<.+>(( )?&)?$"), stl_summary_flags, true);
AddCXXSummary(cpp_category_sp, lldb_private::formatters::LibcxxContainerSummaryProvider, "libc++ std::multimap summary provider", ConstString("^std::__1::multimap<.+>(( )?&)?$"), stl_summary_flags, true);
AddCXXSummary(cpp_category_sp, lldb_private::formatters::LibcxxContainerSummaryProvider, "libc++ std::unordered containers summary provider", ConstString("^(std::__1::)unordered_(multi)?(map|set)<.+> >$"), stl_summary_flags, true);
AddCXXSummary(cpp_category_sp, lldb_private::formatters::LibCxxAtomicSummaryProvider, "libc++ std::atomic summary provider", ConstString("^std::__1::atomic<.+>$"), stl_summary_flags, true);
stl_summary_flags.SetSkipPointers(true);
@ -560,8 +563,6 @@ LoadLibCxxFormatters (lldb::TypeCategoryImplSP cpp_category_sp)
AddCXXSummary(cpp_category_sp, lldb_private::formatters::LibcxxContainerSummaryProvider, "libc++ std::vector<bool> summary provider", ConstString("std::__1::vector<bool, std::__1::allocator<bool> >"), stl_summary_flags);
AddCXXSynthetic(cpp_category_sp, lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEndCreator, "std::map iterator synthetic children", ConstString("^std::__1::__map_iterator<.+>$"), stl_synth_flags, true);
AddFilter(cpp_category_sp, {"__a_"}, "libc++ std::atomic filter", ConstString("^std::__1::atomic<.*>$"), stl_synth_flags, true);
#endif
}

View File

@ -0,0 +1,122 @@
//===-- LibCxxAtomic.cpp ------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "LibCxxAtomic.h"
#include "lldb-forward.h"
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::formatters;
bool
lldb_private::formatters::LibCxxAtomicSummaryProvider (ValueObject& valobj, Stream& stream, const TypeSummaryOptions& options)
{
static ConstString g___a_("__a_");
if (ValueObjectSP child = valobj.GetChildMemberWithName(g___a_, true))
{
std::string summary;
if (child->GetSummaryAsCString(summary, options) && summary.size() > 0)
{
stream.Printf("%s", summary.c_str());
return true;
}
}
return false;
}
namespace lldb_private {
namespace formatters {
class LibcxxStdAtomicSyntheticFrontEnd : public SyntheticChildrenFrontEnd
{
public:
LibcxxStdAtomicSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp);
~LibcxxStdAtomicSyntheticFrontEnd() override = default;
size_t
CalculateNumChildren() override;
lldb::ValueObjectSP
GetChildAtIndex(size_t idx) override;
bool
Update() override;
bool
MightHaveChildren() override;
size_t
GetIndexOfChildWithName(const ConstString &name) override;
lldb::ValueObjectSP
GetSyntheticValue () override;
private:
ValueObject *m_real_child;
};
} // namespace formatters
} // namespace lldb_private
lldb_private::formatters::LibcxxStdAtomicSyntheticFrontEnd::LibcxxStdAtomicSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
SyntheticChildrenFrontEnd(*valobj_sp),
m_real_child(nullptr)
{
}
bool
lldb_private::formatters::LibcxxStdAtomicSyntheticFrontEnd::Update()
{
static ConstString g___a_("__a_");
m_real_child = m_backend.GetChildMemberWithName(g___a_, true).get();
return false;
}
bool
lldb_private::formatters::LibcxxStdAtomicSyntheticFrontEnd::MightHaveChildren()
{
return true;
}
size_t
lldb_private::formatters::LibcxxStdAtomicSyntheticFrontEnd::CalculateNumChildren()
{
return m_real_child ? m_real_child->GetNumChildren() : 0;
}
lldb::ValueObjectSP
lldb_private::formatters::LibcxxStdAtomicSyntheticFrontEnd::GetChildAtIndex(size_t idx)
{
return m_real_child ? m_real_child->GetChildAtIndex(idx, true) : nullptr;
}
size_t
lldb_private::formatters::LibcxxStdAtomicSyntheticFrontEnd::GetIndexOfChildWithName(const ConstString &name)
{
return m_real_child ? m_real_child->GetIndexOfChildWithName(name) : UINT32_MAX;
}
lldb::ValueObjectSP
lldb_private::formatters::LibcxxStdAtomicSyntheticFrontEnd::GetSyntheticValue ()
{
if (m_real_child && m_real_child->CanProvideValue())
return m_real_child->GetSP();
return nullptr;
}
SyntheticChildrenFrontEnd*
lldb_private::formatters::LibcxxAtomicSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
{
if (valobj_sp)
return new LibcxxStdAtomicSyntheticFrontEnd(valobj_sp);
return nullptr;
}

View File

@ -0,0 +1,29 @@
//===-- LibCxxAtomic.h -------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_LibCxxAtomic_h_
#define liblldb_LibCxxAtomic_h_
#include "lldb/Core/Stream.h"
#include "lldb/Core/ValueObject.h"
#include "lldb/DataFormatters/TypeSummary.h"
#include "lldb/DataFormatters/TypeSynthetic.h"
namespace lldb_private {
namespace formatters
{
bool
LibCxxAtomicSummaryProvider (ValueObject& valobj, Stream& stream, const TypeSummaryOptions& options);
SyntheticChildrenFrontEnd* LibcxxAtomicSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP);
} // namespace formatters
} // namespace lldb_private
#endif // liblldb_LibCxxAtomic_h_