1 //===-- ObjCLanguageRuntime.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "clang/AST/Type.h"
11 #include "lldb/Core/Log.h"
12 #include "lldb/Core/MappedHash.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/PluginManager.h"
15 #include "lldb/Core/Timer.h"
16 #include "lldb/Core/ValueObject.h"
17 #include "lldb/Symbol/ClangASTContext.h"
18 #include "lldb/Symbol/SymbolContext.h"
19 #include "lldb/Symbol/SymbolFile.h"
20 #include "lldb/Symbol/Type.h"
21 #include "lldb/Symbol/TypeList.h"
22 #include "lldb/Target/ObjCLanguageRuntime.h"
23 #include "lldb/Target/Target.h"
25 #include "llvm/ADT/StringRef.h"
28 using namespace lldb_private;
30 //----------------------------------------------------------------------
32 //----------------------------------------------------------------------
33 ObjCLanguageRuntime::~ObjCLanguageRuntime()
37 ObjCLanguageRuntime::ObjCLanguageRuntime (Process *process) :
38 LanguageRuntime (process),
40 m_has_new_literals_and_indexing (eLazyBoolCalculate),
41 m_isa_to_descriptor(),
44 m_isa_to_descriptor_stop_id (UINT32_MAX),
45 m_complete_class_cache(),
46 m_negative_complete_class_cache()
51 ObjCLanguageRuntime::AddClass (ObjCISA isa, const ClassDescriptorSP &descriptor_sp, const char *class_name)
55 m_isa_to_descriptor[isa] = descriptor_sp;
56 // class_name is assumed to be valid
57 m_hash_to_isa_map.insert(std::make_pair(MappedHash::HashStringUsingDJB(class_name), isa));
64 ObjCLanguageRuntime::AddToMethodCache (lldb::addr_t class_addr, lldb::addr_t selector, lldb::addr_t impl_addr)
66 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
69 log->Printf ("Caching: class 0x%" PRIx64 " selector 0x%" PRIx64 " implementation 0x%" PRIx64 ".", class_addr, selector, impl_addr);
71 m_impl_cache.insert (std::pair<ClassAndSel,lldb::addr_t> (ClassAndSel(class_addr, selector), impl_addr));
75 ObjCLanguageRuntime::LookupInMethodCache (lldb::addr_t class_addr, lldb::addr_t selector)
77 MsgImplMap::iterator pos, end = m_impl_cache.end();
78 pos = m_impl_cache.find (ClassAndSel(class_addr, selector));
81 return LLDB_INVALID_ADDRESS;
86 ObjCLanguageRuntime::LookupInCompleteClassCache (ConstString &name)
88 CompleteClassMap::iterator complete_class_iter = m_complete_class_cache.find(name);
90 if (complete_class_iter != m_complete_class_cache.end())
92 // Check the weak pointer to make sure the type hasn't been unloaded
93 TypeSP complete_type_sp (complete_class_iter->second.lock());
96 return complete_type_sp;
98 m_complete_class_cache.erase(name);
101 if (m_negative_complete_class_cache.count(name) > 0)
104 const ModuleList &modules = m_process->GetTarget().GetImages();
106 SymbolContextList sc_list;
107 const size_t matching_symbols = modules.FindSymbolsWithNameAndType (name,
108 eSymbolTypeObjCClass,
111 if (matching_symbols)
115 sc_list.GetContextAtIndex(0, sc);
117 ModuleSP module_sp(sc.module_sp);
122 const SymbolContext null_sc;
123 const bool exact_match = true;
124 const uint32_t max_matches = UINT32_MAX;
127 llvm::DenseSet<SymbolFile *> searched_symbol_files;
128 const uint32_t num_types = module_sp->FindTypes (null_sc,
132 searched_symbol_files,
138 for (i = 0; i < num_types; ++i)
140 TypeSP type_sp (types.GetTypeAtIndex(i));
142 if (ClangASTContext::IsObjCObjectOrInterfaceType(type_sp->GetForwardCompilerType ()))
144 if (type_sp->IsCompleteObjCClass())
146 m_complete_class_cache[name] = type_sp;
153 m_negative_complete_class_cache.insert(name);
158 ObjCLanguageRuntime::GetByteOffsetForIvar (CompilerType &parent_qual_type, const char *ivar_name)
160 return LLDB_INVALID_IVAR_OFFSET;
164 ObjCLanguageRuntime::ClassDescriptor::IsPointerValid (lldb::addr_t value,
168 bool check_version_specific) const
172 if ( (value % 2) == 1 && allow_tagged)
174 if ((value % ptr_size) == 0)
175 return (check_version_specific ? CheckPointer(value,ptr_size) : true);
180 ObjCLanguageRuntime::ObjCISA
181 ObjCLanguageRuntime::GetISA(const ConstString &name)
183 ISAToDescriptorIterator pos = GetDescriptorIterator (name);
184 if (pos != m_isa_to_descriptor.end())
189 ObjCLanguageRuntime::ISAToDescriptorIterator
190 ObjCLanguageRuntime::GetDescriptorIterator (const ConstString &name)
192 ISAToDescriptorIterator end = m_isa_to_descriptor.end();
196 UpdateISAToDescriptorMap();
197 if (m_hash_to_isa_map.empty())
199 // No name hashes were provided, we need to just linearly power through the
200 // names and find a match
201 for (ISAToDescriptorIterator pos = m_isa_to_descriptor.begin(); pos != end; ++pos)
203 if (pos->second->GetClassName() == name)
209 // Name hashes were provided, so use them to efficiently lookup name to isa/descriptor
210 const uint32_t name_hash = MappedHash::HashStringUsingDJB (name.GetCString());
211 std::pair <HashToISAIterator, HashToISAIterator> range = m_hash_to_isa_map.equal_range(name_hash);
212 for (HashToISAIterator range_pos = range.first; range_pos != range.second; ++range_pos)
214 ISAToDescriptorIterator pos = m_isa_to_descriptor.find (range_pos->second);
215 if (pos != m_isa_to_descriptor.end())
217 if (pos->second->GetClassName() == name)
226 std::pair<ObjCLanguageRuntime::ISAToDescriptorIterator,ObjCLanguageRuntime::ISAToDescriptorIterator>
227 ObjCLanguageRuntime::GetDescriptorIteratorPair (bool update_if_needed)
229 if (update_if_needed)
230 UpdateISAToDescriptorMapIfNeeded();
232 return std::pair<ObjCLanguageRuntime::ISAToDescriptorIterator,
233 ObjCLanguageRuntime::ISAToDescriptorIterator>(
234 m_isa_to_descriptor.begin(),
235 m_isa_to_descriptor.end());
239 ObjCLanguageRuntime::ObjCISA
240 ObjCLanguageRuntime::GetParentClass(ObjCLanguageRuntime::ObjCISA isa)
242 ClassDescriptorSP objc_class_sp (GetClassDescriptorFromISA(isa));
245 ClassDescriptorSP objc_super_class_sp (objc_class_sp->GetSuperclass());
246 if (objc_super_class_sp)
247 return objc_super_class_sp->GetISA();
253 ObjCLanguageRuntime::GetActualTypeName(ObjCLanguageRuntime::ObjCISA isa)
255 ClassDescriptorSP objc_class_sp (GetNonKVOClassDescriptor(isa));
257 return objc_class_sp->GetClassName();
258 return ConstString();
261 ObjCLanguageRuntime::ClassDescriptorSP
262 ObjCLanguageRuntime::GetClassDescriptorFromClassName (const ConstString &class_name)
264 ISAToDescriptorIterator pos = GetDescriptorIterator (class_name);
265 if (pos != m_isa_to_descriptor.end())
267 return ClassDescriptorSP();
271 ObjCLanguageRuntime::ClassDescriptorSP
272 ObjCLanguageRuntime::GetClassDescriptor (ValueObject& valobj)
274 ClassDescriptorSP objc_class_sp;
275 // if we get an invalid VO (which might still happen when playing around
276 // with pointers returned by the expression parser, don't consider this
277 // a valid ObjC object)
278 if (valobj.GetCompilerType().IsValid())
280 addr_t isa_pointer = valobj.GetPointerValue();
281 if (isa_pointer != LLDB_INVALID_ADDRESS)
283 ExecutionContext exe_ctx (valobj.GetExecutionContextRef());
285 Process *process = exe_ctx.GetProcessPtr();
289 ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error);
290 if (isa != LLDB_INVALID_ADDRESS)
291 objc_class_sp = GetClassDescriptorFromISA (isa);
295 return objc_class_sp;
298 ObjCLanguageRuntime::ClassDescriptorSP
299 ObjCLanguageRuntime::GetNonKVOClassDescriptor (ValueObject& valobj)
301 ObjCLanguageRuntime::ClassDescriptorSP objc_class_sp (GetClassDescriptor (valobj));
304 if (!objc_class_sp->IsKVO())
305 return objc_class_sp;
307 ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass());
308 if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid())
309 return non_kvo_objc_class_sp;
311 return ClassDescriptorSP();
315 ObjCLanguageRuntime::ClassDescriptorSP
316 ObjCLanguageRuntime::GetClassDescriptorFromISA (ObjCISA isa)
320 UpdateISAToDescriptorMap();
321 ObjCLanguageRuntime::ISAToDescriptorIterator pos = m_isa_to_descriptor.find(isa);
322 if (pos != m_isa_to_descriptor.end())
325 return ClassDescriptorSP();
328 ObjCLanguageRuntime::ClassDescriptorSP
329 ObjCLanguageRuntime::GetNonKVOClassDescriptor (ObjCISA isa)
333 ClassDescriptorSP objc_class_sp = GetClassDescriptorFromISA (isa);
334 if (objc_class_sp && objc_class_sp->IsValid())
336 if (!objc_class_sp->IsKVO())
337 return objc_class_sp;
339 ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass());
340 if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid())
341 return non_kvo_objc_class_sp;
344 return ClassDescriptorSP();
349 ObjCLanguageRuntime::EncodingToType::RealizeType (const char* name, bool for_expression)
351 if (m_scratch_ast_ctx_ap)
352 return RealizeType(*m_scratch_ast_ctx_ap, name, for_expression);
353 return CompilerType();
357 ObjCLanguageRuntime::EncodingToType::RealizeType (ClangASTContext& ast_ctx, const char* name, bool for_expression)
359 clang::ASTContext *clang_ast = ast_ctx.getASTContext();
361 return CompilerType();
362 return RealizeType(*clang_ast, name, for_expression);
365 ObjCLanguageRuntime::EncodingToType::~EncodingToType() {}
367 ObjCLanguageRuntime::EncodingToTypeSP
368 ObjCLanguageRuntime::GetEncodingToType ()
374 ObjCLanguageRuntime::GetTypeBitSize (const CompilerType& compiler_type,
377 void *opaque_ptr = compiler_type.GetOpaqueQualType();
378 size = m_type_size_cache.Lookup(opaque_ptr);
379 // an ObjC object will at least have an ISA, so 0 is definitely not OK
383 ClassDescriptorSP class_descriptor_sp = GetClassDescriptorFromClassName(compiler_type.GetTypeName());
384 if (!class_descriptor_sp)
387 int32_t max_offset = INT32_MIN;
388 uint64_t sizeof_max = 0;
392 idx < class_descriptor_sp->GetNumIVars();
395 const auto& ivar = class_descriptor_sp->GetIVarAtIndex(idx);
396 int32_t cur_offset = ivar.m_offset;
397 if (cur_offset > max_offset)
399 max_offset = cur_offset;
400 sizeof_max = ivar.m_size;
405 size = 8 * (max_offset + sizeof_max);
407 m_type_size_cache.Insert(opaque_ptr, size);
412 //------------------------------------------------------------------
413 // Exception breakpoint Precondition class for ObjC:
414 //------------------------------------------------------------------
416 ObjCLanguageRuntime::ObjCExceptionPrecondition::AddClassName(const char *class_name)
418 m_class_names.insert(class_name);
421 ObjCLanguageRuntime::ObjCExceptionPrecondition::ObjCExceptionPrecondition()
426 ObjCLanguageRuntime::ObjCExceptionPrecondition::EvaluatePrecondition(StoppointCallbackContext &context)
432 ObjCLanguageRuntime::ObjCExceptionPrecondition::GetDescription(Stream &stream, lldb::DescriptionLevel level)
437 ObjCLanguageRuntime::ObjCExceptionPrecondition::ConfigurePrecondition(Args &args)
440 if (args.GetArgumentCount() > 0)
441 error.SetErrorString("The ObjC Exception breakpoint doesn't support extra options.");