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