forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 344
[lldb/formatter] Add Swift.UnsafeBufferPointer data formatter #1276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
258 changes: 258 additions & 0 deletions
258
lldb/source/Plugins/Language/Swift/SwiftUnsafeTypes.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
#include "SwiftUnsafeTypes.h" | ||
|
||
#include "lldb/DataFormatters/TypeSynthetic.h" | ||
#include "lldb/Symbol/SwiftASTContext.h" | ||
#include "lldb/Target/SwiftLanguageRuntime.h" | ||
|
||
#include <utility> | ||
|
||
using namespace lldb; | ||
using namespace lldb_private; | ||
|
||
class SwiftUnsafeBufferPointer { | ||
public: | ||
SwiftUnsafeBufferPointer(ValueObject &valobj); | ||
size_t GetCount() const { return m_count; } | ||
addr_t GetStartAddress() const { return m_start_addr; } | ||
CompilerType GetElementType() const { return m_elem_type; } | ||
bool Update(); | ||
|
||
private: | ||
ValueObject &m_valobj; | ||
size_t m_count; | ||
addr_t m_start_addr; | ||
CompilerType m_elem_type; | ||
}; | ||
|
||
SwiftUnsafeBufferPointer::SwiftUnsafeBufferPointer(ValueObject &valobj) | ||
: m_valobj(*valobj.GetNonSyntheticValue().get()) {} | ||
|
||
bool SwiftUnsafeBufferPointer::Update() { | ||
if (!m_valobj.GetNumChildren()) | ||
return false; | ||
|
||
// Here is the layout of Swift's Unsafe[Mutable]BufferPointer. | ||
// | ||
// ▿ UnsafeBufferPointer | ||
// ▿ _position : Optional<UnsafePointer<Int>> | ||
// ▿ some : UnsafePointer<Int> | ||
// - pointerValue : Int | ||
// - count : Int | ||
// | ||
// The structure has 2 children: | ||
// 1. The buffer `count` child stored as a Swift `Int` type. This entry is a | ||
// "value-providing synthetic children", so lldb need to access to its | ||
// children in order to get the actual value. | ||
// 2. An Optional UnsafePointer to the buffer start address. To access the | ||
// pointer address, lldb unfolds every value object child until reaching | ||
// `pointerValue`. | ||
|
||
static ConstString g_count("count"); | ||
ValueObjectSP count_value_sp(m_valobj.GetChildMemberWithName(g_count, true)); | ||
if (!count_value_sp) | ||
return false; | ||
|
||
ValueObjectSP value_provided_child_sp = nullptr; | ||
|
||
// Implement Swift's 'value-providing synthetic children' workaround. | ||
// Depending on whether the value object type is a primitive or a structure, | ||
// lldb should prioritize the synthetic value children. | ||
// If it has no synthetic children then fallback to non synthetic children. | ||
ValueObjectSP synthetic = count_value_sp->GetSyntheticValue(); | ||
if (synthetic) | ||
value_provided_child_sp = synthetic->GetChildAtIndex(0, true); | ||
if (!value_provided_child_sp) | ||
value_provided_child_sp = count_value_sp->GetChildAtIndex(0, true); | ||
// If neither child exists, fail. | ||
if (!value_provided_child_sp) | ||
return false; | ||
|
||
size_t count = value_provided_child_sp->GetValueAsUnsigned(UINT64_MAX); | ||
|
||
if (count == UINT64_MAX) | ||
return false; | ||
|
||
m_count = count; | ||
|
||
static ConstString g_position("_position"); | ||
ValueObjectSP position_value_sp( | ||
m_valobj.GetChildMemberWithName(g_position, true)); | ||
if (!position_value_sp || !position_value_sp->GetNumChildren()) | ||
return false; | ||
|
||
ValueObjectSP some_value_sp = position_value_sp->GetChildAtIndex(0, true); | ||
if (!some_value_sp || !some_value_sp->GetNumChildren()) | ||
return false; | ||
|
||
CompilerType argument_type; | ||
|
||
if (CompilerType type = some_value_sp->GetCompilerType()) | ||
argument_type = SwiftASTContext::GetGenericArgumentType(type, 0); | ||
|
||
if (!argument_type.IsValid()) | ||
return nullptr; | ||
|
||
m_elem_type = argument_type; | ||
|
||
ValueObjectSP pointer_value_sp = some_value_sp->GetChildAtIndex(0, true); | ||
if (!pointer_value_sp) | ||
return false; | ||
|
||
addr_t addr = pointer_value_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS); | ||
|
||
if (!addr || addr == LLDB_INVALID_ADDRESS) | ||
return false; | ||
|
||
m_start_addr = addr; | ||
|
||
return true; | ||
} | ||
|
||
bool lldb_private::formatters::swift::UnsafeBufferPointerSummaryProvider( | ||
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) { | ||
|
||
SwiftUnsafeBufferPointer swift_ubp(valobj); | ||
|
||
if (!swift_ubp.Update()) | ||
return false; | ||
|
||
size_t count = swift_ubp.GetCount(); | ||
addr_t addr = swift_ubp.GetStartAddress(); | ||
|
||
stream.Printf("%zu %s (0x%" PRIx64 ")", count, | ||
(count == 1) ? "value" : "values", addr); | ||
|
||
return true; | ||
} | ||
|
||
namespace lldb_private { | ||
namespace formatters { | ||
namespace swift { | ||
class UnsafeBufferPointerSyntheticFrontEnd : public SyntheticChildrenFrontEnd { | ||
public: | ||
UnsafeBufferPointerSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp); | ||
|
||
virtual size_t CalculateNumChildren(); | ||
|
||
virtual lldb::ValueObjectSP GetChildAtIndex(size_t idx); | ||
|
||
virtual bool Update(); | ||
|
||
virtual bool MightHaveChildren(); | ||
|
||
virtual size_t GetIndexOfChildWithName(ConstString name); | ||
|
||
virtual ~UnsafeBufferPointerSyntheticFrontEnd() = default; | ||
|
||
private: | ||
ExecutionContextRef m_exe_ctx_ref; | ||
uint8_t m_ptr_size; | ||
lldb::ByteOrder m_order; | ||
|
||
SwiftUnsafeBufferPointer m_unsafe_ptr; | ||
size_t m_element_stride; | ||
DataBufferSP m_buffer_sp; | ||
std::vector<ValueObjectSP> m_children; | ||
}; | ||
} // namespace swift | ||
} // namespace formatters | ||
} // namespace lldb_private | ||
|
||
lldb_private::formatters::swift::UnsafeBufferPointerSyntheticFrontEnd:: | ||
UnsafeBufferPointerSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp) | ||
: SyntheticChildrenFrontEnd(*valobj_sp.get()), | ||
m_unsafe_ptr(*valobj_sp.get()) { | ||
|
||
ProcessSP process_sp = valobj_sp->GetProcessSP(); | ||
if (!process_sp) | ||
return; | ||
|
||
m_ptr_size = process_sp->GetAddressByteSize(); | ||
m_order = process_sp->GetByteOrder(); | ||
|
||
if (valobj_sp) | ||
Update(); | ||
} | ||
|
||
size_t lldb_private::formatters::swift::UnsafeBufferPointerSyntheticFrontEnd:: | ||
CalculateNumChildren() { | ||
return m_unsafe_ptr.GetCount(); | ||
} | ||
|
||
lldb::ValueObjectSP lldb_private::formatters::swift:: | ||
UnsafeBufferPointerSyntheticFrontEnd::GetChildAtIndex(size_t idx) { | ||
const size_t num_children = CalculateNumChildren(); | ||
|
||
if (idx >= num_children || idx >= m_children.size()) | ||
return lldb::ValueObjectSP(); | ||
|
||
return m_children[idx]; | ||
} | ||
|
||
bool lldb_private::formatters::swift::UnsafeBufferPointerSyntheticFrontEnd:: | ||
Update() { | ||
m_children.clear(); | ||
ValueObjectSP valobj_sp = m_backend.GetSP(); | ||
if (!valobj_sp) | ||
return false; | ||
m_exe_ctx_ref = valobj_sp->GetExecutionContextRef(); | ||
|
||
lldb::ProcessSP process_sp(valobj_sp->GetProcessSP()); | ||
if (!process_sp) | ||
return false; | ||
if (!m_unsafe_ptr.Update()) | ||
return false; | ||
|
||
const addr_t start_addr = m_unsafe_ptr.GetStartAddress(); | ||
const size_t num_children = CalculateNumChildren(); | ||
const CompilerType element_type = m_unsafe_ptr.GetElementType(); | ||
|
||
auto stride = element_type.GetByteStride(process_sp.get()); | ||
if (!stride) | ||
return false; | ||
|
||
m_element_stride = *stride; | ||
|
||
if (m_children.empty()) { | ||
size_t buffer_size = num_children * m_element_stride; | ||
m_buffer_sp.reset(new DataBufferHeap(buffer_size, 0)); | ||
|
||
Status error; | ||
size_t read_bytes = process_sp->ReadMemory( | ||
start_addr, m_buffer_sp->GetBytes(), buffer_size, error); | ||
|
||
if (!read_bytes || error.Fail()) | ||
return false; | ||
|
||
DataExtractor buffer_data(m_buffer_sp->GetBytes(), | ||
m_buffer_sp->GetByteSize(), m_order, m_ptr_size); | ||
|
||
for (size_t i = 0; i < num_children; i++) { | ||
StreamString idx_name; | ||
idx_name.Printf("[%" PRIu64 "]", i); | ||
DataExtractor data(buffer_data, i * m_element_stride, m_element_stride); | ||
m_children.push_back(CreateValueObjectFromData( | ||
idx_name.GetString(), data, m_exe_ctx_ref, element_type)); | ||
} | ||
} | ||
|
||
return m_children.size() == num_children; | ||
} | ||
|
||
bool lldb_private::formatters::swift::UnsafeBufferPointerSyntheticFrontEnd:: | ||
MightHaveChildren() { | ||
return m_unsafe_ptr.GetCount(); | ||
} | ||
|
||
size_t lldb_private::formatters::swift::UnsafeBufferPointerSyntheticFrontEnd:: | ||
GetIndexOfChildWithName(ConstString name) { | ||
return UINT32_MAX; | ||
} | ||
|
||
SyntheticChildrenFrontEnd * | ||
lldb_private::formatters::swift::UnsafeBufferPointerSyntheticFrontEndCreator( | ||
CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) { | ||
if (!valobj_sp) | ||
return nullptr; | ||
return (new UnsafeBufferPointerSyntheticFrontEnd(valobj_sp)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===-- SwiftUnsafeTypes.h --------------------------------------*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef liblldb_SwiftUnsafeTypes_h_ | ||
#define liblldb_SwiftUnsafeTypes_h_ | ||
|
||
#include "lldb/Core/ValueObject.h" | ||
#include "lldb/Utility/Stream.h" | ||
|
||
namespace lldb_private { | ||
namespace formatters { | ||
namespace swift { | ||
|
||
bool UnsafeBufferPointerSummaryProvider(ValueObject &valobj, Stream &stream, | ||
const TypeSummaryOptions &); | ||
|
||
SyntheticChildrenFrontEnd * | ||
UnsafeBufferPointerSyntheticFrontEndCreator(CXXSyntheticChildren *, | ||
lldb::ValueObjectSP); | ||
|
||
}; // namespace swift | ||
}; // namespace formatters | ||
}; // namespace lldb_private | ||
|
||
#endif |
3 changes: 3 additions & 0 deletions
3
lldb/test/API/functionalities/data-formatter/swift-unsafe/Makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SWIFT_SOURCES := main.swift | ||
|
||
include Makefile.rules |
7 changes: 7 additions & 0 deletions
7
lldb/test/API/functionalities/data-formatter/swift-unsafe/TestSwiftUnsafeTypeFormatters.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
""" | ||
Test that Swift unsafe types get formatted properly | ||
""" | ||
import lldbsuite.test.lldbinline as lldbinline | ||
from lldbsuite.test.decorators import * | ||
|
||
lldbinline.MakeInlineTest(__file__, globals(), decorators=[swiftTest]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.