Greg Clayton | 05e8d19 | 2012-02-01 01:46:19 +0000 | [diff] [blame] | 1 | //===-- ObjCLanguageRuntime.cpp ---------------------------------*- C++ -*-===// |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Jim Ingham | 6c68fb4 | 2010-09-30 00:54:27 +0000 | [diff] [blame] | 9 | #include "clang/AST/Type.h" |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 10 | |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Log.h" |
Greg Clayton | a66c4d9 | 2013-02-13 22:56:14 +0000 | [diff] [blame] | 12 | #include "lldb/Core/MappedHash.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Module.h" |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 14 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Timer.h" |
Jim Ingham | 6c68fb4 | 2010-09-30 00:54:27 +0000 | [diff] [blame] | 16 | #include "lldb/Core/ValueObject.h" |
| 17 | #include "lldb/Symbol/ClangASTContext.h" |
Zachary Turner | 32abc6e | 2015-03-03 19:23:09 +0000 | [diff] [blame] | 18 | #include "lldb/Symbol/SymbolContext.h" |
Jim Ingham | 61be090 | 2011-05-02 18:13:59 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/Type.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/TypeList.h" |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 21 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Target.h" |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 23 | |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringRef.h" |
| 25 | |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | |
| 29 | //---------------------------------------------------------------------- |
| 30 | // Destructor |
| 31 | //---------------------------------------------------------------------- |
| 32 | ObjCLanguageRuntime::~ObjCLanguageRuntime() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | ObjCLanguageRuntime::ObjCLanguageRuntime (Process *process) : |
Sean Callanan | 226b70c | 2012-03-08 02:39:03 +0000 | [diff] [blame] | 37 | LanguageRuntime (process), |
Enrico Granata | 3842b9f | 2015-01-28 00:45:42 +0000 | [diff] [blame] | 38 | m_impl_cache(), |
Sean Callanan | c09d9d8 | 2012-09-15 01:05:12 +0000 | [diff] [blame] | 39 | m_has_new_literals_and_indexing (eLazyBoolCalculate), |
Greg Clayton | a66c4d9 | 2013-02-13 22:56:14 +0000 | [diff] [blame] | 40 | m_isa_to_descriptor(), |
Enrico Granata | 3842b9f | 2015-01-28 00:45:42 +0000 | [diff] [blame] | 41 | m_hash_to_isa_map(), |
| 42 | m_type_size_cache(), |
| 43 | m_isa_to_descriptor_stop_id (UINT32_MAX), |
| 44 | m_complete_class_cache(), |
| 45 | m_negative_complete_class_cache() |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 46 | { |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Greg Clayton | a66c4d9 | 2013-02-13 22:56:14 +0000 | [diff] [blame] | 49 | bool |
| 50 | ObjCLanguageRuntime::AddClass (ObjCISA isa, const ClassDescriptorSP &descriptor_sp, const char *class_name) |
| 51 | { |
| 52 | if (isa != 0) |
| 53 | { |
| 54 | m_isa_to_descriptor[isa] = descriptor_sp; |
| 55 | // class_name is assumed to be valid |
| 56 | m_hash_to_isa_map.insert(std::make_pair(MappedHash::HashStringUsingDJB(class_name), isa)); |
| 57 | return true; |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 62 | void |
| 63 | ObjCLanguageRuntime::AddToMethodCache (lldb::addr_t class_addr, lldb::addr_t selector, lldb::addr_t impl_addr) |
| 64 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 65 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 66 | if (log) |
| 67 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 68 | log->Printf ("Caching: class 0x%" PRIx64 " selector 0x%" PRIx64 " implementation 0x%" PRIx64 ".", class_addr, selector, impl_addr); |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 69 | } |
| 70 | m_impl_cache.insert (std::pair<ClassAndSel,lldb::addr_t> (ClassAndSel(class_addr, selector), impl_addr)); |
| 71 | } |
| 72 | |
| 73 | lldb::addr_t |
| 74 | ObjCLanguageRuntime::LookupInMethodCache (lldb::addr_t class_addr, lldb::addr_t selector) |
| 75 | { |
| 76 | MsgImplMap::iterator pos, end = m_impl_cache.end(); |
| 77 | pos = m_impl_cache.find (ClassAndSel(class_addr, selector)); |
| 78 | if (pos != end) |
| 79 | return (*pos).second; |
| 80 | return LLDB_INVALID_ADDRESS; |
| 81 | } |
Jim Ingham | 61be090 | 2011-05-02 18:13:59 +0000 | [diff] [blame] | 82 | |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 83 | |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 84 | lldb::TypeSP |
| 85 | ObjCLanguageRuntime::LookupInCompleteClassCache (ConstString &name) |
| 86 | { |
| 87 | CompleteClassMap::iterator complete_class_iter = m_complete_class_cache.find(name); |
| 88 | |
| 89 | if (complete_class_iter != m_complete_class_cache.end()) |
| 90 | { |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 91 | // Check the weak pointer to make sure the type hasn't been unloaded |
| 92 | TypeSP complete_type_sp (complete_class_iter->second.lock()); |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 93 | |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 94 | if (complete_type_sp) |
| 95 | return complete_type_sp; |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 96 | else |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 97 | m_complete_class_cache.erase(name); |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Enrico Granata | f15ee4e | 2013-04-05 18:49:06 +0000 | [diff] [blame] | 100 | if (m_negative_complete_class_cache.count(name) > 0) |
| 101 | return TypeSP(); |
| 102 | |
Enrico Granata | 1759848 | 2012-11-08 02:22:02 +0000 | [diff] [blame] | 103 | const ModuleList &modules = m_process->GetTarget().GetImages(); |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 104 | |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 105 | SymbolContextList sc_list; |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 106 | const size_t matching_symbols = modules.FindSymbolsWithNameAndType (name, |
| 107 | eSymbolTypeObjCClass, |
| 108 | sc_list); |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 109 | |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 110 | if (matching_symbols) |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 111 | { |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 112 | SymbolContext sc; |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 113 | |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 114 | sc_list.GetContextAtIndex(0, sc); |
| 115 | |
| 116 | ModuleSP module_sp(sc.module_sp); |
| 117 | |
| 118 | if (!module_sp) |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 119 | return TypeSP(); |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 120 | |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 121 | const SymbolContext null_sc; |
| 122 | const bool exact_match = true; |
| 123 | const uint32_t max_matches = UINT32_MAX; |
| 124 | TypeList types; |
| 125 | |
| 126 | const uint32_t num_types = module_sp->FindTypes (null_sc, |
| 127 | name, |
| 128 | exact_match, |
| 129 | max_matches, |
| 130 | types); |
| 131 | |
| 132 | if (num_types) |
Sean Callanan | 7be70e8 | 2012-12-19 23:05:01 +0000 | [diff] [blame] | 133 | { |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 134 | uint32_t i; |
| 135 | for (i = 0; i < num_types; ++i) |
| 136 | { |
| 137 | TypeSP type_sp (types.GetTypeAtIndex(i)); |
| 138 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 139 | if (ClangASTContext::IsObjCObjectOrInterfaceType(type_sp->GetClangForwardType())) |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 140 | { |
| 141 | if (type_sp->IsCompleteObjCClass()) |
| 142 | { |
| 143 | m_complete_class_cache[name] = type_sp; |
| 144 | return type_sp; |
| 145 | } |
Greg Clayton | d64dd12 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
Enrico Granata | f15ee4e | 2013-04-05 18:49:06 +0000 | [diff] [blame] | 150 | m_negative_complete_class_cache.insert(name); |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 151 | return TypeSP(); |
| 152 | } |
| 153 | |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 154 | size_t |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame^] | 155 | ObjCLanguageRuntime::GetByteOffsetForIvar (CompilerType &parent_qual_type, const char *ivar_name) |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 156 | { |
| 157 | return LLDB_INVALID_IVAR_OFFSET; |
| 158 | } |
| 159 | |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 160 | void |
| 161 | ObjCLanguageRuntime::MethodName::Clear() |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 162 | { |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 163 | m_full.Clear(); |
| 164 | m_class.Clear(); |
| 165 | m_category.Clear(); |
| 166 | m_selector.Clear(); |
| 167 | m_type = eTypeUnspecified; |
| 168 | m_category_is_valid = false; |
| 169 | } |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 170 | |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 171 | //bool |
| 172 | //ObjCLanguageRuntime::MethodName::SetName (const char *name, bool strict) |
| 173 | //{ |
| 174 | // Clear(); |
| 175 | // if (name && name[0]) |
| 176 | // { |
| 177 | // // If "strict" is true. then the method must be specified with a |
| 178 | // // '+' or '-' at the beginning. If "strict" is false, then the '+' |
| 179 | // // or '-' can be omitted |
| 180 | // bool valid_prefix = false; |
| 181 | // |
| 182 | // if (name[0] == '+' || name[0] == '-') |
| 183 | // { |
| 184 | // valid_prefix = name[1] == '['; |
| 185 | // } |
| 186 | // else if (!strict) |
| 187 | // { |
| 188 | // // "strict" is false, the name just needs to start with '[' |
| 189 | // valid_prefix = name[0] == '['; |
| 190 | // } |
| 191 | // |
| 192 | // if (valid_prefix) |
| 193 | // { |
| 194 | // static RegularExpression g_regex("^([-+]?)\\[([A-Za-z_][A-Za-z_0-9]*)(\\([A-Za-z_][A-Za-z_0-9]*\\))? ([A-Za-z_][A-Za-z_0-9:]*)\\]$"); |
| 195 | // llvm::StringRef matches[4]; |
| 196 | // // Since we are using a global regular expression, we must use the threadsafe version of execute |
| 197 | // if (g_regex.ExecuteThreadSafe(name, matches, 4)) |
| 198 | // { |
| 199 | // m_full.SetCString(name); |
| 200 | // if (matches[0].empty()) |
| 201 | // m_type = eTypeUnspecified; |
| 202 | // else if (matches[0][0] == '+') |
| 203 | // m_type = eTypeClassMethod; |
| 204 | // else |
| 205 | // m_type = eTypeInstanceMethod; |
| 206 | // m_class.SetString(matches[1]); |
| 207 | // m_selector.SetString(matches[3]); |
| 208 | // if (!matches[2].empty()) |
| 209 | // m_category.SetString(matches[2]); |
| 210 | // } |
| 211 | // } |
| 212 | // } |
| 213 | // return IsValid(strict); |
| 214 | //} |
| 215 | |
| 216 | bool |
| 217 | ObjCLanguageRuntime::MethodName::SetName (const char *name, bool strict) |
| 218 | { |
| 219 | Clear(); |
| 220 | if (name && name[0]) |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 221 | { |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 222 | // If "strict" is true. then the method must be specified with a |
| 223 | // '+' or '-' at the beginning. If "strict" is false, then the '+' |
| 224 | // or '-' can be omitted |
| 225 | bool valid_prefix = false; |
| 226 | |
| 227 | if (name[0] == '+' || name[0] == '-') |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 228 | { |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 229 | valid_prefix = name[1] == '['; |
| 230 | if (name[0] == '+') |
| 231 | m_type = eTypeClassMethod; |
| 232 | else |
| 233 | m_type = eTypeInstanceMethod; |
| 234 | } |
| 235 | else if (!strict) |
| 236 | { |
| 237 | // "strict" is false, the name just needs to start with '[' |
| 238 | valid_prefix = name[0] == '['; |
| 239 | } |
| 240 | |
| 241 | if (valid_prefix) |
| 242 | { |
| 243 | int name_len = strlen (name); |
| 244 | // Objective C methods must have at least: |
| 245 | // "-[" or "+[" prefix |
| 246 | // One character for a class name |
| 247 | // One character for the space between the class name |
| 248 | // One character for the method name |
| 249 | // "]" suffix |
| 250 | if (name_len >= (5 + (strict ? 1 : 0)) && name[name_len - 1] == ']') |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 251 | { |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 252 | m_full.SetCStringWithLength(name, name_len); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | return IsValid(strict); |
| 257 | } |
| 258 | |
| 259 | const ConstString & |
| 260 | ObjCLanguageRuntime::MethodName::GetClassName () |
| 261 | { |
| 262 | if (!m_class) |
| 263 | { |
| 264 | if (IsValid(false)) |
| 265 | { |
| 266 | const char *full = m_full.GetCString(); |
| 267 | const char *class_start = (full[0] == '[' ? full + 1 : full + 2); |
| 268 | const char *paren_pos = strchr (class_start, '('); |
| 269 | if (paren_pos) |
| 270 | { |
| 271 | m_class.SetCStringWithLength (class_start, paren_pos - class_start); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | // No '(' was found in the full name, we can definitively say |
| 276 | // that our category was valid (and empty). |
| 277 | m_category_is_valid = true; |
| 278 | const char *space_pos = strchr (full, ' '); |
| 279 | if (space_pos) |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 280 | { |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 281 | m_class.SetCStringWithLength (class_start, space_pos - class_start); |
| 282 | if (!m_class_category) |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 283 | { |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 284 | // No category in name, so we can also fill in the m_class_category |
| 285 | m_class_category = m_class; |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 289 | } |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 290 | } |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 291 | return m_class; |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 292 | } |
Enrico Granata | 3467d80 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 293 | |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 294 | const ConstString & |
| 295 | ObjCLanguageRuntime::MethodName::GetClassNameWithCategory () |
| 296 | { |
| 297 | if (!m_class_category) |
| 298 | { |
| 299 | if (IsValid(false)) |
| 300 | { |
| 301 | const char *full = m_full.GetCString(); |
| 302 | const char *class_start = (full[0] == '[' ? full + 1 : full + 2); |
| 303 | const char *space_pos = strchr (full, ' '); |
| 304 | if (space_pos) |
| 305 | { |
| 306 | m_class_category.SetCStringWithLength (class_start, space_pos - class_start); |
| 307 | // If m_class hasn't been filled in and the class with category doesn't |
| 308 | // contain a '(', then we can also fill in the m_class |
| 309 | if (!m_class && strchr (m_class_category.GetCString(), '(') == NULL) |
| 310 | { |
| 311 | m_class = m_class_category; |
| 312 | // No '(' was found in the full name, we can definitively say |
| 313 | // that our category was valid (and empty). |
| 314 | m_category_is_valid = true; |
| 315 | |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | return m_class_category; |
| 321 | } |
| 322 | |
| 323 | const ConstString & |
| 324 | ObjCLanguageRuntime::MethodName::GetSelector () |
| 325 | { |
| 326 | if (!m_selector) |
| 327 | { |
| 328 | if (IsValid(false)) |
| 329 | { |
| 330 | const char *full = m_full.GetCString(); |
| 331 | const char *space_pos = strchr (full, ' '); |
| 332 | if (space_pos) |
| 333 | { |
| 334 | ++space_pos; // skip the space |
| 335 | m_selector.SetCStringWithLength (space_pos, m_full.GetLength() - (space_pos - full) - 1); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | return m_selector; |
| 340 | } |
| 341 | |
| 342 | const ConstString & |
| 343 | ObjCLanguageRuntime::MethodName::GetCategory () |
| 344 | { |
| 345 | if (!m_category_is_valid && !m_category) |
| 346 | { |
| 347 | if (IsValid(false)) |
| 348 | { |
| 349 | m_category_is_valid = true; |
| 350 | const char *full = m_full.GetCString(); |
| 351 | const char *class_start = (full[0] == '[' ? full + 1 : full + 2); |
| 352 | const char *open_paren_pos = strchr (class_start, '('); |
| 353 | if (open_paren_pos) |
| 354 | { |
| 355 | ++open_paren_pos; // Skip the open paren |
| 356 | const char *close_paren_pos = strchr (open_paren_pos, ')'); |
| 357 | if (close_paren_pos) |
| 358 | m_category.SetCStringWithLength (open_paren_pos, close_paren_pos - open_paren_pos); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | return m_category; |
| 363 | } |
| 364 | |
| 365 | ConstString |
| 366 | ObjCLanguageRuntime::MethodName::GetFullNameWithoutCategory (bool empty_if_no_category) |
| 367 | { |
| 368 | if (IsValid(false)) |
| 369 | { |
| 370 | if (HasCategory()) |
| 371 | { |
| 372 | StreamString strm; |
| 373 | if (m_type == eTypeClassMethod) |
| 374 | strm.PutChar('+'); |
| 375 | else if (m_type == eTypeInstanceMethod) |
| 376 | strm.PutChar('-'); |
| 377 | strm.Printf("[%s %s]", GetClassName().GetCString(), GetSelector().GetCString()); |
| 378 | return ConstString(strm.GetString().c_str()); |
| 379 | } |
| 380 | |
| 381 | if (!empty_if_no_category) |
| 382 | { |
| 383 | // Just return the full name since it doesn't have a category |
| 384 | return GetFullName(); |
| 385 | } |
| 386 | } |
| 387 | return ConstString(); |
| 388 | } |
| 389 | |
| 390 | size_t |
| 391 | ObjCLanguageRuntime::MethodName::GetFullNames (std::vector<ConstString> &names, bool append) |
| 392 | { |
| 393 | if (!append) |
| 394 | names.clear(); |
| 395 | if (IsValid(false)) |
| 396 | { |
| 397 | StreamString strm; |
| 398 | const bool is_class_method = m_type == eTypeClassMethod; |
| 399 | const bool is_instance_method = m_type == eTypeInstanceMethod; |
| 400 | const ConstString &category = GetCategory(); |
| 401 | if (is_class_method || is_instance_method) |
| 402 | { |
| 403 | names.push_back (m_full); |
| 404 | if (category) |
| 405 | { |
| 406 | strm.Printf("%c[%s %s]", |
| 407 | is_class_method ? '+' : '-', |
| 408 | GetClassName().GetCString(), |
| 409 | GetSelector().GetCString()); |
| 410 | names.push_back(ConstString(strm.GetString().c_str())); |
| 411 | } |
| 412 | } |
| 413 | else |
| 414 | { |
| 415 | const ConstString &class_name = GetClassName(); |
| 416 | const ConstString &selector = GetSelector(); |
| 417 | strm.Printf("+[%s %s]", class_name.GetCString(), selector.GetCString()); |
| 418 | names.push_back(ConstString(strm.GetString().c_str())); |
| 419 | strm.Clear(); |
| 420 | strm.Printf("-[%s %s]", class_name.GetCString(), selector.GetCString()); |
| 421 | names.push_back(ConstString(strm.GetString().c_str())); |
| 422 | strm.Clear(); |
| 423 | if (category) |
| 424 | { |
| 425 | strm.Printf("+[%s(%s) %s]", class_name.GetCString(), category.GetCString(), selector.GetCString()); |
| 426 | names.push_back(ConstString(strm.GetString().c_str())); |
| 427 | strm.Clear(); |
| 428 | strm.Printf("-[%s(%s) %s]", class_name.GetCString(), category.GetCString(), selector.GetCString()); |
| 429 | names.push_back(ConstString(strm.GetString().c_str())); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | return names.size(); |
| 434 | } |
| 435 | |
| 436 | |
Enrico Granata | 3467d80 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 437 | bool |
| 438 | ObjCLanguageRuntime::ClassDescriptor::IsPointerValid (lldb::addr_t value, |
| 439 | uint32_t ptr_size, |
| 440 | bool allow_NULLs, |
| 441 | bool allow_tagged, |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 442 | bool check_version_specific) const |
Enrico Granata | 3467d80 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 443 | { |
| 444 | if (!value) |
| 445 | return allow_NULLs; |
| 446 | if ( (value % 2) == 1 && allow_tagged) |
| 447 | return true; |
| 448 | if ((value % ptr_size) == 0) |
| 449 | return (check_version_specific ? CheckPointer(value,ptr_size) : true); |
| 450 | else |
| 451 | return false; |
| 452 | } |
| 453 | |
| 454 | ObjCLanguageRuntime::ObjCISA |
Sean Callanan | bc47dfc | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 455 | ObjCLanguageRuntime::GetISA(const ConstString &name) |
| 456 | { |
Greg Clayton | a66c4d9 | 2013-02-13 22:56:14 +0000 | [diff] [blame] | 457 | ISAToDescriptorIterator pos = GetDescriptorIterator (name); |
| 458 | if (pos != m_isa_to_descriptor.end()) |
| 459 | return pos->first; |
Sean Callanan | bc47dfc | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
Greg Clayton | a66c4d9 | 2013-02-13 22:56:14 +0000 | [diff] [blame] | 463 | ObjCLanguageRuntime::ISAToDescriptorIterator |
| 464 | ObjCLanguageRuntime::GetDescriptorIterator (const ConstString &name) |
| 465 | { |
| 466 | ISAToDescriptorIterator end = m_isa_to_descriptor.end(); |
| 467 | |
| 468 | if (name) |
| 469 | { |
| 470 | UpdateISAToDescriptorMap(); |
| 471 | if (m_hash_to_isa_map.empty()) |
| 472 | { |
| 473 | // No name hashes were provided, we need to just linearly power through the |
| 474 | // names and find a match |
| 475 | for (ISAToDescriptorIterator pos = m_isa_to_descriptor.begin(); pos != end; ++pos) |
| 476 | { |
| 477 | if (pos->second->GetClassName() == name) |
| 478 | return pos; |
| 479 | } |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | // Name hashes were provided, so use them to efficiently lookup name to isa/descriptor |
| 484 | const uint32_t name_hash = MappedHash::HashStringUsingDJB (name.GetCString()); |
| 485 | std::pair <HashToISAIterator, HashToISAIterator> range = m_hash_to_isa_map.equal_range(name_hash); |
| 486 | for (HashToISAIterator range_pos = range.first; range_pos != range.second; ++range_pos) |
| 487 | { |
| 488 | ISAToDescriptorIterator pos = m_isa_to_descriptor.find (range_pos->second); |
| 489 | if (pos != m_isa_to_descriptor.end()) |
| 490 | { |
| 491 | if (pos->second->GetClassName() == name) |
| 492 | return pos; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | return end; |
| 498 | } |
| 499 | |
Enrico Granata | ba4b8b0 | 2015-05-06 21:01:07 +0000 | [diff] [blame] | 500 | std::pair<ObjCLanguageRuntime::ISAToDescriptorIterator,ObjCLanguageRuntime::ISAToDescriptorIterator> |
| 501 | ObjCLanguageRuntime::GetDescriptorIteratorPair (bool update_if_needed) |
| 502 | { |
| 503 | if (update_if_needed) |
| 504 | UpdateISAToDescriptorMapIfNeeded(); |
Colin Riley | 5de4251 | 2015-05-08 12:17:27 +0000 | [diff] [blame] | 505 | |
| 506 | return std::pair<ObjCLanguageRuntime::ISAToDescriptorIterator, |
| 507 | ObjCLanguageRuntime::ISAToDescriptorIterator>( |
| 508 | m_isa_to_descriptor.begin(), |
| 509 | m_isa_to_descriptor.end()); |
Enrico Granata | ba4b8b0 | 2015-05-06 21:01:07 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Greg Clayton | a66c4d9 | 2013-02-13 22:56:14 +0000 | [diff] [blame] | 512 | |
Sean Callanan | bc47dfc | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 513 | ObjCLanguageRuntime::ObjCISA |
Enrico Granata | 3467d80 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 514 | ObjCLanguageRuntime::GetParentClass(ObjCLanguageRuntime::ObjCISA isa) |
| 515 | { |
Greg Clayton | 03da4cc | 2013-04-19 21:31:16 +0000 | [diff] [blame] | 516 | ClassDescriptorSP objc_class_sp (GetClassDescriptorFromISA(isa)); |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 517 | if (objc_class_sp) |
Enrico Granata | 3467d80 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 518 | { |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 519 | ClassDescriptorSP objc_super_class_sp (objc_class_sp->GetSuperclass()); |
| 520 | if (objc_super_class_sp) |
| 521 | return objc_super_class_sp->GetISA(); |
Enrico Granata | 3467d80 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 522 | } |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 523 | return 0; |
Enrico Granata | 3467d80 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Enrico Granata | 3467d80 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 526 | ConstString |
| 527 | ObjCLanguageRuntime::GetActualTypeName(ObjCLanguageRuntime::ObjCISA isa) |
| 528 | { |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 529 | ClassDescriptorSP objc_class_sp (GetNonKVOClassDescriptor(isa)); |
| 530 | if (objc_class_sp) |
| 531 | return objc_class_sp->GetClassName(); |
| 532 | return ConstString(); |
Enrico Granata | 3467d80 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 533 | } |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 534 | |
| 535 | ObjCLanguageRuntime::ClassDescriptorSP |
Greg Clayton | 03da4cc | 2013-04-19 21:31:16 +0000 | [diff] [blame] | 536 | ObjCLanguageRuntime::GetClassDescriptorFromClassName (const ConstString &class_name) |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 537 | { |
Greg Clayton | a66c4d9 | 2013-02-13 22:56:14 +0000 | [diff] [blame] | 538 | ISAToDescriptorIterator pos = GetDescriptorIterator (class_name); |
| 539 | if (pos != m_isa_to_descriptor.end()) |
| 540 | return pos->second; |
Greg Clayton | f0246d1 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 541 | return ClassDescriptorSP(); |
| 542 | |
| 543 | } |
| 544 | |
| 545 | ObjCLanguageRuntime::ClassDescriptorSP |
| 546 | ObjCLanguageRuntime::GetClassDescriptor (ValueObject& valobj) |
| 547 | { |
| 548 | ClassDescriptorSP objc_class_sp; |
| 549 | // if we get an invalid VO (which might still happen when playing around |
| 550 | // with pointers returned by the expression parser, don't consider this |
| 551 | // a valid ObjC object) |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 552 | if (valobj.GetClangType().IsValid()) |
Greg Clayton | f0246d1 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 553 | { |
| 554 | addr_t isa_pointer = valobj.GetPointerValue(); |
| 555 | if (isa_pointer != LLDB_INVALID_ADDRESS) |
| 556 | { |
| 557 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 558 | |
| 559 | Process *process = exe_ctx.GetProcessPtr(); |
| 560 | if (process) |
| 561 | { |
| 562 | Error error; |
| 563 | ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error); |
| 564 | if (isa != LLDB_INVALID_ADDRESS) |
Greg Clayton | 03da4cc | 2013-04-19 21:31:16 +0000 | [diff] [blame] | 565 | objc_class_sp = GetClassDescriptorFromISA (isa); |
Greg Clayton | f0246d1 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | } |
| 569 | return objc_class_sp; |
| 570 | } |
| 571 | |
| 572 | ObjCLanguageRuntime::ClassDescriptorSP |
| 573 | ObjCLanguageRuntime::GetNonKVOClassDescriptor (ValueObject& valobj) |
| 574 | { |
| 575 | ObjCLanguageRuntime::ClassDescriptorSP objc_class_sp (GetClassDescriptor (valobj)); |
| 576 | if (objc_class_sp) |
| 577 | { |
| 578 | if (!objc_class_sp->IsKVO()) |
| 579 | return objc_class_sp; |
| 580 | |
| 581 | ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); |
| 582 | if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) |
| 583 | return non_kvo_objc_class_sp; |
| 584 | } |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 585 | return ClassDescriptorSP(); |
| 586 | } |
| 587 | |
Greg Clayton | f0246d1 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 588 | |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 589 | ObjCLanguageRuntime::ClassDescriptorSP |
Greg Clayton | 03da4cc | 2013-04-19 21:31:16 +0000 | [diff] [blame] | 590 | ObjCLanguageRuntime::GetClassDescriptorFromISA (ObjCISA isa) |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 591 | { |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 592 | if (isa) |
| 593 | { |
Greg Clayton | f0246d1 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 594 | UpdateISAToDescriptorMap(); |
Greg Clayton | a66c4d9 | 2013-02-13 22:56:14 +0000 | [diff] [blame] | 595 | ObjCLanguageRuntime::ISAToDescriptorIterator pos = m_isa_to_descriptor.find(isa); |
| 596 | if (pos != m_isa_to_descriptor.end()) |
Greg Clayton | f0246d1 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 597 | return pos->second; |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 598 | } |
Greg Clayton | f0246d1 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 599 | return ClassDescriptorSP(); |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | ObjCLanguageRuntime::ClassDescriptorSP |
| 603 | ObjCLanguageRuntime::GetNonKVOClassDescriptor (ObjCISA isa) |
| 604 | { |
| 605 | if (isa) |
| 606 | { |
Greg Clayton | 03da4cc | 2013-04-19 21:31:16 +0000 | [diff] [blame] | 607 | ClassDescriptorSP objc_class_sp = GetClassDescriptorFromISA (isa); |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 608 | if (objc_class_sp && objc_class_sp->IsValid()) |
| 609 | { |
Greg Clayton | f0246d1 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 610 | if (!objc_class_sp->IsKVO()) |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 611 | return objc_class_sp; |
Greg Clayton | f0246d1 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 612 | |
| 613 | ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); |
| 614 | if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) |
| 615 | return non_kvo_objc_class_sp; |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | return ClassDescriptorSP(); |
| 619 | } |
| 620 | |
| 621 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame^] | 622 | CompilerType |
Sean Callanan | a330933 | 2014-10-31 18:02:30 +0000 | [diff] [blame] | 623 | ObjCLanguageRuntime::EncodingToType::RealizeType (const char* name, bool for_expression) |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 624 | { |
| 625 | if (m_scratch_ast_ctx_ap) |
Sean Callanan | a330933 | 2014-10-31 18:02:30 +0000 | [diff] [blame] | 626 | return RealizeType(*m_scratch_ast_ctx_ap, name, for_expression); |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame^] | 627 | return CompilerType(); |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 628 | } |
Greg Clayton | 77fbc81 | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 629 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame^] | 630 | CompilerType |
Sean Callanan | a330933 | 2014-10-31 18:02:30 +0000 | [diff] [blame] | 631 | ObjCLanguageRuntime::EncodingToType::RealizeType (ClangASTContext& ast_ctx, const char* name, bool for_expression) |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 632 | { |
| 633 | clang::ASTContext *clang_ast = ast_ctx.getASTContext(); |
| 634 | if (!clang_ast) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame^] | 635 | return CompilerType(); |
Sean Callanan | a330933 | 2014-10-31 18:02:30 +0000 | [diff] [blame] | 636 | return RealizeType(*clang_ast, name, for_expression); |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | ObjCLanguageRuntime::EncodingToType::~EncodingToType() {} |
| 640 | |
| 641 | ObjCLanguageRuntime::EncodingToTypeSP |
| 642 | ObjCLanguageRuntime::GetEncodingToType () |
| 643 | { |
| 644 | return nullptr; |
| 645 | } |
Enrico Granata | 3842b9f | 2015-01-28 00:45:42 +0000 | [diff] [blame] | 646 | |
| 647 | bool |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame^] | 648 | ObjCLanguageRuntime::GetTypeBitSize (const CompilerType& clang_type, |
Enrico Granata | 3842b9f | 2015-01-28 00:45:42 +0000 | [diff] [blame] | 649 | uint64_t &size) |
| 650 | { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 651 | void *opaque_ptr = clang_type.GetOpaqueQualType(); |
Enrico Granata | 3842b9f | 2015-01-28 00:45:42 +0000 | [diff] [blame] | 652 | size = m_type_size_cache.Lookup(opaque_ptr); |
| 653 | // an ObjC object will at least have an ISA, so 0 is definitely not OK |
| 654 | if (size > 0) |
| 655 | return true; |
| 656 | |
| 657 | ClassDescriptorSP class_descriptor_sp = GetClassDescriptorFromClassName(clang_type.GetTypeName()); |
| 658 | if (!class_descriptor_sp) |
| 659 | return false; |
| 660 | |
| 661 | int32_t max_offset = INT32_MIN; |
| 662 | uint64_t sizeof_max = 0; |
| 663 | bool found = false; |
| 664 | |
| 665 | for (size_t idx = 0; |
| 666 | idx < class_descriptor_sp->GetNumIVars(); |
| 667 | idx++) |
| 668 | { |
| 669 | const auto& ivar = class_descriptor_sp->GetIVarAtIndex(idx); |
| 670 | int32_t cur_offset = ivar.m_offset; |
| 671 | if (cur_offset > max_offset) |
| 672 | { |
| 673 | max_offset = cur_offset; |
| 674 | sizeof_max = ivar.m_size; |
| 675 | found = true; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | size = 8 * (max_offset + sizeof_max); |
| 680 | if (found) |
| 681 | m_type_size_cache.Insert(opaque_ptr, size); |
| 682 | |
| 683 | return found; |
| 684 | } |
Jim Ingham | a72b31c | 2015-04-22 19:42:18 +0000 | [diff] [blame] | 685 | |
| 686 | //------------------------------------------------------------------ |
| 687 | // Exception breakpoint Precondition class for ObjC: |
| 688 | //------------------------------------------------------------------ |
| 689 | void |
| 690 | ObjCLanguageRuntime::ObjCExceptionPrecondition::AddClassName(const char *class_name) |
| 691 | { |
| 692 | m_class_names.insert(class_name); |
| 693 | } |
| 694 | |
| 695 | ObjCLanguageRuntime::ObjCExceptionPrecondition::ObjCExceptionPrecondition() |
| 696 | { |
| 697 | } |
| 698 | |
| 699 | bool |
| 700 | ObjCLanguageRuntime::ObjCExceptionPrecondition::EvaluatePrecondition(StoppointCallbackContext &context) |
| 701 | { |
| 702 | return true; |
| 703 | } |
| 704 | |
| 705 | void |
| 706 | ObjCLanguageRuntime::ObjCExceptionPrecondition::DescribePrecondition(Stream &stream, lldb::DescriptionLevel level) |
| 707 | { |
| 708 | } |
| 709 | |
| 710 | Error |
| 711 | ObjCLanguageRuntime::ObjCExceptionPrecondition::ConfigurePrecondition(Args &args) |
| 712 | { |
| 713 | Error error; |
| 714 | if (args.GetArgumentCount() > 0) |
| 715 | error.SetErrorString("The ObjC Exception breakpoint doesn't support extra options."); |
| 716 | return error; |
| 717 | } |