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