| 1 | //===-- NSException.cpp ---------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/AST/DeclCXX.h" |
| 10 | |
| 11 | #include "Cocoa.h" |
| 12 | |
| 13 | #include "lldb/DataFormatters/FormattersHelpers.h" |
| 14 | #include "lldb/Target/Target.h" |
| 15 | #include "lldb/Utility/DataBufferHeap.h" |
| 16 | #include "lldb/Utility/Endian.h" |
| 17 | #include "lldb/Utility/Status.h" |
| 18 | #include "lldb/Utility/Stream.h" |
| 19 | #include "lldb/ValueObject/ValueObject.h" |
| 20 | #include "lldb/ValueObject/ValueObjectConstResult.h" |
| 21 | |
| 22 | #include "Plugins/Language/ObjC/NSString.h" |
| 23 | #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" |
| 24 | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
| 25 | |
| 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | using namespace lldb_private::formatters; |
| 29 | |
| 30 | static bool (ValueObject &valobj, ValueObjectSP *name_sp, |
| 31 | ValueObjectSP *reason_sp, ValueObjectSP *userinfo_sp, |
| 32 | ValueObjectSP *reserved_sp) { |
| 33 | ProcessSP process_sp(valobj.GetProcessSP()); |
| 34 | if (!process_sp) |
| 35 | return false; |
| 36 | |
| 37 | lldb::addr_t ptr = LLDB_INVALID_ADDRESS; |
| 38 | |
| 39 | CompilerType valobj_type(valobj.GetCompilerType()); |
| 40 | Flags type_flags(valobj_type.GetTypeInfo()); |
| 41 | if (type_flags.AllClear(mask: eTypeHasValue)) { |
| 42 | if (valobj.IsBaseClass() && valobj.GetParent()) |
| 43 | ptr = valobj.GetParent()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS); |
| 44 | } else { |
| 45 | ptr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS); |
| 46 | } |
| 47 | |
| 48 | if (ptr == LLDB_INVALID_ADDRESS) |
| 49 | return false; |
| 50 | size_t ptr_size = process_sp->GetAddressByteSize(); |
| 51 | |
| 52 | Status error; |
| 53 | auto name = process_sp->ReadPointerFromMemory(vm_addr: ptr + 1 * ptr_size, error); |
| 54 | if (error.Fail() || name == LLDB_INVALID_ADDRESS) |
| 55 | return false; |
| 56 | auto reason = process_sp->ReadPointerFromMemory(vm_addr: ptr + 2 * ptr_size, error); |
| 57 | if (error.Fail() || reason == LLDB_INVALID_ADDRESS) |
| 58 | return false; |
| 59 | auto userinfo = process_sp->ReadPointerFromMemory(vm_addr: ptr + 3 * ptr_size, error); |
| 60 | if (error.Fail() || userinfo == LLDB_INVALID_ADDRESS) |
| 61 | return false; |
| 62 | auto reserved = process_sp->ReadPointerFromMemory(vm_addr: ptr + 4 * ptr_size, error); |
| 63 | if (error.Fail() || reserved == LLDB_INVALID_ADDRESS) |
| 64 | return false; |
| 65 | |
| 66 | InferiorSizedWord name_isw(name, *process_sp); |
| 67 | InferiorSizedWord reason_isw(reason, *process_sp); |
| 68 | InferiorSizedWord userinfo_isw(userinfo, *process_sp); |
| 69 | InferiorSizedWord reserved_isw(reserved, *process_sp); |
| 70 | |
| 71 | TypeSystemClangSP scratch_ts_sp = |
| 72 | ScratchTypeSystemClang::GetForTarget(target&: process_sp->GetTarget()); |
| 73 | if (!scratch_ts_sp) |
| 74 | return false; |
| 75 | |
| 76 | CompilerType voidstar = |
| 77 | scratch_ts_sp->GetBasicType(type: lldb::eBasicTypeVoid).GetPointerType(); |
| 78 | |
| 79 | if (name_sp) |
| 80 | *name_sp = ValueObject::CreateValueObjectFromData( |
| 81 | name: "name" , data: name_isw.GetAsData(byte_order: process_sp->GetByteOrder()), |
| 82 | exe_ctx: valobj.GetExecutionContextRef(), type: voidstar); |
| 83 | if (reason_sp) |
| 84 | *reason_sp = ValueObject::CreateValueObjectFromData( |
| 85 | name: "reason" , data: reason_isw.GetAsData(byte_order: process_sp->GetByteOrder()), |
| 86 | exe_ctx: valobj.GetExecutionContextRef(), type: voidstar); |
| 87 | if (userinfo_sp) |
| 88 | *userinfo_sp = ValueObject::CreateValueObjectFromData( |
| 89 | name: "userInfo" , data: userinfo_isw.GetAsData(byte_order: process_sp->GetByteOrder()), |
| 90 | exe_ctx: valobj.GetExecutionContextRef(), type: voidstar); |
| 91 | if (reserved_sp) |
| 92 | *reserved_sp = ValueObject::CreateValueObjectFromData( |
| 93 | name: "reserved" , data: reserved_isw.GetAsData(byte_order: process_sp->GetByteOrder()), |
| 94 | exe_ctx: valobj.GetExecutionContextRef(), type: voidstar); |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | bool lldb_private::formatters::NSException_SummaryProvider( |
| 100 | ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) { |
| 101 | lldb::ValueObjectSP reason_sp; |
| 102 | if (!ExtractFields(valobj, name_sp: nullptr, reason_sp: &reason_sp, userinfo_sp: nullptr, reserved_sp: nullptr)) |
| 103 | return false; |
| 104 | |
| 105 | if (!reason_sp) { |
| 106 | stream.Printf(format: "No reason" ); |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | StreamString reason_str_summary; |
| 111 | if (NSStringSummaryProvider(valobj&: *reason_sp, stream&: reason_str_summary, options) && |
| 112 | !reason_str_summary.Empty()) { |
| 113 | stream.Printf(format: "%s" , reason_str_summary.GetData()); |
| 114 | return true; |
| 115 | } else |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | class NSExceptionSyntheticFrontEnd : public SyntheticChildrenFrontEnd { |
| 120 | public: |
| 121 | NSExceptionSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp) |
| 122 | : SyntheticChildrenFrontEnd(*valobj_sp) {} |
| 123 | |
| 124 | ~NSExceptionSyntheticFrontEnd() override = default; |
| 125 | |
| 126 | llvm::Expected<uint32_t> CalculateNumChildren() override { return 4; } |
| 127 | |
| 128 | lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override { |
| 129 | switch (idx) { |
| 130 | case 0: return m_name_sp; |
| 131 | case 1: return m_reason_sp; |
| 132 | case 2: return m_userinfo_sp; |
| 133 | case 3: return m_reserved_sp; |
| 134 | } |
| 135 | return lldb::ValueObjectSP(); |
| 136 | } |
| 137 | |
| 138 | lldb::ChildCacheState Update() override { |
| 139 | m_name_sp.reset(); |
| 140 | m_reason_sp.reset(); |
| 141 | m_userinfo_sp.reset(); |
| 142 | m_reserved_sp.reset(); |
| 143 | |
| 144 | const auto ret = ExtractFields(valobj&: m_backend, name_sp: &m_name_sp, reason_sp: &m_reason_sp, |
| 145 | userinfo_sp: &m_userinfo_sp, reserved_sp: &m_reserved_sp); |
| 146 | |
| 147 | return ret ? lldb::ChildCacheState::eReuse |
| 148 | : lldb::ChildCacheState::eRefetch; |
| 149 | } |
| 150 | |
| 151 | llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override { |
| 152 | // NSException has 4 members: |
| 153 | // NSString *name; |
| 154 | // NSString *reason; |
| 155 | // NSDictionary *userInfo; |
| 156 | // id reserved; |
| 157 | static ConstString g_name("name" ); |
| 158 | static ConstString g_reason("reason" ); |
| 159 | static ConstString g_userInfo("userInfo" ); |
| 160 | static ConstString g_reserved("reserved" ); |
| 161 | if (name == g_name) return 0; |
| 162 | if (name == g_reason) return 1; |
| 163 | if (name == g_userInfo) return 2; |
| 164 | if (name == g_reserved) return 3; |
| 165 | return llvm::createStringError(Fmt: "Type has no child named '%s'" , |
| 166 | Vals: name.AsCString()); |
| 167 | } |
| 168 | |
| 169 | private: |
| 170 | ValueObjectSP m_name_sp; |
| 171 | ValueObjectSP m_reason_sp; |
| 172 | ValueObjectSP m_userinfo_sp; |
| 173 | ValueObjectSP m_reserved_sp; |
| 174 | }; |
| 175 | |
| 176 | SyntheticChildrenFrontEnd * |
| 177 | lldb_private::formatters::NSExceptionSyntheticFrontEndCreator( |
| 178 | CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) { |
| 179 | lldb::ProcessSP process_sp(valobj_sp->GetProcessSP()); |
| 180 | if (!process_sp) |
| 181 | return nullptr; |
| 182 | ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(process&: *process_sp); |
| 183 | if (!runtime) |
| 184 | return nullptr; |
| 185 | |
| 186 | ObjCLanguageRuntime::ClassDescriptorSP descriptor( |
| 187 | runtime->GetClassDescriptor(in_value&: *valobj_sp.get())); |
| 188 | |
| 189 | if (!descriptor.get() || !descriptor->IsValid()) |
| 190 | return nullptr; |
| 191 | |
| 192 | const char *class_name = descriptor->GetClassName().GetCString(); |
| 193 | |
| 194 | if (!class_name || !*class_name) |
| 195 | return nullptr; |
| 196 | |
| 197 | if (!strcmp(s1: class_name, s2: "NSException" )) |
| 198 | return (new NSExceptionSyntheticFrontEnd(valobj_sp)); |
| 199 | else if (!strcmp(s1: class_name, s2: "NSCFException" )) |
| 200 | return (new NSExceptionSyntheticFrontEnd(valobj_sp)); |
| 201 | else if (!strcmp(s1: class_name, s2: "__NSCFException" )) |
| 202 | return (new NSExceptionSyntheticFrontEnd(valobj_sp)); |
| 203 | |
| 204 | return nullptr; |
| 205 | } |
| 206 | |