Skip to content

[lldb] Fix issues in LanguageRuntime/Swift (NFC) #7680

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 1 commit into from
Oct 26, 2023
Merged
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
18 changes: 15 additions & 3 deletions lldb/source/Plugins/LanguageRuntime/Swift/LLDBMemoryReader.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
//===-- LLDBMemoryReader.cpp ----------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "LLDBMemoryReader.h"
#include "lldb/Core/Address.h"
#include "lldb/Core/Section.h"
Expand Down Expand Up @@ -475,8 +487,8 @@ LLDBMemoryReader::addModuleToAddressMap(ModuleSP module,
auto last_section =
section_list->GetSectionAtIndex(section_list->GetSize() - 1);

// The total size is the last section's file address plus size, subtracting the
// first section's file address.
// The total size is the last section's file address plus size, subtracting
// the first section's file address.
auto start_file_address = first_section->GetFileAddress();
uint64_t end_file_address =
last_section->GetFileAddress() + last_section->GetByteSize();
Expand Down Expand Up @@ -551,7 +563,7 @@ LLDBMemoryReader::getFileAddressAndModuleForTaggedAddress(
// when constructing the range to module map.
file_address = tagged_address - std::prev(pair_iterator)->first;

// We also need to add the module's file address, since we subtract it when
// We also need to add the module's file address, since we subtract it when
// building the range to module map.
file_address += section_list->GetSectionAtIndex(0)->GetFileAddress();
return {{file_address, module}};
Expand Down
41 changes: 24 additions & 17 deletions lldb/source/Plugins/LanguageRuntime/Swift/LLDBMemoryReader.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
//===-- LLDBMemoryReader.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_LLDBMemoryReader_h_
#define liblldb_LLDBMemoryReader_h_


#include "SwiftLanguageRuntime.h"

// We need to include ReflectionContext.h before TypeLowering.h to avoid
Expand All @@ -13,7 +23,6 @@
#include "llvm/ADT/SmallSet.h"
#include "llvm/Support/Memory.h"


namespace lldb_private {
class LLDBMemoryReader : public swift::remote::MemoryReader {
public:
Expand Down Expand Up @@ -67,19 +76,19 @@ class LLDBMemoryReader : public swift::remote::MemoryReader {
llvm::Optional<std::pair<uint64_t, lldb::ModuleSP>>
getFileAddressAndModuleForTaggedAddress(uint64_t tagged_address) const;

/// Resolves the address by either mapping a tagged address back to an LLDB
/// Address with section + offset, or, in case the address is not tagged,
/// Resolves the address by either mapping a tagged address back to an LLDB
/// Address with section + offset, or, in case the address is not tagged,
/// constructing an LLDB address with just the offset.
/// \return an Address with Section + offset if we succesfully converted a tagged
/// address back, an Address with just an offset if the address was not tagged,
/// and None if the address was tagged but we couldn't convert it back to an
/// Address.
/// \return an Address with Section + offset if we succesfully converted a
/// tagged address back, an Address with just an offset if the address was not
/// tagged, and None if the address was tagged but we couldn't convert it back
/// to an Address.
llvm::Optional<Address> resolveRemoteAddress(uint64_t address) const;

/// Reads memory from the symbol rich binary from the address into dest.
/// \return true if it was able to successfully read memory.
llvm::Optional<Address> resolveRemoteAddressFromSymbolObjectFile(uint64_t address) const;

/// Reads memory from the symbol rich binary from the address into dest.
/// \return true if it was able to successfully read memory.
llvm::Optional<Address>
resolveRemoteAddressFromSymbolObjectFile(uint64_t address) const;

private:
Process &m_process;
Expand All @@ -104,17 +113,15 @@ llvm::Optional<Address> resolveRemoteAddressFromSymbolObjectFile(uint64_t addres

/// The set of modules where we should read memory from the symbol file's
/// object file instead of the main object file.
llvm::SmallSet<lldb::ModuleSP, 8>
m_modules_with_metadata_in_symbol_obj_file;
llvm::SmallSet<lldb::ModuleSP, 8> m_modules_with_metadata_in_symbol_obj_file;

/// The bit used to tag LLDB's virtual addresses as such. See \c
/// m_range_module_map.
const static uint64_t LLDB_FILE_ADDRESS_BIT = 0x2000000000000000;
static_assert(LLDB_FILE_ADDRESS_BIT & SWIFT_ABI_X86_64_SWIFT_SPARE_BITS_MASK,
"LLDB file address bit not in spare bits mask!");
"LLDB file address bit not in spare bits mask!");
static_assert(LLDB_FILE_ADDRESS_BIT & SWIFT_ABI_ARM64_SWIFT_SPARE_BITS_MASK,
"LLDB file address bit not in spare bits mask!");

"LLDB file address bit not in spare bits mask!");
};
} // namespace lldb_private
#endif
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
//===-- ReflectionContextInterface.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_SwiftReflectionContextInterface_h_
#define liblldb_SwiftReflectionContextInterface_h_

Expand Down
38 changes: 19 additions & 19 deletions lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ static bool HasReflectionInfo(ObjectFile *obj_file) {
return hasReflectionSection;
}

ThreadSafeReflectionContext
ThreadSafeReflectionContext
SwiftLanguageRuntimeImpl::GetReflectionContext() {
m_reflection_ctx_mutex.lock();
SetupReflection();
Expand Down Expand Up @@ -502,7 +502,7 @@ void SwiftLanguageRuntimeImpl::SetupReflection() {
std::lock_guard<std::recursive_mutex> lock(m_reflection_ctx_mutex);
if (m_initialized_reflection_ctx)
return;

// The global ABI bit is read by the Swift runtime library.
SetupABIBit();

Expand Down Expand Up @@ -929,7 +929,7 @@ void SwiftLanguageRuntimeImpl::ModulesDidLoad(const ModuleList &module_list) {
m_modules_to_add.AppendIfNeeded(module_list);
}

std::string
std::string
SwiftLanguageRuntimeImpl::GetObjectDescriptionExpr_Result(ValueObject &object) {
Log *log(GetLog(LLDBLog::DataFormatters | LLDBLog::Expressions));
std::string expr_string
Expand All @@ -941,12 +941,12 @@ SwiftLanguageRuntimeImpl::GetObjectDescriptionExpr_Result(ValueObject &object) {
return expr_string;
}

std::string
std::string
SwiftLanguageRuntimeImpl::GetObjectDescriptionExpr_Ref(ValueObject &object) {
Log *log(GetLog(LLDBLog::DataFormatters | LLDBLog::Expressions));

StreamString expr_string;
std::string expr_str
std::string expr_str
= llvm::formatv("Swift._DebuggerSupport.stringForPrintObject(Swift."
"unsafeBitCast({0:x}, to: AnyObject.self))",
object.GetValueAsUnsigned(0)).str();
Expand All @@ -963,7 +963,7 @@ static const ExecutionContextRef *GetSwiftExeCtx(ValueObject &valobj) {
: nullptr;
}

std::string
std::string
SwiftLanguageRuntimeImpl::GetObjectDescriptionExpr_Copy(ValueObject &object,
lldb::addr_t &copy_location)
{
Expand All @@ -981,7 +981,7 @@ SwiftLanguageRuntimeImpl::GetObjectDescriptionExpr_Copy(ValueObject &object,
// printing, as IRGen requires a fully realized type to work on.
StackFrameSP frame_sp = object.GetFrameSP();
if (!frame_sp)
frame_sp
frame_sp
= m_process.GetThreadList().GetSelectedThread()
->GetSelectedFrame(DoNoSelectMostRelevantFrame);

Expand Down Expand Up @@ -1020,20 +1020,20 @@ SwiftLanguageRuntimeImpl::GetObjectDescriptionExpr_Copy(ValueObject &object,
return {};
}

std::string expr_string
std::string expr_string
= llvm::formatv("Swift._DebuggerSupport.stringForPrintObject(Swift."
"UnsafePointer<{0}>(bitPattern: {1:x})!.pointee)",
static_type.GetTypeName().GetCString(), copy_location).str();
if (log)
log->Printf("[GetObjectDescriptionExpr_Copy] expression: %s",
expr_string.c_str());

return expr_string;
}

bool
SwiftLanguageRuntimeImpl::RunObjectDescriptionExpr(ValueObject &object,
std::string &expr_string,
bool
SwiftLanguageRuntimeImpl::RunObjectDescriptionExpr(ValueObject &object,
std::string &expr_string,
Stream &result)
{
Log *log(GetLog(LLDBLog::DataFormatters | LLDBLog::Expressions));
Expand All @@ -1043,10 +1043,10 @@ SwiftLanguageRuntimeImpl::RunObjectDescriptionExpr(ValueObject &object,
eval_options.SetSuppressPersistentResult(true);
eval_options.SetGenerateDebugInfo(true);
eval_options.SetTimeout(m_process.GetUtilityExpressionTimeout());

StackFrameSP frame_sp = object.GetFrameSP();
if (!frame_sp)
frame_sp
frame_sp
= m_process.GetThreadList().GetSelectedThread()
->GetSelectedFrame(DoNoSelectMostRelevantFrame);
if (!frame_sp) {
Expand All @@ -1059,7 +1059,7 @@ SwiftLanguageRuntimeImpl::RunObjectDescriptionExpr(ValueObject &object,
result_sp, eval_options);

if (log) {
const char *eval_result_str
const char *eval_result_str
= m_process.ExecutionResultAsCString(eval_result);
log->Printf("[RunObjectDescriptionExpr] %s", eval_result_str);
}
Expand Down Expand Up @@ -1902,7 +1902,7 @@ void SwiftLanguageRuntimeImpl::WillStartExecutingUserExpression(
if (!ts) {
LLDB_LOG(log, "type system no longer live");
return;
}
}
ConstString BoolName("bool");
llvm::Optional<uint64_t> bool_size =
ts->GetBuiltinTypeByName(BoolName).GetByteSize(nullptr);
Expand Down Expand Up @@ -1977,7 +1977,7 @@ void SwiftLanguageRuntimeImpl::DidFinishExecutingUserExpression(
if (!ts) {
LLDB_LOG(log, "type system no longer live");
return;
}
}
ConstString BoolName("bool");
llvm::Optional<uint64_t> bool_size =
ts->GetBuiltinTypeByName(BoolName).GetByteSize(nullptr);
Expand Down Expand Up @@ -2581,7 +2581,7 @@ SwiftLanguageRuntime::GetRuntimeUnwindPlan(ProcessSP process_sp,
RegisterContext *regctx,
bool &behaves_like_zeroth_frame) {
LLDB_SCOPED_TIMER();

Target &target(process_sp->GetTarget());
auto arch = target.GetArchitecture();
llvm::Optional<AsyncUnwindRegisterNumbers> regnums =
Expand Down Expand Up @@ -2757,7 +2757,7 @@ static UnwindPlanSP
GetFollowAsyncContextUnwindPlan(RegisterContext *regctx, ArchSpec &arch,
bool &behaves_like_zeroth_frame) {
LLDB_SCOPED_TIMER();

UnwindPlan::RowSP row(new UnwindPlan::Row);
const int32_t ptr_size = 8;
row->SetOffset(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class SwiftLanguageRuntime : public LanguageRuntime {
/// Return true if name is a Swift async function, await resume partial
/// function, or suspend resume partial function symbol.
static bool IsAnySwiftAsyncFunctionSymbol(llvm::StringRef name);

/// Return the async context address using the target's specific register.
static lldb::addr_t GetAsyncContext(RegisterContext *regctx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,10 @@ class LLDBTypeInfoProvider : public swift::remote::TypeInfoProvider {
mangledName.str().c_str());
return nullptr;
}

return GetOrCreateTypeInfo(clang_type);
}

const swift::reflection::TypeInfo *
GetOrCreateTypeInfo(CompilerType clang_type) {
if (auto ti = m_runtime.lookupClangTypeInfo(clang_type))
Expand Down Expand Up @@ -1239,7 +1239,7 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Pack(
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
if (!reflection_ctx)
return false;

// Return a tuple type, with one element per pack element and its
// type has all DependentGenericParamType that appear in type packs
// substituted.
Expand Down Expand Up @@ -1269,7 +1269,7 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Pack(

Target &target = m_process.GetTarget();
size_t ptr_size = m_process.GetAddressByteSize();

swift::Demangle::Demangler dem;

auto expand_pack_type = [&](ConstString mangled_pack_type,
Expand Down Expand Up @@ -1535,7 +1535,7 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Class(
if (auto *tr = sc.get_typeref()) {
swift::Demangle::Demangler dem;
swift::Demangle::NodePointer node = tr->getDemangling(dem);
// Skip private Foundation types since it's unlikely that would be
// Skip private Foundation types since it's unlikely that would be
// useful to users.
if (IsPrivateNSClass(node))
return false;
Expand Down Expand Up @@ -2067,7 +2067,7 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_IndirectEnumCase(
lldb::addr_t box_location = m_process.ReadPointerFromMemory(box_addr, error);
if (box_location == LLDB_INVALID_ADDRESS)
return false;

box_location = MaskMaybeBridgedPointer(m_process, box_location);
lldb::addr_t box_value = box_addr + in_value.GetByteOffset();
Flags type_info(child_type.GetTypeInfo());
Expand Down Expand Up @@ -2539,7 +2539,7 @@ SwiftLanguageRuntimeImpl::GetTypeRef(CompilerType type,
{"$s14CoreFoundation7CGFloatVD", "$s12CoreGraphics7CGFloatVD"}};

auto it = known_types_with_redefined_modules.find(mangled_name);
if (it != known_types_with_redefined_modules.end())
if (it != known_types_with_redefined_modules.end())
mangled_name = it->second;

swift::Demangle::NodePointer node =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ class SwiftLanguageRuntimeImpl {
// types of swift objects.
std::string GetObjectDescriptionExpr_Result(ValueObject &object);
std::string GetObjectDescriptionExpr_Ref(ValueObject &object);
std::string GetObjectDescriptionExpr_Copy(ValueObject &object,
std::string GetObjectDescriptionExpr_Copy(ValueObject &object,
lldb::addr_t &copy_location);
bool RunObjectDescriptionExpr(ValueObject &object, std::string &expr_string,
bool RunObjectDescriptionExpr(ValueObject &object, std::string &expr_string,
Stream &result);
/// We have to load swift dependent libraries by hand, but if they
/// are missing, we shouldn't keep trying.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ std::string SwiftLanguageRuntime::DemangleSymbolAsString(
options.DisplayLocalNameContexts = false;
options.DisplayDebuggerGeneratedModule = false;
options.ShowFunctionArgumentTypes = true;
break;
break;
}

if (sc) {
Expand Down Expand Up @@ -1315,7 +1315,7 @@ SwiftLanguageRuntime::GetGenericSignature(StringRef function_name,

if (error)
return {};

// Build the maps associating value and type packs with their count
// arguments.
unsigned next_count = 0;
Expand Down
12 changes: 12 additions & 0 deletions lldb/source/Plugins/LanguageRuntime/Swift/SwiftMetadataCache.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
//===-- SwiftMetadataCache.cpp --------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "SwiftMetadataCache.h"

#include "lldb/Utility/DataEncoder.h"
Expand Down
Loading