1 //===-- NSError.cpp ---------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/AST/DeclCXX.h" 11 12 #include "Cocoa.h" 13 14 #include "lldb/Core/ValueObject.h" 15 #include "lldb/Core/ValueObjectConstResult.h" 16 #include "lldb/DataFormatters/FormattersHelpers.h" 17 #include "lldb/Symbol/ClangASTContext.h" 18 #include "lldb/Target/ObjCLanguageRuntime.h" 19 #include "lldb/Target/ProcessStructReader.h" 20 #include "lldb/Target/Target.h" 21 #include "lldb/Utility/DataBufferHeap.h" 22 #include "lldb/Utility/Endian.h" 23 #include "lldb/Utility/Status.h" 24 #include "lldb/Utility/Stream.h" 25 26 #include "Plugins/Language/ObjC/NSString.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 using namespace lldb_private::formatters; 31 32 static lldb::addr_t DerefToNSErrorPointer(ValueObject &valobj) { 33 CompilerType valobj_type(valobj.GetCompilerType()); 34 Flags type_flags(valobj_type.GetTypeInfo()); 35 if (type_flags.AllClear(eTypeHasValue)) { 36 if (valobj.IsBaseClass() && valobj.GetParent()) 37 return valobj.GetParent()->GetValueAsUnsigned(LLDB_INVALID_ADDRESS); 38 } else { 39 lldb::addr_t ptr_value = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS); 40 if (type_flags.AllSet(eTypeIsPointer)) { 41 CompilerType pointee_type(valobj_type.GetPointeeType()); 42 Flags pointee_flags(pointee_type.GetTypeInfo()); 43 if (pointee_flags.AllSet(eTypeIsPointer)) { 44 if (ProcessSP process_sp = valobj.GetProcessSP()) { 45 Status error; 46 ptr_value = process_sp->ReadPointerFromMemory(ptr_value, error); 47 } 48 } 49 } 50 return ptr_value; 51 } 52 53 return LLDB_INVALID_ADDRESS; 54 } 55 56 bool lldb_private::formatters::NSError_SummaryProvider( 57 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) { 58 ProcessSP process_sp(valobj.GetProcessSP()); 59 if (!process_sp) 60 return false; 61 62 lldb::addr_t ptr_value = DerefToNSErrorPointer(valobj); 63 if (ptr_value == LLDB_INVALID_ADDRESS) 64 return false; 65 66 size_t ptr_size = process_sp->GetAddressByteSize(); 67 lldb::addr_t code_location = ptr_value + 2 * ptr_size; 68 lldb::addr_t domain_location = ptr_value + 3 * ptr_size; 69 70 Status error; 71 uint64_t code = process_sp->ReadUnsignedIntegerFromMemory(code_location, 72 ptr_size, 0, error); 73 if (error.Fail()) 74 return false; 75 76 lldb::addr_t domain_str_value = 77 process_sp->ReadPointerFromMemory(domain_location, error); 78 if (error.Fail() || domain_str_value == LLDB_INVALID_ADDRESS) 79 return false; 80 81 if (!domain_str_value) { 82 stream.Printf("domain: nil - code: %" PRIu64, code); 83 return true; 84 } 85 86 InferiorSizedWord isw(domain_str_value, *process_sp); 87 88 ValueObjectSP domain_str_sp = ValueObject::CreateValueObjectFromData( 89 "domain_str", isw.GetAsData(process_sp->GetByteOrder()), 90 valobj.GetExecutionContextRef(), process_sp->GetTarget() 91 .GetScratchClangASTContext() 92 ->GetBasicType(lldb::eBasicTypeVoid) 93 .GetPointerType()); 94 95 if (!domain_str_sp) 96 return false; 97 98 StreamString domain_str_summary; 99 if (NSStringSummaryProvider(*domain_str_sp, domain_str_summary, options) && 100 !domain_str_summary.Empty()) { 101 stream.Printf("domain: %s - code: %" PRIu64, domain_str_summary.GetData(), 102 code); 103 return true; 104 } else { 105 stream.Printf("domain: nil - code: %" PRIu64, code); 106 return true; 107 } 108 } 109 110 class NSErrorSyntheticFrontEnd : public SyntheticChildrenFrontEnd { 111 public: 112 NSErrorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp) 113 : SyntheticChildrenFrontEnd(*valobj_sp) {} 114 115 ~NSErrorSyntheticFrontEnd() override = default; 116 // no need to delete m_child_ptr - it's kept alive by the cluster manager on 117 // our behalf 118 119 size_t CalculateNumChildren() override { 120 if (m_child_ptr) 121 return 1; 122 if (m_child_sp) 123 return 1; 124 return 0; 125 } 126 127 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override { 128 if (idx != 0) 129 return lldb::ValueObjectSP(); 130 131 if (m_child_ptr) 132 return m_child_ptr->GetSP(); 133 return m_child_sp; 134 } 135 136 bool Update() override { 137 m_child_ptr = nullptr; 138 m_child_sp.reset(); 139 140 ProcessSP process_sp(m_backend.GetProcessSP()); 141 if (!process_sp) 142 return false; 143 144 lldb::addr_t userinfo_location = DerefToNSErrorPointer(m_backend); 145 if (userinfo_location == LLDB_INVALID_ADDRESS) 146 return false; 147 148 size_t ptr_size = process_sp->GetAddressByteSize(); 149 150 userinfo_location += 4 * ptr_size; 151 Status error; 152 lldb::addr_t userinfo = 153 process_sp->ReadPointerFromMemory(userinfo_location, error); 154 if (userinfo == LLDB_INVALID_ADDRESS || error.Fail()) 155 return false; 156 InferiorSizedWord isw(userinfo, *process_sp); 157 m_child_sp = CreateValueObjectFromData( 158 "_userInfo", isw.GetAsData(process_sp->GetByteOrder()), 159 m_backend.GetExecutionContextRef(), 160 process_sp->GetTarget().GetScratchClangASTContext()->GetBasicType( 161 lldb::eBasicTypeObjCID)); 162 return false; 163 } 164 165 bool MightHaveChildren() override { return true; } 166 167 size_t GetIndexOfChildWithName(const ConstString &name) override { 168 static ConstString g___userInfo("_userInfo"); 169 if (name == g___userInfo) 170 return 0; 171 return UINT32_MAX; 172 } 173 174 private: 175 // the child here can be "real" (i.e. an actual child of the root) or 176 // synthetized from raw memory if the former, I need to store a plain pointer 177 // to it - or else a loop of references will cause this entire hierarchy of 178 // values to leak if the latter, then I need to store a SharedPointer to it - 179 // so that it only goes away when everyone else in the cluster goes away oh 180 // joy! 181 ValueObject *m_child_ptr; 182 ValueObjectSP m_child_sp; 183 }; 184 185 SyntheticChildrenFrontEnd * 186 lldb_private::formatters::NSErrorSyntheticFrontEndCreator( 187 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) { 188 lldb::ProcessSP process_sp(valobj_sp->GetProcessSP()); 189 if (!process_sp) 190 return nullptr; 191 ObjCLanguageRuntime *runtime = 192 (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime( 193 lldb::eLanguageTypeObjC); 194 if (!runtime) 195 return nullptr; 196 197 ObjCLanguageRuntime::ClassDescriptorSP descriptor( 198 runtime->GetClassDescriptor(*valobj_sp.get())); 199 200 if (!descriptor.get() || !descriptor->IsValid()) 201 return nullptr; 202 203 const char *class_name = descriptor->GetClassName().GetCString(); 204 205 if (!class_name || !*class_name) 206 return nullptr; 207 208 if (!strcmp(class_name, "NSError")) 209 return (new NSErrorSyntheticFrontEnd(valobj_sp)); 210 else if (!strcmp(class_name, "__NSCFError")) 211 return (new NSErrorSyntheticFrontEnd(valobj_sp)); 212 213 return nullptr; 214 } 215