blob: 6aeb4c46df48b6b22f810d8109c65f00eb89de32 [file] [log] [blame]
Greg Clayton05e8d192012-02-01 01:46:19 +00001//===-- ObjCLanguageRuntime.cpp ---------------------------------*- C++ -*-===//
Jim Ingham22777012010-09-23 02:01:19 +00002//
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//===----------------------------------------------------------------------===//
Jim Ingham6c68fb42010-09-30 00:54:27 +00009#include "clang/AST/Type.h"
Jim Ingham22777012010-09-23 02:01:19 +000010
Greg Claytona66c4d92013-02-13 22:56:14 +000011#include "lldb/Core/MappedHash.h"
Greg Clayton1f746072012-08-29 21:13:06 +000012#include "lldb/Core/Module.h"
Jim Ingham22777012010-09-23 02:01:19 +000013#include "lldb/Core/PluginManager.h"
Greg Clayton1b3815c2013-01-30 00:18:29 +000014#include "lldb/Core/Timer.h"
Jim Ingham6c68fb42010-09-30 00:54:27 +000015#include "lldb/Core/ValueObject.h"
16#include "lldb/Symbol/ClangASTContext.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000017#include "lldb/Symbol/SymbolContext.h"
Greg Claytonae088e52016-02-10 21:28:13 +000018#include "lldb/Symbol/SymbolFile.h"
Jim Ingham61be0902011-05-02 18:13:59 +000019#include "lldb/Symbol/Type.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Symbol/TypeList.h"
Jim Ingham5a369122010-09-28 01:25:32 +000021#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan72772842012-02-22 23:57:45 +000022#include "lldb/Target/Target.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000023#include "lldb/Utility/Log.h"
Jim Ingham22777012010-09-23 02:01:19 +000024
Greg Clayton1b3815c2013-01-30 00:18:29 +000025#include "llvm/ADT/StringRef.h"
26
Jim Ingham22777012010-09-23 02:01:19 +000027using namespace lldb;
28using namespace lldb_private;
29
30//----------------------------------------------------------------------
31// Destructor
32//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000033ObjCLanguageRuntime::~ObjCLanguageRuntime() {}
34
35ObjCLanguageRuntime::ObjCLanguageRuntime(Process *process)
36 : LanguageRuntime(process), m_impl_cache(),
37 m_has_new_literals_and_indexing(eLazyBoolCalculate),
38 m_isa_to_descriptor(), m_hash_to_isa_map(), m_type_size_cache(),
39 m_isa_to_descriptor_stop_id(UINT32_MAX), m_complete_class_cache(),
40 m_negative_complete_class_cache() {}
41
42bool ObjCLanguageRuntime::AddClass(ObjCISA isa,
43 const ClassDescriptorSP &descriptor_sp,
44 const char *class_name) {
45 if (isa != 0) {
46 m_isa_to_descriptor[isa] = descriptor_sp;
47 // class_name is assumed to be valid
48 m_hash_to_isa_map.insert(
49 std::make_pair(MappedHash::HashStringUsingDJB(class_name), isa));
50 return true;
51 }
52 return false;
Jim Ingham22777012010-09-23 02:01:19 +000053}
54
Kate Stoneb9c1b512016-09-06 20:57:50 +000055void ObjCLanguageRuntime::AddToMethodCache(lldb::addr_t class_addr,
56 lldb::addr_t selector,
57 lldb::addr_t impl_addr) {
58 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
59 if (log) {
60 log->Printf("Caching: class 0x%" PRIx64 " selector 0x%" PRIx64
61 " implementation 0x%" PRIx64 ".",
62 class_addr, selector, impl_addr);
63 }
64 m_impl_cache.insert(std::pair<ClassAndSel, lldb::addr_t>(
65 ClassAndSel(class_addr, selector), impl_addr));
Jim Ingham5a369122010-09-28 01:25:32 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068lldb::addr_t ObjCLanguageRuntime::LookupInMethodCache(lldb::addr_t class_addr,
69 lldb::addr_t selector) {
70 MsgImplMap::iterator pos, end = m_impl_cache.end();
71 pos = m_impl_cache.find(ClassAndSel(class_addr, selector));
72 if (pos != end)
73 return (*pos).second;
74 return LLDB_INVALID_ADDRESS;
75}
76
77lldb::TypeSP
78ObjCLanguageRuntime::LookupInCompleteClassCache(ConstString &name) {
79 CompleteClassMap::iterator complete_class_iter =
80 m_complete_class_cache.find(name);
81
82 if (complete_class_iter != m_complete_class_cache.end()) {
83 // Check the weak pointer to make sure the type hasn't been unloaded
84 TypeSP complete_type_sp(complete_class_iter->second.lock());
85
86 if (complete_type_sp)
87 return complete_type_sp;
88 else
89 m_complete_class_cache.erase(name);
90 }
91
92 if (m_negative_complete_class_cache.count(name) > 0)
93 return TypeSP();
94
95 const ModuleList &modules = m_process->GetTarget().GetImages();
96
97 SymbolContextList sc_list;
98 const size_t matching_symbols =
99 modules.FindSymbolsWithNameAndType(name, eSymbolTypeObjCClass, sc_list);
100
101 if (matching_symbols) {
102 SymbolContext sc;
103
104 sc_list.GetContextAtIndex(0, sc);
105
106 ModuleSP module_sp(sc.module_sp);
107
108 if (!module_sp)
109 return TypeSP();
110
111 const SymbolContext null_sc;
112 const bool exact_match = true;
113 const uint32_t max_matches = UINT32_MAX;
114 TypeList types;
115
116 llvm::DenseSet<SymbolFile *> searched_symbol_files;
117 const uint32_t num_types = module_sp->FindTypes(
118 null_sc, name, exact_match, max_matches, searched_symbol_files, types);
119
120 if (num_types) {
121 uint32_t i;
122 for (i = 0; i < num_types; ++i) {
123 TypeSP type_sp(types.GetTypeAtIndex(i));
124
125 if (ClangASTContext::IsObjCObjectOrInterfaceType(
126 type_sp->GetForwardCompilerType())) {
127 if (type_sp->IsCompleteObjCClass()) {
128 m_complete_class_cache[name] = type_sp;
129 return type_sp;
130 }
131 }
132 }
Greg Claytona66c4d92013-02-13 22:56:14 +0000133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 }
135 m_negative_complete_class_cache.insert(name);
136 return TypeSP();
137}
138
139size_t ObjCLanguageRuntime::GetByteOffsetForIvar(CompilerType &parent_qual_type,
140 const char *ivar_name) {
141 return LLDB_INVALID_IVAR_OFFSET;
142}
143
144bool ObjCLanguageRuntime::ClassDescriptor::IsPointerValid(
145 lldb::addr_t value, uint32_t ptr_size, bool allow_NULLs, bool allow_tagged,
146 bool check_version_specific) const {
147 if (!value)
148 return allow_NULLs;
149 if ((value % 2) == 1 && allow_tagged)
150 return true;
151 if ((value % ptr_size) == 0)
152 return (check_version_specific ? CheckPointer(value, ptr_size) : true);
153 else
Greg Claytona66c4d92013-02-13 22:56:14 +0000154 return false;
155}
156
Enrico Granata3467d802012-09-04 18:47:54 +0000157ObjCLanguageRuntime::ObjCISA
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158ObjCLanguageRuntime::GetISA(const ConstString &name) {
159 ISAToDescriptorIterator pos = GetDescriptorIterator(name);
160 if (pos != m_isa_to_descriptor.end())
161 return pos->first;
162 return 0;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000163}
164
Greg Claytona66c4d92013-02-13 22:56:14 +0000165ObjCLanguageRuntime::ISAToDescriptorIterator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166ObjCLanguageRuntime::GetDescriptorIterator(const ConstString &name) {
167 ISAToDescriptorIterator end = m_isa_to_descriptor.end();
Greg Claytona66c4d92013-02-13 22:56:14 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 if (name) {
170 UpdateISAToDescriptorMap();
171 if (m_hash_to_isa_map.empty()) {
172 // No name hashes were provided, we need to just linearly power through
173 // the
174 // names and find a match
175 for (ISAToDescriptorIterator pos = m_isa_to_descriptor.begin();
176 pos != end; ++pos) {
177 if (pos->second->GetClassName() == name)
178 return pos;
179 }
180 } else {
181 // Name hashes were provided, so use them to efficiently lookup name to
182 // isa/descriptor
183 const uint32_t name_hash =
184 MappedHash::HashStringUsingDJB(name.GetCString());
185 std::pair<HashToISAIterator, HashToISAIterator> range =
186 m_hash_to_isa_map.equal_range(name_hash);
187 for (HashToISAIterator range_pos = range.first; range_pos != range.second;
188 ++range_pos) {
189 ISAToDescriptorIterator pos =
190 m_isa_to_descriptor.find(range_pos->second);
191 if (pos != m_isa_to_descriptor.end()) {
192 if (pos->second->GetClassName() == name)
193 return pos;
Greg Claytona66c4d92013-02-13 22:56:14 +0000194 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 }
Greg Claytona66c4d92013-02-13 22:56:14 +0000196 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 }
198 return end;
Greg Claytona66c4d92013-02-13 22:56:14 +0000199}
200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201std::pair<ObjCLanguageRuntime::ISAToDescriptorIterator,
202 ObjCLanguageRuntime::ISAToDescriptorIterator>
203ObjCLanguageRuntime::GetDescriptorIteratorPair(bool update_if_needed) {
204 if (update_if_needed)
205 UpdateISAToDescriptorMapIfNeeded();
Enrico Granataba4b8b02015-05-06 21:01:07 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 return std::pair<ObjCLanguageRuntime::ISAToDescriptorIterator,
208 ObjCLanguageRuntime::ISAToDescriptorIterator>(
209 m_isa_to_descriptor.begin(), m_isa_to_descriptor.end());
210}
Greg Claytona66c4d92013-02-13 22:56:14 +0000211
Sean Callananbc47dfc2012-09-11 21:44:01 +0000212ObjCLanguageRuntime::ObjCISA
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213ObjCLanguageRuntime::GetParentClass(ObjCLanguageRuntime::ObjCISA isa) {
214 ClassDescriptorSP objc_class_sp(GetClassDescriptorFromISA(isa));
215 if (objc_class_sp) {
216 ClassDescriptorSP objc_super_class_sp(objc_class_sp->GetSuperclass());
217 if (objc_super_class_sp)
218 return objc_super_class_sp->GetISA();
219 }
220 return 0;
Enrico Granata3467d802012-09-04 18:47:54 +0000221}
222
Enrico Granata3467d802012-09-04 18:47:54 +0000223ConstString
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224ObjCLanguageRuntime::GetActualTypeName(ObjCLanguageRuntime::ObjCISA isa) {
225 ClassDescriptorSP objc_class_sp(GetNonKVOClassDescriptor(isa));
226 if (objc_class_sp)
227 return objc_class_sp->GetClassName();
228 return ConstString();
Enrico Granata3467d802012-09-04 18:47:54 +0000229}
Greg Clayton77fbc812012-10-09 17:51:53 +0000230
231ObjCLanguageRuntime::ClassDescriptorSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232ObjCLanguageRuntime::GetClassDescriptorFromClassName(
233 const ConstString &class_name) {
234 ISAToDescriptorIterator pos = GetDescriptorIterator(class_name);
235 if (pos != m_isa_to_descriptor.end())
236 return pos->second;
237 return ClassDescriptorSP();
238}
239
240ObjCLanguageRuntime::ClassDescriptorSP
241ObjCLanguageRuntime::GetClassDescriptor(ValueObject &valobj) {
242 ClassDescriptorSP objc_class_sp;
243 // if we get an invalid VO (which might still happen when playing around
244 // with pointers returned by the expression parser, don't consider this
245 // a valid ObjC object)
246 if (valobj.GetCompilerType().IsValid()) {
247 addr_t isa_pointer = valobj.GetPointerValue();
248 if (isa_pointer != LLDB_INVALID_ADDRESS) {
249 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
250
251 Process *process = exe_ctx.GetProcessPtr();
252 if (process) {
253 Error error;
254 ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error);
255 if (isa != LLDB_INVALID_ADDRESS)
256 objc_class_sp = GetClassDescriptorFromISA(isa);
257 }
258 }
259 }
260 return objc_class_sp;
261}
262
263ObjCLanguageRuntime::ClassDescriptorSP
264ObjCLanguageRuntime::GetNonKVOClassDescriptor(ValueObject &valobj) {
265 ObjCLanguageRuntime::ClassDescriptorSP objc_class_sp(
266 GetClassDescriptor(valobj));
267 if (objc_class_sp) {
268 if (!objc_class_sp->IsKVO())
269 return objc_class_sp;
270
271 ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass());
272 if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid())
273 return non_kvo_objc_class_sp;
274 }
275 return ClassDescriptorSP();
276}
277
278ObjCLanguageRuntime::ClassDescriptorSP
279ObjCLanguageRuntime::GetClassDescriptorFromISA(ObjCISA isa) {
280 if (isa) {
281 UpdateISAToDescriptorMap();
282 ObjCLanguageRuntime::ISAToDescriptorIterator pos =
283 m_isa_to_descriptor.find(isa);
Greg Claytona66c4d92013-02-13 22:56:14 +0000284 if (pos != m_isa_to_descriptor.end())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 return pos->second;
286 }
287 return ClassDescriptorSP();
Greg Claytonf0246d12012-10-11 18:07:21 +0000288}
289
290ObjCLanguageRuntime::ClassDescriptorSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291ObjCLanguageRuntime::GetNonKVOClassDescriptor(ObjCISA isa) {
292 if (isa) {
293 ClassDescriptorSP objc_class_sp = GetClassDescriptorFromISA(isa);
294 if (objc_class_sp && objc_class_sp->IsValid()) {
295 if (!objc_class_sp->IsKVO())
296 return objc_class_sp;
297
298 ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass());
299 if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid())
300 return non_kvo_objc_class_sp;
Greg Claytonf0246d12012-10-11 18:07:21 +0000301 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 }
303 return ClassDescriptorSP();
Greg Claytonf0246d12012-10-11 18:07:21 +0000304}
305
Greg Claytona1e5dc82015-08-11 22:53:00 +0000306CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307ObjCLanguageRuntime::EncodingToType::RealizeType(const char *name,
308 bool for_expression) {
309 if (m_scratch_ast_ctx_ap)
310 return RealizeType(*m_scratch_ast_ctx_ap, name, for_expression);
311 return CompilerType();
312}
313
314CompilerType ObjCLanguageRuntime::EncodingToType::RealizeType(
315 ClangASTContext &ast_ctx, const char *name, bool for_expression) {
316 clang::ASTContext *clang_ast = ast_ctx.getASTContext();
317 if (!clang_ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000318 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 return RealizeType(*clang_ast, name, for_expression);
Enrico Granata5d84a692014-08-19 21:46:37 +0000320}
321
322ObjCLanguageRuntime::EncodingToType::~EncodingToType() {}
323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324ObjCLanguageRuntime::EncodingToTypeSP ObjCLanguageRuntime::GetEncodingToType() {
325 return nullptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000326}
Enrico Granata3842b9f2015-01-28 00:45:42 +0000327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328bool ObjCLanguageRuntime::GetTypeBitSize(const CompilerType &compiler_type,
329 uint64_t &size) {
330 void *opaque_ptr = compiler_type.GetOpaqueQualType();
331 size = m_type_size_cache.Lookup(opaque_ptr);
332 // an ObjC object will at least have an ISA, so 0 is definitely not OK
333 if (size > 0)
334 return true;
335
336 ClassDescriptorSP class_descriptor_sp =
337 GetClassDescriptorFromClassName(compiler_type.GetTypeName());
338 if (!class_descriptor_sp)
339 return false;
340
341 int32_t max_offset = INT32_MIN;
342 uint64_t sizeof_max = 0;
343 bool found = false;
344
345 for (size_t idx = 0; idx < class_descriptor_sp->GetNumIVars(); idx++) {
346 const auto &ivar = class_descriptor_sp->GetIVarAtIndex(idx);
347 int32_t cur_offset = ivar.m_offset;
348 if (cur_offset > max_offset) {
349 max_offset = cur_offset;
350 sizeof_max = ivar.m_size;
351 found = true;
Enrico Granata3842b9f2015-01-28 00:45:42 +0000352 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 }
354
355 size = 8 * (max_offset + sizeof_max);
356 if (found)
357 m_type_size_cache.Insert(opaque_ptr, size);
358
359 return found;
Enrico Granata3842b9f2015-01-28 00:45:42 +0000360}
Jim Inghama72b31c2015-04-22 19:42:18 +0000361
362//------------------------------------------------------------------
363// Exception breakpoint Precondition class for ObjC:
364//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365void ObjCLanguageRuntime::ObjCExceptionPrecondition::AddClassName(
366 const char *class_name) {
367 m_class_names.insert(class_name);
Jim Inghama72b31c2015-04-22 19:42:18 +0000368}
369
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370ObjCLanguageRuntime::ObjCExceptionPrecondition::ObjCExceptionPrecondition() {}
371
372bool ObjCLanguageRuntime::ObjCExceptionPrecondition::EvaluatePrecondition(
373 StoppointCallbackContext &context) {
374 return true;
Jim Inghama72b31c2015-04-22 19:42:18 +0000375}
376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377void ObjCLanguageRuntime::ObjCExceptionPrecondition::GetDescription(
378 Stream &stream, lldb::DescriptionLevel level) {}
Jim Inghama72b31c2015-04-22 19:42:18 +0000379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380Error ObjCLanguageRuntime::ObjCExceptionPrecondition::ConfigurePrecondition(
381 Args &args) {
382 Error error;
383 if (args.GetArgumentCount() > 0)
384 error.SetErrorString(
385 "The ObjC Exception breakpoint doesn't support extra options.");
386 return error;
Jim Inghama72b31c2015-04-22 19:42:18 +0000387}