LLDB mainline
ObjCLanguageRuntime.cpp
Go to the documentation of this file.
1//===-- ObjCLanguageRuntime.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#include "clang/AST/Type.h"
9
10#include "ObjCLanguageRuntime.h"
11
14#include "lldb/Core/Module.h"
19#include "lldb/Symbol/Type.h"
22#include "lldb/Target/ABI.h"
23#include "lldb/Target/Target.h"
25#include "lldb/Utility/Log.h"
26#include "lldb/Utility/Timer.h"
27
28#include "llvm/ADT/StringRef.h"
29#include "llvm/Support/DJB.h"
30#include <optional>
31
32using namespace lldb;
33using namespace lldb_private;
34
36
37// Destructor
39
41 : LanguageRuntime(process), m_impl_cache(), m_impl_str_cache(),
42 m_has_new_literals_and_indexing(eLazyBoolCalculate),
43 m_isa_to_descriptor(), m_hash_to_isa_map(), m_type_size_cache(),
44 m_isa_to_descriptor_stop_id(UINT32_MAX), m_complete_class_cache(),
45 m_negative_complete_class_cache() {}
46
48 static ConstString g_self = ConstString("self");
49 static ConstString g_cmd = ConstString("_cmd");
50 return name == g_self || name == g_cmd;
51}
52
54 const ClassDescriptorSP &descriptor_sp,
55 const char *class_name) {
56 if (isa != 0) {
57 m_isa_to_descriptor[isa] = descriptor_sp;
58 // class_name is assumed to be valid
59 m_hash_to_isa_map.insert(std::make_pair(llvm::djbHash(class_name), isa));
60 return true;
61 }
62 return false;
63}
64
66 lldb::addr_t selector,
67 lldb::addr_t impl_addr) {
68 Log *log = GetLog(LLDBLog::Step);
69 if (log) {
70 LLDB_LOGF(log,
71 "Caching: class 0x%" PRIx64 " selector 0x%" PRIx64
72 " implementation 0x%" PRIx64 ".",
73 class_addr, selector, impl_addr);
74 }
75 m_impl_cache.insert(std::pair<ClassAndSel, lldb::addr_t>(
76 ClassAndSel(class_addr, selector), impl_addr));
77}
78
80 llvm::StringRef sel_str,
81 lldb::addr_t impl_addr) {
82 Log *log = GetLog(LLDBLog::Step);
83
84 LLDB_LOG(log, "Caching: class {0} selector {1} implementation {2}.",
85 class_addr, sel_str, impl_addr);
86
87 m_impl_str_cache.insert(std::pair<ClassAndSelStr, lldb::addr_t>(
88 ClassAndSelStr(class_addr, sel_str), impl_addr));
89}
90
92 lldb::addr_t selector) {
93 MsgImplMap::iterator pos, end = m_impl_cache.end();
94 pos = m_impl_cache.find(ClassAndSel(class_addr, selector));
95 if (pos != end)
96 return (*pos).second;
98}
99
101 llvm::StringRef sel_str) {
102 MsgImplStrMap::iterator pos, end = m_impl_str_cache.end();
103 pos = m_impl_str_cache.find(ClassAndSelStr(class_addr, sel_str));
104 if (pos != end)
105 return (*pos).second;
107}
108
109lldb::TypeSP
111 CompleteClassMap::iterator complete_class_iter =
112 m_complete_class_cache.find(name);
113
114 if (complete_class_iter != m_complete_class_cache.end()) {
115 // Check the weak pointer to make sure the type hasn't been unloaded
116 TypeSP complete_type_sp(complete_class_iter->second.lock());
117
118 if (complete_type_sp)
119 return complete_type_sp;
120 else
121 m_complete_class_cache.erase(name);
122 }
123
124 if (m_negative_complete_class_cache.count(name) > 0)
125 return TypeSP();
126
127 const ModuleList &modules = m_process->GetTarget().GetImages();
128
129 SymbolContextList sc_list;
130 modules.FindSymbolsWithNameAndType(name, eSymbolTypeObjCClass, sc_list);
131 const size_t matching_symbols = sc_list.GetSize();
132
133 if (matching_symbols) {
134 SymbolContext sc;
135
136 sc_list.GetContextAtIndex(0, sc);
137
138 ModuleSP module_sp(sc.module_sp);
139
140 if (!module_sp)
141 return TypeSP();
142
143 const bool exact_match = true;
144 const uint32_t max_matches = UINT32_MAX;
145 TypeList types;
146
147 llvm::DenseSet<SymbolFile *> searched_symbol_files;
148 module_sp->FindTypes(name, exact_match, max_matches, searched_symbol_files,
149 types);
150
151 for (uint32_t i = 0; i < types.GetSize(); ++i) {
152 TypeSP type_sp(types.GetTypeAtIndex(i));
153
155 type_sp->GetForwardCompilerType())) {
156 if (TypePayloadClang(type_sp->GetPayload()).IsCompleteObjCClass()) {
157 m_complete_class_cache[name] = type_sp;
158 return type_sp;
159 }
160 }
161 }
162 }
164 return TypeSP();
165}
166
168 const char *ivar_name) {
170}
171
173 lldb::addr_t value, uint32_t ptr_size, bool allow_NULLs, bool allow_tagged,
174 bool check_version_specific) const {
175 if (!value)
176 return allow_NULLs;
177 if ((value % 2) == 1 && allow_tagged)
178 return true;
179 if ((value % ptr_size) == 0)
180 return (check_version_specific ? CheckPointer(value, ptr_size) : true);
181 else
182 return false;
183}
184
188 if (pos != m_isa_to_descriptor.end())
189 return pos->first;
190 return 0;
191}
192
196
197 if (name) {
199 if (m_hash_to_isa_map.empty()) {
200 // No name hashes were provided, we need to just linearly power through
201 // the names and find a match
203 pos != end; ++pos) {
204 if (pos->second->GetClassName() == name)
205 return pos;
206 }
207 } else {
208 // Name hashes were provided, so use them to efficiently lookup name to
209 // isa/descriptor
210 const uint32_t name_hash = llvm::djbHash(name.GetStringRef());
211 std::pair<HashToISAIterator, HashToISAIterator> range =
212 m_hash_to_isa_map.equal_range(name_hash);
213 for (HashToISAIterator range_pos = range.first; range_pos != range.second;
214 ++range_pos) {
216 m_isa_to_descriptor.find(range_pos->second);
217 if (pos != m_isa_to_descriptor.end()) {
218 if (pos->second->GetClassName() == name)
219 return pos;
220 }
221 }
222 }
223 }
224 return end;
225}
226
230 if (update_if_needed)
232
236}
237
241 if (objc_class_sp) {
242 ClassDescriptorSP objc_super_class_sp(objc_class_sp->GetSuperclass());
243 if (objc_super_class_sp)
244 return objc_super_class_sp->GetISA();
245 }
246 return 0;
247}
248
251 ConstString class_name) {
253 if (pos != m_isa_to_descriptor.end())
254 return pos->second;
255 return ClassDescriptorSP();
256}
257
260 ClassDescriptorSP objc_class_sp;
261 // if we get an invalid VO (which might still happen when playing around with
262 // pointers returned by the expression parser, don't consider this a valid
263 // ObjC object)
264 if (valobj.GetCompilerType().IsValid()) {
265 addr_t isa_pointer = valobj.GetPointerValue();
266 if (isa_pointer != LLDB_INVALID_ADDRESS) {
268
269 Process *process = exe_ctx.GetProcessPtr();
270 if (process) {
272 ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error);
273 if (isa != LLDB_INVALID_ADDRESS)
274 objc_class_sp = GetClassDescriptorFromISA(isa);
275 }
276 }
277 }
278 return objc_class_sp;
279}
280
284 GetClassDescriptor(valobj));
285 if (objc_class_sp) {
286 if (!objc_class_sp->IsKVO())
287 return objc_class_sp;
288
289 ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass());
290 if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid())
291 return non_kvo_objc_class_sp;
292 }
293 return ClassDescriptorSP();
294}
295
298 if (isa) {
300
302 m_isa_to_descriptor.find(isa);
303 if (pos != m_isa_to_descriptor.end())
304 return pos->second;
305
306 if (ABISP abi_sp = m_process->GetABI()) {
307 pos = m_isa_to_descriptor.find(abi_sp->FixCodeAddress(isa));
308 if (pos != m_isa_to_descriptor.end())
309 return pos->second;
310 }
311 }
312 return ClassDescriptorSP();
313}
314
317 if (isa) {
318 ClassDescriptorSP objc_class_sp = GetClassDescriptorFromISA(isa);
319 if (objc_class_sp && objc_class_sp->IsValid()) {
320 if (!objc_class_sp->IsKVO())
321 return objc_class_sp;
322
323 ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass());
324 if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid())
325 return non_kvo_objc_class_sp;
326 }
327 }
328 return ClassDescriptorSP();
329}
330
333 bool for_expression) {
334 if (m_scratch_ast_ctx_sp)
335 return RealizeType(*m_scratch_ast_ctx_sp, name, for_expression);
336 return CompilerType();
337}
338
340
342 return nullptr;
343}
344
346 uint64_t &size) {
347 void *opaque_ptr = compiler_type.GetOpaqueQualType();
348 size = m_type_size_cache.Lookup(opaque_ptr);
349 // an ObjC object will at least have an ISA, so 0 is definitely not OK
350 if (size > 0)
351 return true;
352
353 ClassDescriptorSP class_descriptor_sp =
355 if (!class_descriptor_sp)
356 return false;
357
358 int32_t max_offset = INT32_MIN;
359 uint64_t sizeof_max = 0;
360 bool found = false;
361
362 for (size_t idx = 0; idx < class_descriptor_sp->GetNumIVars(); idx++) {
363 const auto &ivar = class_descriptor_sp->GetIVarAtIndex(idx);
364 int32_t cur_offset = ivar.m_offset;
365 if (cur_offset > max_offset) {
366 max_offset = cur_offset;
367 sizeof_max = ivar.m_size;
368 found = true;
369 }
370 }
371
372 size = 8 * (max_offset + sizeof_max);
373 if (found)
374 m_type_size_cache.Insert(opaque_ptr, size);
375
376 return found;
377}
378
379lldb::BreakpointPreconditionSP
381 bool throw_bp) {
382 if (language != eLanguageTypeObjC)
383 return lldb::BreakpointPreconditionSP();
384 if (!throw_bp)
385 return lldb::BreakpointPreconditionSP();
386 BreakpointPreconditionSP precondition_sp(
388 return precondition_sp;
389}
390
391// Exception breakpoint Precondition class for ObjC:
393 const char *class_name) {
394 m_class_names.insert(class_name);
395}
396
398 default;
399
401 StoppointCallbackContext &context) {
402 return true;
403}
404
406 Stream &stream, lldb::DescriptionLevel level) {}
407
409 Args &args) {
411 if (args.GetArgumentCount() > 0)
412 error.SetErrorString(
413 "The ObjC Exception breakpoint doesn't support extra options.");
414 return error;
415}
416
417std::optional<CompilerType>
419 CompilerType class_type;
420 bool is_pointer_type = false;
421
422 if (TypeSystemClang::IsObjCObjectPointerType(base_type, &class_type))
423 is_pointer_type = true;
425 class_type = base_type;
426 else
427 return std::nullopt;
428
429 if (!class_type)
430 return std::nullopt;
431
432 ConstString class_name(class_type.GetTypeName());
433 if (!class_name)
434 return std::nullopt;
435
436 TypeSP complete_objc_class_type_sp = LookupInCompleteClassCache(class_name);
437 if (!complete_objc_class_type_sp)
438 return std::nullopt;
439
440 CompilerType complete_class(
441 complete_objc_class_type_sp->GetFullCompilerType());
442 if (complete_class.GetCompleteType()) {
443 if (is_pointer_type)
444 return complete_class.GetPointerType();
445 else
446 return complete_class;
447 }
448
449 return std::nullopt;
450}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:337
#define LLDB_LOGF(log,...)
Definition: Log.h:344
A command line argument class.
Definition: Args.h:33
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.h:116
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
lldb::opaque_compiler_type_t GetOpaqueQualType() const
Definition: CompilerType.h:232
ConstString GetTypeName(bool BaseOnly=false) const
bool GetCompleteType() const
Type Completion.
A uniqued constant string class.
Definition: ConstString.h:39
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:201
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Process * GetProcessPtr() const
Returns a pointer to the process object.
A collection class for Module objects.
Definition: ModuleList.h:82
void FindSymbolsWithNameAndType(ConstString name, lldb::SymbolType symbol_type, SymbolContextList &sc_list) const
Definition: ModuleList.cpp:509
bool IsPointerValid(lldb::addr_t value, uint32_t ptr_size, bool allow_NULLs=false, bool allow_tagged=false, bool check_version_specific=false) const
virtual bool CheckPointer(lldb::addr_t value, uint32_t ptr_size) const
virtual CompilerType RealizeType(TypeSystemClang &ast_ctx, const char *name, bool for_expression)=0
void GetDescription(Stream &stream, lldb::DescriptionLevel level) override
bool EvaluatePrecondition(StoppointCallbackContext &context) override
HashToISAMap::iterator HashToISAIterator
virtual ObjCISA GetISA(ConstString name)
virtual EncodingToTypeSP GetEncodingToType()
std::shared_ptr< ClassDescriptor > ClassDescriptorSP
bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp)
ISAToDescriptorMap::iterator ISAToDescriptorIterator
std::pair< ISAToDescriptorIterator, ISAToDescriptorIterator > GetDescriptorIteratorPair(bool update_if_needed=true)
virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type, const char *ivar_name)
ISAToDescriptorIterator GetDescriptorIterator(ConstString name)
bool GetTypeBitSize(const CompilerType &compiler_type, uint64_t &size) override
lldb::addr_t LookupInMethodCache(lldb::addr_t class_addr, lldb::addr_t sel)
lldb::TypeSP LookupInCompleteClassCache(ConstString &name)
ClassDescriptorSP GetNonKVOClassDescriptor(ValueObject &in_value)
virtual void UpdateISAToDescriptorMapIfNeeded()=0
std::optional< CompilerType > GetRuntimeType(CompilerType base_type) override
virtual ClassDescriptorSP GetClassDescriptorFromISA(ObjCISA isa)
bool IsAllowedRuntimeValue(ConstString name) override
Check whether the name is "self" or "_cmd" and should show up in "frame variable".
virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value)
virtual ClassDescriptorSP GetClassDescriptorFromClassName(ConstString class_name)
void AddToMethodCache(lldb::addr_t class_addr, lldb::addr_t sel, lldb::addr_t impl_addr)
static lldb::BreakpointPreconditionSP GetBreakpointExceptionPrecondition(lldb::LanguageType language, bool throw_bp)
virtual ObjCISA GetParentClass(ObjCISA isa)
std::shared_ptr< EncodingToType > EncodingToTypeSP
A plug-in interface definition class for debugging a process.
Definition: Process.h:342
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition: Process.cpp:2089
const lldb::ABISP & GetABI()
Definition: Process.cpp:1487
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1219
Process * m_process
Definition: Runtime.h:29
An error handling class.
Definition: Status.h:44
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
Defines a list of symbol context objects.
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
uint32_t GetSize() const
Get accessor for a symbol context list size.
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
lldb::ModuleSP module_sp
The Module for a given query.
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:946
void Insert(_KeyType k, _ValueType v)
uint32_t GetSize() const
Definition: TypeList.cpp:60
TypeList FindTypes(ConstString name)
lldb::TypeSP GetTypeAtIndex(uint32_t idx)
Definition: TypeList.cpp:66
The implementation of lldb::Type's m_payload field for TypeSystemClang.
static bool IsObjCObjectOrInterfaceType(const CompilerType &type)
static bool IsObjCObjectPointerType(const CompilerType &type, CompilerType *target_type=nullptr)
lldb::addr_t GetPointerValue(AddressType *address_type=nullptr)
CompilerType GetCompilerType()
Definition: ValueObject.h:352
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
#define UINT32_MAX
Definition: lldb-defines.h:19
#define LLDB_INVALID_IVAR_OFFSET
Definition: lldb-defines.h:76
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:309
Definition: SBAddress.h:15
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
LanguageType
Programming language type.
@ eLanguageTypeObjC
Objective-C.
@ eSymbolTypeObjCClass
uint64_t addr_t
Definition: lldb-types.h:83