Greg Clayton | 9b8ff51 | 2012-02-01 01:46:19 +0000 | [diff] [blame] | 1 | //===-- ObjCLanguageRuntime.cpp ---------------------------------*- C++ -*-===// |
Jim Ingham | 642036f | 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 | 324067b | 2010-09-30 00:54:27 +0000 | [diff] [blame] | 9 | #include "clang/AST/Type.h" |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 10 | |
Jim Ingham | b66cd07 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Log.h" |
Greg Clayton | ce490e3 | 2013-02-13 22:56:14 +0000 | [diff] [blame^] | 12 | #include "lldb/Core/MappedHash.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Module.h" |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 14 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Timer.h" |
Jim Ingham | 324067b | 2010-09-30 00:54:27 +0000 | [diff] [blame] | 16 | #include "lldb/Core/ValueObject.h" |
| 17 | #include "lldb/Symbol/ClangASTContext.h" |
Jim Ingham | ef80aab | 2011-05-02 18:13:59 +0000 | [diff] [blame] | 18 | #include "lldb/Symbol/Type.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/TypeList.h" |
Jim Ingham | b66cd07 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 20 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Target.h" |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 22 | |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringRef.h" |
| 24 | |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 25 | using namespace lldb; |
| 26 | using namespace lldb_private; |
| 27 | |
| 28 | //---------------------------------------------------------------------- |
| 29 | // Destructor |
| 30 | //---------------------------------------------------------------------- |
| 31 | ObjCLanguageRuntime::~ObjCLanguageRuntime() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | ObjCLanguageRuntime::ObjCLanguageRuntime (Process *process) : |
Sean Callanan | 6e12c7a | 2012-03-08 02:39:03 +0000 | [diff] [blame] | 36 | LanguageRuntime (process), |
Sean Callanan | 6fe8d36 | 2012-09-15 01:05:12 +0000 | [diff] [blame] | 37 | m_has_new_literals_and_indexing (eLazyBoolCalculate), |
Greg Clayton | ce490e3 | 2013-02-13 22:56:14 +0000 | [diff] [blame^] | 38 | m_isa_to_descriptor(), |
| 39 | m_isa_to_descriptor_stop_id (UINT32_MAX) |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 40 | { |
| 41 | |
Jim Ingham | b66cd07 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Greg Clayton | ce490e3 | 2013-02-13 22:56:14 +0000 | [diff] [blame^] | 44 | bool |
| 45 | ObjCLanguageRuntime::AddClass (ObjCISA isa, const ClassDescriptorSP &descriptor_sp, const char *class_name) |
| 46 | { |
| 47 | if (isa != 0) |
| 48 | { |
| 49 | m_isa_to_descriptor[isa] = descriptor_sp; |
| 50 | // class_name is assumed to be valid |
| 51 | m_hash_to_isa_map.insert(std::make_pair(MappedHash::HashStringUsingDJB(class_name), isa)); |
| 52 | return true; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
Jim Ingham | b66cd07 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 57 | void |
| 58 | ObjCLanguageRuntime::AddToMethodCache (lldb::addr_t class_addr, lldb::addr_t selector, lldb::addr_t impl_addr) |
| 59 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 60 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | b66cd07 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 61 | if (log) |
| 62 | { |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 63 | log->Printf ("Caching: class 0x%" PRIx64 " selector 0x%" PRIx64 " implementation 0x%" PRIx64 ".", class_addr, selector, impl_addr); |
Jim Ingham | b66cd07 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 64 | } |
| 65 | m_impl_cache.insert (std::pair<ClassAndSel,lldb::addr_t> (ClassAndSel(class_addr, selector), impl_addr)); |
| 66 | } |
| 67 | |
| 68 | lldb::addr_t |
| 69 | ObjCLanguageRuntime::LookupInMethodCache (lldb::addr_t class_addr, lldb::addr_t selector) |
| 70 | { |
| 71 | MsgImplMap::iterator pos, end = m_impl_cache.end(); |
| 72 | pos = m_impl_cache.find (ClassAndSel(class_addr, selector)); |
| 73 | if (pos != end) |
| 74 | return (*pos).second; |
| 75 | return LLDB_INVALID_ADDRESS; |
| 76 | } |
Jim Ingham | ef80aab | 2011-05-02 18:13:59 +0000 | [diff] [blame] | 77 | |
Jim Ingham | 5851366 | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 78 | |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 79 | lldb::TypeSP |
| 80 | ObjCLanguageRuntime::LookupInCompleteClassCache (ConstString &name) |
| 81 | { |
| 82 | CompleteClassMap::iterator complete_class_iter = m_complete_class_cache.find(name); |
| 83 | |
| 84 | if (complete_class_iter != m_complete_class_cache.end()) |
| 85 | { |
Greg Clayton | ef22b90 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 86 | // Check the weak pointer to make sure the type hasn't been unloaded |
| 87 | TypeSP complete_type_sp (complete_class_iter->second.lock()); |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 88 | |
Greg Clayton | ef22b90 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 89 | if (complete_type_sp) |
| 90 | return complete_type_sp; |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 91 | else |
Greg Clayton | ef22b90 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 92 | m_complete_class_cache.erase(name); |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Enrico Granata | 146d952 | 2012-11-08 02:22:02 +0000 | [diff] [blame] | 95 | const ModuleList &modules = m_process->GetTarget().GetImages(); |
Greg Clayton | ef22b90 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 96 | |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 97 | SymbolContextList sc_list; |
Greg Clayton | ef22b90 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 98 | const size_t matching_symbols = modules.FindSymbolsWithNameAndType (name, |
| 99 | eSymbolTypeObjCClass, |
| 100 | sc_list); |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 101 | |
Greg Clayton | ef22b90 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 102 | if (matching_symbols) |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 103 | { |
Greg Clayton | ef22b90 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 104 | SymbolContext sc; |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 105 | |
Greg Clayton | ef22b90 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 106 | sc_list.GetContextAtIndex(0, sc); |
| 107 | |
| 108 | ModuleSP module_sp(sc.module_sp); |
| 109 | |
| 110 | if (!module_sp) |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 111 | return TypeSP(); |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 112 | |
Greg Clayton | ef22b90 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 113 | const SymbolContext null_sc; |
| 114 | const bool exact_match = true; |
| 115 | const uint32_t max_matches = UINT32_MAX; |
| 116 | TypeList types; |
| 117 | |
| 118 | const uint32_t num_types = module_sp->FindTypes (null_sc, |
| 119 | name, |
| 120 | exact_match, |
| 121 | max_matches, |
| 122 | types); |
| 123 | |
| 124 | if (num_types) |
Sean Callanan | 282c22c | 2012-12-19 23:05:01 +0000 | [diff] [blame] | 125 | { |
Greg Clayton | ef22b90 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 126 | uint32_t i; |
| 127 | for (i = 0; i < num_types; ++i) |
| 128 | { |
| 129 | TypeSP type_sp (types.GetTypeAtIndex(i)); |
| 130 | |
| 131 | if (ClangASTContext::IsObjCClassType(type_sp->GetClangForwardType())) |
| 132 | { |
| 133 | if (type_sp->IsCompleteObjCClass()) |
| 134 | { |
| 135 | m_complete_class_cache[name] = type_sp; |
| 136 | return type_sp; |
| 137 | } |
Greg Clayton | ef22b90 | 2012-10-23 22:41:19 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 142 | return TypeSP(); |
| 143 | } |
| 144 | |
Jim Ingham | 5851366 | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 145 | size_t |
| 146 | ObjCLanguageRuntime::GetByteOffsetForIvar (ClangASTType &parent_qual_type, const char *ivar_name) |
| 147 | { |
| 148 | return LLDB_INVALID_IVAR_OFFSET; |
| 149 | } |
| 150 | |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 151 | void |
| 152 | ObjCLanguageRuntime::MethodName::Clear() |
Jim Ingham | 3ad4da0 | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 153 | { |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 154 | m_full.Clear(); |
| 155 | m_class.Clear(); |
| 156 | m_category.Clear(); |
| 157 | m_selector.Clear(); |
| 158 | m_type = eTypeUnspecified; |
| 159 | m_category_is_valid = false; |
| 160 | } |
Greg Clayton | 662e567 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 161 | |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 162 | //bool |
| 163 | //ObjCLanguageRuntime::MethodName::SetName (const char *name, bool strict) |
| 164 | //{ |
| 165 | // Clear(); |
| 166 | // if (name && name[0]) |
| 167 | // { |
| 168 | // // If "strict" is true. then the method must be specified with a |
| 169 | // // '+' or '-' at the beginning. If "strict" is false, then the '+' |
| 170 | // // or '-' can be omitted |
| 171 | // bool valid_prefix = false; |
| 172 | // |
| 173 | // if (name[0] == '+' || name[0] == '-') |
| 174 | // { |
| 175 | // valid_prefix = name[1] == '['; |
| 176 | // } |
| 177 | // else if (!strict) |
| 178 | // { |
| 179 | // // "strict" is false, the name just needs to start with '[' |
| 180 | // valid_prefix = name[0] == '['; |
| 181 | // } |
| 182 | // |
| 183 | // if (valid_prefix) |
| 184 | // { |
| 185 | // 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:]*)\\]$"); |
| 186 | // llvm::StringRef matches[4]; |
| 187 | // // Since we are using a global regular expression, we must use the threadsafe version of execute |
| 188 | // if (g_regex.ExecuteThreadSafe(name, matches, 4)) |
| 189 | // { |
| 190 | // m_full.SetCString(name); |
| 191 | // if (matches[0].empty()) |
| 192 | // m_type = eTypeUnspecified; |
| 193 | // else if (matches[0][0] == '+') |
| 194 | // m_type = eTypeClassMethod; |
| 195 | // else |
| 196 | // m_type = eTypeInstanceMethod; |
| 197 | // m_class.SetString(matches[1]); |
| 198 | // m_selector.SetString(matches[3]); |
| 199 | // if (!matches[2].empty()) |
| 200 | // m_category.SetString(matches[2]); |
| 201 | // } |
| 202 | // } |
| 203 | // } |
| 204 | // return IsValid(strict); |
| 205 | //} |
| 206 | |
| 207 | bool |
| 208 | ObjCLanguageRuntime::MethodName::SetName (const char *name, bool strict) |
| 209 | { |
| 210 | Clear(); |
| 211 | if (name && name[0]) |
Jim Ingham | 3ad4da0 | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 212 | { |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 213 | // If "strict" is true. then the method must be specified with a |
| 214 | // '+' or '-' at the beginning. If "strict" is false, then the '+' |
| 215 | // or '-' can be omitted |
| 216 | bool valid_prefix = false; |
| 217 | |
| 218 | if (name[0] == '+' || name[0] == '-') |
Jim Ingham | 3ad4da0 | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 219 | { |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 220 | valid_prefix = name[1] == '['; |
| 221 | if (name[0] == '+') |
| 222 | m_type = eTypeClassMethod; |
| 223 | else |
| 224 | m_type = eTypeInstanceMethod; |
| 225 | } |
| 226 | else if (!strict) |
| 227 | { |
| 228 | // "strict" is false, the name just needs to start with '[' |
| 229 | valid_prefix = name[0] == '['; |
| 230 | } |
| 231 | |
| 232 | if (valid_prefix) |
| 233 | { |
| 234 | int name_len = strlen (name); |
| 235 | // Objective C methods must have at least: |
| 236 | // "-[" or "+[" prefix |
| 237 | // One character for a class name |
| 238 | // One character for the space between the class name |
| 239 | // One character for the method name |
| 240 | // "]" suffix |
| 241 | if (name_len >= (5 + (strict ? 1 : 0)) && name[name_len - 1] == ']') |
Jim Ingham | 3ad4da0 | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 242 | { |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 243 | m_full.SetCStringWithLength(name, name_len); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | return IsValid(strict); |
| 248 | } |
| 249 | |
| 250 | const ConstString & |
| 251 | ObjCLanguageRuntime::MethodName::GetClassName () |
| 252 | { |
| 253 | if (!m_class) |
| 254 | { |
| 255 | if (IsValid(false)) |
| 256 | { |
| 257 | const char *full = m_full.GetCString(); |
| 258 | const char *class_start = (full[0] == '[' ? full + 1 : full + 2); |
| 259 | const char *paren_pos = strchr (class_start, '('); |
| 260 | if (paren_pos) |
| 261 | { |
| 262 | m_class.SetCStringWithLength (class_start, paren_pos - class_start); |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | // No '(' was found in the full name, we can definitively say |
| 267 | // that our category was valid (and empty). |
| 268 | m_category_is_valid = true; |
| 269 | const char *space_pos = strchr (full, ' '); |
| 270 | if (space_pos) |
Greg Clayton | 662e567 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 271 | { |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 272 | m_class.SetCStringWithLength (class_start, space_pos - class_start); |
| 273 | if (!m_class_category) |
Jim Ingham | 3ad4da0 | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 274 | { |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 275 | // No category in name, so we can also fill in the m_class_category |
| 276 | m_class_category = m_class; |
Jim Ingham | 3ad4da0 | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | } |
Jim Ingham | 3ad4da0 | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 280 | } |
Jim Ingham | 3ad4da0 | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 281 | } |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 282 | return m_class; |
Jim Ingham | 3ad4da0 | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 283 | } |
Enrico Granata | ae2ae94 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 284 | |
Greg Clayton | f892c42 | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 285 | const ConstString & |
| 286 | ObjCLanguageRuntime::MethodName::GetClassNameWithCategory () |
| 287 | { |
| 288 | if (!m_class_category) |
| 289 | { |
| 290 | if (IsValid(false)) |
| 291 | { |
| 292 | const char *full = m_full.GetCString(); |
| 293 | const char *class_start = (full[0] == '[' ? full + 1 : full + 2); |
| 294 | const char *space_pos = strchr (full, ' '); |
| 295 | if (space_pos) |
| 296 | { |
| 297 | m_class_category.SetCStringWithLength (class_start, space_pos - class_start); |
| 298 | // If m_class hasn't been filled in and the class with category doesn't |
| 299 | // contain a '(', then we can also fill in the m_class |
| 300 | if (!m_class && strchr (m_class_category.GetCString(), '(') == NULL) |
| 301 | { |
| 302 | m_class = m_class_category; |
| 303 | // No '(' was found in the full name, we can definitively say |
| 304 | // that our category was valid (and empty). |
| 305 | m_category_is_valid = true; |
| 306 | |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | return m_class_category; |
| 312 | } |
| 313 | |
| 314 | const ConstString & |
| 315 | ObjCLanguageRuntime::MethodName::GetSelector () |
| 316 | { |
| 317 | if (!m_selector) |
| 318 | { |
| 319 | if (IsValid(false)) |
| 320 | { |
| 321 | const char *full = m_full.GetCString(); |
| 322 | const char *space_pos = strchr (full, ' '); |
| 323 | if (space_pos) |
| 324 | { |
| 325 | ++space_pos; // skip the space |
| 326 | m_selector.SetCStringWithLength (space_pos, m_full.GetLength() - (space_pos - full) - 1); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | return m_selector; |
| 331 | } |
| 332 | |
| 333 | const ConstString & |
| 334 | ObjCLanguageRuntime::MethodName::GetCategory () |
| 335 | { |
| 336 | if (!m_category_is_valid && !m_category) |
| 337 | { |
| 338 | if (IsValid(false)) |
| 339 | { |
| 340 | m_category_is_valid = true; |
| 341 | const char *full = m_full.GetCString(); |
| 342 | const char *class_start = (full[0] == '[' ? full + 1 : full + 2); |
| 343 | const char *open_paren_pos = strchr (class_start, '('); |
| 344 | if (open_paren_pos) |
| 345 | { |
| 346 | ++open_paren_pos; // Skip the open paren |
| 347 | const char *close_paren_pos = strchr (open_paren_pos, ')'); |
| 348 | if (close_paren_pos) |
| 349 | m_category.SetCStringWithLength (open_paren_pos, close_paren_pos - open_paren_pos); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | return m_category; |
| 354 | } |
| 355 | |
| 356 | ConstString |
| 357 | ObjCLanguageRuntime::MethodName::GetFullNameWithoutCategory (bool empty_if_no_category) |
| 358 | { |
| 359 | if (IsValid(false)) |
| 360 | { |
| 361 | if (HasCategory()) |
| 362 | { |
| 363 | StreamString strm; |
| 364 | if (m_type == eTypeClassMethod) |
| 365 | strm.PutChar('+'); |
| 366 | else if (m_type == eTypeInstanceMethod) |
| 367 | strm.PutChar('-'); |
| 368 | strm.Printf("[%s %s]", GetClassName().GetCString(), GetSelector().GetCString()); |
| 369 | return ConstString(strm.GetString().c_str()); |
| 370 | } |
| 371 | |
| 372 | if (!empty_if_no_category) |
| 373 | { |
| 374 | // Just return the full name since it doesn't have a category |
| 375 | return GetFullName(); |
| 376 | } |
| 377 | } |
| 378 | return ConstString(); |
| 379 | } |
| 380 | |
| 381 | size_t |
| 382 | ObjCLanguageRuntime::MethodName::GetFullNames (std::vector<ConstString> &names, bool append) |
| 383 | { |
| 384 | if (!append) |
| 385 | names.clear(); |
| 386 | if (IsValid(false)) |
| 387 | { |
| 388 | StreamString strm; |
| 389 | const bool is_class_method = m_type == eTypeClassMethod; |
| 390 | const bool is_instance_method = m_type == eTypeInstanceMethod; |
| 391 | const ConstString &category = GetCategory(); |
| 392 | if (is_class_method || is_instance_method) |
| 393 | { |
| 394 | names.push_back (m_full); |
| 395 | if (category) |
| 396 | { |
| 397 | strm.Printf("%c[%s %s]", |
| 398 | is_class_method ? '+' : '-', |
| 399 | GetClassName().GetCString(), |
| 400 | GetSelector().GetCString()); |
| 401 | names.push_back(ConstString(strm.GetString().c_str())); |
| 402 | } |
| 403 | } |
| 404 | else |
| 405 | { |
| 406 | const ConstString &class_name = GetClassName(); |
| 407 | const ConstString &selector = GetSelector(); |
| 408 | strm.Printf("+[%s %s]", class_name.GetCString(), selector.GetCString()); |
| 409 | names.push_back(ConstString(strm.GetString().c_str())); |
| 410 | strm.Clear(); |
| 411 | strm.Printf("-[%s %s]", class_name.GetCString(), selector.GetCString()); |
| 412 | names.push_back(ConstString(strm.GetString().c_str())); |
| 413 | strm.Clear(); |
| 414 | if (category) |
| 415 | { |
| 416 | strm.Printf("+[%s(%s) %s]", class_name.GetCString(), category.GetCString(), selector.GetCString()); |
| 417 | names.push_back(ConstString(strm.GetString().c_str())); |
| 418 | strm.Clear(); |
| 419 | strm.Printf("-[%s(%s) %s]", class_name.GetCString(), category.GetCString(), selector.GetCString()); |
| 420 | names.push_back(ConstString(strm.GetString().c_str())); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | return names.size(); |
| 425 | } |
| 426 | |
| 427 | |
Enrico Granata | ae2ae94 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 428 | bool |
| 429 | ObjCLanguageRuntime::ClassDescriptor::IsPointerValid (lldb::addr_t value, |
| 430 | uint32_t ptr_size, |
| 431 | bool allow_NULLs, |
| 432 | bool allow_tagged, |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 433 | bool check_version_specific) const |
Enrico Granata | ae2ae94 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 434 | { |
| 435 | if (!value) |
| 436 | return allow_NULLs; |
| 437 | if ( (value % 2) == 1 && allow_tagged) |
| 438 | return true; |
| 439 | if ((value % ptr_size) == 0) |
| 440 | return (check_version_specific ? CheckPointer(value,ptr_size) : true); |
| 441 | else |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | ObjCLanguageRuntime::ObjCISA |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 446 | ObjCLanguageRuntime::GetISA(const ConstString &name) |
| 447 | { |
Greg Clayton | ce490e3 | 2013-02-13 22:56:14 +0000 | [diff] [blame^] | 448 | ISAToDescriptorIterator pos = GetDescriptorIterator (name); |
| 449 | if (pos != m_isa_to_descriptor.end()) |
| 450 | return pos->first; |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | |
Greg Clayton | ce490e3 | 2013-02-13 22:56:14 +0000 | [diff] [blame^] | 454 | ObjCLanguageRuntime::ISAToDescriptorIterator |
| 455 | ObjCLanguageRuntime::GetDescriptorIterator (const ConstString &name) |
| 456 | { |
| 457 | ISAToDescriptorIterator end = m_isa_to_descriptor.end(); |
| 458 | |
| 459 | if (name) |
| 460 | { |
| 461 | UpdateISAToDescriptorMap(); |
| 462 | if (m_hash_to_isa_map.empty()) |
| 463 | { |
| 464 | // No name hashes were provided, we need to just linearly power through the |
| 465 | // names and find a match |
| 466 | for (ISAToDescriptorIterator pos = m_isa_to_descriptor.begin(); pos != end; ++pos) |
| 467 | { |
| 468 | if (pos->second->GetClassName() == name) |
| 469 | return pos; |
| 470 | } |
| 471 | } |
| 472 | else |
| 473 | { |
| 474 | // Name hashes were provided, so use them to efficiently lookup name to isa/descriptor |
| 475 | const uint32_t name_hash = MappedHash::HashStringUsingDJB (name.GetCString()); |
| 476 | std::pair <HashToISAIterator, HashToISAIterator> range = m_hash_to_isa_map.equal_range(name_hash); |
| 477 | for (HashToISAIterator range_pos = range.first; range_pos != range.second; ++range_pos) |
| 478 | { |
| 479 | ISAToDescriptorIterator pos = m_isa_to_descriptor.find (range_pos->second); |
| 480 | if (pos != m_isa_to_descriptor.end()) |
| 481 | { |
| 482 | if (pos->second->GetClassName() == name) |
| 483 | return pos; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | return end; |
| 489 | } |
| 490 | |
| 491 | |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 492 | ObjCLanguageRuntime::ObjCISA |
Enrico Granata | ae2ae94 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 493 | ObjCLanguageRuntime::GetParentClass(ObjCLanguageRuntime::ObjCISA isa) |
| 494 | { |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 495 | ClassDescriptorSP objc_class_sp (GetClassDescriptor(isa)); |
| 496 | if (objc_class_sp) |
Enrico Granata | ae2ae94 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 497 | { |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 498 | ClassDescriptorSP objc_super_class_sp (objc_class_sp->GetSuperclass()); |
| 499 | if (objc_super_class_sp) |
| 500 | return objc_super_class_sp->GetISA(); |
Enrico Granata | ae2ae94 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 501 | } |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 502 | return 0; |
Enrico Granata | ae2ae94 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Enrico Granata | ae2ae94 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 505 | ConstString |
| 506 | ObjCLanguageRuntime::GetActualTypeName(ObjCLanguageRuntime::ObjCISA isa) |
| 507 | { |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 508 | ClassDescriptorSP objc_class_sp (GetNonKVOClassDescriptor(isa)); |
| 509 | if (objc_class_sp) |
| 510 | return objc_class_sp->GetClassName(); |
| 511 | return ConstString(); |
Enrico Granata | ae2ae94 | 2012-09-04 18:47:54 +0000 | [diff] [blame] | 512 | } |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 513 | |
| 514 | ObjCLanguageRuntime::ClassDescriptorSP |
Greg Clayton | a510437 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 515 | ObjCLanguageRuntime::GetClassDescriptor (const ConstString &class_name) |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 516 | { |
Greg Clayton | ce490e3 | 2013-02-13 22:56:14 +0000 | [diff] [blame^] | 517 | ISAToDescriptorIterator pos = GetDescriptorIterator (class_name); |
| 518 | if (pos != m_isa_to_descriptor.end()) |
| 519 | return pos->second; |
Greg Clayton | a510437 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 520 | return ClassDescriptorSP(); |
| 521 | |
| 522 | } |
| 523 | |
| 524 | ObjCLanguageRuntime::ClassDescriptorSP |
| 525 | ObjCLanguageRuntime::GetClassDescriptor (ValueObject& valobj) |
| 526 | { |
| 527 | ClassDescriptorSP objc_class_sp; |
| 528 | // if we get an invalid VO (which might still happen when playing around |
| 529 | // with pointers returned by the expression parser, don't consider this |
| 530 | // a valid ObjC object) |
| 531 | if (valobj.GetValue().GetContextType() != Value::eContextTypeInvalid) |
| 532 | { |
| 533 | addr_t isa_pointer = valobj.GetPointerValue(); |
| 534 | if (isa_pointer != LLDB_INVALID_ADDRESS) |
| 535 | { |
| 536 | ExecutionContext exe_ctx (valobj.GetExecutionContextRef()); |
| 537 | |
| 538 | Process *process = exe_ctx.GetProcessPtr(); |
| 539 | if (process) |
| 540 | { |
| 541 | Error error; |
| 542 | ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error); |
| 543 | if (isa != LLDB_INVALID_ADDRESS) |
| 544 | objc_class_sp = GetClassDescriptor (isa); |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | return objc_class_sp; |
| 549 | } |
| 550 | |
| 551 | ObjCLanguageRuntime::ClassDescriptorSP |
| 552 | ObjCLanguageRuntime::GetNonKVOClassDescriptor (ValueObject& valobj) |
| 553 | { |
| 554 | ObjCLanguageRuntime::ClassDescriptorSP objc_class_sp (GetClassDescriptor (valobj)); |
| 555 | if (objc_class_sp) |
| 556 | { |
| 557 | if (!objc_class_sp->IsKVO()) |
| 558 | return objc_class_sp; |
| 559 | |
| 560 | ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); |
| 561 | if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) |
| 562 | return non_kvo_objc_class_sp; |
| 563 | } |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 564 | return ClassDescriptorSP(); |
| 565 | } |
| 566 | |
Greg Clayton | a510437 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 567 | |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 568 | ObjCLanguageRuntime::ClassDescriptorSP |
| 569 | ObjCLanguageRuntime::GetClassDescriptor (ObjCISA isa) |
| 570 | { |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 571 | if (isa) |
| 572 | { |
Greg Clayton | a510437 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 573 | UpdateISAToDescriptorMap(); |
Greg Clayton | ce490e3 | 2013-02-13 22:56:14 +0000 | [diff] [blame^] | 574 | ObjCLanguageRuntime::ISAToDescriptorIterator pos = m_isa_to_descriptor.find(isa); |
| 575 | if (pos != m_isa_to_descriptor.end()) |
Greg Clayton | a510437 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 576 | return pos->second; |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 577 | } |
Greg Clayton | a510437 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 578 | return ClassDescriptorSP(); |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | ObjCLanguageRuntime::ClassDescriptorSP |
| 582 | ObjCLanguageRuntime::GetNonKVOClassDescriptor (ObjCISA isa) |
| 583 | { |
| 584 | if (isa) |
| 585 | { |
| 586 | ClassDescriptorSP objc_class_sp = GetClassDescriptor (isa); |
| 587 | if (objc_class_sp && objc_class_sp->IsValid()) |
| 588 | { |
Greg Clayton | a510437 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 589 | if (!objc_class_sp->IsKVO()) |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 590 | return objc_class_sp; |
Greg Clayton | a510437 | 2012-10-11 18:07:21 +0000 | [diff] [blame] | 591 | |
| 592 | ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); |
| 593 | if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) |
| 594 | return non_kvo_objc_class_sp; |
Greg Clayton | be2f3aa | 2012-10-09 17:51:53 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | return ClassDescriptorSP(); |
| 598 | } |
| 599 | |
| 600 | |
| 601 | |