| 1 | //===-- AppleObjCRuntimeV2.cpp --------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" |
| 10 | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
| 11 | |
| 12 | #include "lldb/Core/Debugger.h" |
| 13 | #include "lldb/Core/DebuggerEvents.h" |
| 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Core/PluginManager.h" |
| 16 | #include "lldb/Core/Section.h" |
| 17 | #include "lldb/Expression/DiagnosticManager.h" |
| 18 | #include "lldb/Expression/FunctionCaller.h" |
| 19 | #include "lldb/Expression/UtilityFunction.h" |
| 20 | #include "lldb/Host/OptionParser.h" |
| 21 | #include "lldb/Interpreter/CommandObject.h" |
| 22 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
| 23 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 24 | #include "lldb/Interpreter/OptionArgParser.h" |
| 25 | #include "lldb/Interpreter/OptionValueBoolean.h" |
| 26 | #include "lldb/Symbol/CompilerType.h" |
| 27 | #include "lldb/Symbol/ObjectFile.h" |
| 28 | #include "lldb/Symbol/Symbol.h" |
| 29 | #include "lldb/Symbol/TypeList.h" |
| 30 | #include "lldb/Symbol/VariableList.h" |
| 31 | #include "lldb/Target/ABI.h" |
| 32 | #include "lldb/Target/DynamicLoader.h" |
| 33 | #include "lldb/Target/ExecutionContext.h" |
| 34 | #include "lldb/Target/LanguageRuntime.h" |
| 35 | #include "lldb/Target/Platform.h" |
| 36 | #include "lldb/Target/Process.h" |
| 37 | #include "lldb/Target/RegisterContext.h" |
| 38 | #include "lldb/Target/StackFrameRecognizer.h" |
| 39 | #include "lldb/Target/Target.h" |
| 40 | #include "lldb/Target/Thread.h" |
| 41 | #include "lldb/Utility/ConstString.h" |
| 42 | #include "lldb/Utility/LLDBLog.h" |
| 43 | #include "lldb/Utility/Log.h" |
| 44 | #include "lldb/Utility/Scalar.h" |
| 45 | #include "lldb/Utility/Status.h" |
| 46 | #include "lldb/Utility/Stream.h" |
| 47 | #include "lldb/Utility/StreamString.h" |
| 48 | #include "lldb/Utility/Timer.h" |
| 49 | #include "lldb/ValueObject/ValueObjectConstResult.h" |
| 50 | #include "lldb/ValueObject/ValueObjectVariable.h" |
| 51 | #include "lldb/lldb-enumerations.h" |
| 52 | |
| 53 | #include "AppleObjCClassDescriptorV2.h" |
| 54 | #include "AppleObjCDeclVendor.h" |
| 55 | #include "AppleObjCRuntimeV2.h" |
| 56 | #include "AppleObjCTrampolineHandler.h" |
| 57 | #include "AppleObjCTypeEncodingParser.h" |
| 58 | |
| 59 | #include "clang/AST/ASTContext.h" |
| 60 | #include "clang/AST/DeclObjC.h" |
| 61 | #include "clang/Basic/TargetInfo.h" |
| 62 | #include "llvm/ADT/ScopeExit.h" |
| 63 | |
| 64 | #include <cstdint> |
| 65 | #include <memory> |
| 66 | #include <string> |
| 67 | #include <vector> |
| 68 | |
| 69 | using namespace lldb; |
| 70 | using namespace lldb_private; |
| 71 | |
| 72 | char AppleObjCRuntimeV2::ID = 0; |
| 73 | |
| 74 | static const char *g_get_dynamic_class_info_name = |
| 75 | "__lldb_apple_objc_v2_get_dynamic_class_info" ; |
| 76 | |
| 77 | static const char *g_get_dynamic_class_info_body = R"( |
| 78 | |
| 79 | extern "C" |
| 80 | { |
| 81 | size_t strlen(const char *); |
| 82 | char *strncpy (char * s1, const char * s2, size_t n); |
| 83 | int printf(const char * format, ...); |
| 84 | } |
| 85 | #define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__) |
| 86 | |
| 87 | typedef struct _NXMapTable { |
| 88 | void *prototype; |
| 89 | unsigned num_classes; |
| 90 | unsigned num_buckets_minus_one; |
| 91 | void *buckets; |
| 92 | } NXMapTable; |
| 93 | |
| 94 | #define NX_MAPNOTAKEY ((void *)(-1)) |
| 95 | |
| 96 | typedef struct BucketInfo |
| 97 | { |
| 98 | const char *name_ptr; |
| 99 | Class isa; |
| 100 | } BucketInfo; |
| 101 | |
| 102 | struct ClassInfo |
| 103 | { |
| 104 | Class isa; |
| 105 | uint32_t hash; |
| 106 | } __attribute__((__packed__)); |
| 107 | |
| 108 | uint32_t |
| 109 | __lldb_apple_objc_v2_get_dynamic_class_info (void *gdb_objc_realized_classes_ptr, |
| 110 | void *class_infos_ptr, |
| 111 | uint32_t class_infos_byte_size, |
| 112 | uint32_t should_log) |
| 113 | { |
| 114 | DEBUG_PRINTF ("gdb_objc_realized_classes_ptr = %p\n", gdb_objc_realized_classes_ptr); |
| 115 | DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr); |
| 116 | DEBUG_PRINTF ("class_infos_byte_size = %u\n", class_infos_byte_size); |
| 117 | const NXMapTable *grc = (const NXMapTable *)gdb_objc_realized_classes_ptr; |
| 118 | if (grc) |
| 119 | { |
| 120 | const unsigned num_classes = grc->num_classes; |
| 121 | DEBUG_PRINTF ("num_classes = %u\n", grc->num_classes); |
| 122 | if (class_infos_ptr) |
| 123 | { |
| 124 | const unsigned num_buckets_minus_one = grc->num_buckets_minus_one; |
| 125 | DEBUG_PRINTF ("num_buckets_minus_one = %u\n", num_buckets_minus_one); |
| 126 | |
| 127 | const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo); |
| 128 | DEBUG_PRINTF ("max_class_infos = %u\n", max_class_infos); |
| 129 | |
| 130 | ClassInfo *class_infos = (ClassInfo *)class_infos_ptr; |
| 131 | BucketInfo *buckets = (BucketInfo *)grc->buckets; |
| 132 | |
| 133 | uint32_t idx = 0; |
| 134 | for (unsigned i=0; i<=num_buckets_minus_one; ++i) |
| 135 | { |
| 136 | if (buckets[i].name_ptr != NX_MAPNOTAKEY) |
| 137 | { |
| 138 | if (idx < max_class_infos) |
| 139 | { |
| 140 | const char *s = buckets[i].name_ptr; |
| 141 | uint32_t h = 5381; |
| 142 | for (unsigned char c = *s; c; c = *++s) |
| 143 | h = ((h << 5) + h) + c; |
| 144 | class_infos[idx].hash = h; |
| 145 | class_infos[idx].isa = buckets[i].isa; |
| 146 | DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, buckets[i].name_ptr); |
| 147 | } |
| 148 | ++idx; |
| 149 | } |
| 150 | } |
| 151 | if (idx < max_class_infos) |
| 152 | { |
| 153 | class_infos[idx].isa = NULL; |
| 154 | class_infos[idx].hash = 0; |
| 155 | } |
| 156 | } |
| 157 | return num_classes; |
| 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | )" ; |
| 163 | |
| 164 | static const char *g_get_dynamic_class_info2_name = |
| 165 | "__lldb_apple_objc_v2_get_dynamic_class_info2" ; |
| 166 | |
| 167 | static const char *g_get_dynamic_class_info2_body = R"( |
| 168 | |
| 169 | extern "C" { |
| 170 | int printf(const char * format, ...); |
| 171 | void free(void *ptr); |
| 172 | Class* objc_copyRealizedClassList_nolock(unsigned int *outCount); |
| 173 | const char* objc_debug_class_getNameRaw(Class cls); |
| 174 | } |
| 175 | |
| 176 | #define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__) |
| 177 | |
| 178 | struct ClassInfo |
| 179 | { |
| 180 | Class isa; |
| 181 | uint32_t hash; |
| 182 | } __attribute__((__packed__)); |
| 183 | |
| 184 | uint32_t |
| 185 | __lldb_apple_objc_v2_get_dynamic_class_info2(void *gdb_objc_realized_classes_ptr, |
| 186 | void *class_infos_ptr, |
| 187 | uint32_t class_infos_byte_size, |
| 188 | uint32_t should_log) |
| 189 | { |
| 190 | DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr); |
| 191 | DEBUG_PRINTF ("class_infos_byte_size = %u\n", class_infos_byte_size); |
| 192 | |
| 193 | const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo); |
| 194 | DEBUG_PRINTF ("max_class_infos = %u\n", max_class_infos); |
| 195 | |
| 196 | ClassInfo *class_infos = (ClassInfo *)class_infos_ptr; |
| 197 | |
| 198 | uint32_t count = 0; |
| 199 | Class* realized_class_list = objc_copyRealizedClassList_nolock(&count); |
| 200 | DEBUG_PRINTF ("count = %u\n", count); |
| 201 | |
| 202 | uint32_t idx = 0; |
| 203 | for (uint32_t i=0; i<count; ++i) |
| 204 | { |
| 205 | if (idx < max_class_infos) |
| 206 | { |
| 207 | Class isa = realized_class_list[i]; |
| 208 | const char *name_ptr = objc_debug_class_getNameRaw(isa); |
| 209 | if (!name_ptr) |
| 210 | continue; |
| 211 | const char *s = name_ptr; |
| 212 | uint32_t h = 5381; |
| 213 | for (unsigned char c = *s; c; c = *++s) |
| 214 | h = ((h << 5) + h) + c; |
| 215 | class_infos[idx].hash = h; |
| 216 | class_infos[idx].isa = isa; |
| 217 | DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name_ptr); |
| 218 | } |
| 219 | idx++; |
| 220 | } |
| 221 | |
| 222 | if (idx < max_class_infos) |
| 223 | { |
| 224 | class_infos[idx].isa = NULL; |
| 225 | class_infos[idx].hash = 0; |
| 226 | } |
| 227 | |
| 228 | free(realized_class_list); |
| 229 | return count; |
| 230 | } |
| 231 | )" ; |
| 232 | |
| 233 | static const char *g_get_dynamic_class_info3_name = |
| 234 | "__lldb_apple_objc_v2_get_dynamic_class_info3" ; |
| 235 | |
| 236 | static const char *g_get_dynamic_class_info3_body = R"( |
| 237 | |
| 238 | extern "C" { |
| 239 | int printf(const char * format, ...); |
| 240 | void free(void *ptr); |
| 241 | size_t objc_getRealizedClassList_trylock(Class *buffer, size_t len); |
| 242 | const char* objc_debug_class_getNameRaw(Class cls); |
| 243 | const char* class_getName(Class cls); |
| 244 | } |
| 245 | |
| 246 | #define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__) |
| 247 | |
| 248 | struct ClassInfo |
| 249 | { |
| 250 | Class isa; |
| 251 | uint32_t hash; |
| 252 | } __attribute__((__packed__)); |
| 253 | |
| 254 | uint32_t |
| 255 | __lldb_apple_objc_v2_get_dynamic_class_info3(void *gdb_objc_realized_classes_ptr, |
| 256 | void *class_infos_ptr, |
| 257 | uint32_t class_infos_byte_size, |
| 258 | void *class_buffer, |
| 259 | uint32_t class_buffer_len, |
| 260 | uint32_t should_log) |
| 261 | { |
| 262 | DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr); |
| 263 | DEBUG_PRINTF ("class_infos_byte_size = %u\n", class_infos_byte_size); |
| 264 | |
| 265 | const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo); |
| 266 | DEBUG_PRINTF ("max_class_infos = %u\n", max_class_infos); |
| 267 | |
| 268 | ClassInfo *class_infos = (ClassInfo *)class_infos_ptr; |
| 269 | |
| 270 | Class *realized_class_list = (Class*)class_buffer; |
| 271 | |
| 272 | uint32_t count = objc_getRealizedClassList_trylock(realized_class_list, |
| 273 | class_buffer_len); |
| 274 | DEBUG_PRINTF ("count = %u\n", count); |
| 275 | |
| 276 | uint32_t idx = 0; |
| 277 | for (uint32_t i=0; i<count; ++i) |
| 278 | { |
| 279 | if (idx < max_class_infos) |
| 280 | { |
| 281 | Class isa = realized_class_list[i]; |
| 282 | const char *name_ptr = objc_debug_class_getNameRaw(isa); |
| 283 | if (!name_ptr) { |
| 284 | class_getName(isa); // Realize name of lazy classes. |
| 285 | name_ptr = objc_debug_class_getNameRaw(isa); |
| 286 | } |
| 287 | if (!name_ptr) |
| 288 | continue; |
| 289 | const char *s = name_ptr; |
| 290 | uint32_t h = 5381; |
| 291 | for (unsigned char c = *s; c; c = *++s) |
| 292 | h = ((h << 5) + h) + c; |
| 293 | class_infos[idx].hash = h; |
| 294 | class_infos[idx].isa = isa; |
| 295 | DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name_ptr); |
| 296 | } |
| 297 | idx++; |
| 298 | } |
| 299 | |
| 300 | if (idx < max_class_infos) |
| 301 | { |
| 302 | class_infos[idx].isa = NULL; |
| 303 | class_infos[idx].hash = 0; |
| 304 | } |
| 305 | |
| 306 | return count; |
| 307 | } |
| 308 | )" ; |
| 309 | |
| 310 | // We'll substitute in class_getName or class_getNameRaw depending |
| 311 | // on which is present. |
| 312 | static const char *g_shared_cache_class_name_funcptr = R"( |
| 313 | extern "C" |
| 314 | { |
| 315 | const char *%s(void *objc_class); |
| 316 | const char *(*class_name_lookup_func)(void *) = %s; |
| 317 | } |
| 318 | )" ; |
| 319 | |
| 320 | static const char *g_get_shared_cache_class_info_name = |
| 321 | "__lldb_apple_objc_v2_get_shared_cache_class_info" ; |
| 322 | |
| 323 | static const char *g_get_shared_cache_class_info_body = R"( |
| 324 | |
| 325 | extern "C" |
| 326 | { |
| 327 | size_t strlen(const char *); |
| 328 | char *strncpy (char * s1, const char * s2, size_t n); |
| 329 | int printf(const char * format, ...); |
| 330 | } |
| 331 | |
| 332 | #define DEBUG_PRINTF(fmt, ...) if (should_log) printf(fmt, ## __VA_ARGS__) |
| 333 | |
| 334 | |
| 335 | struct objc_classheader_t { |
| 336 | int32_t clsOffset; |
| 337 | int32_t hiOffset; |
| 338 | }; |
| 339 | |
| 340 | struct objc_classheader_v16_t { |
| 341 | uint64_t isDuplicate : 1, |
| 342 | objectCacheOffset : 47, // Offset from the shared cache base |
| 343 | dylibObjCIndex : 16; |
| 344 | }; |
| 345 | |
| 346 | struct objc_clsopt_t { |
| 347 | uint32_t capacity; |
| 348 | uint32_t occupied; |
| 349 | uint32_t shift; |
| 350 | uint32_t mask; |
| 351 | uint32_t zero; |
| 352 | uint32_t unused; |
| 353 | uint64_t salt; |
| 354 | uint32_t scramble[256]; |
| 355 | uint8_t tab[0]; // tab[mask+1] |
| 356 | // uint8_t checkbytes[capacity]; |
| 357 | // int32_t offset[capacity]; |
| 358 | // objc_classheader_t clsOffsets[capacity]; |
| 359 | // uint32_t duplicateCount; |
| 360 | // objc_classheader_t duplicateOffsets[duplicateCount]; |
| 361 | }; |
| 362 | |
| 363 | struct objc_clsopt_v16_t { |
| 364 | uint32_t version; |
| 365 | uint32_t capacity; |
| 366 | uint32_t occupied; |
| 367 | uint32_t shift; |
| 368 | uint32_t mask; |
| 369 | uint32_t zero; |
| 370 | uint64_t salt; |
| 371 | uint32_t scramble[256]; |
| 372 | uint8_t tab[0]; // tab[mask+1] |
| 373 | // uint8_t checkbytes[capacity]; |
| 374 | // int32_t offset[capacity]; |
| 375 | // objc_classheader_t clsOffsets[capacity]; |
| 376 | // uint32_t duplicateCount; |
| 377 | // objc_classheader_t duplicateOffsets[duplicateCount]; |
| 378 | }; |
| 379 | |
| 380 | struct objc_opt_t { |
| 381 | uint32_t version; |
| 382 | int32_t selopt_offset; |
| 383 | int32_t headeropt_offset; |
| 384 | int32_t clsopt_offset; |
| 385 | }; |
| 386 | |
| 387 | struct objc_opt_v14_t { |
| 388 | uint32_t version; |
| 389 | uint32_t flags; |
| 390 | int32_t selopt_offset; |
| 391 | int32_t headeropt_offset; |
| 392 | int32_t clsopt_offset; |
| 393 | }; |
| 394 | |
| 395 | struct objc_opt_v16_t { |
| 396 | uint32_t version; |
| 397 | uint32_t flags; |
| 398 | int32_t selopt_offset; |
| 399 | int32_t headeropt_ro_offset; |
| 400 | int32_t unused_clsopt_offset; |
| 401 | int32_t unused_protocolopt_offset; |
| 402 | int32_t headeropt_rw_offset; |
| 403 | int32_t unused_protocolopt2_offset; |
| 404 | int32_t largeSharedCachesClassOffset; |
| 405 | int32_t largeSharedCachesProtocolOffset; |
| 406 | uint64_t relativeMethodSelectorBaseAddressCacheOffset; |
| 407 | }; |
| 408 | |
| 409 | struct ClassInfo |
| 410 | { |
| 411 | Class isa; |
| 412 | uint32_t hash; |
| 413 | } __attribute__((__packed__)); |
| 414 | |
| 415 | uint32_t |
| 416 | __lldb_apple_objc_v2_get_shared_cache_class_info (void *objc_opt_ro_ptr, |
| 417 | void *shared_cache_base_ptr, |
| 418 | void *class_infos_ptr, |
| 419 | uint64_t *relative_selector_offset, |
| 420 | uint32_t class_infos_byte_size, |
| 421 | uint32_t should_log) |
| 422 | { |
| 423 | *relative_selector_offset = 0; |
| 424 | uint32_t idx = 0; |
| 425 | DEBUG_PRINTF ("objc_opt_ro_ptr = %p\n", objc_opt_ro_ptr); |
| 426 | DEBUG_PRINTF ("shared_cache_base_ptr = %p\n", shared_cache_base_ptr); |
| 427 | DEBUG_PRINTF ("class_infos_ptr = %p\n", class_infos_ptr); |
| 428 | DEBUG_PRINTF ("class_infos_byte_size = %u (%llu class infos)\n", class_infos_byte_size, (uint64_t)(class_infos_byte_size/sizeof(ClassInfo))); |
| 429 | if (objc_opt_ro_ptr) |
| 430 | { |
| 431 | const objc_opt_t *objc_opt = (objc_opt_t *)objc_opt_ro_ptr; |
| 432 | const objc_opt_v14_t* objc_opt_v14 = (objc_opt_v14_t*)objc_opt_ro_ptr; |
| 433 | const objc_opt_v16_t* objc_opt_v16 = (objc_opt_v16_t*)objc_opt_ro_ptr; |
| 434 | if (objc_opt->version >= 16) |
| 435 | { |
| 436 | *relative_selector_offset = objc_opt_v16->relativeMethodSelectorBaseAddressCacheOffset; |
| 437 | DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt_v16->version); |
| 438 | DEBUG_PRINTF ("objc_opt->flags = %u\n", objc_opt_v16->flags); |
| 439 | DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt_v16->selopt_offset); |
| 440 | DEBUG_PRINTF ("objc_opt->headeropt_ro_offset = %d\n", objc_opt_v16->headeropt_ro_offset); |
| 441 | DEBUG_PRINTF ("objc_opt->relativeMethodSelectorBaseAddressCacheOffset = %d\n", *relative_selector_offset); |
| 442 | } |
| 443 | else if (objc_opt->version >= 14) |
| 444 | { |
| 445 | DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt_v14->version); |
| 446 | DEBUG_PRINTF ("objc_opt->flags = %u\n", objc_opt_v14->flags); |
| 447 | DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt_v14->selopt_offset); |
| 448 | DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt_v14->headeropt_offset); |
| 449 | DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt_v14->clsopt_offset); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | DEBUG_PRINTF ("objc_opt->version = %u\n", objc_opt->version); |
| 454 | DEBUG_PRINTF ("objc_opt->selopt_offset = %d\n", objc_opt->selopt_offset); |
| 455 | DEBUG_PRINTF ("objc_opt->headeropt_offset = %d\n", objc_opt->headeropt_offset); |
| 456 | DEBUG_PRINTF ("objc_opt->clsopt_offset = %d\n", objc_opt->clsopt_offset); |
| 457 | } |
| 458 | |
| 459 | if (objc_opt->version == 16) |
| 460 | { |
| 461 | int32_t large_offset = objc_opt_v16->largeSharedCachesClassOffset; |
| 462 | const objc_clsopt_v16_t* clsopt = (const objc_clsopt_v16_t*)((uint8_t *)objc_opt + large_offset); |
| 463 | // Work around a bug in some version shared cache builder where the offset overflows 2GiB (rdar://146432183). |
| 464 | uint32_t unsigned_offset = (uint32_t)large_offset; |
| 465 | if (unsigned_offset > 0x7fffffff && unsigned_offset < 0x82000000) { |
| 466 | clsopt = (const objc_clsopt_v16_t*)((uint8_t *)objc_opt + unsigned_offset); |
| 467 | DEBUG_PRINTF("warning: applying largeSharedCachesClassOffset overflow workaround!\n"); |
| 468 | } |
| 469 | const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo); |
| 470 | |
| 471 | DEBUG_PRINTF("max_class_infos = %llu\n", (uint64_t)max_class_infos); |
| 472 | |
| 473 | ClassInfo *class_infos = (ClassInfo *)class_infos_ptr; |
| 474 | |
| 475 | const uint8_t *checkbytes = &clsopt->tab[clsopt->mask+1]; |
| 476 | const int32_t *offsets = (const int32_t *)(checkbytes + clsopt->capacity); |
| 477 | const objc_classheader_v16_t *classOffsets = (const objc_classheader_v16_t *)(offsets + clsopt->capacity); |
| 478 | |
| 479 | DEBUG_PRINTF ("clsopt->capacity = %u\n", clsopt->capacity); |
| 480 | DEBUG_PRINTF ("clsopt->mask = 0x%8.8x\n", clsopt->mask); |
| 481 | DEBUG_PRINTF ("classOffsets = %p\n", classOffsets); |
| 482 | |
| 483 | for (uint32_t i=0; i<clsopt->capacity; ++i) |
| 484 | { |
| 485 | const uint64_t objectCacheOffset = classOffsets[i].objectCacheOffset; |
| 486 | DEBUG_PRINTF("objectCacheOffset[%u] = %u\n", i, objectCacheOffset); |
| 487 | |
| 488 | if (classOffsets[i].isDuplicate) { |
| 489 | DEBUG_PRINTF("isDuplicate = true\n"); |
| 490 | continue; // duplicate |
| 491 | } |
| 492 | |
| 493 | if (objectCacheOffset == 0) { |
| 494 | DEBUG_PRINTF("objectCacheOffset == invalidEntryOffset\n"); |
| 495 | continue; // invalid offset |
| 496 | } |
| 497 | |
| 498 | if (class_infos && idx < max_class_infos) |
| 499 | { |
| 500 | class_infos[idx].isa = (Class)((uint8_t *)shared_cache_base_ptr + objectCacheOffset); |
| 501 | |
| 502 | // Lookup the class name. |
| 503 | const char *name = class_name_lookup_func(class_infos[idx].isa); |
| 504 | DEBUG_PRINTF("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name); |
| 505 | |
| 506 | // Hash the class name so we don't have to read it. |
| 507 | const char *s = name; |
| 508 | uint32_t h = 5381; |
| 509 | for (unsigned char c = *s; c; c = *++s) |
| 510 | { |
| 511 | // class_getName demangles swift names and the hash must |
| 512 | // be calculated on the mangled name. hash==0 means lldb |
| 513 | // will fetch the mangled name and compute the hash in |
| 514 | // ParseClassInfoArray. |
| 515 | if (c == '.') |
| 516 | { |
| 517 | h = 0; |
| 518 | break; |
| 519 | } |
| 520 | h = ((h << 5) + h) + c; |
| 521 | } |
| 522 | class_infos[idx].hash = h; |
| 523 | } |
| 524 | else |
| 525 | { |
| 526 | DEBUG_PRINTF("not(class_infos && idx < max_class_infos)\n"); |
| 527 | } |
| 528 | ++idx; |
| 529 | } |
| 530 | |
| 531 | const uint32_t *duplicate_count_ptr = (uint32_t *)&classOffsets[clsopt->capacity]; |
| 532 | const uint32_t duplicate_count = *duplicate_count_ptr; |
| 533 | const objc_classheader_v16_t *duplicateClassOffsets = (const objc_classheader_v16_t *)(&duplicate_count_ptr[1]); |
| 534 | |
| 535 | DEBUG_PRINTF ("duplicate_count = %u\n", duplicate_count); |
| 536 | DEBUG_PRINTF ("duplicateClassOffsets = %p\n", duplicateClassOffsets); |
| 537 | |
| 538 | for (uint32_t i=0; i<duplicate_count; ++i) |
| 539 | { |
| 540 | const uint64_t objectCacheOffset = classOffsets[i].objectCacheOffset; |
| 541 | DEBUG_PRINTF("objectCacheOffset[%u] = %u\n", i, objectCacheOffset); |
| 542 | |
| 543 | if (classOffsets[i].isDuplicate) { |
| 544 | DEBUG_PRINTF("isDuplicate = true\n"); |
| 545 | continue; // duplicate |
| 546 | } |
| 547 | |
| 548 | if (objectCacheOffset == 0) { |
| 549 | DEBUG_PRINTF("objectCacheOffset == invalidEntryOffset\n"); |
| 550 | continue; // invalid offset |
| 551 | } |
| 552 | |
| 553 | if (class_infos && idx < max_class_infos) |
| 554 | { |
| 555 | class_infos[idx].isa = (Class)((uint8_t *)shared_cache_base_ptr + objectCacheOffset); |
| 556 | |
| 557 | // Lookup the class name. |
| 558 | const char *name = class_name_lookup_func(class_infos[idx].isa); |
| 559 | DEBUG_PRINTF("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name); |
| 560 | |
| 561 | // Hash the class name so we don't have to read it. |
| 562 | const char *s = name; |
| 563 | uint32_t h = 5381; |
| 564 | for (unsigned char c = *s; c; c = *++s) |
| 565 | { |
| 566 | // class_getName demangles swift names and the hash must |
| 567 | // be calculated on the mangled name. hash==0 means lldb |
| 568 | // will fetch the mangled name and compute the hash in |
| 569 | // ParseClassInfoArray. |
| 570 | if (c == '.') |
| 571 | { |
| 572 | h = 0; |
| 573 | break; |
| 574 | } |
| 575 | h = ((h << 5) + h) + c; |
| 576 | } |
| 577 | class_infos[idx].hash = h; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | else if (objc_opt->version >= 12 && objc_opt->version <= 15) |
| 582 | { |
| 583 | const objc_clsopt_t* clsopt = NULL; |
| 584 | if (objc_opt->version >= 14) |
| 585 | clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt_v14 + objc_opt_v14->clsopt_offset); |
| 586 | else |
| 587 | clsopt = (const objc_clsopt_t*)((uint8_t *)objc_opt + objc_opt->clsopt_offset); |
| 588 | const size_t max_class_infos = class_infos_byte_size/sizeof(ClassInfo); |
| 589 | DEBUG_PRINTF("max_class_infos = %llu\n", (uint64_t)max_class_infos); |
| 590 | ClassInfo *class_infos = (ClassInfo *)class_infos_ptr; |
| 591 | int32_t invalidEntryOffset = 0; |
| 592 | // this is safe to do because the version field order is invariant |
| 593 | if (objc_opt->version == 12) |
| 594 | invalidEntryOffset = 16; |
| 595 | const uint8_t *checkbytes = &clsopt->tab[clsopt->mask+1]; |
| 596 | const int32_t *offsets = (const int32_t *)(checkbytes + clsopt->capacity); |
| 597 | const objc_classheader_t *classOffsets = (const objc_classheader_t *)(offsets + clsopt->capacity); |
| 598 | DEBUG_PRINTF ("clsopt->capacity = %u\n", clsopt->capacity); |
| 599 | DEBUG_PRINTF ("clsopt->mask = 0x%8.8x\n", clsopt->mask); |
| 600 | DEBUG_PRINTF ("classOffsets = %p\n", classOffsets); |
| 601 | DEBUG_PRINTF("invalidEntryOffset = %d\n", invalidEntryOffset); |
| 602 | for (uint32_t i=0; i<clsopt->capacity; ++i) |
| 603 | { |
| 604 | const int32_t clsOffset = classOffsets[i].clsOffset; |
| 605 | DEBUG_PRINTF("clsOffset[%u] = %u\n", i, clsOffset); |
| 606 | if (clsOffset & 1) |
| 607 | { |
| 608 | DEBUG_PRINTF("clsOffset & 1\n"); |
| 609 | continue; // duplicate |
| 610 | } |
| 611 | else if (clsOffset == invalidEntryOffset) |
| 612 | { |
| 613 | DEBUG_PRINTF("clsOffset == invalidEntryOffset\n"); |
| 614 | continue; // invalid offset |
| 615 | } |
| 616 | |
| 617 | if (class_infos && idx < max_class_infos) |
| 618 | { |
| 619 | class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset); |
| 620 | const char *name = class_name_lookup_func (class_infos[idx].isa); |
| 621 | DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name); |
| 622 | // Hash the class name so we don't have to read it |
| 623 | const char *s = name; |
| 624 | uint32_t h = 5381; |
| 625 | for (unsigned char c = *s; c; c = *++s) |
| 626 | { |
| 627 | // class_getName demangles swift names and the hash must |
| 628 | // be calculated on the mangled name. hash==0 means lldb |
| 629 | // will fetch the mangled name and compute the hash in |
| 630 | // ParseClassInfoArray. |
| 631 | if (c == '.') |
| 632 | { |
| 633 | h = 0; |
| 634 | break; |
| 635 | } |
| 636 | h = ((h << 5) + h) + c; |
| 637 | } |
| 638 | class_infos[idx].hash = h; |
| 639 | } |
| 640 | else |
| 641 | { |
| 642 | DEBUG_PRINTF("not(class_infos && idx < max_class_infos)\n"); |
| 643 | } |
| 644 | ++idx; |
| 645 | } |
| 646 | |
| 647 | const uint32_t *duplicate_count_ptr = (uint32_t *)&classOffsets[clsopt->capacity]; |
| 648 | const uint32_t duplicate_count = *duplicate_count_ptr; |
| 649 | const objc_classheader_t *duplicateClassOffsets = (const objc_classheader_t *)(&duplicate_count_ptr[1]); |
| 650 | DEBUG_PRINTF ("duplicate_count = %u\n", duplicate_count); |
| 651 | DEBUG_PRINTF ("duplicateClassOffsets = %p\n", duplicateClassOffsets); |
| 652 | for (uint32_t i=0; i<duplicate_count; ++i) |
| 653 | { |
| 654 | const int32_t clsOffset = duplicateClassOffsets[i].clsOffset; |
| 655 | if (clsOffset & 1) |
| 656 | continue; // duplicate |
| 657 | else if (clsOffset == invalidEntryOffset) |
| 658 | continue; // invalid offset |
| 659 | |
| 660 | if (class_infos && idx < max_class_infos) |
| 661 | { |
| 662 | class_infos[idx].isa = (Class)((uint8_t *)clsopt + clsOffset); |
| 663 | const char *name = class_name_lookup_func (class_infos[idx].isa); |
| 664 | DEBUG_PRINTF ("[%u] isa = %8p %s\n", idx, class_infos[idx].isa, name); |
| 665 | // Hash the class name so we don't have to read it |
| 666 | const char *s = name; |
| 667 | uint32_t h = 5381; |
| 668 | for (unsigned char c = *s; c; c = *++s) |
| 669 | { |
| 670 | // class_getName demangles swift names and the hash must |
| 671 | // be calculated on the mangled name. hash==0 means lldb |
| 672 | // will fetch the mangled name and compute the hash in |
| 673 | // ParseClassInfoArray. |
| 674 | if (c == '.') |
| 675 | { |
| 676 | h = 0; |
| 677 | break; |
| 678 | } |
| 679 | h = ((h << 5) + h) + c; |
| 680 | } |
| 681 | class_infos[idx].hash = h; |
| 682 | } |
| 683 | ++idx; |
| 684 | } |
| 685 | } |
| 686 | DEBUG_PRINTF ("%u class_infos\n", idx); |
| 687 | DEBUG_PRINTF ("done\n"); |
| 688 | } |
| 689 | return idx; |
| 690 | } |
| 691 | |
| 692 | |
| 693 | )" ; |
| 694 | |
| 695 | static uint64_t |
| 696 | (Process *process, ConstString name, |
| 697 | const ModuleSP &module_sp, Status &error, |
| 698 | bool read_value = true, uint8_t byte_size = 0, |
| 699 | uint64_t default_value = LLDB_INVALID_ADDRESS, |
| 700 | SymbolType sym_type = lldb::eSymbolTypeData) { |
| 701 | if (!process) { |
| 702 | error = Status::FromErrorString(str: "no process" ); |
| 703 | return default_value; |
| 704 | } |
| 705 | |
| 706 | if (!module_sp) { |
| 707 | error = Status::FromErrorString(str: "no module" ); |
| 708 | return default_value; |
| 709 | } |
| 710 | |
| 711 | if (!byte_size) |
| 712 | byte_size = process->GetAddressByteSize(); |
| 713 | const Symbol *symbol = |
| 714 | module_sp->FindFirstSymbolWithNameAndType(name, symbol_type: lldb::eSymbolTypeData); |
| 715 | |
| 716 | if (!symbol || !symbol->ValueIsAddress()) { |
| 717 | error = Status::FromErrorString(str: "no symbol" ); |
| 718 | return default_value; |
| 719 | } |
| 720 | |
| 721 | lldb::addr_t symbol_load_addr = |
| 722 | symbol->GetAddressRef().GetLoadAddress(target: &process->GetTarget()); |
| 723 | if (symbol_load_addr == LLDB_INVALID_ADDRESS) { |
| 724 | error = Status::FromErrorString(str: "symbol address invalid" ); |
| 725 | return default_value; |
| 726 | } |
| 727 | |
| 728 | if (read_value) |
| 729 | return process->ReadUnsignedIntegerFromMemory(load_addr: symbol_load_addr, byte_size, |
| 730 | fail_value: default_value, error); |
| 731 | return symbol_load_addr; |
| 732 | } |
| 733 | |
| 734 | static void RegisterObjCExceptionRecognizer(Process *process); |
| 735 | |
| 736 | AppleObjCRuntimeV2::AppleObjCRuntimeV2(Process *process, |
| 737 | const ModuleSP &objc_module_sp) |
| 738 | : AppleObjCRuntime(process), m_objc_module_sp(objc_module_sp), |
| 739 | m_dynamic_class_info_extractor(*this), |
| 740 | m_shared_cache_class_info_extractor(*this), m_decl_vendor_up(), |
| 741 | m_tagged_pointer_obfuscator(LLDB_INVALID_ADDRESS), |
| 742 | m_isa_hash_table_ptr(LLDB_INVALID_ADDRESS), |
| 743 | m_relative_selector_base(LLDB_INVALID_ADDRESS), m_hash_signature(), |
| 744 | m_has_object_getClass(false), m_has_objc_copyRealizedClassList(false), |
| 745 | m_has_objc_getRealizedClassList_trylock(false), m_loaded_objc_opt(false), |
| 746 | m_non_pointer_isa_cache_up(), |
| 747 | m_tagged_pointer_vendor_up( |
| 748 | TaggedPointerVendorV2::CreateInstance(runtime&: *this, objc_module_sp)), |
| 749 | m_encoding_to_type_sp(), m_CFBoolean_values(), |
| 750 | m_realized_class_generation_count(0) { |
| 751 | static const ConstString g_gdb_object_getClass("gdb_object_getClass" ); |
| 752 | m_has_object_getClass = HasSymbol(Name: g_gdb_object_getClass); |
| 753 | static const ConstString g_objc_copyRealizedClassList( |
| 754 | "_ZL33objc_copyRealizedClassList_nolockPj" ); |
| 755 | static const ConstString g_objc_getRealizedClassList_trylock( |
| 756 | "_objc_getRealizedClassList_trylock" ); |
| 757 | m_has_objc_copyRealizedClassList = HasSymbol(Name: g_objc_copyRealizedClassList); |
| 758 | m_has_objc_getRealizedClassList_trylock = |
| 759 | HasSymbol(Name: g_objc_getRealizedClassList_trylock); |
| 760 | WarnIfNoExpandedSharedCache(); |
| 761 | RegisterObjCExceptionRecognizer(process); |
| 762 | } |
| 763 | |
| 764 | LanguageRuntime * |
| 765 | AppleObjCRuntimeV2::GetPreferredLanguageRuntime(ValueObject &in_value) { |
| 766 | if (auto process_sp = in_value.GetProcessSP()) { |
| 767 | assert(process_sp.get() == m_process); |
| 768 | if (auto descriptor_sp = GetNonKVOClassDescriptor(in_value)) { |
| 769 | LanguageType impl_lang = descriptor_sp->GetImplementationLanguage(); |
| 770 | if (impl_lang != eLanguageTypeUnknown) |
| 771 | return process_sp->GetLanguageRuntime(language: impl_lang); |
| 772 | } |
| 773 | } |
| 774 | return nullptr; |
| 775 | } |
| 776 | |
| 777 | bool AppleObjCRuntimeV2::GetDynamicTypeAndAddress( |
| 778 | ValueObject &in_value, lldb::DynamicValueType use_dynamic, |
| 779 | TypeAndOrName &class_type_or_name, Address &address, |
| 780 | Value::ValueType &value_type, llvm::ArrayRef<uint8_t> &local_buffer) { |
| 781 | // We should never get here with a null process... |
| 782 | assert(m_process != nullptr); |
| 783 | |
| 784 | // The Runtime is attached to a particular process, you shouldn't pass in a |
| 785 | // value from another process. Note, however, the process might be NULL (e.g. |
| 786 | // if the value was made with SBTarget::EvaluateExpression...) in which case |
| 787 | // it is sufficient if the target's match: |
| 788 | |
| 789 | Process *process = in_value.GetProcessSP().get(); |
| 790 | if (process) |
| 791 | assert(process == m_process); |
| 792 | else |
| 793 | assert(in_value.GetTargetSP().get() == m_process->CalculateTarget().get()); |
| 794 | |
| 795 | class_type_or_name.Clear(); |
| 796 | value_type = Value::ValueType::Scalar; |
| 797 | |
| 798 | // Make sure we can have a dynamic value before starting... |
| 799 | if (CouldHaveDynamicValue(in_value)) { |
| 800 | // First job, pull out the address at 0 offset from the object That will |
| 801 | // be the ISA pointer. |
| 802 | ClassDescriptorSP objc_class_sp(GetNonKVOClassDescriptor(in_value)); |
| 803 | if (objc_class_sp) { |
| 804 | const addr_t object_ptr = in_value.GetPointerValue().address; |
| 805 | address.SetRawAddress(object_ptr); |
| 806 | |
| 807 | ConstString class_name(objc_class_sp->GetClassName()); |
| 808 | class_type_or_name.SetName(class_name); |
| 809 | TypeSP type_sp(objc_class_sp->GetType()); |
| 810 | if (type_sp) |
| 811 | class_type_or_name.SetTypeSP(type_sp); |
| 812 | else { |
| 813 | type_sp = LookupInCompleteClassCache(name&: class_name); |
| 814 | if (type_sp) { |
| 815 | objc_class_sp->SetType(type_sp); |
| 816 | class_type_or_name.SetTypeSP(type_sp); |
| 817 | } else { |
| 818 | // try to go for a CompilerType at least |
| 819 | if (auto *vendor = GetDeclVendor()) { |
| 820 | auto types = vendor->FindTypes(name: class_name, /*max_matches*/ 1); |
| 821 | if (!types.empty()) |
| 822 | class_type_or_name.SetCompilerType(types.front()); |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | return !class_type_or_name.IsEmpty(); |
| 829 | } |
| 830 | |
| 831 | // Static Functions |
| 832 | LanguageRuntime *AppleObjCRuntimeV2::CreateInstance(Process *process, |
| 833 | LanguageType language) { |
| 834 | // FIXME: This should be a MacOS or iOS process, and we need to look for the |
| 835 | // OBJC section to make |
| 836 | // sure we aren't using the V1 runtime. |
| 837 | if (language == eLanguageTypeObjC) { |
| 838 | ModuleSP objc_module_sp; |
| 839 | |
| 840 | if (AppleObjCRuntime::GetObjCVersion(process, objc_module_sp) == |
| 841 | ObjCRuntimeVersions::eAppleObjC_V2) |
| 842 | return new AppleObjCRuntimeV2(process, objc_module_sp); |
| 843 | return nullptr; |
| 844 | } |
| 845 | return nullptr; |
| 846 | } |
| 847 | |
| 848 | static constexpr OptionDefinition g_objc_classtable_dump_options[] = { |
| 849 | {LLDB_OPT_SET_ALL, |
| 850 | .required: false, |
| 851 | .long_option: "verbose" , |
| 852 | .short_option: 'v', |
| 853 | .option_has_arg: OptionParser::eNoArgument, |
| 854 | .validator: nullptr, |
| 855 | .enum_values: {}, |
| 856 | .completion_type: 0, |
| 857 | .argument_type: eArgTypeNone, |
| 858 | .usage_text: "Print ivar and method information in detail" }}; |
| 859 | |
| 860 | class CommandObjectObjC_ClassTable_Dump : public CommandObjectParsed { |
| 861 | public: |
| 862 | class CommandOptions : public Options { |
| 863 | public: |
| 864 | CommandOptions() : Options(), m_verbose(false, false) {} |
| 865 | |
| 866 | ~CommandOptions() override = default; |
| 867 | |
| 868 | Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, |
| 869 | ExecutionContext *execution_context) override { |
| 870 | Status error; |
| 871 | const int short_option = m_getopt_table[option_idx].val; |
| 872 | switch (short_option) { |
| 873 | case 'v': |
| 874 | m_verbose.SetCurrentValue(true); |
| 875 | m_verbose.SetOptionWasSet(); |
| 876 | break; |
| 877 | |
| 878 | default: |
| 879 | error = Status::FromErrorStringWithFormat( |
| 880 | format: "unrecognized short option '%c'" , short_option); |
| 881 | break; |
| 882 | } |
| 883 | |
| 884 | return error; |
| 885 | } |
| 886 | |
| 887 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 888 | m_verbose.Clear(); |
| 889 | } |
| 890 | |
| 891 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
| 892 | return llvm::ArrayRef(g_objc_classtable_dump_options); |
| 893 | } |
| 894 | |
| 895 | OptionValueBoolean m_verbose; |
| 896 | }; |
| 897 | |
| 898 | CommandObjectObjC_ClassTable_Dump(CommandInterpreter &interpreter) |
| 899 | : CommandObjectParsed(interpreter, "dump" , |
| 900 | "Dump information on Objective-C classes " |
| 901 | "known to the current process." , |
| 902 | "language objc class-table dump" , |
| 903 | eCommandRequiresProcess | |
| 904 | eCommandProcessMustBeLaunched | |
| 905 | eCommandProcessMustBePaused), |
| 906 | m_options() { |
| 907 | AddSimpleArgumentList(arg_type: eArgTypeRegularExpression, repetition_type: eArgRepeatOptional); |
| 908 | } |
| 909 | |
| 910 | ~CommandObjectObjC_ClassTable_Dump() override = default; |
| 911 | |
| 912 | Options *GetOptions() override { return &m_options; } |
| 913 | |
| 914 | protected: |
| 915 | void DoExecute(Args &command, CommandReturnObject &result) override { |
| 916 | std::unique_ptr<RegularExpression> regex_up; |
| 917 | switch (command.GetArgumentCount()) { |
| 918 | case 0: |
| 919 | break; |
| 920 | case 1: { |
| 921 | regex_up = |
| 922 | std::make_unique<RegularExpression>(args: command.GetArgumentAtIndex(idx: 0)); |
| 923 | if (!regex_up->IsValid()) { |
| 924 | result.AppendError( |
| 925 | in_string: "invalid argument - please provide a valid regular expression" ); |
| 926 | result.SetStatus(lldb::eReturnStatusFailed); |
| 927 | return; |
| 928 | } |
| 929 | break; |
| 930 | } |
| 931 | default: { |
| 932 | result.AppendError(in_string: "please provide 0 or 1 arguments" ); |
| 933 | result.SetStatus(lldb::eReturnStatusFailed); |
| 934 | return; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 939 | ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(process&: *process); |
| 940 | if (objc_runtime) { |
| 941 | auto iterators_pair = objc_runtime->GetDescriptorIteratorPair(); |
| 942 | auto iterator = iterators_pair.first; |
| 943 | auto &std_out = result.GetOutputStream(); |
| 944 | for (; iterator != iterators_pair.second; iterator++) { |
| 945 | if (iterator->second) { |
| 946 | const char *class_name = |
| 947 | iterator->second->GetClassName().AsCString(value_if_empty: "<unknown>" ); |
| 948 | if (regex_up && class_name && |
| 949 | !regex_up->Execute(string: llvm::StringRef(class_name))) |
| 950 | continue; |
| 951 | std_out.Printf(format: "isa = 0x%" PRIx64, iterator->first); |
| 952 | std_out.Printf(format: " name = %s" , class_name); |
| 953 | std_out.Printf(format: " instance size = %" PRIu64, |
| 954 | iterator->second->GetInstanceSize()); |
| 955 | std_out.Printf(format: " num ivars = %" PRIuPTR, |
| 956 | (uintptr_t)iterator->second->GetNumIVars()); |
| 957 | if (auto superclass = iterator->second->GetSuperclass()) { |
| 958 | std_out.Printf(format: " superclass = %s" , |
| 959 | superclass->GetClassName().AsCString(value_if_empty: "<unknown>" )); |
| 960 | } |
| 961 | std_out.Printf(format: "\n" ); |
| 962 | if (m_options.m_verbose) { |
| 963 | for (size_t i = 0; i < iterator->second->GetNumIVars(); i++) { |
| 964 | auto ivar = iterator->second->GetIVarAtIndex(idx: i); |
| 965 | std_out.Printf( |
| 966 | format: " ivar name = %s type = %s size = %" PRIu64 |
| 967 | " offset = %" PRId32 "\n" , |
| 968 | ivar.m_name.AsCString(value_if_empty: "<unknown>" ), |
| 969 | ivar.m_type.GetDisplayTypeName().AsCString(value_if_empty: "<unknown>" ), |
| 970 | ivar.m_size, ivar.m_offset); |
| 971 | } |
| 972 | |
| 973 | iterator->second->Describe( |
| 974 | superclass_func: nullptr, |
| 975 | instance_method_func: [&std_out](const char *name, const char *type) -> bool { |
| 976 | std_out.Printf(format: " instance method name = %s type = %s\n" , |
| 977 | name, type); |
| 978 | return false; |
| 979 | }, |
| 980 | class_method_func: [&std_out](const char *name, const char *type) -> bool { |
| 981 | std_out.Printf(format: " class method name = %s type = %s\n" , name, |
| 982 | type); |
| 983 | return false; |
| 984 | }, |
| 985 | ivar_func: nullptr); |
| 986 | } |
| 987 | } else { |
| 988 | if (regex_up && !regex_up->Execute(string: llvm::StringRef())) |
| 989 | continue; |
| 990 | std_out.Printf(format: "isa = 0x%" PRIx64 " has no associated class.\n" , |
| 991 | iterator->first); |
| 992 | } |
| 993 | } |
| 994 | result.SetStatus(lldb::eReturnStatusSuccessFinishResult); |
| 995 | return; |
| 996 | } |
| 997 | result.AppendError(in_string: "current process has no Objective-C runtime loaded" ); |
| 998 | result.SetStatus(lldb::eReturnStatusFailed); |
| 999 | } |
| 1000 | |
| 1001 | CommandOptions m_options; |
| 1002 | }; |
| 1003 | |
| 1004 | class CommandObjectMultiwordObjC_TaggedPointer_Info |
| 1005 | : public CommandObjectParsed { |
| 1006 | public: |
| 1007 | CommandObjectMultiwordObjC_TaggedPointer_Info(CommandInterpreter &interpreter) |
| 1008 | : CommandObjectParsed( |
| 1009 | interpreter, "info" , "Dump information on a tagged pointer." , |
| 1010 | "language objc tagged-pointer info" , |
| 1011 | eCommandRequiresProcess | eCommandProcessMustBeLaunched | |
| 1012 | eCommandProcessMustBePaused) { |
| 1013 | AddSimpleArgumentList(arg_type: eArgTypeAddress, repetition_type: eArgRepeatPlus); |
| 1014 | } |
| 1015 | |
| 1016 | ~CommandObjectMultiwordObjC_TaggedPointer_Info() override = default; |
| 1017 | |
| 1018 | protected: |
| 1019 | void DoExecute(Args &command, CommandReturnObject &result) override { |
| 1020 | if (command.GetArgumentCount() == 0) { |
| 1021 | result.AppendError(in_string: "this command requires arguments" ); |
| 1022 | result.SetStatus(lldb::eReturnStatusFailed); |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | Process *process = m_exe_ctx.GetProcessPtr(); |
| 1027 | ExecutionContext exe_ctx(process); |
| 1028 | |
| 1029 | ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(process&: *process); |
| 1030 | if (!objc_runtime) { |
| 1031 | result.AppendError(in_string: "current process has no Objective-C runtime loaded" ); |
| 1032 | result.SetStatus(lldb::eReturnStatusFailed); |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| 1036 | ObjCLanguageRuntime::TaggedPointerVendor *tagged_ptr_vendor = |
| 1037 | objc_runtime->GetTaggedPointerVendor(); |
| 1038 | if (!tagged_ptr_vendor) { |
| 1039 | result.AppendError(in_string: "current process has no tagged pointer support" ); |
| 1040 | result.SetStatus(lldb::eReturnStatusFailed); |
| 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | for (size_t i = 0; i < command.GetArgumentCount(); i++) { |
| 1045 | const char *arg_str = command.GetArgumentAtIndex(idx: i); |
| 1046 | if (!arg_str) |
| 1047 | continue; |
| 1048 | |
| 1049 | Status error; |
| 1050 | lldb::addr_t arg_addr = OptionArgParser::ToRawAddress( |
| 1051 | exe_ctx: &exe_ctx, s: arg_str, LLDB_INVALID_ADDRESS, error_ptr: &error); |
| 1052 | if (arg_addr == 0 || arg_addr == LLDB_INVALID_ADDRESS || error.Fail()) { |
| 1053 | result.AppendErrorWithFormatv( |
| 1054 | format: "could not convert '{0}' to a valid address\n" , args&: arg_str); |
| 1055 | result.SetStatus(lldb::eReturnStatusFailed); |
| 1056 | return; |
| 1057 | } |
| 1058 | |
| 1059 | if (!tagged_ptr_vendor->IsPossibleTaggedPointer(ptr: arg_addr)) { |
| 1060 | result.GetOutputStream().Format(format: "{0:x16} is not tagged\n" , args&: arg_addr); |
| 1061 | continue; |
| 1062 | } |
| 1063 | |
| 1064 | auto descriptor_sp = tagged_ptr_vendor->GetClassDescriptor(ptr: arg_addr); |
| 1065 | if (!descriptor_sp) { |
| 1066 | result.AppendErrorWithFormatv( |
| 1067 | format: "could not get class descriptor for {0:x16}\n" , args&: arg_addr); |
| 1068 | result.SetStatus(lldb::eReturnStatusFailed); |
| 1069 | return; |
| 1070 | } |
| 1071 | |
| 1072 | uint64_t info_bits = 0; |
| 1073 | uint64_t value_bits = 0; |
| 1074 | uint64_t payload = 0; |
| 1075 | if (descriptor_sp->GetTaggedPointerInfo(info_bits: &info_bits, value_bits: &value_bits, |
| 1076 | payload: &payload)) { |
| 1077 | result.GetOutputStream().Format( |
| 1078 | format: "{0:x} is tagged\n" |
| 1079 | "\tpayload = {1:x16}\n" |
| 1080 | "\tvalue = {2:x16}\n" |
| 1081 | "\tinfo bits = {3:x16}\n" |
| 1082 | "\tclass = {4}\n" , |
| 1083 | args&: arg_addr, args&: payload, args&: value_bits, args&: info_bits, |
| 1084 | args: descriptor_sp->GetClassName().AsCString(value_if_empty: "<unknown>" )); |
| 1085 | } else { |
| 1086 | result.GetOutputStream().Format(format: "{0:x16} is not tagged\n" , args&: arg_addr); |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | result.SetStatus(lldb::eReturnStatusSuccessFinishResult); |
| 1091 | } |
| 1092 | }; |
| 1093 | |
| 1094 | class CommandObjectMultiwordObjC_ClassTable : public CommandObjectMultiword { |
| 1095 | public: |
| 1096 | CommandObjectMultiwordObjC_ClassTable(CommandInterpreter &interpreter) |
| 1097 | : CommandObjectMultiword( |
| 1098 | interpreter, "class-table" , |
| 1099 | "Commands for operating on the Objective-C class table." , |
| 1100 | "class-table <subcommand> [<subcommand-options>]" ) { |
| 1101 | LoadSubCommand( |
| 1102 | cmd_name: "dump" , |
| 1103 | command_obj: CommandObjectSP(new CommandObjectObjC_ClassTable_Dump(interpreter))); |
| 1104 | } |
| 1105 | |
| 1106 | ~CommandObjectMultiwordObjC_ClassTable() override = default; |
| 1107 | }; |
| 1108 | |
| 1109 | class CommandObjectMultiwordObjC_TaggedPointer : public CommandObjectMultiword { |
| 1110 | public: |
| 1111 | CommandObjectMultiwordObjC_TaggedPointer(CommandInterpreter &interpreter) |
| 1112 | : CommandObjectMultiword( |
| 1113 | interpreter, "tagged-pointer" , |
| 1114 | "Commands for operating on Objective-C tagged pointers." , |
| 1115 | "tagged-pointer <subcommand> [<subcommand-options>]" ) { |
| 1116 | LoadSubCommand( |
| 1117 | cmd_name: "info" , |
| 1118 | command_obj: CommandObjectSP( |
| 1119 | new CommandObjectMultiwordObjC_TaggedPointer_Info(interpreter))); |
| 1120 | } |
| 1121 | |
| 1122 | ~CommandObjectMultiwordObjC_TaggedPointer() override = default; |
| 1123 | }; |
| 1124 | |
| 1125 | class CommandObjectMultiwordObjC : public CommandObjectMultiword { |
| 1126 | public: |
| 1127 | CommandObjectMultiwordObjC(CommandInterpreter &interpreter) |
| 1128 | : CommandObjectMultiword( |
| 1129 | interpreter, "objc" , |
| 1130 | "Commands for operating on the Objective-C language runtime." , |
| 1131 | "objc <subcommand> [<subcommand-options>]" ) { |
| 1132 | LoadSubCommand(cmd_name: "class-table" , |
| 1133 | command_obj: CommandObjectSP( |
| 1134 | new CommandObjectMultiwordObjC_ClassTable(interpreter))); |
| 1135 | LoadSubCommand(cmd_name: "tagged-pointer" , |
| 1136 | command_obj: CommandObjectSP(new CommandObjectMultiwordObjC_TaggedPointer( |
| 1137 | interpreter))); |
| 1138 | } |
| 1139 | |
| 1140 | ~CommandObjectMultiwordObjC() override = default; |
| 1141 | }; |
| 1142 | |
| 1143 | void AppleObjCRuntimeV2::Initialize() { |
| 1144 | PluginManager::RegisterPlugin( |
| 1145 | name: GetPluginNameStatic(), description: "Apple Objective-C Language Runtime - Version 2" , |
| 1146 | create_callback: CreateInstance, |
| 1147 | command_callback: [](CommandInterpreter &interpreter) -> lldb::CommandObjectSP { |
| 1148 | return CommandObjectSP(new CommandObjectMultiwordObjC(interpreter)); |
| 1149 | }, |
| 1150 | precondition_callback: GetBreakpointExceptionPrecondition); |
| 1151 | } |
| 1152 | |
| 1153 | void AppleObjCRuntimeV2::Terminate() { |
| 1154 | PluginManager::UnregisterPlugin(create_callback: CreateInstance); |
| 1155 | } |
| 1156 | |
| 1157 | BreakpointResolverSP |
| 1158 | AppleObjCRuntimeV2::CreateExceptionResolver(const BreakpointSP &bkpt, |
| 1159 | bool catch_bp, bool throw_bp) { |
| 1160 | BreakpointResolverSP resolver_sp; |
| 1161 | |
| 1162 | if (throw_bp) |
| 1163 | resolver_sp = std::make_shared<BreakpointResolverName>( |
| 1164 | args: bkpt, args: std::get<1>(t: GetExceptionThrowLocation()).AsCString(), |
| 1165 | args: eFunctionNameTypeBase, args: eLanguageTypeUnknown, args: Breakpoint::Exact, args: 0, |
| 1166 | args: eLazyBoolNo); |
| 1167 | // FIXME: We don't do catch breakpoints for ObjC yet. |
| 1168 | // Should there be some way for the runtime to specify what it can do in this |
| 1169 | // regard? |
| 1170 | return resolver_sp; |
| 1171 | } |
| 1172 | |
| 1173 | llvm::Expected<std::unique_ptr<UtilityFunction>> |
| 1174 | AppleObjCRuntimeV2::CreateObjectChecker(std::string name, |
| 1175 | ExecutionContext &exe_ctx) { |
| 1176 | char check_function_code[2048]; |
| 1177 | |
| 1178 | int len = 0; |
| 1179 | if (m_has_object_getClass) { |
| 1180 | len = ::snprintf(s: check_function_code, maxlen: sizeof(check_function_code), format: R"( |
| 1181 | extern "C" void *gdb_object_getClass(void *); |
| 1182 | extern "C" int printf(const char *format, ...); |
| 1183 | extern "C" void |
| 1184 | %s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) { |
| 1185 | if ($__lldb_arg_obj == (void *)0) |
| 1186 | return; // nil is ok |
| 1187 | if (!gdb_object_getClass($__lldb_arg_obj)) { |
| 1188 | *((volatile int *)0) = 'ocgc'; |
| 1189 | } else if ($__lldb_arg_selector != (void *)0) { |
| 1190 | signed char $responds = (signed char) |
| 1191 | [(id)$__lldb_arg_obj respondsToSelector: |
| 1192 | (void *) $__lldb_arg_selector]; |
| 1193 | if ($responds == (signed char) 0) |
| 1194 | *((volatile int *)0) = 'ocgc'; |
| 1195 | } |
| 1196 | })" , |
| 1197 | name.c_str()); |
| 1198 | } else { |
| 1199 | len = ::snprintf(s: check_function_code, maxlen: sizeof(check_function_code), format: R"( |
| 1200 | extern "C" void *gdb_class_getClass(void *); |
| 1201 | extern "C" int printf(const char *format, ...); |
| 1202 | extern "C" void |
| 1203 | %s(void *$__lldb_arg_obj, void *$__lldb_arg_selector) { |
| 1204 | if ($__lldb_arg_obj == (void *)0) |
| 1205 | return; // nil is ok |
| 1206 | void **$isa_ptr = (void **)$__lldb_arg_obj; |
| 1207 | if (*$isa_ptr == (void *)0 || |
| 1208 | !gdb_class_getClass(*$isa_ptr)) |
| 1209 | *((volatile int *)0) = 'ocgc'; |
| 1210 | else if ($__lldb_arg_selector != (void *)0) { |
| 1211 | signed char $responds = (signed char) |
| 1212 | [(id)$__lldb_arg_obj respondsToSelector: |
| 1213 | (void *) $__lldb_arg_selector]; |
| 1214 | if ($responds == (signed char) 0) |
| 1215 | *((volatile int *)0) = 'ocgc'; |
| 1216 | } |
| 1217 | })" , |
| 1218 | name.c_str()); |
| 1219 | } |
| 1220 | |
| 1221 | assert(len < (int)sizeof(check_function_code)); |
| 1222 | UNUSED_IF_ASSERT_DISABLED(len); |
| 1223 | |
| 1224 | return GetTargetRef().CreateUtilityFunction(expression: check_function_code, name, |
| 1225 | language: eLanguageTypeC, exe_ctx); |
| 1226 | } |
| 1227 | |
| 1228 | size_t AppleObjCRuntimeV2::GetByteOffsetForIvar(CompilerType &parent_ast_type, |
| 1229 | const char *ivar_name) { |
| 1230 | uint32_t ivar_offset = LLDB_INVALID_IVAR_OFFSET; |
| 1231 | |
| 1232 | ConstString class_name = parent_ast_type.GetTypeName(); |
| 1233 | if (!class_name.IsEmpty() && ivar_name && ivar_name[0]) { |
| 1234 | // Make the objective C V2 mangled name for the ivar offset from the class |
| 1235 | // name and ivar name |
| 1236 | std::string buffer("OBJC_IVAR_$_" ); |
| 1237 | buffer.append(s: class_name.AsCString()); |
| 1238 | buffer.push_back(c: '.'); |
| 1239 | buffer.append(s: ivar_name); |
| 1240 | ConstString ivar_const_str(buffer.c_str()); |
| 1241 | |
| 1242 | // Try to get the ivar offset address from the symbol table first using the |
| 1243 | // name we created above |
| 1244 | SymbolContextList sc_list; |
| 1245 | Target &target = m_process->GetTarget(); |
| 1246 | target.GetImages().FindSymbolsWithNameAndType(name: ivar_const_str, |
| 1247 | symbol_type: eSymbolTypeObjCIVar, sc_list); |
| 1248 | |
| 1249 | addr_t ivar_offset_address = LLDB_INVALID_ADDRESS; |
| 1250 | |
| 1251 | Status error; |
| 1252 | SymbolContext ivar_offset_symbol; |
| 1253 | if (sc_list.GetSize() == 1 && |
| 1254 | sc_list.GetContextAtIndex(idx: 0, sc&: ivar_offset_symbol)) { |
| 1255 | if (ivar_offset_symbol.symbol) |
| 1256 | ivar_offset_address = |
| 1257 | ivar_offset_symbol.symbol->GetLoadAddress(target: &target); |
| 1258 | } |
| 1259 | |
| 1260 | // If we didn't get the ivar offset address from the symbol table, fall |
| 1261 | // back to getting it from the runtime |
| 1262 | if (ivar_offset_address == LLDB_INVALID_ADDRESS) |
| 1263 | ivar_offset_address = LookupRuntimeSymbol(name: ivar_const_str); |
| 1264 | |
| 1265 | if (ivar_offset_address != LLDB_INVALID_ADDRESS) |
| 1266 | ivar_offset = m_process->ReadUnsignedIntegerFromMemory( |
| 1267 | load_addr: ivar_offset_address, byte_size: 4, LLDB_INVALID_IVAR_OFFSET, error); |
| 1268 | } |
| 1269 | return ivar_offset; |
| 1270 | } |
| 1271 | |
| 1272 | // tagged pointers are special not-a-real-pointer values that contain both type |
| 1273 | // and value information this routine attempts to check with as little |
| 1274 | // computational effort as possible whether something could possibly be a |
| 1275 | // tagged pointer - false positives are possible but false negatives shouldn't |
| 1276 | bool AppleObjCRuntimeV2::IsTaggedPointer(addr_t ptr) { |
| 1277 | if (!m_tagged_pointer_vendor_up) |
| 1278 | return false; |
| 1279 | return m_tagged_pointer_vendor_up->IsPossibleTaggedPointer(ptr); |
| 1280 | } |
| 1281 | |
| 1282 | class RemoteNXMapTable { |
| 1283 | public: |
| 1284 | RemoteNXMapTable() : m_end_iterator(*this, -1) {} |
| 1285 | |
| 1286 | void Dump() { |
| 1287 | printf(format: "RemoteNXMapTable.m_load_addr = 0x%" PRIx64 "\n" , m_load_addr); |
| 1288 | printf(format: "RemoteNXMapTable.m_count = %u\n" , m_count); |
| 1289 | printf(format: "RemoteNXMapTable.m_num_buckets_minus_one = %u\n" , |
| 1290 | m_num_buckets_minus_one); |
| 1291 | printf(format: "RemoteNXMapTable.m_buckets_ptr = 0x%" PRIX64 "\n" , m_buckets_ptr); |
| 1292 | } |
| 1293 | |
| 1294 | bool (Process *process, lldb::addr_t load_addr) { |
| 1295 | m_process = process; |
| 1296 | m_load_addr = load_addr; |
| 1297 | m_map_pair_size = m_process->GetAddressByteSize() * 2; |
| 1298 | m_invalid_key = |
| 1299 | m_process->GetAddressByteSize() == 8 ? UINT64_MAX : UINT32_MAX; |
| 1300 | Status err; |
| 1301 | |
| 1302 | // This currently holds true for all platforms we support, but we might |
| 1303 | // need to change this to use get the actually byte size of "unsigned" from |
| 1304 | // the target AST... |
| 1305 | const uint32_t unsigned_byte_size = sizeof(uint32_t); |
| 1306 | // Skip the prototype as we don't need it (const struct |
| 1307 | // +NXMapTablePrototype *prototype) |
| 1308 | |
| 1309 | bool success = true; |
| 1310 | if (load_addr == LLDB_INVALID_ADDRESS) |
| 1311 | success = false; |
| 1312 | else { |
| 1313 | lldb::addr_t cursor = load_addr + m_process->GetAddressByteSize(); |
| 1314 | |
| 1315 | // unsigned count; |
| 1316 | m_count = m_process->ReadUnsignedIntegerFromMemory( |
| 1317 | load_addr: cursor, byte_size: unsigned_byte_size, fail_value: 0, error&: err); |
| 1318 | if (m_count) { |
| 1319 | cursor += unsigned_byte_size; |
| 1320 | |
| 1321 | // unsigned nbBucketsMinusOne; |
| 1322 | m_num_buckets_minus_one = m_process->ReadUnsignedIntegerFromMemory( |
| 1323 | load_addr: cursor, byte_size: unsigned_byte_size, fail_value: 0, error&: err); |
| 1324 | cursor += unsigned_byte_size; |
| 1325 | |
| 1326 | // void *buckets; |
| 1327 | m_buckets_ptr = m_process->ReadPointerFromMemory(vm_addr: cursor, error&: err); |
| 1328 | |
| 1329 | success = m_count > 0 && m_buckets_ptr != LLDB_INVALID_ADDRESS; |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | if (!success) { |
| 1334 | m_count = 0; |
| 1335 | m_num_buckets_minus_one = 0; |
| 1336 | m_buckets_ptr = LLDB_INVALID_ADDRESS; |
| 1337 | } |
| 1338 | return success; |
| 1339 | } |
| 1340 | |
| 1341 | // const_iterator mimics NXMapState and its code comes from NXInitMapState |
| 1342 | // and NXNextMapState. |
| 1343 | typedef std::pair<ConstString, ObjCLanguageRuntime::ObjCISA> element; |
| 1344 | |
| 1345 | friend class const_iterator; |
| 1346 | class const_iterator { |
| 1347 | public: |
| 1348 | const_iterator(RemoteNXMapTable &parent, int index) |
| 1349 | : m_parent(parent), m_index(index) { |
| 1350 | AdvanceToValidIndex(); |
| 1351 | } |
| 1352 | |
| 1353 | const_iterator(const const_iterator &rhs) |
| 1354 | : m_parent(rhs.m_parent), m_index(rhs.m_index) { |
| 1355 | // AdvanceToValidIndex() has been called by rhs already. |
| 1356 | } |
| 1357 | |
| 1358 | const_iterator &operator=(const const_iterator &rhs) { |
| 1359 | // AdvanceToValidIndex() has been called by rhs already. |
| 1360 | assert(&m_parent == &rhs.m_parent); |
| 1361 | m_index = rhs.m_index; |
| 1362 | return *this; |
| 1363 | } |
| 1364 | |
| 1365 | bool operator==(const const_iterator &rhs) const { |
| 1366 | if (&m_parent != &rhs.m_parent) |
| 1367 | return false; |
| 1368 | if (m_index != rhs.m_index) |
| 1369 | return false; |
| 1370 | |
| 1371 | return true; |
| 1372 | } |
| 1373 | |
| 1374 | bool operator!=(const const_iterator &rhs) const { |
| 1375 | return !(operator==(rhs)); |
| 1376 | } |
| 1377 | |
| 1378 | const_iterator &operator++() { |
| 1379 | AdvanceToValidIndex(); |
| 1380 | return *this; |
| 1381 | } |
| 1382 | |
| 1383 | element operator*() const { |
| 1384 | if (m_index == -1) { |
| 1385 | // TODO find a way to make this an error, but not an assert |
| 1386 | return element(); |
| 1387 | } |
| 1388 | |
| 1389 | lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr; |
| 1390 | size_t map_pair_size = m_parent.m_map_pair_size; |
| 1391 | lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size); |
| 1392 | |
| 1393 | Status err; |
| 1394 | |
| 1395 | lldb::addr_t key = |
| 1396 | m_parent.m_process->ReadPointerFromMemory(vm_addr: pair_ptr, error&: err); |
| 1397 | if (!err.Success()) |
| 1398 | return element(); |
| 1399 | lldb::addr_t value = m_parent.m_process->ReadPointerFromMemory( |
| 1400 | vm_addr: pair_ptr + m_parent.m_process->GetAddressByteSize(), error&: err); |
| 1401 | if (!err.Success()) |
| 1402 | return element(); |
| 1403 | |
| 1404 | std::string key_string; |
| 1405 | |
| 1406 | m_parent.m_process->ReadCStringFromMemory(vm_addr: key, out_str&: key_string, error&: err); |
| 1407 | if (!err.Success()) |
| 1408 | return element(); |
| 1409 | |
| 1410 | return element(ConstString(key_string.c_str()), |
| 1411 | (ObjCLanguageRuntime::ObjCISA)value); |
| 1412 | } |
| 1413 | |
| 1414 | private: |
| 1415 | void AdvanceToValidIndex() { |
| 1416 | if (m_index == -1) |
| 1417 | return; |
| 1418 | |
| 1419 | const lldb::addr_t pairs_ptr = m_parent.m_buckets_ptr; |
| 1420 | const size_t map_pair_size = m_parent.m_map_pair_size; |
| 1421 | const lldb::addr_t invalid_key = m_parent.m_invalid_key; |
| 1422 | Status err; |
| 1423 | |
| 1424 | while (m_index--) { |
| 1425 | lldb::addr_t pair_ptr = pairs_ptr + (m_index * map_pair_size); |
| 1426 | lldb::addr_t key = |
| 1427 | m_parent.m_process->ReadPointerFromMemory(vm_addr: pair_ptr, error&: err); |
| 1428 | |
| 1429 | if (!err.Success()) { |
| 1430 | m_index = -1; |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | if (key != invalid_key) |
| 1435 | return; |
| 1436 | } |
| 1437 | } |
| 1438 | RemoteNXMapTable &m_parent; |
| 1439 | int m_index; |
| 1440 | }; |
| 1441 | |
| 1442 | const_iterator begin() { |
| 1443 | return const_iterator(*this, m_num_buckets_minus_one + 1); |
| 1444 | } |
| 1445 | |
| 1446 | const_iterator end() { return m_end_iterator; } |
| 1447 | |
| 1448 | uint32_t GetCount() const { return m_count; } |
| 1449 | |
| 1450 | uint32_t GetBucketCount() const { return m_num_buckets_minus_one; } |
| 1451 | |
| 1452 | lldb::addr_t GetBucketDataPointer() const { return m_buckets_ptr; } |
| 1453 | |
| 1454 | lldb::addr_t GetTableLoadAddress() const { return m_load_addr; } |
| 1455 | |
| 1456 | private: |
| 1457 | // contents of _NXMapTable struct |
| 1458 | uint32_t m_count = 0; |
| 1459 | uint32_t m_num_buckets_minus_one = 0; |
| 1460 | lldb::addr_t m_buckets_ptr = LLDB_INVALID_ADDRESS; |
| 1461 | lldb_private::Process *m_process = nullptr; |
| 1462 | const_iterator m_end_iterator; |
| 1463 | lldb::addr_t m_load_addr = LLDB_INVALID_ADDRESS; |
| 1464 | size_t m_map_pair_size = 0; |
| 1465 | lldb::addr_t m_invalid_key = 0; |
| 1466 | }; |
| 1467 | |
| 1468 | AppleObjCRuntimeV2::HashTableSignature::HashTableSignature() = default; |
| 1469 | |
| 1470 | void AppleObjCRuntimeV2::HashTableSignature::UpdateSignature( |
| 1471 | const RemoteNXMapTable &hash_table) { |
| 1472 | m_count = hash_table.GetCount(); |
| 1473 | m_num_buckets = hash_table.GetBucketCount(); |
| 1474 | m_buckets_ptr = hash_table.GetBucketDataPointer(); |
| 1475 | } |
| 1476 | |
| 1477 | bool AppleObjCRuntimeV2::HashTableSignature::NeedsUpdate( |
| 1478 | Process *process, AppleObjCRuntimeV2 *runtime, |
| 1479 | RemoteNXMapTable &hash_table) { |
| 1480 | if (!hash_table.ParseHeader(process, load_addr: runtime->GetISAHashTablePointer())) { |
| 1481 | return false; // Failed to parse the header, no need to update anything |
| 1482 | } |
| 1483 | |
| 1484 | // Check with out current signature and return true if the count, number of |
| 1485 | // buckets or the hash table address changes. |
| 1486 | if (m_count == hash_table.GetCount() && |
| 1487 | m_num_buckets == hash_table.GetBucketCount() && |
| 1488 | m_buckets_ptr == hash_table.GetBucketDataPointer()) { |
| 1489 | // Hash table hasn't changed |
| 1490 | return false; |
| 1491 | } |
| 1492 | // Hash table data has changed, we need to update |
| 1493 | return true; |
| 1494 | } |
| 1495 | |
| 1496 | ObjCLanguageRuntime::ClassDescriptorSP |
| 1497 | AppleObjCRuntimeV2::GetClassDescriptorFromISA(ObjCISA isa) { |
| 1498 | ObjCLanguageRuntime::ClassDescriptorSP class_descriptor_sp; |
| 1499 | if (auto *non_pointer_isa_cache = GetNonPointerIsaCache()) |
| 1500 | class_descriptor_sp = non_pointer_isa_cache->GetClassDescriptor(isa); |
| 1501 | if (!class_descriptor_sp) |
| 1502 | class_descriptor_sp = ObjCLanguageRuntime::GetClassDescriptorFromISA(isa); |
| 1503 | return class_descriptor_sp; |
| 1504 | } |
| 1505 | |
| 1506 | ObjCLanguageRuntime::ClassDescriptorSP |
| 1507 | AppleObjCRuntimeV2::GetClassDescriptor(ValueObject &valobj) { |
| 1508 | ClassDescriptorSP objc_class_sp; |
| 1509 | if (valobj.IsBaseClass()) { |
| 1510 | ValueObject *parent = valobj.GetParent(); |
| 1511 | // if I am my own parent, bail out of here fast.. |
| 1512 | if (parent && parent != &valobj) { |
| 1513 | ClassDescriptorSP parent_descriptor_sp = GetClassDescriptor(valobj&: *parent); |
| 1514 | if (parent_descriptor_sp) |
| 1515 | return parent_descriptor_sp->GetSuperclass(); |
| 1516 | } |
| 1517 | return nullptr; |
| 1518 | } |
| 1519 | // if we get an invalid VO (which might still happen when playing around with |
| 1520 | // pointers returned by the expression parser, don't consider this a valid |
| 1521 | // ObjC object) |
| 1522 | if (!valobj.GetCompilerType().IsValid()) |
| 1523 | return objc_class_sp; |
| 1524 | addr_t isa_pointer = valobj.GetPointerValue().address; |
| 1525 | |
| 1526 | // tagged pointer |
| 1527 | if (IsTaggedPointer(ptr: isa_pointer)) |
| 1528 | return m_tagged_pointer_vendor_up->GetClassDescriptor(ptr: isa_pointer); |
| 1529 | ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); |
| 1530 | |
| 1531 | Process *process = exe_ctx.GetProcessPtr(); |
| 1532 | if (!process) |
| 1533 | return objc_class_sp; |
| 1534 | |
| 1535 | Status error; |
| 1536 | ObjCISA isa = process->ReadPointerFromMemory(vm_addr: isa_pointer, error); |
| 1537 | if (isa == LLDB_INVALID_ADDRESS) |
| 1538 | return objc_class_sp; |
| 1539 | |
| 1540 | objc_class_sp = GetClassDescriptorFromISA(isa); |
| 1541 | if (!objc_class_sp) { |
| 1542 | if (ABISP abi_sp = process->GetABI()) |
| 1543 | isa = abi_sp->FixCodeAddress(pc: isa); |
| 1544 | objc_class_sp = GetClassDescriptorFromISA(isa); |
| 1545 | } |
| 1546 | |
| 1547 | if (isa && !objc_class_sp) { |
| 1548 | Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types); |
| 1549 | LLDB_LOGF(log, |
| 1550 | "0x%" PRIx64 ": AppleObjCRuntimeV2::GetClassDescriptor() ISA was " |
| 1551 | "not in class descriptor cache 0x%" PRIx64, |
| 1552 | isa_pointer, isa); |
| 1553 | } |
| 1554 | return objc_class_sp; |
| 1555 | } |
| 1556 | |
| 1557 | lldb::addr_t AppleObjCRuntimeV2::GetTaggedPointerObfuscator() { |
| 1558 | if (m_tagged_pointer_obfuscator != LLDB_INVALID_ADDRESS) |
| 1559 | return m_tagged_pointer_obfuscator; |
| 1560 | |
| 1561 | Process *process = GetProcess(); |
| 1562 | ModuleSP objc_module_sp(GetObjCModule()); |
| 1563 | |
| 1564 | if (!objc_module_sp) |
| 1565 | return LLDB_INVALID_ADDRESS; |
| 1566 | |
| 1567 | static ConstString g_gdb_objc_obfuscator( |
| 1568 | "objc_debug_taggedpointer_obfuscator" ); |
| 1569 | |
| 1570 | const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType( |
| 1571 | name: g_gdb_objc_obfuscator, symbol_type: lldb::eSymbolTypeAny); |
| 1572 | if (symbol) { |
| 1573 | lldb::addr_t g_gdb_obj_obfuscator_ptr = |
| 1574 | symbol->GetLoadAddress(target: &process->GetTarget()); |
| 1575 | |
| 1576 | if (g_gdb_obj_obfuscator_ptr != LLDB_INVALID_ADDRESS) { |
| 1577 | Status error; |
| 1578 | m_tagged_pointer_obfuscator = |
| 1579 | process->ReadPointerFromMemory(vm_addr: g_gdb_obj_obfuscator_ptr, error); |
| 1580 | } |
| 1581 | } |
| 1582 | // If we don't have a correct value at this point, there must be no |
| 1583 | // obfuscation. |
| 1584 | if (m_tagged_pointer_obfuscator == LLDB_INVALID_ADDRESS) |
| 1585 | m_tagged_pointer_obfuscator = 0; |
| 1586 | |
| 1587 | return m_tagged_pointer_obfuscator; |
| 1588 | } |
| 1589 | |
| 1590 | lldb::addr_t AppleObjCRuntimeV2::GetISAHashTablePointer() { |
| 1591 | if (m_isa_hash_table_ptr == LLDB_INVALID_ADDRESS) { |
| 1592 | Process *process = GetProcess(); |
| 1593 | |
| 1594 | ModuleSP objc_module_sp(GetObjCModule()); |
| 1595 | |
| 1596 | if (!objc_module_sp) |
| 1597 | return LLDB_INVALID_ADDRESS; |
| 1598 | |
| 1599 | static ConstString g_gdb_objc_realized_classes("gdb_objc_realized_classes" ); |
| 1600 | |
| 1601 | const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType( |
| 1602 | name: g_gdb_objc_realized_classes, symbol_type: lldb::eSymbolTypeAny); |
| 1603 | if (symbol) { |
| 1604 | lldb::addr_t gdb_objc_realized_classes_ptr = |
| 1605 | symbol->GetLoadAddress(target: &process->GetTarget()); |
| 1606 | |
| 1607 | if (gdb_objc_realized_classes_ptr != LLDB_INVALID_ADDRESS) { |
| 1608 | Status error; |
| 1609 | m_isa_hash_table_ptr = process->ReadPointerFromMemory( |
| 1610 | vm_addr: gdb_objc_realized_classes_ptr, error); |
| 1611 | } |
| 1612 | } |
| 1613 | } |
| 1614 | return m_isa_hash_table_ptr; |
| 1615 | } |
| 1616 | |
| 1617 | std::unique_ptr<AppleObjCRuntimeV2::SharedCacheImageHeaders> |
| 1618 | AppleObjCRuntimeV2::SharedCacheImageHeaders::( |
| 1619 | AppleObjCRuntimeV2 &runtime) { |
| 1620 | Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types); |
| 1621 | Process *process = runtime.GetProcess(); |
| 1622 | ModuleSP objc_module_sp(runtime.GetObjCModule()); |
| 1623 | if (!objc_module_sp || !process) |
| 1624 | return nullptr; |
| 1625 | |
| 1626 | const Symbol *symbol = objc_module_sp->FindFirstSymbolWithNameAndType( |
| 1627 | name: ConstString("objc_debug_headerInfoRWs" ), symbol_type: lldb::eSymbolTypeAny); |
| 1628 | if (!symbol) { |
| 1629 | LLDB_LOG(log, "Symbol 'objc_debug_headerInfoRWs' unavailable. Some " |
| 1630 | "information concerning the shared cache may be unavailable" ); |
| 1631 | return nullptr; |
| 1632 | } |
| 1633 | |
| 1634 | lldb::addr_t = |
| 1635 | symbol->GetLoadAddress(target: &process->GetTarget()); |
| 1636 | if (objc_debug_headerInfoRWs_addr == LLDB_INVALID_ADDRESS) { |
| 1637 | LLDB_LOG(log, "Symbol 'objc_debug_headerInfoRWs' was found but we were " |
| 1638 | "unable to get its load address" ); |
| 1639 | return nullptr; |
| 1640 | } |
| 1641 | |
| 1642 | Status error; |
| 1643 | lldb::addr_t = |
| 1644 | process->ReadPointerFromMemory(vm_addr: objc_debug_headerInfoRWs_addr, error); |
| 1645 | if (error.Fail()) { |
| 1646 | LLDB_LOG(log, |
| 1647 | "Failed to read address of 'objc_debug_headerInfoRWs' at {0:x}" , |
| 1648 | objc_debug_headerInfoRWs_addr); |
| 1649 | return nullptr; |
| 1650 | } |
| 1651 | |
| 1652 | const size_t metadata_size = |
| 1653 | sizeof(uint32_t) + sizeof(uint32_t); // count + entsize |
| 1654 | DataBufferHeap metadata_buffer(metadata_size, '\0'); |
| 1655 | process->ReadMemory(vm_addr: objc_debug_headerInfoRWs_ptr, buf: metadata_buffer.GetBytes(), |
| 1656 | size: metadata_size, error); |
| 1657 | if (error.Fail()) { |
| 1658 | LLDB_LOG(log, |
| 1659 | "Unable to read metadata for 'objc_debug_headerInfoRWs' at {0:x}" , |
| 1660 | objc_debug_headerInfoRWs_ptr); |
| 1661 | return nullptr; |
| 1662 | } |
| 1663 | |
| 1664 | DataExtractor (metadata_buffer.GetBytes(), metadata_size, |
| 1665 | process->GetByteOrder(), |
| 1666 | process->GetAddressByteSize()); |
| 1667 | lldb::offset_t cursor = 0; |
| 1668 | uint32_t count = metadata_extractor.GetU32_unchecked(offset_ptr: &cursor); |
| 1669 | uint32_t entsize = metadata_extractor.GetU32_unchecked(offset_ptr: &cursor); |
| 1670 | if (count == 0 || entsize == 0) { |
| 1671 | LLDB_LOG(log, |
| 1672 | "'objc_debug_headerInfoRWs' had count {0} with entsize {1}. These " |
| 1673 | "should both be non-zero." , |
| 1674 | count, entsize); |
| 1675 | return nullptr; |
| 1676 | } |
| 1677 | |
| 1678 | std::unique_ptr<SharedCacheImageHeaders> ( |
| 1679 | new SharedCacheImageHeaders(runtime, objc_debug_headerInfoRWs_ptr, count, |
| 1680 | entsize)); |
| 1681 | if (auto Err = shared_cache_image_headers->UpdateIfNeeded()) { |
| 1682 | LLDB_LOG_ERROR(log, std::move(Err), |
| 1683 | "Failed to update SharedCacheImageHeaders: {0}" ); |
| 1684 | return nullptr; |
| 1685 | } |
| 1686 | |
| 1687 | return shared_cache_image_headers; |
| 1688 | } |
| 1689 | |
| 1690 | llvm::Error AppleObjCRuntimeV2::SharedCacheImageHeaders::() { |
| 1691 | if (!m_needs_update) |
| 1692 | return llvm::Error::success(); |
| 1693 | |
| 1694 | Process *process = m_runtime.GetProcess(); |
| 1695 | constexpr lldb::addr_t metadata_size = |
| 1696 | sizeof(uint32_t) + sizeof(uint32_t); // count + entsize |
| 1697 | |
| 1698 | Status error; |
| 1699 | const lldb::addr_t = m_headerInfoRWs_ptr + metadata_size; |
| 1700 | DataBufferHeap (m_entsize, '\0'); |
| 1701 | lldb::offset_t cursor = 0; |
| 1702 | for (uint32_t i = 0; i < m_count; i++) { |
| 1703 | const lldb::addr_t = first_header_addr + (i * m_entsize); |
| 1704 | process->ReadMemory(vm_addr: header_addr, buf: header_buffer.GetBytes(), size: m_entsize, |
| 1705 | error); |
| 1706 | if (error.Fail()) |
| 1707 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 1708 | S: "Failed to read memory from inferior when " |
| 1709 | "populating SharedCacheImageHeaders" ); |
| 1710 | |
| 1711 | DataExtractor (header_buffer.GetBytes(), m_entsize, |
| 1712 | process->GetByteOrder(), |
| 1713 | process->GetAddressByteSize()); |
| 1714 | cursor = 0; |
| 1715 | bool is_loaded = false; |
| 1716 | if (m_entsize == 4) { |
| 1717 | uint32_t = header_extractor.GetU32_unchecked(offset_ptr: &cursor); |
| 1718 | if (header & 1) |
| 1719 | is_loaded = true; |
| 1720 | } else { |
| 1721 | uint64_t = header_extractor.GetU64_unchecked(offset_ptr: &cursor); |
| 1722 | if (header & 1) |
| 1723 | is_loaded = true; |
| 1724 | } |
| 1725 | |
| 1726 | if (is_loaded) |
| 1727 | m_loaded_images.set(i); |
| 1728 | else |
| 1729 | m_loaded_images.reset(Idx: i); |
| 1730 | } |
| 1731 | m_needs_update = false; |
| 1732 | m_version++; |
| 1733 | return llvm::Error::success(); |
| 1734 | } |
| 1735 | |
| 1736 | bool AppleObjCRuntimeV2::SharedCacheImageHeaders::( |
| 1737 | uint16_t image_index) { |
| 1738 | if (image_index >= m_count) |
| 1739 | return false; |
| 1740 | if (auto Err = UpdateIfNeeded()) { |
| 1741 | Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types); |
| 1742 | LLDB_LOG_ERROR(log, std::move(Err), |
| 1743 | "Failed to update SharedCacheImageHeaders: {0}" ); |
| 1744 | } |
| 1745 | return m_loaded_images.test(Idx: image_index); |
| 1746 | } |
| 1747 | |
| 1748 | uint64_t AppleObjCRuntimeV2::SharedCacheImageHeaders::() { |
| 1749 | if (auto Err = UpdateIfNeeded()) { |
| 1750 | Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types); |
| 1751 | LLDB_LOG_ERROR(log, std::move(Err), |
| 1752 | "Failed to update SharedCacheImageHeaders: {0}" ); |
| 1753 | } |
| 1754 | return m_version; |
| 1755 | } |
| 1756 | |
| 1757 | std::unique_ptr<UtilityFunction> |
| 1758 | AppleObjCRuntimeV2::DynamicClassInfoExtractor::( |
| 1759 | ExecutionContext &exe_ctx, Helper helper, std::string code, |
| 1760 | std::string name) { |
| 1761 | Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types); |
| 1762 | |
| 1763 | LLDB_LOG(log, "Creating utility function {0}" , name); |
| 1764 | |
| 1765 | TypeSystemClangSP scratch_ts_sp = |
| 1766 | ScratchTypeSystemClang::GetForTarget(target&: exe_ctx.GetTargetRef()); |
| 1767 | if (!scratch_ts_sp) |
| 1768 | return {}; |
| 1769 | |
| 1770 | auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction( |
| 1771 | expression: std::move(code), name: std::move(name), language: eLanguageTypeC, exe_ctx); |
| 1772 | if (!utility_fn_or_error) { |
| 1773 | LLDB_LOG_ERROR( |
| 1774 | log, utility_fn_or_error.takeError(), |
| 1775 | "Failed to get utility function for dynamic info extractor: {0}" ); |
| 1776 | return {}; |
| 1777 | } |
| 1778 | |
| 1779 | // Make some types for our arguments. |
| 1780 | CompilerType clang_uint32_t_type = |
| 1781 | scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingUint, bit_size: 32); |
| 1782 | CompilerType clang_void_pointer_type = |
| 1783 | scratch_ts_sp->GetBasicType(type: eBasicTypeVoid).GetPointerType(); |
| 1784 | |
| 1785 | // Make the runner function for our implementation utility function. |
| 1786 | ValueList arguments; |
| 1787 | Value value; |
| 1788 | value.SetValueType(Value::ValueType::Scalar); |
| 1789 | value.SetCompilerType(clang_void_pointer_type); |
| 1790 | arguments.PushValue(value); |
| 1791 | arguments.PushValue(value); |
| 1792 | value.SetValueType(Value::ValueType::Scalar); |
| 1793 | value.SetCompilerType(clang_uint32_t_type); |
| 1794 | arguments.PushValue(value); |
| 1795 | |
| 1796 | // objc_getRealizedClassList_trylock takes an additional buffer and length. |
| 1797 | if (helper == Helper::objc_getRealizedClassList_trylock) { |
| 1798 | value.SetCompilerType(clang_void_pointer_type); |
| 1799 | arguments.PushValue(value); |
| 1800 | value.SetCompilerType(clang_uint32_t_type); |
| 1801 | arguments.PushValue(value); |
| 1802 | } |
| 1803 | |
| 1804 | arguments.PushValue(value); |
| 1805 | |
| 1806 | std::unique_ptr<UtilityFunction> utility_fn = std::move(*utility_fn_or_error); |
| 1807 | |
| 1808 | Status error; |
| 1809 | utility_fn->MakeFunctionCaller(return_type: clang_uint32_t_type, arg_value_list: arguments, |
| 1810 | compilation_thread: exe_ctx.GetThreadSP(), error); |
| 1811 | |
| 1812 | if (error.Fail()) { |
| 1813 | LLDB_LOG(log, |
| 1814 | "Failed to make function caller for implementation lookup: {0}." , |
| 1815 | error.AsCString()); |
| 1816 | return {}; |
| 1817 | } |
| 1818 | |
| 1819 | return utility_fn; |
| 1820 | } |
| 1821 | |
| 1822 | UtilityFunction * |
| 1823 | AppleObjCRuntimeV2::DynamicClassInfoExtractor::( |
| 1824 | ExecutionContext &exe_ctx, Helper helper) { |
| 1825 | switch (helper) { |
| 1826 | case gdb_objc_realized_classes: { |
| 1827 | if (!m_gdb_objc_realized_classes_helper.utility_function) |
| 1828 | m_gdb_objc_realized_classes_helper.utility_function = |
| 1829 | GetClassInfoUtilityFunctionImpl(exe_ctx, helper, |
| 1830 | code: g_get_dynamic_class_info_body, |
| 1831 | name: g_get_dynamic_class_info_name); |
| 1832 | return m_gdb_objc_realized_classes_helper.utility_function.get(); |
| 1833 | } |
| 1834 | case objc_copyRealizedClassList: { |
| 1835 | if (!m_objc_copyRealizedClassList_helper.utility_function) |
| 1836 | m_objc_copyRealizedClassList_helper.utility_function = |
| 1837 | GetClassInfoUtilityFunctionImpl(exe_ctx, helper, |
| 1838 | code: g_get_dynamic_class_info2_body, |
| 1839 | name: g_get_dynamic_class_info2_name); |
| 1840 | return m_objc_copyRealizedClassList_helper.utility_function.get(); |
| 1841 | } |
| 1842 | case objc_getRealizedClassList_trylock: { |
| 1843 | if (!m_objc_getRealizedClassList_trylock_helper.utility_function) |
| 1844 | m_objc_getRealizedClassList_trylock_helper.utility_function = |
| 1845 | GetClassInfoUtilityFunctionImpl(exe_ctx, helper, |
| 1846 | code: g_get_dynamic_class_info3_body, |
| 1847 | name: g_get_dynamic_class_info3_name); |
| 1848 | return m_objc_getRealizedClassList_trylock_helper.utility_function.get(); |
| 1849 | } |
| 1850 | } |
| 1851 | llvm_unreachable("Unexpected helper" ); |
| 1852 | } |
| 1853 | |
| 1854 | lldb::addr_t & |
| 1855 | AppleObjCRuntimeV2::DynamicClassInfoExtractor::(Helper helper) { |
| 1856 | switch (helper) { |
| 1857 | case gdb_objc_realized_classes: |
| 1858 | return m_gdb_objc_realized_classes_helper.args; |
| 1859 | case objc_copyRealizedClassList: |
| 1860 | return m_objc_copyRealizedClassList_helper.args; |
| 1861 | case objc_getRealizedClassList_trylock: |
| 1862 | return m_objc_getRealizedClassList_trylock_helper.args; |
| 1863 | } |
| 1864 | llvm_unreachable("Unexpected helper" ); |
| 1865 | } |
| 1866 | |
| 1867 | AppleObjCRuntimeV2::DynamicClassInfoExtractor::Helper |
| 1868 | AppleObjCRuntimeV2::DynamicClassInfoExtractor::( |
| 1869 | ExecutionContext &exe_ctx) const { |
| 1870 | if (!m_runtime.m_has_objc_copyRealizedClassList && |
| 1871 | !m_runtime.m_has_objc_getRealizedClassList_trylock) |
| 1872 | return DynamicClassInfoExtractor::gdb_objc_realized_classes; |
| 1873 | |
| 1874 | if (Process *process = m_runtime.GetProcess()) { |
| 1875 | if (DynamicLoader *loader = process->GetDynamicLoader()) { |
| 1876 | if (loader->IsFullyInitialized()) { |
| 1877 | switch (exe_ctx.GetTargetRef().GetDynamicClassInfoHelper()) { |
| 1878 | case eDynamicClassInfoHelperAuto: |
| 1879 | [[fallthrough]]; |
| 1880 | case eDynamicClassInfoHelperGetRealizedClassList: |
| 1881 | if (m_runtime.m_has_objc_getRealizedClassList_trylock) |
| 1882 | return DynamicClassInfoExtractor::objc_getRealizedClassList_trylock; |
| 1883 | [[fallthrough]]; |
| 1884 | case eDynamicClassInfoHelperCopyRealizedClassList: |
| 1885 | if (m_runtime.m_has_objc_copyRealizedClassList) |
| 1886 | return DynamicClassInfoExtractor::objc_copyRealizedClassList; |
| 1887 | [[fallthrough]]; |
| 1888 | case eDynamicClassInfoHelperRealizedClassesStruct: |
| 1889 | return DynamicClassInfoExtractor::gdb_objc_realized_classes; |
| 1890 | } |
| 1891 | } |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | return DynamicClassInfoExtractor::gdb_objc_realized_classes; |
| 1896 | } |
| 1897 | |
| 1898 | std::unique_ptr<UtilityFunction> |
| 1899 | AppleObjCRuntimeV2::SharedCacheClassInfoExtractor:: |
| 1900 | (ExecutionContext &exe_ctx) { |
| 1901 | Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types); |
| 1902 | |
| 1903 | LLDB_LOG(log, "Creating utility function {0}" , |
| 1904 | g_get_shared_cache_class_info_name); |
| 1905 | |
| 1906 | TypeSystemClangSP scratch_ts_sp = |
| 1907 | ScratchTypeSystemClang::GetForTarget(target&: exe_ctx.GetTargetRef()); |
| 1908 | if (!scratch_ts_sp) |
| 1909 | return {}; |
| 1910 | |
| 1911 | // If the inferior objc.dylib has the class_getNameRaw function, use that in |
| 1912 | // our jitted expression. Else fall back to the old class_getName. |
| 1913 | static ConstString g_class_getName_symbol_name("class_getName" ); |
| 1914 | static ConstString g_class_getNameRaw_symbol_name( |
| 1915 | "objc_debug_class_getNameRaw" ); |
| 1916 | |
| 1917 | ConstString class_name_getter_function_name = |
| 1918 | m_runtime.HasSymbol(Name: g_class_getNameRaw_symbol_name) |
| 1919 | ? g_class_getNameRaw_symbol_name |
| 1920 | : g_class_getName_symbol_name; |
| 1921 | |
| 1922 | // Substitute in the correct class_getName / class_getNameRaw function name, |
| 1923 | // concatenate the two parts of our expression text. The format string has |
| 1924 | // two %s's, so provide the name twice. |
| 1925 | std::string shared_class_expression; |
| 1926 | llvm::raw_string_ostream(shared_class_expression) |
| 1927 | << llvm::format(Fmt: g_shared_cache_class_name_funcptr, |
| 1928 | Vals: class_name_getter_function_name.AsCString(), |
| 1929 | Vals: class_name_getter_function_name.AsCString()); |
| 1930 | |
| 1931 | shared_class_expression += g_get_shared_cache_class_info_body; |
| 1932 | |
| 1933 | auto utility_fn_or_error = exe_ctx.GetTargetRef().CreateUtilityFunction( |
| 1934 | expression: std::move(shared_class_expression), name: g_get_shared_cache_class_info_name, |
| 1935 | language: eLanguageTypeC, exe_ctx); |
| 1936 | |
| 1937 | if (!utility_fn_or_error) { |
| 1938 | LLDB_LOG_ERROR( |
| 1939 | log, utility_fn_or_error.takeError(), |
| 1940 | "Failed to get utility function for shared class info extractor: {0}" ); |
| 1941 | return nullptr; |
| 1942 | } |
| 1943 | |
| 1944 | // Make some types for our arguments. |
| 1945 | CompilerType clang_uint32_t_type = |
| 1946 | scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingUint, bit_size: 32); |
| 1947 | CompilerType clang_void_pointer_type = |
| 1948 | scratch_ts_sp->GetBasicType(type: eBasicTypeVoid).GetPointerType(); |
| 1949 | CompilerType clang_uint64_t_pointer_type = |
| 1950 | scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingUint, bit_size: 64) |
| 1951 | .GetPointerType(); |
| 1952 | |
| 1953 | // Next make the function caller for our implementation utility function. |
| 1954 | ValueList arguments; |
| 1955 | Value value; |
| 1956 | value.SetValueType(Value::ValueType::Scalar); |
| 1957 | value.SetCompilerType(clang_void_pointer_type); |
| 1958 | arguments.PushValue(value); |
| 1959 | arguments.PushValue(value); |
| 1960 | arguments.PushValue(value); |
| 1961 | |
| 1962 | value.SetValueType(Value::ValueType::Scalar); |
| 1963 | value.SetCompilerType(clang_uint64_t_pointer_type); |
| 1964 | arguments.PushValue(value); |
| 1965 | |
| 1966 | value.SetValueType(Value::ValueType::Scalar); |
| 1967 | value.SetCompilerType(clang_uint32_t_type); |
| 1968 | arguments.PushValue(value); |
| 1969 | arguments.PushValue(value); |
| 1970 | |
| 1971 | std::unique_ptr<UtilityFunction> utility_fn = std::move(*utility_fn_or_error); |
| 1972 | |
| 1973 | Status error; |
| 1974 | utility_fn->MakeFunctionCaller(return_type: clang_uint32_t_type, arg_value_list: arguments, |
| 1975 | compilation_thread: exe_ctx.GetThreadSP(), error); |
| 1976 | |
| 1977 | if (error.Fail()) { |
| 1978 | LLDB_LOG(log, |
| 1979 | "Failed to make function caller for implementation lookup: {0}." , |
| 1980 | error.AsCString()); |
| 1981 | return {}; |
| 1982 | } |
| 1983 | |
| 1984 | return utility_fn; |
| 1985 | } |
| 1986 | |
| 1987 | UtilityFunction * |
| 1988 | AppleObjCRuntimeV2::SharedCacheClassInfoExtractor::( |
| 1989 | ExecutionContext &exe_ctx) { |
| 1990 | if (!m_utility_function) |
| 1991 | m_utility_function = GetClassInfoUtilityFunctionImpl(exe_ctx); |
| 1992 | return m_utility_function.get(); |
| 1993 | } |
| 1994 | |
| 1995 | AppleObjCRuntimeV2::DescriptorMapUpdateResult |
| 1996 | AppleObjCRuntimeV2::DynamicClassInfoExtractor::( |
| 1997 | RemoteNXMapTable &hash_table) { |
| 1998 | Process *process = m_runtime.GetProcess(); |
| 1999 | if (process == nullptr) |
| 2000 | return DescriptorMapUpdateResult::Fail(); |
| 2001 | |
| 2002 | uint32_t num_class_infos = 0; |
| 2003 | |
| 2004 | Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types); |
| 2005 | |
| 2006 | ExecutionContext exe_ctx; |
| 2007 | |
| 2008 | ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread(); |
| 2009 | |
| 2010 | if (!thread_sp) |
| 2011 | return DescriptorMapUpdateResult::Fail(); |
| 2012 | |
| 2013 | if (!thread_sp->SafeToCallFunctions()) |
| 2014 | return DescriptorMapUpdateResult::Retry(); |
| 2015 | |
| 2016 | thread_sp->CalculateExecutionContext(exe_ctx); |
| 2017 | TypeSystemClangSP scratch_ts_sp = |
| 2018 | ScratchTypeSystemClang::GetForTarget(target&: process->GetTarget()); |
| 2019 | |
| 2020 | if (!scratch_ts_sp) |
| 2021 | return DescriptorMapUpdateResult::Fail(); |
| 2022 | |
| 2023 | Address function_address; |
| 2024 | |
| 2025 | const uint32_t addr_size = process->GetAddressByteSize(); |
| 2026 | |
| 2027 | Status err; |
| 2028 | |
| 2029 | // Compute which helper we're going to use for this update. |
| 2030 | const DynamicClassInfoExtractor::Helper helper = ComputeHelper(exe_ctx); |
| 2031 | |
| 2032 | // Read the total number of classes from the hash table |
| 2033 | const uint32_t num_classes = |
| 2034 | helper == DynamicClassInfoExtractor::gdb_objc_realized_classes |
| 2035 | ? hash_table.GetCount() |
| 2036 | : m_runtime.m_realized_class_generation_count; |
| 2037 | if (num_classes == 0) { |
| 2038 | LLDB_LOGF(log, "No dynamic classes found." ); |
| 2039 | return DescriptorMapUpdateResult::Success(found: 0); |
| 2040 | } |
| 2041 | |
| 2042 | UtilityFunction *get_class_info_code = |
| 2043 | GetClassInfoUtilityFunction(exe_ctx, helper); |
| 2044 | if (!get_class_info_code) { |
| 2045 | // The callee will have already logged a useful error message. |
| 2046 | return DescriptorMapUpdateResult::Fail(); |
| 2047 | } |
| 2048 | |
| 2049 | FunctionCaller *get_class_info_function = |
| 2050 | get_class_info_code->GetFunctionCaller(); |
| 2051 | |
| 2052 | if (!get_class_info_function) { |
| 2053 | LLDB_LOGF(log, "Failed to get implementation lookup function caller." ); |
| 2054 | return DescriptorMapUpdateResult::Fail(); |
| 2055 | } |
| 2056 | |
| 2057 | ValueList arguments = get_class_info_function->GetArgumentValues(); |
| 2058 | |
| 2059 | DiagnosticManager diagnostics; |
| 2060 | |
| 2061 | const uint32_t class_info_byte_size = addr_size + 4; |
| 2062 | const uint32_t class_infos_byte_size = num_classes * class_info_byte_size; |
| 2063 | lldb::addr_t class_infos_addr = process->AllocateMemory( |
| 2064 | size: class_infos_byte_size, permissions: ePermissionsReadable | ePermissionsWritable, error&: err); |
| 2065 | |
| 2066 | if (class_infos_addr == LLDB_INVALID_ADDRESS) { |
| 2067 | LLDB_LOGF(log, |
| 2068 | "unable to allocate %" PRIu32 |
| 2069 | " bytes in process for shared cache read" , |
| 2070 | class_infos_byte_size); |
| 2071 | return DescriptorMapUpdateResult::Fail(); |
| 2072 | } |
| 2073 | |
| 2074 | auto deallocate_class_infos = llvm::make_scope_exit(F: [&] { |
| 2075 | // Deallocate the memory we allocated for the ClassInfo array |
| 2076 | if (class_infos_addr != LLDB_INVALID_ADDRESS) |
| 2077 | process->DeallocateMemory(ptr: class_infos_addr); |
| 2078 | }); |
| 2079 | |
| 2080 | lldb::addr_t class_buffer_addr = LLDB_INVALID_ADDRESS; |
| 2081 | const uint32_t class_byte_size = addr_size; |
| 2082 | const uint32_t class_buffer_len = num_classes; |
| 2083 | const uint32_t class_buffer_byte_size = class_buffer_len * class_byte_size; |
| 2084 | if (helper == Helper::objc_getRealizedClassList_trylock) { |
| 2085 | class_buffer_addr = process->AllocateMemory( |
| 2086 | size: class_buffer_byte_size, permissions: ePermissionsReadable | ePermissionsWritable, |
| 2087 | error&: err); |
| 2088 | if (class_buffer_addr == LLDB_INVALID_ADDRESS) { |
| 2089 | LLDB_LOGF(log, |
| 2090 | "unable to allocate %" PRIu32 |
| 2091 | " bytes in process for shared cache read" , |
| 2092 | class_buffer_byte_size); |
| 2093 | return DescriptorMapUpdateResult::Fail(); |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | auto deallocate_class_buffer = llvm::make_scope_exit(F: [&] { |
| 2098 | // Deallocate the memory we allocated for the Class array |
| 2099 | if (class_buffer_addr != LLDB_INVALID_ADDRESS) |
| 2100 | process->DeallocateMemory(ptr: class_buffer_addr); |
| 2101 | }); |
| 2102 | |
| 2103 | std::lock_guard<std::mutex> guard(m_mutex); |
| 2104 | |
| 2105 | // Fill in our function argument values |
| 2106 | uint32_t index = 0; |
| 2107 | arguments.GetValueAtIndex(idx: index++)->GetScalar() = |
| 2108 | hash_table.GetTableLoadAddress(); |
| 2109 | arguments.GetValueAtIndex(idx: index++)->GetScalar() = class_infos_addr; |
| 2110 | arguments.GetValueAtIndex(idx: index++)->GetScalar() = class_infos_byte_size; |
| 2111 | |
| 2112 | if (class_buffer_addr != LLDB_INVALID_ADDRESS) { |
| 2113 | arguments.GetValueAtIndex(idx: index++)->GetScalar() = class_buffer_addr; |
| 2114 | arguments.GetValueAtIndex(idx: index++)->GetScalar() = class_buffer_byte_size; |
| 2115 | } |
| 2116 | |
| 2117 | // Only dump the runtime classes from the expression evaluation if the log is |
| 2118 | // verbose: |
| 2119 | Log *type_log = GetLog(mask: LLDBLog::Types); |
| 2120 | bool dump_log = type_log && type_log->GetVerbose(); |
| 2121 | |
| 2122 | arguments.GetValueAtIndex(idx: index++)->GetScalar() = dump_log ? 1 : 0; |
| 2123 | |
| 2124 | bool success = false; |
| 2125 | |
| 2126 | diagnostics.Clear(); |
| 2127 | |
| 2128 | // Write our function arguments into the process so we can run our function |
| 2129 | if (get_class_info_function->WriteFunctionArguments( |
| 2130 | exe_ctx, args_addr_ref&: GetClassInfoArgs(helper), arg_values&: arguments, diagnostic_manager&: diagnostics)) { |
| 2131 | EvaluateExpressionOptions options; |
| 2132 | options.SetUnwindOnError(true); |
| 2133 | options.SetTryAllThreads(false); |
| 2134 | options.SetStopOthers(true); |
| 2135 | options.SetIgnoreBreakpoints(true); |
| 2136 | options.SetTimeout(process->GetUtilityExpressionTimeout()); |
| 2137 | options.SetIsForUtilityExpr(true); |
| 2138 | |
| 2139 | CompilerType clang_uint32_t_type = |
| 2140 | scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingUint, bit_size: 32); |
| 2141 | |
| 2142 | Value return_value; |
| 2143 | return_value.SetValueType(Value::ValueType::Scalar); |
| 2144 | return_value.SetCompilerType(clang_uint32_t_type); |
| 2145 | return_value.GetScalar() = 0; |
| 2146 | |
| 2147 | diagnostics.Clear(); |
| 2148 | |
| 2149 | // Run the function |
| 2150 | ExpressionResults results = get_class_info_function->ExecuteFunction( |
| 2151 | exe_ctx, args_addr_ptr: &GetClassInfoArgs(helper), options, diagnostic_manager&: diagnostics, results&: return_value); |
| 2152 | |
| 2153 | if (results == eExpressionCompleted) { |
| 2154 | // The result is the number of ClassInfo structures that were filled in |
| 2155 | num_class_infos = return_value.GetScalar().ULong(); |
| 2156 | LLDB_LOG(log, "Discovered {0} Objective-C classes" , num_class_infos); |
| 2157 | if (num_class_infos > 0) { |
| 2158 | // Read the ClassInfo structures |
| 2159 | DataBufferHeap buffer(num_class_infos * class_info_byte_size, 0); |
| 2160 | if (process->ReadMemory(vm_addr: class_infos_addr, buf: buffer.GetBytes(), |
| 2161 | size: buffer.GetByteSize(), |
| 2162 | error&: err) == buffer.GetByteSize()) { |
| 2163 | DataExtractor class_infos_data(buffer.GetBytes(), |
| 2164 | buffer.GetByteSize(), |
| 2165 | process->GetByteOrder(), addr_size); |
| 2166 | m_runtime.ParseClassInfoArray(data: class_infos_data, num_class_infos); |
| 2167 | } |
| 2168 | } |
| 2169 | success = true; |
| 2170 | } else { |
| 2171 | if (log) { |
| 2172 | LLDB_LOGF(log, "Error evaluating our find class name function." ); |
| 2173 | diagnostics.Dump(log); |
| 2174 | } |
| 2175 | } |
| 2176 | } else { |
| 2177 | if (log) { |
| 2178 | LLDB_LOGF(log, "Error writing function arguments." ); |
| 2179 | diagnostics.Dump(log); |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | return DescriptorMapUpdateResult(success, false, num_class_infos); |
| 2184 | } |
| 2185 | |
| 2186 | uint32_t AppleObjCRuntimeV2::(const DataExtractor &data, |
| 2187 | uint32_t num_class_infos) { |
| 2188 | // Parses an array of "num_class_infos" packed ClassInfo structures: |
| 2189 | // |
| 2190 | // struct ClassInfo |
| 2191 | // { |
| 2192 | // Class isa; |
| 2193 | // uint32_t hash; |
| 2194 | // } __attribute__((__packed__)); |
| 2195 | |
| 2196 | Log *log = GetLog(mask: LLDBLog::Types); |
| 2197 | bool should_log = log && log->GetVerbose(); |
| 2198 | |
| 2199 | uint32_t num_parsed = 0; |
| 2200 | |
| 2201 | // Iterate through all ClassInfo structures |
| 2202 | lldb::offset_t offset = 0; |
| 2203 | for (uint32_t i = 0; i < num_class_infos; ++i) { |
| 2204 | ObjCISA isa = data.GetAddress(offset_ptr: &offset); |
| 2205 | |
| 2206 | if (isa == 0) { |
| 2207 | if (should_log) |
| 2208 | LLDB_LOGF( |
| 2209 | log, "AppleObjCRuntimeV2 found NULL isa, ignoring this class info" ); |
| 2210 | continue; |
| 2211 | } |
| 2212 | // Check if we already know about this ISA, if we do, the info will never |
| 2213 | // change, so we can just skip it. |
| 2214 | if (ISAIsCached(isa)) { |
| 2215 | if (should_log) |
| 2216 | LLDB_LOGF(log, |
| 2217 | "AppleObjCRuntimeV2 found cached isa=0x%" PRIx64 |
| 2218 | ", ignoring this class info" , |
| 2219 | isa); |
| 2220 | offset += 4; |
| 2221 | } else { |
| 2222 | // Read the 32 bit hash for the class name |
| 2223 | const uint32_t name_hash = data.GetU32(offset_ptr: &offset); |
| 2224 | ClassDescriptorSP descriptor_sp( |
| 2225 | new ClassDescriptorV2(*this, isa, nullptr)); |
| 2226 | |
| 2227 | // The code in g_get_shared_cache_class_info_body sets the value of the |
| 2228 | // hash to 0 to signal a demangled symbol. We use class_getName() in that |
| 2229 | // code to find the class name, but this returns a demangled name for |
| 2230 | // Swift symbols. For those symbols, recompute the hash here by reading |
| 2231 | // their name from the runtime. |
| 2232 | if (name_hash) |
| 2233 | AddClass(isa, descriptor_sp, class_name_hash: name_hash); |
| 2234 | else |
| 2235 | AddClass(isa, descriptor_sp, |
| 2236 | class_name: descriptor_sp->GetClassName().AsCString(value_if_empty: nullptr)); |
| 2237 | num_parsed++; |
| 2238 | if (should_log) |
| 2239 | LLDB_LOGF(log, |
| 2240 | "AppleObjCRuntimeV2 added isa=0x%" PRIx64 |
| 2241 | ", hash=0x%8.8x, name=%s" , |
| 2242 | isa, name_hash, |
| 2243 | descriptor_sp->GetClassName().AsCString("<unknown>" )); |
| 2244 | } |
| 2245 | } |
| 2246 | if (should_log) |
| 2247 | LLDB_LOGF(log, "AppleObjCRuntimeV2 parsed %" PRIu32 " class infos" , |
| 2248 | num_parsed); |
| 2249 | return num_parsed; |
| 2250 | } |
| 2251 | |
| 2252 | bool AppleObjCRuntimeV2::HasSymbol(ConstString Name) { |
| 2253 | if (!m_objc_module_sp) |
| 2254 | return false; |
| 2255 | if (const Symbol *symbol = m_objc_module_sp->FindFirstSymbolWithNameAndType( |
| 2256 | name: Name, symbol_type: lldb::eSymbolTypeCode)) { |
| 2257 | if (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid()) |
| 2258 | return true; |
| 2259 | } |
| 2260 | return false; |
| 2261 | } |
| 2262 | |
| 2263 | AppleObjCRuntimeV2::DescriptorMapUpdateResult |
| 2264 | AppleObjCRuntimeV2::SharedCacheClassInfoExtractor::() { |
| 2265 | Process *process = m_runtime.GetProcess(); |
| 2266 | if (process == nullptr) |
| 2267 | return DescriptorMapUpdateResult::Fail(); |
| 2268 | |
| 2269 | Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types); |
| 2270 | |
| 2271 | ExecutionContext exe_ctx; |
| 2272 | |
| 2273 | ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread(); |
| 2274 | |
| 2275 | if (!thread_sp) |
| 2276 | return DescriptorMapUpdateResult::Fail(); |
| 2277 | |
| 2278 | if (!thread_sp->SafeToCallFunctions()) |
| 2279 | return DescriptorMapUpdateResult::Retry(); |
| 2280 | |
| 2281 | thread_sp->CalculateExecutionContext(exe_ctx); |
| 2282 | TypeSystemClangSP scratch_ts_sp = |
| 2283 | ScratchTypeSystemClang::GetForTarget(target&: process->GetTarget()); |
| 2284 | |
| 2285 | if (!scratch_ts_sp) |
| 2286 | return DescriptorMapUpdateResult::Fail(); |
| 2287 | |
| 2288 | Address function_address; |
| 2289 | |
| 2290 | const uint32_t addr_size = process->GetAddressByteSize(); |
| 2291 | |
| 2292 | Status err; |
| 2293 | |
| 2294 | uint32_t num_class_infos = 0; |
| 2295 | |
| 2296 | const lldb::addr_t objc_opt_ptr = m_runtime.GetSharedCacheReadOnlyAddress(); |
| 2297 | const lldb::addr_t shared_cache_base_addr = |
| 2298 | m_runtime.GetSharedCacheBaseAddress(); |
| 2299 | |
| 2300 | if (objc_opt_ptr == LLDB_INVALID_ADDRESS || |
| 2301 | shared_cache_base_addr == LLDB_INVALID_ADDRESS) |
| 2302 | return DescriptorMapUpdateResult::Fail(); |
| 2303 | |
| 2304 | // The number of entries to pre-allocate room for. |
| 2305 | // Each entry is (addrsize + 4) bytes |
| 2306 | // FIXME: It is not sustainable to continue incrementing this value every time |
| 2307 | // the shared cache grows. This is because it requires allocating memory in |
| 2308 | // the inferior process and some inferior processes have small memory limits. |
| 2309 | const uint32_t max_num_classes = 212992; |
| 2310 | |
| 2311 | UtilityFunction *get_class_info_code = GetClassInfoUtilityFunction(exe_ctx); |
| 2312 | if (!get_class_info_code) { |
| 2313 | // The callee will have already logged a useful error message. |
| 2314 | return DescriptorMapUpdateResult::Fail(); |
| 2315 | } |
| 2316 | |
| 2317 | FunctionCaller *get_shared_cache_class_info_function = |
| 2318 | get_class_info_code->GetFunctionCaller(); |
| 2319 | |
| 2320 | if (!get_shared_cache_class_info_function) { |
| 2321 | LLDB_LOGF(log, "Failed to get implementation lookup function caller." ); |
| 2322 | return DescriptorMapUpdateResult::Fail(); |
| 2323 | } |
| 2324 | |
| 2325 | ValueList arguments = |
| 2326 | get_shared_cache_class_info_function->GetArgumentValues(); |
| 2327 | |
| 2328 | DiagnosticManager diagnostics; |
| 2329 | |
| 2330 | const uint32_t class_info_byte_size = addr_size + 4; |
| 2331 | const uint32_t class_infos_byte_size = max_num_classes * class_info_byte_size; |
| 2332 | lldb::addr_t class_infos_addr = process->AllocateMemory( |
| 2333 | size: class_infos_byte_size, permissions: ePermissionsReadable | ePermissionsWritable, error&: err); |
| 2334 | const uint32_t relative_selector_offset_addr_size = 64; |
| 2335 | lldb::addr_t relative_selector_offset_addr = |
| 2336 | process->AllocateMemory(size: relative_selector_offset_addr_size, |
| 2337 | permissions: ePermissionsReadable | ePermissionsWritable, error&: err); |
| 2338 | |
| 2339 | if (class_infos_addr == LLDB_INVALID_ADDRESS) { |
| 2340 | LLDB_LOGF(log, |
| 2341 | "unable to allocate %" PRIu32 |
| 2342 | " bytes in process for shared cache read" , |
| 2343 | class_infos_byte_size); |
| 2344 | return DescriptorMapUpdateResult::Fail(); |
| 2345 | } |
| 2346 | |
| 2347 | std::lock_guard<std::mutex> guard(m_mutex); |
| 2348 | |
| 2349 | // Fill in our function argument values |
| 2350 | arguments.GetValueAtIndex(idx: 0)->GetScalar() = objc_opt_ptr; |
| 2351 | arguments.GetValueAtIndex(idx: 1)->GetScalar() = shared_cache_base_addr; |
| 2352 | arguments.GetValueAtIndex(idx: 2)->GetScalar() = class_infos_addr; |
| 2353 | arguments.GetValueAtIndex(idx: 3)->GetScalar() = relative_selector_offset_addr; |
| 2354 | arguments.GetValueAtIndex(idx: 4)->GetScalar() = class_infos_byte_size; |
| 2355 | // Only dump the runtime classes from the expression evaluation if the log is |
| 2356 | // verbose: |
| 2357 | Log *type_log = GetLog(mask: LLDBLog::Types); |
| 2358 | bool dump_log = type_log && type_log->GetVerbose(); |
| 2359 | |
| 2360 | arguments.GetValueAtIndex(idx: 5)->GetScalar() = dump_log ? 1 : 0; |
| 2361 | |
| 2362 | bool success = false; |
| 2363 | |
| 2364 | diagnostics.Clear(); |
| 2365 | |
| 2366 | // Write our function arguments into the process so we can run our function |
| 2367 | if (get_shared_cache_class_info_function->WriteFunctionArguments( |
| 2368 | exe_ctx, args_addr_ref&: m_args, arg_values&: arguments, diagnostic_manager&: diagnostics)) { |
| 2369 | EvaluateExpressionOptions options; |
| 2370 | options.SetUnwindOnError(true); |
| 2371 | options.SetTryAllThreads(false); |
| 2372 | options.SetStopOthers(true); |
| 2373 | options.SetIgnoreBreakpoints(true); |
| 2374 | options.SetTimeout(process->GetUtilityExpressionTimeout()); |
| 2375 | options.SetIsForUtilityExpr(true); |
| 2376 | |
| 2377 | CompilerType clang_uint32_t_type = |
| 2378 | scratch_ts_sp->GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingUint, bit_size: 32); |
| 2379 | |
| 2380 | Value return_value; |
| 2381 | return_value.SetValueType(Value::ValueType::Scalar); |
| 2382 | return_value.SetCompilerType(clang_uint32_t_type); |
| 2383 | return_value.GetScalar() = 0; |
| 2384 | |
| 2385 | diagnostics.Clear(); |
| 2386 | |
| 2387 | // Run the function |
| 2388 | ExpressionResults results = |
| 2389 | get_shared_cache_class_info_function->ExecuteFunction( |
| 2390 | exe_ctx, args_addr_ptr: &m_args, options, diagnostic_manager&: diagnostics, results&: return_value); |
| 2391 | |
| 2392 | if (results == eExpressionCompleted) { |
| 2393 | // The result is the number of ClassInfo structures that were filled in |
| 2394 | num_class_infos = return_value.GetScalar().ULong(); |
| 2395 | LLDB_LOG(log, "Discovered {0} Objective-C classes in the shared cache" , |
| 2396 | num_class_infos); |
| 2397 | // Assert if there were more classes than we pre-allocated |
| 2398 | // room for. |
| 2399 | assert(num_class_infos <= max_num_classes); |
| 2400 | if (num_class_infos > 0) { |
| 2401 | if (num_class_infos > max_num_classes) { |
| 2402 | num_class_infos = max_num_classes; |
| 2403 | |
| 2404 | success = false; |
| 2405 | } else { |
| 2406 | success = true; |
| 2407 | } |
| 2408 | |
| 2409 | // Read the relative selector offset. |
| 2410 | DataBufferHeap relative_selector_offset_buffer(64, 0); |
| 2411 | if (process->ReadMemory(vm_addr: relative_selector_offset_addr, |
| 2412 | buf: relative_selector_offset_buffer.GetBytes(), |
| 2413 | size: relative_selector_offset_buffer.GetByteSize(), |
| 2414 | error&: err) == |
| 2415 | relative_selector_offset_buffer.GetByteSize()) { |
| 2416 | DataExtractor relative_selector_offset_data( |
| 2417 | relative_selector_offset_buffer.GetBytes(), |
| 2418 | relative_selector_offset_buffer.GetByteSize(), |
| 2419 | process->GetByteOrder(), addr_size); |
| 2420 | lldb::offset_t offset = 0; |
| 2421 | uint64_t relative_selector_offset = |
| 2422 | relative_selector_offset_data.GetU64(offset_ptr: &offset); |
| 2423 | if (relative_selector_offset > 0) { |
| 2424 | // The offset is relative to the objc_opt struct. |
| 2425 | m_runtime.SetRelativeSelectorBaseAddr(objc_opt_ptr + |
| 2426 | relative_selector_offset); |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | // Read the ClassInfo structures |
| 2431 | DataBufferHeap class_infos_buffer( |
| 2432 | num_class_infos * class_info_byte_size, 0); |
| 2433 | if (process->ReadMemory(vm_addr: class_infos_addr, buf: class_infos_buffer.GetBytes(), |
| 2434 | size: class_infos_buffer.GetByteSize(), |
| 2435 | error&: err) == class_infos_buffer.GetByteSize()) { |
| 2436 | DataExtractor class_infos_data(class_infos_buffer.GetBytes(), |
| 2437 | class_infos_buffer.GetByteSize(), |
| 2438 | process->GetByteOrder(), addr_size); |
| 2439 | |
| 2440 | m_runtime.ParseClassInfoArray(data: class_infos_data, num_class_infos); |
| 2441 | } |
| 2442 | } else { |
| 2443 | success = true; |
| 2444 | } |
| 2445 | } else { |
| 2446 | if (log) { |
| 2447 | LLDB_LOGF(log, "Error evaluating our find class name function." ); |
| 2448 | diagnostics.Dump(log); |
| 2449 | } |
| 2450 | } |
| 2451 | } else { |
| 2452 | if (log) { |
| 2453 | LLDB_LOGF(log, "Error writing function arguments." ); |
| 2454 | diagnostics.Dump(log); |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | // Deallocate the memory we allocated for the ClassInfo array |
| 2459 | process->DeallocateMemory(ptr: class_infos_addr); |
| 2460 | |
| 2461 | return DescriptorMapUpdateResult(success, false, num_class_infos); |
| 2462 | } |
| 2463 | |
| 2464 | lldb::addr_t AppleObjCRuntimeV2::GetSharedCacheReadOnlyAddress() { |
| 2465 | Process *process = GetProcess(); |
| 2466 | |
| 2467 | if (process) { |
| 2468 | ModuleSP objc_module_sp(GetObjCModule()); |
| 2469 | |
| 2470 | if (objc_module_sp) { |
| 2471 | ObjectFile *objc_object = objc_module_sp->GetObjectFile(); |
| 2472 | |
| 2473 | if (objc_object) { |
| 2474 | SectionList *section_list = objc_module_sp->GetSectionList(); |
| 2475 | |
| 2476 | if (section_list) { |
| 2477 | SectionSP text_segment_sp( |
| 2478 | section_list->FindSectionByName(section_dstr: ConstString("__TEXT" ))); |
| 2479 | |
| 2480 | if (text_segment_sp) { |
| 2481 | SectionSP objc_opt_section_sp( |
| 2482 | text_segment_sp->GetChildren().FindSectionByName( |
| 2483 | section_dstr: ConstString("__objc_opt_ro" ))); |
| 2484 | |
| 2485 | if (objc_opt_section_sp) { |
| 2486 | return objc_opt_section_sp->GetLoadBaseAddress( |
| 2487 | target: &process->GetTarget()); |
| 2488 | } |
| 2489 | } |
| 2490 | } |
| 2491 | } |
| 2492 | } |
| 2493 | } |
| 2494 | return LLDB_INVALID_ADDRESS; |
| 2495 | } |
| 2496 | |
| 2497 | lldb::addr_t AppleObjCRuntimeV2::GetSharedCacheBaseAddress() { |
| 2498 | StructuredData::ObjectSP info = m_process->GetSharedCacheInfo(); |
| 2499 | if (!info) |
| 2500 | return LLDB_INVALID_ADDRESS; |
| 2501 | |
| 2502 | StructuredData::Dictionary *info_dict = info->GetAsDictionary(); |
| 2503 | if (!info_dict) |
| 2504 | return LLDB_INVALID_ADDRESS; |
| 2505 | |
| 2506 | StructuredData::ObjectSP value = |
| 2507 | info_dict->GetValueForKey(key: "shared_cache_base_address" ); |
| 2508 | if (!value) |
| 2509 | return LLDB_INVALID_ADDRESS; |
| 2510 | |
| 2511 | return value->GetUnsignedIntegerValue(LLDB_INVALID_ADDRESS); |
| 2512 | } |
| 2513 | |
| 2514 | void AppleObjCRuntimeV2::UpdateISAToDescriptorMapIfNeeded() { |
| 2515 | LLDB_SCOPED_TIMER(); |
| 2516 | |
| 2517 | Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types); |
| 2518 | |
| 2519 | // Else we need to check with our process to see when the map was updated. |
| 2520 | Process *process = GetProcess(); |
| 2521 | |
| 2522 | if (process) { |
| 2523 | RemoteNXMapTable hash_table; |
| 2524 | |
| 2525 | // Update the process stop ID that indicates the last time we updated the |
| 2526 | // map, whether it was successful or not. |
| 2527 | m_isa_to_descriptor_stop_id = process->GetStopID(); |
| 2528 | |
| 2529 | // Ask the runtime is the realized class generation count changed. Unlike |
| 2530 | // the hash table, this accounts for lazily named classes. |
| 2531 | const bool class_count_changed = RealizedClassGenerationCountChanged(); |
| 2532 | |
| 2533 | if (!m_hash_signature.NeedsUpdate(process, runtime: this, hash_table) && |
| 2534 | !class_count_changed) |
| 2535 | return; |
| 2536 | |
| 2537 | m_hash_signature.UpdateSignature(hash_table); |
| 2538 | |
| 2539 | // Grab the dynamically loaded Objective-C classes from memory. |
| 2540 | DescriptorMapUpdateResult dynamic_update_result = |
| 2541 | m_dynamic_class_info_extractor.UpdateISAToDescriptorMap(hash_table); |
| 2542 | |
| 2543 | // Now get the objc classes that are baked into the Objective-C runtime in |
| 2544 | // the shared cache, but only once per process as this data never changes |
| 2545 | if (!m_loaded_objc_opt) { |
| 2546 | // it is legitimately possible for the shared cache to be empty - in that |
| 2547 | // case, the dynamic hash table will contain all the class information we |
| 2548 | // need; the situation we're trying to detect is one where we aren't |
| 2549 | // seeing class information from the runtime - in order to detect that |
| 2550 | // vs. just the shared cache being empty or sparsely populated, we set an |
| 2551 | // arbitrary (very low) threshold for the number of classes that we want |
| 2552 | // to see in a "good" scenario - anything below that is suspicious |
| 2553 | // (Foundation alone has thousands of classes) |
| 2554 | const uint32_t num_classes_to_warn_at = 500; |
| 2555 | |
| 2556 | DescriptorMapUpdateResult shared_cache_update_result = |
| 2557 | m_shared_cache_class_info_extractor.UpdateISAToDescriptorMap(); |
| 2558 | |
| 2559 | LLDB_LOGF(log, |
| 2560 | "attempted to read objc class data - results: " |
| 2561 | "[dynamic_update]: ran: %s, retry: %s, count: %" PRIu32 |
| 2562 | " [shared_cache_update]: ran: %s, retry: %s, count: %" PRIu32, |
| 2563 | dynamic_update_result.m_update_ran ? "yes" : "no" , |
| 2564 | dynamic_update_result.m_retry_update ? "yes" : "no" , |
| 2565 | dynamic_update_result.m_num_found, |
| 2566 | shared_cache_update_result.m_update_ran ? "yes" : "no" , |
| 2567 | shared_cache_update_result.m_retry_update ? "yes" : "no" , |
| 2568 | shared_cache_update_result.m_num_found); |
| 2569 | |
| 2570 | // warn if: |
| 2571 | // - we could not run either expression |
| 2572 | // - we found fewer than num_classes_to_warn_at classes total |
| 2573 | if (dynamic_update_result.m_retry_update || |
| 2574 | shared_cache_update_result.m_retry_update) |
| 2575 | WarnIfNoClassesCached(reason: SharedCacheWarningReason::eExpressionUnableToRun); |
| 2576 | else if ((!shared_cache_update_result.m_update_ran) || |
| 2577 | (!dynamic_update_result.m_update_ran)) |
| 2578 | WarnIfNoClassesCached( |
| 2579 | reason: SharedCacheWarningReason::eExpressionExecutionFailure); |
| 2580 | else if (dynamic_update_result.m_num_found + |
| 2581 | shared_cache_update_result.m_num_found < |
| 2582 | num_classes_to_warn_at) |
| 2583 | WarnIfNoClassesCached(reason: SharedCacheWarningReason::eNotEnoughClassesRead); |
| 2584 | else |
| 2585 | m_loaded_objc_opt = true; |
| 2586 | } |
| 2587 | } else { |
| 2588 | m_isa_to_descriptor_stop_id = UINT32_MAX; |
| 2589 | } |
| 2590 | } |
| 2591 | |
| 2592 | bool AppleObjCRuntimeV2::RealizedClassGenerationCountChanged() { |
| 2593 | Process *process = GetProcess(); |
| 2594 | if (!process) |
| 2595 | return false; |
| 2596 | |
| 2597 | Status error; |
| 2598 | uint64_t objc_debug_realized_class_generation_count = |
| 2599 | ExtractRuntimeGlobalSymbol( |
| 2600 | process, name: ConstString("objc_debug_realized_class_generation_count" ), |
| 2601 | module_sp: GetObjCModule(), error); |
| 2602 | if (error.Fail()) |
| 2603 | return false; |
| 2604 | |
| 2605 | if (m_realized_class_generation_count == |
| 2606 | objc_debug_realized_class_generation_count) |
| 2607 | return false; |
| 2608 | |
| 2609 | Log *log = GetLog(mask: LLDBLog::Process | LLDBLog::Types); |
| 2610 | LLDB_LOG(log, |
| 2611 | "objc_debug_realized_class_generation_count changed from {0} to {1}" , |
| 2612 | m_realized_class_generation_count, |
| 2613 | objc_debug_realized_class_generation_count); |
| 2614 | |
| 2615 | m_realized_class_generation_count = |
| 2616 | objc_debug_realized_class_generation_count; |
| 2617 | |
| 2618 | return true; |
| 2619 | } |
| 2620 | |
| 2621 | static bool DoesProcessHaveSharedCache(Process &process) { |
| 2622 | PlatformSP platform_sp = process.GetTarget().GetPlatform(); |
| 2623 | if (!platform_sp) |
| 2624 | return true; // this should not happen |
| 2625 | |
| 2626 | llvm::StringRef platform_plugin_name_sr = platform_sp->GetPluginName(); |
| 2627 | if (platform_plugin_name_sr.ends_with(Suffix: "-simulator" )) |
| 2628 | return false; |
| 2629 | |
| 2630 | return true; |
| 2631 | } |
| 2632 | |
| 2633 | void AppleObjCRuntimeV2::WarnIfNoClassesCached( |
| 2634 | SharedCacheWarningReason reason) { |
| 2635 | if (GetProcess() && !DoesProcessHaveSharedCache(process&: *GetProcess())) { |
| 2636 | // Simulators do not have the objc_opt_ro class table so don't actually |
| 2637 | // complain to the user |
| 2638 | return; |
| 2639 | } |
| 2640 | |
| 2641 | Debugger &debugger(GetProcess()->GetTarget().GetDebugger()); |
| 2642 | switch (reason) { |
| 2643 | case SharedCacheWarningReason::eNotEnoughClassesRead: |
| 2644 | Debugger::ReportWarning(message: "could not find Objective-C class data in " |
| 2645 | "the process. This may reduce the quality of type " |
| 2646 | "information available.\n" , |
| 2647 | debugger_id: debugger.GetID(), once: &m_no_classes_cached_warning); |
| 2648 | break; |
| 2649 | case SharedCacheWarningReason::eExpressionExecutionFailure: |
| 2650 | Debugger::ReportWarning( |
| 2651 | message: "could not execute support code to read " |
| 2652 | "Objective-C class data in the process. This may " |
| 2653 | "reduce the quality of type information available.\n" , |
| 2654 | debugger_id: debugger.GetID(), once: &m_no_classes_cached_warning); |
| 2655 | break; |
| 2656 | case SharedCacheWarningReason::eExpressionUnableToRun: |
| 2657 | Debugger::ReportWarning( |
| 2658 | message: "could not execute support code to read Objective-C class data because " |
| 2659 | "it's not yet safe to do so, and will be retried later.\n" , |
| 2660 | debugger_id: debugger.GetID(), once: nullptr); |
| 2661 | break; |
| 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | void AppleObjCRuntimeV2::WarnIfNoExpandedSharedCache() { |
| 2666 | if (!m_objc_module_sp) |
| 2667 | return; |
| 2668 | |
| 2669 | ObjectFile *object_file = m_objc_module_sp->GetObjectFile(); |
| 2670 | if (!object_file) |
| 2671 | return; |
| 2672 | |
| 2673 | if (!object_file->IsInMemory()) |
| 2674 | return; |
| 2675 | |
| 2676 | if (!GetProcess()->IsLiveDebugSession()) |
| 2677 | return; |
| 2678 | |
| 2679 | Target &target = GetProcess()->GetTarget(); |
| 2680 | Debugger &debugger = target.GetDebugger(); |
| 2681 | |
| 2682 | std::string buffer; |
| 2683 | llvm::raw_string_ostream os(buffer); |
| 2684 | |
| 2685 | os << "libobjc.A.dylib is being read from process memory. This " |
| 2686 | "indicates that LLDB could not " ; |
| 2687 | if (PlatformSP platform_sp = target.GetPlatform()) { |
| 2688 | if (platform_sp->IsHost()) { |
| 2689 | os << "read from the host's in-memory shared cache" ; |
| 2690 | } else { |
| 2691 | os << "find the on-disk shared cache for this device" ; |
| 2692 | } |
| 2693 | } else { |
| 2694 | os << "read from the shared cache" ; |
| 2695 | } |
| 2696 | os << ". This will likely reduce debugging performance.\n" ; |
| 2697 | |
| 2698 | Debugger::ReportWarning(message: buffer, debugger_id: debugger.GetID(), |
| 2699 | once: &m_no_expanded_cache_warning); |
| 2700 | } |
| 2701 | |
| 2702 | DeclVendor *AppleObjCRuntimeV2::GetDeclVendor() { |
| 2703 | if (!m_decl_vendor_up) |
| 2704 | m_decl_vendor_up = std::make_unique<AppleObjCDeclVendor>(args&: *this); |
| 2705 | |
| 2706 | return m_decl_vendor_up.get(); |
| 2707 | } |
| 2708 | |
| 2709 | lldb::addr_t AppleObjCRuntimeV2::LookupRuntimeSymbol(ConstString name) { |
| 2710 | lldb::addr_t ret = LLDB_INVALID_ADDRESS; |
| 2711 | |
| 2712 | const char *name_cstr = name.AsCString(); |
| 2713 | |
| 2714 | if (name_cstr) { |
| 2715 | llvm::StringRef name_strref(name_cstr); |
| 2716 | |
| 2717 | llvm::StringRef ivar_prefix("OBJC_IVAR_$_" ); |
| 2718 | llvm::StringRef class_prefix("OBJC_CLASS_$_" ); |
| 2719 | |
| 2720 | if (name_strref.starts_with(Prefix: ivar_prefix)) { |
| 2721 | llvm::StringRef ivar_skipped_prefix = |
| 2722 | name_strref.substr(Start: ivar_prefix.size()); |
| 2723 | std::pair<llvm::StringRef, llvm::StringRef> class_and_ivar = |
| 2724 | ivar_skipped_prefix.split(Separator: '.'); |
| 2725 | |
| 2726 | if (!class_and_ivar.first.empty() && !class_and_ivar.second.empty()) { |
| 2727 | const ConstString class_name_cs(class_and_ivar.first); |
| 2728 | ClassDescriptorSP descriptor = |
| 2729 | ObjCLanguageRuntime::GetClassDescriptorFromClassName(class_name: class_name_cs); |
| 2730 | |
| 2731 | if (descriptor) { |
| 2732 | const ConstString ivar_name_cs(class_and_ivar.second); |
| 2733 | const char *ivar_name_cstr = ivar_name_cs.AsCString(); |
| 2734 | |
| 2735 | auto ivar_func = [&ret, |
| 2736 | ivar_name_cstr](const char *name, const char *type, |
| 2737 | lldb::addr_t offset_addr, |
| 2738 | uint64_t size) -> lldb::addr_t { |
| 2739 | if (!strcmp(s1: name, s2: ivar_name_cstr)) { |
| 2740 | ret = offset_addr; |
| 2741 | return true; |
| 2742 | } |
| 2743 | return false; |
| 2744 | }; |
| 2745 | |
| 2746 | descriptor->Describe( |
| 2747 | superclass_func: std::function<void(ObjCISA)>(nullptr), |
| 2748 | instance_method_func: std::function<bool(const char *, const char *)>(nullptr), |
| 2749 | class_method_func: std::function<bool(const char *, const char *)>(nullptr), |
| 2750 | ivar_func); |
| 2751 | } |
| 2752 | } |
| 2753 | } else if (name_strref.starts_with(Prefix: class_prefix)) { |
| 2754 | llvm::StringRef class_skipped_prefix = |
| 2755 | name_strref.substr(Start: class_prefix.size()); |
| 2756 | const ConstString class_name_cs(class_skipped_prefix); |
| 2757 | ClassDescriptorSP descriptor = |
| 2758 | GetClassDescriptorFromClassName(class_name: class_name_cs); |
| 2759 | |
| 2760 | if (descriptor) |
| 2761 | ret = descriptor->GetISA(); |
| 2762 | } |
| 2763 | } |
| 2764 | |
| 2765 | return ret; |
| 2766 | } |
| 2767 | |
| 2768 | AppleObjCRuntimeV2::NonPointerISACache * |
| 2769 | AppleObjCRuntimeV2::NonPointerISACache::CreateInstance( |
| 2770 | AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp) { |
| 2771 | Process *process(runtime.GetProcess()); |
| 2772 | |
| 2773 | Status error; |
| 2774 | |
| 2775 | Log *log = GetLog(mask: LLDBLog::Types); |
| 2776 | |
| 2777 | auto objc_debug_isa_magic_mask = ExtractRuntimeGlobalSymbol( |
| 2778 | process, name: ConstString("objc_debug_isa_magic_mask" ), module_sp: objc_module_sp, error); |
| 2779 | if (error.Fail()) |
| 2780 | return nullptr; |
| 2781 | |
| 2782 | auto objc_debug_isa_magic_value = ExtractRuntimeGlobalSymbol( |
| 2783 | process, name: ConstString("objc_debug_isa_magic_value" ), module_sp: objc_module_sp, |
| 2784 | error); |
| 2785 | if (error.Fail()) |
| 2786 | return nullptr; |
| 2787 | |
| 2788 | auto objc_debug_isa_class_mask = ExtractRuntimeGlobalSymbol( |
| 2789 | process, name: ConstString("objc_debug_isa_class_mask" ), module_sp: objc_module_sp, error); |
| 2790 | if (error.Fail()) |
| 2791 | return nullptr; |
| 2792 | |
| 2793 | if (log) |
| 2794 | log->PutCString(cstr: "AOCRT::NPI: Found all the non-indexed ISA masks" ); |
| 2795 | |
| 2796 | bool foundError = false; |
| 2797 | auto objc_debug_indexed_isa_magic_mask = ExtractRuntimeGlobalSymbol( |
| 2798 | process, name: ConstString("objc_debug_indexed_isa_magic_mask" ), module_sp: objc_module_sp, |
| 2799 | error); |
| 2800 | foundError |= error.Fail(); |
| 2801 | |
| 2802 | auto objc_debug_indexed_isa_magic_value = ExtractRuntimeGlobalSymbol( |
| 2803 | process, name: ConstString("objc_debug_indexed_isa_magic_value" ), |
| 2804 | module_sp: objc_module_sp, error); |
| 2805 | foundError |= error.Fail(); |
| 2806 | |
| 2807 | auto objc_debug_indexed_isa_index_mask = ExtractRuntimeGlobalSymbol( |
| 2808 | process, name: ConstString("objc_debug_indexed_isa_index_mask" ), module_sp: objc_module_sp, |
| 2809 | error); |
| 2810 | foundError |= error.Fail(); |
| 2811 | |
| 2812 | auto objc_debug_indexed_isa_index_shift = ExtractRuntimeGlobalSymbol( |
| 2813 | process, name: ConstString("objc_debug_indexed_isa_index_shift" ), |
| 2814 | module_sp: objc_module_sp, error); |
| 2815 | foundError |= error.Fail(); |
| 2816 | |
| 2817 | auto objc_indexed_classes = |
| 2818 | ExtractRuntimeGlobalSymbol(process, name: ConstString("objc_indexed_classes" ), |
| 2819 | module_sp: objc_module_sp, error, read_value: false); |
| 2820 | foundError |= error.Fail(); |
| 2821 | |
| 2822 | if (log) |
| 2823 | log->PutCString(cstr: "AOCRT::NPI: Found all the indexed ISA masks" ); |
| 2824 | |
| 2825 | // we might want to have some rules to outlaw these other values (e.g if the |
| 2826 | // mask is zero but the value is non-zero, ...) |
| 2827 | |
| 2828 | return new NonPointerISACache( |
| 2829 | runtime, objc_module_sp, objc_debug_isa_class_mask, |
| 2830 | objc_debug_isa_magic_mask, objc_debug_isa_magic_value, |
| 2831 | objc_debug_indexed_isa_magic_mask, objc_debug_indexed_isa_magic_value, |
| 2832 | objc_debug_indexed_isa_index_mask, objc_debug_indexed_isa_index_shift, |
| 2833 | foundError ? 0 : objc_indexed_classes); |
| 2834 | } |
| 2835 | |
| 2836 | AppleObjCRuntimeV2::TaggedPointerVendorV2 * |
| 2837 | AppleObjCRuntimeV2::TaggedPointerVendorV2::CreateInstance( |
| 2838 | AppleObjCRuntimeV2 &runtime, const lldb::ModuleSP &objc_module_sp) { |
| 2839 | Process *process(runtime.GetProcess()); |
| 2840 | |
| 2841 | Status error; |
| 2842 | |
| 2843 | auto objc_debug_taggedpointer_mask = ExtractRuntimeGlobalSymbol( |
| 2844 | process, name: ConstString("objc_debug_taggedpointer_mask" ), module_sp: objc_module_sp, |
| 2845 | error); |
| 2846 | if (error.Fail()) |
| 2847 | return new TaggedPointerVendorLegacy(runtime); |
| 2848 | |
| 2849 | auto objc_debug_taggedpointer_slot_shift = ExtractRuntimeGlobalSymbol( |
| 2850 | process, name: ConstString("objc_debug_taggedpointer_slot_shift" ), |
| 2851 | module_sp: objc_module_sp, error, read_value: true, byte_size: 4); |
| 2852 | if (error.Fail()) |
| 2853 | return new TaggedPointerVendorLegacy(runtime); |
| 2854 | |
| 2855 | auto objc_debug_taggedpointer_slot_mask = ExtractRuntimeGlobalSymbol( |
| 2856 | process, name: ConstString("objc_debug_taggedpointer_slot_mask" ), |
| 2857 | module_sp: objc_module_sp, error, read_value: true, byte_size: 4); |
| 2858 | if (error.Fail()) |
| 2859 | return new TaggedPointerVendorLegacy(runtime); |
| 2860 | |
| 2861 | auto objc_debug_taggedpointer_payload_lshift = ExtractRuntimeGlobalSymbol( |
| 2862 | process, name: ConstString("objc_debug_taggedpointer_payload_lshift" ), |
| 2863 | module_sp: objc_module_sp, error, read_value: true, byte_size: 4); |
| 2864 | if (error.Fail()) |
| 2865 | return new TaggedPointerVendorLegacy(runtime); |
| 2866 | |
| 2867 | auto objc_debug_taggedpointer_payload_rshift = ExtractRuntimeGlobalSymbol( |
| 2868 | process, name: ConstString("objc_debug_taggedpointer_payload_rshift" ), |
| 2869 | module_sp: objc_module_sp, error, read_value: true, byte_size: 4); |
| 2870 | if (error.Fail()) |
| 2871 | return new TaggedPointerVendorLegacy(runtime); |
| 2872 | |
| 2873 | auto objc_debug_taggedpointer_classes = ExtractRuntimeGlobalSymbol( |
| 2874 | process, name: ConstString("objc_debug_taggedpointer_classes" ), module_sp: objc_module_sp, |
| 2875 | error, read_value: false); |
| 2876 | if (error.Fail()) |
| 2877 | return new TaggedPointerVendorLegacy(runtime); |
| 2878 | |
| 2879 | // try to detect the "extended tagged pointer" variables - if any are |
| 2880 | // missing, use the non-extended vendor |
| 2881 | do { |
| 2882 | auto objc_debug_taggedpointer_ext_mask = ExtractRuntimeGlobalSymbol( |
| 2883 | process, name: ConstString("objc_debug_taggedpointer_ext_mask" ), |
| 2884 | module_sp: objc_module_sp, error); |
| 2885 | if (error.Fail()) |
| 2886 | break; |
| 2887 | |
| 2888 | auto objc_debug_taggedpointer_ext_slot_shift = ExtractRuntimeGlobalSymbol( |
| 2889 | process, name: ConstString("objc_debug_taggedpointer_ext_slot_shift" ), |
| 2890 | module_sp: objc_module_sp, error, read_value: true, byte_size: 4); |
| 2891 | if (error.Fail()) |
| 2892 | break; |
| 2893 | |
| 2894 | auto objc_debug_taggedpointer_ext_slot_mask = ExtractRuntimeGlobalSymbol( |
| 2895 | process, name: ConstString("objc_debug_taggedpointer_ext_slot_mask" ), |
| 2896 | module_sp: objc_module_sp, error, read_value: true, byte_size: 4); |
| 2897 | if (error.Fail()) |
| 2898 | break; |
| 2899 | |
| 2900 | auto objc_debug_taggedpointer_ext_classes = ExtractRuntimeGlobalSymbol( |
| 2901 | process, name: ConstString("objc_debug_taggedpointer_ext_classes" ), |
| 2902 | module_sp: objc_module_sp, error, read_value: false); |
| 2903 | if (error.Fail()) |
| 2904 | break; |
| 2905 | |
| 2906 | auto objc_debug_taggedpointer_ext_payload_lshift = |
| 2907 | ExtractRuntimeGlobalSymbol( |
| 2908 | process, name: ConstString("objc_debug_taggedpointer_ext_payload_lshift" ), |
| 2909 | module_sp: objc_module_sp, error, read_value: true, byte_size: 4); |
| 2910 | if (error.Fail()) |
| 2911 | break; |
| 2912 | |
| 2913 | auto objc_debug_taggedpointer_ext_payload_rshift = |
| 2914 | ExtractRuntimeGlobalSymbol( |
| 2915 | process, name: ConstString("objc_debug_taggedpointer_ext_payload_rshift" ), |
| 2916 | module_sp: objc_module_sp, error, read_value: true, byte_size: 4); |
| 2917 | if (error.Fail()) |
| 2918 | break; |
| 2919 | |
| 2920 | return new TaggedPointerVendorExtended( |
| 2921 | runtime, objc_debug_taggedpointer_mask, |
| 2922 | objc_debug_taggedpointer_ext_mask, objc_debug_taggedpointer_slot_shift, |
| 2923 | objc_debug_taggedpointer_ext_slot_shift, |
| 2924 | objc_debug_taggedpointer_slot_mask, |
| 2925 | objc_debug_taggedpointer_ext_slot_mask, |
| 2926 | objc_debug_taggedpointer_payload_lshift, |
| 2927 | objc_debug_taggedpointer_payload_rshift, |
| 2928 | objc_debug_taggedpointer_ext_payload_lshift, |
| 2929 | objc_debug_taggedpointer_ext_payload_rshift, |
| 2930 | objc_debug_taggedpointer_classes, objc_debug_taggedpointer_ext_classes); |
| 2931 | } while (false); |
| 2932 | |
| 2933 | // we might want to have some rules to outlaw these values (e.g if the |
| 2934 | // table's address is zero) |
| 2935 | |
| 2936 | return new TaggedPointerVendorRuntimeAssisted( |
| 2937 | runtime, objc_debug_taggedpointer_mask, |
| 2938 | objc_debug_taggedpointer_slot_shift, objc_debug_taggedpointer_slot_mask, |
| 2939 | objc_debug_taggedpointer_payload_lshift, |
| 2940 | objc_debug_taggedpointer_payload_rshift, |
| 2941 | objc_debug_taggedpointer_classes); |
| 2942 | } |
| 2943 | |
| 2944 | bool AppleObjCRuntimeV2::TaggedPointerVendorLegacy::IsPossibleTaggedPointer( |
| 2945 | lldb::addr_t ptr) { |
| 2946 | return (ptr & 1); |
| 2947 | } |
| 2948 | |
| 2949 | ObjCLanguageRuntime::ClassDescriptorSP |
| 2950 | AppleObjCRuntimeV2::TaggedPointerVendorLegacy::GetClassDescriptor( |
| 2951 | lldb::addr_t ptr) { |
| 2952 | if (!IsPossibleTaggedPointer(ptr)) |
| 2953 | return ObjCLanguageRuntime::ClassDescriptorSP(); |
| 2954 | |
| 2955 | uint32_t foundation_version = m_runtime.GetFoundationVersion(); |
| 2956 | |
| 2957 | if (foundation_version == LLDB_INVALID_MODULE_VERSION) |
| 2958 | return ObjCLanguageRuntime::ClassDescriptorSP(); |
| 2959 | |
| 2960 | uint64_t class_bits = (ptr & 0xE) >> 1; |
| 2961 | ConstString name; |
| 2962 | |
| 2963 | static ConstString g_NSAtom("NSAtom" ); |
| 2964 | static ConstString g_NSNumber("NSNumber" ); |
| 2965 | static ConstString g_NSDateTS("NSDateTS" ); |
| 2966 | static ConstString g_NSManagedObject("NSManagedObject" ); |
| 2967 | static ConstString g_NSDate("NSDate" ); |
| 2968 | |
| 2969 | if (foundation_version >= 900) { |
| 2970 | switch (class_bits) { |
| 2971 | case 0: |
| 2972 | name = g_NSAtom; |
| 2973 | break; |
| 2974 | case 3: |
| 2975 | name = g_NSNumber; |
| 2976 | break; |
| 2977 | case 4: |
| 2978 | name = g_NSDateTS; |
| 2979 | break; |
| 2980 | case 5: |
| 2981 | name = g_NSManagedObject; |
| 2982 | break; |
| 2983 | case 6: |
| 2984 | name = g_NSDate; |
| 2985 | break; |
| 2986 | default: |
| 2987 | return ObjCLanguageRuntime::ClassDescriptorSP(); |
| 2988 | } |
| 2989 | } else { |
| 2990 | switch (class_bits) { |
| 2991 | case 1: |
| 2992 | name = g_NSNumber; |
| 2993 | break; |
| 2994 | case 5: |
| 2995 | name = g_NSManagedObject; |
| 2996 | break; |
| 2997 | case 6: |
| 2998 | name = g_NSDate; |
| 2999 | break; |
| 3000 | case 7: |
| 3001 | name = g_NSDateTS; |
| 3002 | break; |
| 3003 | default: |
| 3004 | return ObjCLanguageRuntime::ClassDescriptorSP(); |
| 3005 | } |
| 3006 | } |
| 3007 | |
| 3008 | lldb::addr_t unobfuscated = ptr ^ m_runtime.GetTaggedPointerObfuscator(); |
| 3009 | return ClassDescriptorSP(new ClassDescriptorV2Tagged(name, unobfuscated)); |
| 3010 | } |
| 3011 | |
| 3012 | AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted:: |
| 3013 | TaggedPointerVendorRuntimeAssisted( |
| 3014 | AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask, |
| 3015 | uint32_t objc_debug_taggedpointer_slot_shift, |
| 3016 | uint32_t objc_debug_taggedpointer_slot_mask, |
| 3017 | uint32_t objc_debug_taggedpointer_payload_lshift, |
| 3018 | uint32_t objc_debug_taggedpointer_payload_rshift, |
| 3019 | lldb::addr_t objc_debug_taggedpointer_classes) |
| 3020 | : TaggedPointerVendorV2(runtime), m_cache(), |
| 3021 | m_objc_debug_taggedpointer_mask(objc_debug_taggedpointer_mask), |
| 3022 | m_objc_debug_taggedpointer_slot_shift( |
| 3023 | objc_debug_taggedpointer_slot_shift), |
| 3024 | m_objc_debug_taggedpointer_slot_mask(objc_debug_taggedpointer_slot_mask), |
| 3025 | m_objc_debug_taggedpointer_payload_lshift( |
| 3026 | objc_debug_taggedpointer_payload_lshift), |
| 3027 | m_objc_debug_taggedpointer_payload_rshift( |
| 3028 | objc_debug_taggedpointer_payload_rshift), |
| 3029 | m_objc_debug_taggedpointer_classes(objc_debug_taggedpointer_classes) {} |
| 3030 | |
| 3031 | bool AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted:: |
| 3032 | IsPossibleTaggedPointer(lldb::addr_t ptr) { |
| 3033 | return (ptr & m_objc_debug_taggedpointer_mask) != 0; |
| 3034 | } |
| 3035 | |
| 3036 | ObjCLanguageRuntime::ClassDescriptorSP |
| 3037 | AppleObjCRuntimeV2::TaggedPointerVendorRuntimeAssisted::GetClassDescriptor( |
| 3038 | lldb::addr_t ptr) { |
| 3039 | ClassDescriptorSP actual_class_descriptor_sp; |
| 3040 | uint64_t unobfuscated = (ptr) ^ m_runtime.GetTaggedPointerObfuscator(); |
| 3041 | |
| 3042 | if (!IsPossibleTaggedPointer(ptr: unobfuscated)) |
| 3043 | return ObjCLanguageRuntime::ClassDescriptorSP(); |
| 3044 | |
| 3045 | uintptr_t slot = (ptr >> m_objc_debug_taggedpointer_slot_shift) & |
| 3046 | m_objc_debug_taggedpointer_slot_mask; |
| 3047 | |
| 3048 | CacheIterator iterator = m_cache.find(x: slot), end = m_cache.end(); |
| 3049 | if (iterator != end) { |
| 3050 | actual_class_descriptor_sp = iterator->second; |
| 3051 | } else { |
| 3052 | Process *process(m_runtime.GetProcess()); |
| 3053 | uintptr_t slot_ptr = slot * process->GetAddressByteSize() + |
| 3054 | m_objc_debug_taggedpointer_classes; |
| 3055 | Status error; |
| 3056 | uintptr_t slot_data = process->ReadPointerFromMemory(vm_addr: slot_ptr, error); |
| 3057 | if (error.Fail() || slot_data == 0 || |
| 3058 | slot_data == uintptr_t(LLDB_INVALID_ADDRESS)) |
| 3059 | return nullptr; |
| 3060 | actual_class_descriptor_sp = |
| 3061 | m_runtime.GetClassDescriptorFromISA(isa: (ObjCISA)slot_data); |
| 3062 | if (!actual_class_descriptor_sp) { |
| 3063 | if (ABISP abi_sp = process->GetABI()) { |
| 3064 | ObjCISA fixed_isa = abi_sp->FixCodeAddress(pc: (ObjCISA)slot_data); |
| 3065 | actual_class_descriptor_sp = |
| 3066 | m_runtime.GetClassDescriptorFromISA(isa: fixed_isa); |
| 3067 | } |
| 3068 | } |
| 3069 | if (!actual_class_descriptor_sp) |
| 3070 | return ObjCLanguageRuntime::ClassDescriptorSP(); |
| 3071 | m_cache[slot] = actual_class_descriptor_sp; |
| 3072 | } |
| 3073 | |
| 3074 | uint64_t data_payload = |
| 3075 | ((unobfuscated << m_objc_debug_taggedpointer_payload_lshift) >> |
| 3076 | m_objc_debug_taggedpointer_payload_rshift); |
| 3077 | int64_t data_payload_signed = |
| 3078 | ((int64_t)(unobfuscated << m_objc_debug_taggedpointer_payload_lshift) >> |
| 3079 | m_objc_debug_taggedpointer_payload_rshift); |
| 3080 | return ClassDescriptorSP(new ClassDescriptorV2Tagged( |
| 3081 | actual_class_descriptor_sp, data_payload, data_payload_signed)); |
| 3082 | } |
| 3083 | |
| 3084 | AppleObjCRuntimeV2::TaggedPointerVendorExtended::TaggedPointerVendorExtended( |
| 3085 | AppleObjCRuntimeV2 &runtime, uint64_t objc_debug_taggedpointer_mask, |
| 3086 | uint64_t objc_debug_taggedpointer_ext_mask, |
| 3087 | uint32_t objc_debug_taggedpointer_slot_shift, |
| 3088 | uint32_t objc_debug_taggedpointer_ext_slot_shift, |
| 3089 | uint32_t objc_debug_taggedpointer_slot_mask, |
| 3090 | uint32_t objc_debug_taggedpointer_ext_slot_mask, |
| 3091 | uint32_t objc_debug_taggedpointer_payload_lshift, |
| 3092 | uint32_t objc_debug_taggedpointer_payload_rshift, |
| 3093 | uint32_t objc_debug_taggedpointer_ext_payload_lshift, |
| 3094 | uint32_t objc_debug_taggedpointer_ext_payload_rshift, |
| 3095 | lldb::addr_t objc_debug_taggedpointer_classes, |
| 3096 | lldb::addr_t objc_debug_taggedpointer_ext_classes) |
| 3097 | : TaggedPointerVendorRuntimeAssisted( |
| 3098 | runtime, objc_debug_taggedpointer_mask, |
| 3099 | objc_debug_taggedpointer_slot_shift, |
| 3100 | objc_debug_taggedpointer_slot_mask, |
| 3101 | objc_debug_taggedpointer_payload_lshift, |
| 3102 | objc_debug_taggedpointer_payload_rshift, |
| 3103 | objc_debug_taggedpointer_classes), |
| 3104 | m_ext_cache(), |
| 3105 | m_objc_debug_taggedpointer_ext_mask(objc_debug_taggedpointer_ext_mask), |
| 3106 | m_objc_debug_taggedpointer_ext_slot_shift( |
| 3107 | objc_debug_taggedpointer_ext_slot_shift), |
| 3108 | m_objc_debug_taggedpointer_ext_slot_mask( |
| 3109 | objc_debug_taggedpointer_ext_slot_mask), |
| 3110 | m_objc_debug_taggedpointer_ext_payload_lshift( |
| 3111 | objc_debug_taggedpointer_ext_payload_lshift), |
| 3112 | m_objc_debug_taggedpointer_ext_payload_rshift( |
| 3113 | objc_debug_taggedpointer_ext_payload_rshift), |
| 3114 | m_objc_debug_taggedpointer_ext_classes( |
| 3115 | objc_debug_taggedpointer_ext_classes) {} |
| 3116 | |
| 3117 | bool AppleObjCRuntimeV2::TaggedPointerVendorExtended:: |
| 3118 | IsPossibleExtendedTaggedPointer(lldb::addr_t ptr) { |
| 3119 | if (!IsPossibleTaggedPointer(ptr)) |
| 3120 | return false; |
| 3121 | |
| 3122 | if (m_objc_debug_taggedpointer_ext_mask == 0) |
| 3123 | return false; |
| 3124 | |
| 3125 | return ((ptr & m_objc_debug_taggedpointer_ext_mask) == |
| 3126 | m_objc_debug_taggedpointer_ext_mask); |
| 3127 | } |
| 3128 | |
| 3129 | ObjCLanguageRuntime::ClassDescriptorSP |
| 3130 | AppleObjCRuntimeV2::TaggedPointerVendorExtended::GetClassDescriptor( |
| 3131 | lldb::addr_t ptr) { |
| 3132 | ClassDescriptorSP actual_class_descriptor_sp; |
| 3133 | uint64_t unobfuscated = (ptr) ^ m_runtime.GetTaggedPointerObfuscator(); |
| 3134 | |
| 3135 | if (!IsPossibleTaggedPointer(ptr: unobfuscated)) |
| 3136 | return ObjCLanguageRuntime::ClassDescriptorSP(); |
| 3137 | |
| 3138 | if (!IsPossibleExtendedTaggedPointer(ptr: unobfuscated)) |
| 3139 | return this->TaggedPointerVendorRuntimeAssisted::GetClassDescriptor(ptr); |
| 3140 | |
| 3141 | uintptr_t slot = (ptr >> m_objc_debug_taggedpointer_ext_slot_shift) & |
| 3142 | m_objc_debug_taggedpointer_ext_slot_mask; |
| 3143 | |
| 3144 | CacheIterator iterator = m_ext_cache.find(x: slot), end = m_ext_cache.end(); |
| 3145 | if (iterator != end) { |
| 3146 | actual_class_descriptor_sp = iterator->second; |
| 3147 | } else { |
| 3148 | Process *process(m_runtime.GetProcess()); |
| 3149 | uintptr_t slot_ptr = slot * process->GetAddressByteSize() + |
| 3150 | m_objc_debug_taggedpointer_ext_classes; |
| 3151 | Status error; |
| 3152 | uintptr_t slot_data = process->ReadPointerFromMemory(vm_addr: slot_ptr, error); |
| 3153 | if (error.Fail() || slot_data == 0 || |
| 3154 | slot_data == uintptr_t(LLDB_INVALID_ADDRESS)) |
| 3155 | return nullptr; |
| 3156 | actual_class_descriptor_sp = |
| 3157 | m_runtime.GetClassDescriptorFromISA(isa: (ObjCISA)slot_data); |
| 3158 | if (!actual_class_descriptor_sp) |
| 3159 | return ObjCLanguageRuntime::ClassDescriptorSP(); |
| 3160 | m_ext_cache[slot] = actual_class_descriptor_sp; |
| 3161 | } |
| 3162 | |
| 3163 | uint64_t data_payload = (((uint64_t)unobfuscated |
| 3164 | << m_objc_debug_taggedpointer_ext_payload_lshift) >> |
| 3165 | m_objc_debug_taggedpointer_ext_payload_rshift); |
| 3166 | int64_t data_payload_signed = |
| 3167 | ((int64_t)((uint64_t)unobfuscated |
| 3168 | << m_objc_debug_taggedpointer_ext_payload_lshift) >> |
| 3169 | m_objc_debug_taggedpointer_ext_payload_rshift); |
| 3170 | |
| 3171 | return ClassDescriptorSP(new ClassDescriptorV2Tagged( |
| 3172 | actual_class_descriptor_sp, data_payload, data_payload_signed)); |
| 3173 | } |
| 3174 | |
| 3175 | AppleObjCRuntimeV2::NonPointerISACache::NonPointerISACache( |
| 3176 | AppleObjCRuntimeV2 &runtime, const ModuleSP &objc_module_sp, |
| 3177 | uint64_t objc_debug_isa_class_mask, uint64_t objc_debug_isa_magic_mask, |
| 3178 | uint64_t objc_debug_isa_magic_value, |
| 3179 | uint64_t objc_debug_indexed_isa_magic_mask, |
| 3180 | uint64_t objc_debug_indexed_isa_magic_value, |
| 3181 | uint64_t objc_debug_indexed_isa_index_mask, |
| 3182 | uint64_t objc_debug_indexed_isa_index_shift, |
| 3183 | lldb::addr_t objc_indexed_classes) |
| 3184 | : m_runtime(runtime), m_cache(), m_objc_module_wp(objc_module_sp), |
| 3185 | m_objc_debug_isa_class_mask(objc_debug_isa_class_mask), |
| 3186 | m_objc_debug_isa_magic_mask(objc_debug_isa_magic_mask), |
| 3187 | m_objc_debug_isa_magic_value(objc_debug_isa_magic_value), |
| 3188 | m_objc_debug_indexed_isa_magic_mask(objc_debug_indexed_isa_magic_mask), |
| 3189 | m_objc_debug_indexed_isa_magic_value(objc_debug_indexed_isa_magic_value), |
| 3190 | m_objc_debug_indexed_isa_index_mask(objc_debug_indexed_isa_index_mask), |
| 3191 | m_objc_debug_indexed_isa_index_shift(objc_debug_indexed_isa_index_shift), |
| 3192 | m_objc_indexed_classes(objc_indexed_classes), m_indexed_isa_cache() {} |
| 3193 | |
| 3194 | ObjCLanguageRuntime::ClassDescriptorSP |
| 3195 | AppleObjCRuntimeV2::NonPointerISACache::GetClassDescriptor(ObjCISA isa) { |
| 3196 | ObjCISA real_isa = 0; |
| 3197 | if (!EvaluateNonPointerISA(isa, ret_isa&: real_isa)) |
| 3198 | return ObjCLanguageRuntime::ClassDescriptorSP(); |
| 3199 | auto cache_iter = m_cache.find(x: real_isa); |
| 3200 | if (cache_iter != m_cache.end()) |
| 3201 | return cache_iter->second; |
| 3202 | auto descriptor_sp = |
| 3203 | m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(isa: real_isa); |
| 3204 | if (descriptor_sp) // cache only positive matches since the table might grow |
| 3205 | m_cache[real_isa] = descriptor_sp; |
| 3206 | return descriptor_sp; |
| 3207 | } |
| 3208 | |
| 3209 | bool AppleObjCRuntimeV2::NonPointerISACache::EvaluateNonPointerISA( |
| 3210 | ObjCISA isa, ObjCISA &ret_isa) { |
| 3211 | Log *log = GetLog(mask: LLDBLog::Types); |
| 3212 | |
| 3213 | LLDB_LOGF(log, "AOCRT::NPI Evaluate(isa = 0x%" PRIx64 ")" , (uint64_t)isa); |
| 3214 | |
| 3215 | if ((isa & ~m_objc_debug_isa_class_mask) == 0) |
| 3216 | return false; |
| 3217 | |
| 3218 | // If all of the indexed ISA variables are set, then its possible that this |
| 3219 | // ISA is indexed, and we should first try to get its value using the index. |
| 3220 | // Note, we check these variables first as the ObjC runtime will set at least |
| 3221 | // one of their values to 0 if they aren't needed. |
| 3222 | if (m_objc_debug_indexed_isa_magic_mask && |
| 3223 | m_objc_debug_indexed_isa_magic_value && |
| 3224 | m_objc_debug_indexed_isa_index_mask && |
| 3225 | m_objc_debug_indexed_isa_index_shift && m_objc_indexed_classes) { |
| 3226 | if ((isa & ~m_objc_debug_indexed_isa_index_mask) == 0) |
| 3227 | return false; |
| 3228 | |
| 3229 | if ((isa & m_objc_debug_indexed_isa_magic_mask) == |
| 3230 | m_objc_debug_indexed_isa_magic_value) { |
| 3231 | // Magic bits are correct, so try extract the index. |
| 3232 | uintptr_t index = (isa & m_objc_debug_indexed_isa_index_mask) >> |
| 3233 | m_objc_debug_indexed_isa_index_shift; |
| 3234 | // If the index is out of bounds of the length of the array then check if |
| 3235 | // the array has been updated. If that is the case then we should try |
| 3236 | // read the count again, and update the cache if the count has been |
| 3237 | // updated. |
| 3238 | if (index > m_indexed_isa_cache.size()) { |
| 3239 | LLDB_LOGF(log, |
| 3240 | "AOCRT::NPI (index = %" PRIu64 |
| 3241 | ") exceeds cache (size = %" PRIu64 ")" , |
| 3242 | (uint64_t)index, (uint64_t)m_indexed_isa_cache.size()); |
| 3243 | |
| 3244 | Process *process(m_runtime.GetProcess()); |
| 3245 | |
| 3246 | ModuleSP objc_module_sp(m_objc_module_wp.lock()); |
| 3247 | if (!objc_module_sp) |
| 3248 | return false; |
| 3249 | |
| 3250 | Status error; |
| 3251 | auto objc_indexed_classes_count = ExtractRuntimeGlobalSymbol( |
| 3252 | process, name: ConstString("objc_indexed_classes_count" ), module_sp: objc_module_sp, |
| 3253 | error); |
| 3254 | if (error.Fail()) |
| 3255 | return false; |
| 3256 | |
| 3257 | LLDB_LOGF(log, "AOCRT::NPI (new class count = %" PRIu64 ")" , |
| 3258 | (uint64_t)objc_indexed_classes_count); |
| 3259 | |
| 3260 | if (objc_indexed_classes_count > m_indexed_isa_cache.size()) { |
| 3261 | // Read the class entries we don't have. We should just read all of |
| 3262 | // them instead of just the one we need as then we can cache those we |
| 3263 | // may need later. |
| 3264 | auto num_new_classes = |
| 3265 | objc_indexed_classes_count - m_indexed_isa_cache.size(); |
| 3266 | const uint32_t addr_size = process->GetAddressByteSize(); |
| 3267 | DataBufferHeap buffer(num_new_classes * addr_size, 0); |
| 3268 | |
| 3269 | lldb::addr_t last_read_class = |
| 3270 | m_objc_indexed_classes + (m_indexed_isa_cache.size() * addr_size); |
| 3271 | size_t bytes_read = process->ReadMemory( |
| 3272 | vm_addr: last_read_class, buf: buffer.GetBytes(), size: buffer.GetByteSize(), error); |
| 3273 | if (error.Fail() || bytes_read != buffer.GetByteSize()) |
| 3274 | return false; |
| 3275 | |
| 3276 | LLDB_LOGF(log, "AOCRT::NPI (read new classes count = %" PRIu64 ")" , |
| 3277 | (uint64_t)num_new_classes); |
| 3278 | |
| 3279 | // Append the new entries to the existing cache. |
| 3280 | DataExtractor data(buffer.GetBytes(), buffer.GetByteSize(), |
| 3281 | process->GetByteOrder(), |
| 3282 | process->GetAddressByteSize()); |
| 3283 | |
| 3284 | lldb::offset_t offset = 0; |
| 3285 | for (unsigned i = 0; i != num_new_classes; ++i) |
| 3286 | m_indexed_isa_cache.push_back(x: data.GetAddress(offset_ptr: &offset)); |
| 3287 | } |
| 3288 | } |
| 3289 | |
| 3290 | // If the index is still out of range then this isn't a pointer. |
| 3291 | if (index >= m_indexed_isa_cache.size()) |
| 3292 | return false; |
| 3293 | |
| 3294 | LLDB_LOGF(log, "AOCRT::NPI Evaluate(ret_isa = 0x%" PRIx64 ")" , |
| 3295 | (uint64_t)m_indexed_isa_cache[index]); |
| 3296 | |
| 3297 | ret_isa = m_indexed_isa_cache[index]; |
| 3298 | return (ret_isa != 0); // this is a pointer so 0 is not a valid value |
| 3299 | } |
| 3300 | |
| 3301 | return false; |
| 3302 | } |
| 3303 | |
| 3304 | // Definitely not an indexed ISA, so try to use a mask to extract the pointer |
| 3305 | // from the ISA. |
| 3306 | if ((isa & m_objc_debug_isa_magic_mask) == m_objc_debug_isa_magic_value) { |
| 3307 | ret_isa = isa & m_objc_debug_isa_class_mask; |
| 3308 | return (ret_isa != 0); // this is a pointer so 0 is not a valid value |
| 3309 | } |
| 3310 | return false; |
| 3311 | } |
| 3312 | |
| 3313 | ObjCLanguageRuntime::EncodingToTypeSP AppleObjCRuntimeV2::GetEncodingToType() { |
| 3314 | if (!m_encoding_to_type_sp) |
| 3315 | m_encoding_to_type_sp = |
| 3316 | std::make_shared<AppleObjCTypeEncodingParser>(args&: *this); |
| 3317 | return m_encoding_to_type_sp; |
| 3318 | } |
| 3319 | |
| 3320 | lldb_private::AppleObjCRuntime::ObjCISA |
| 3321 | AppleObjCRuntimeV2::GetPointerISA(ObjCISA isa) { |
| 3322 | ObjCISA ret = isa; |
| 3323 | |
| 3324 | if (auto *non_pointer_isa_cache = GetNonPointerIsaCache()) |
| 3325 | non_pointer_isa_cache->EvaluateNonPointerISA(isa, ret_isa&: ret); |
| 3326 | |
| 3327 | return ret; |
| 3328 | } |
| 3329 | |
| 3330 | bool AppleObjCRuntimeV2::GetCFBooleanValuesIfNeeded() { |
| 3331 | if (m_CFBoolean_values) |
| 3332 | return true; |
| 3333 | |
| 3334 | static ConstString g_dunder_kCFBooleanFalse("__kCFBooleanFalse" ); |
| 3335 | static ConstString g_dunder_kCFBooleanTrue("__kCFBooleanTrue" ); |
| 3336 | static ConstString g_kCFBooleanFalse("kCFBooleanFalse" ); |
| 3337 | static ConstString g_kCFBooleanTrue("kCFBooleanTrue" ); |
| 3338 | |
| 3339 | std::function<lldb::addr_t(ConstString, ConstString)> get_symbol = |
| 3340 | [this](ConstString sym, ConstString real_sym) -> lldb::addr_t { |
| 3341 | SymbolContextList sc_list; |
| 3342 | GetProcess()->GetTarget().GetImages().FindSymbolsWithNameAndType( |
| 3343 | name: sym, symbol_type: lldb::eSymbolTypeData, sc_list); |
| 3344 | if (sc_list.GetSize() == 1) { |
| 3345 | SymbolContext sc; |
| 3346 | sc_list.GetContextAtIndex(idx: 0, sc); |
| 3347 | if (sc.symbol) |
| 3348 | return sc.symbol->GetLoadAddress(target: &GetProcess()->GetTarget()); |
| 3349 | } |
| 3350 | GetProcess()->GetTarget().GetImages().FindSymbolsWithNameAndType( |
| 3351 | name: real_sym, symbol_type: lldb::eSymbolTypeData, sc_list); |
| 3352 | if (sc_list.GetSize() != 1) |
| 3353 | return LLDB_INVALID_ADDRESS; |
| 3354 | |
| 3355 | SymbolContext sc; |
| 3356 | sc_list.GetContextAtIndex(idx: 0, sc); |
| 3357 | if (!sc.symbol) |
| 3358 | return LLDB_INVALID_ADDRESS; |
| 3359 | |
| 3360 | lldb::addr_t addr = sc.symbol->GetLoadAddress(target: &GetProcess()->GetTarget()); |
| 3361 | Status error; |
| 3362 | addr = GetProcess()->ReadPointerFromMemory(vm_addr: addr, error); |
| 3363 | if (error.Fail()) |
| 3364 | return LLDB_INVALID_ADDRESS; |
| 3365 | return addr; |
| 3366 | }; |
| 3367 | |
| 3368 | lldb::addr_t false_addr = get_symbol(g_dunder_kCFBooleanFalse, g_kCFBooleanFalse); |
| 3369 | lldb::addr_t true_addr = get_symbol(g_dunder_kCFBooleanTrue, g_kCFBooleanTrue); |
| 3370 | |
| 3371 | return (m_CFBoolean_values = {false_addr, true_addr}).operator bool(); |
| 3372 | } |
| 3373 | |
| 3374 | void AppleObjCRuntimeV2::GetValuesForGlobalCFBooleans(lldb::addr_t &cf_true, |
| 3375 | lldb::addr_t &cf_false) { |
| 3376 | if (GetCFBooleanValuesIfNeeded()) { |
| 3377 | cf_true = m_CFBoolean_values->second; |
| 3378 | cf_false = m_CFBoolean_values->first; |
| 3379 | } else |
| 3380 | this->AppleObjCRuntime::GetValuesForGlobalCFBooleans(cf_true, cf_false); |
| 3381 | } |
| 3382 | |
| 3383 | void AppleObjCRuntimeV2::ModulesDidLoad(const ModuleList &module_list) { |
| 3384 | AppleObjCRuntime::ModulesDidLoad(module_list); |
| 3385 | if (HasReadObjCLibrary() && m_shared_cache_image_headers_up) |
| 3386 | m_shared_cache_image_headers_up->SetNeedsUpdate(); |
| 3387 | } |
| 3388 | |
| 3389 | bool AppleObjCRuntimeV2::IsSharedCacheImageLoaded(uint16_t image_index) { |
| 3390 | if (!m_shared_cache_image_headers_up) { |
| 3391 | m_shared_cache_image_headers_up = |
| 3392 | SharedCacheImageHeaders::CreateSharedCacheImageHeaders(runtime&: *this); |
| 3393 | } |
| 3394 | if (m_shared_cache_image_headers_up) |
| 3395 | return m_shared_cache_image_headers_up->IsImageLoaded(image_index); |
| 3396 | |
| 3397 | return false; |
| 3398 | } |
| 3399 | |
| 3400 | std::optional<uint64_t> AppleObjCRuntimeV2::() { |
| 3401 | if (!m_shared_cache_image_headers_up) { |
| 3402 | m_shared_cache_image_headers_up = |
| 3403 | SharedCacheImageHeaders::CreateSharedCacheImageHeaders(runtime&: *this); |
| 3404 | } |
| 3405 | if (m_shared_cache_image_headers_up) |
| 3406 | return m_shared_cache_image_headers_up->GetVersion(); |
| 3407 | |
| 3408 | return std::nullopt; |
| 3409 | } |
| 3410 | |
| 3411 | StructuredData::ObjectSP |
| 3412 | AppleObjCRuntimeV2::GetLanguageSpecificData(SymbolContext sc) { |
| 3413 | auto dict_up = std::make_unique<StructuredData::Dictionary>(); |
| 3414 | dict_up->AddItem(key: "Objective-C runtime version" , |
| 3415 | value_sp: std::make_unique<StructuredData::UnsignedInteger>(args: 2)); |
| 3416 | return dict_up; |
| 3417 | } |
| 3418 | |
| 3419 | #pragma mark Frame recognizers |
| 3420 | |
| 3421 | class ObjCExceptionRecognizedStackFrame : public RecognizedStackFrame { |
| 3422 | public: |
| 3423 | ObjCExceptionRecognizedStackFrame(StackFrameSP frame_sp) { |
| 3424 | ThreadSP thread_sp = frame_sp->GetThread(); |
| 3425 | ProcessSP process_sp = thread_sp->GetProcess(); |
| 3426 | |
| 3427 | const lldb::ABISP &abi = process_sp->GetABI(); |
| 3428 | if (!abi) |
| 3429 | return; |
| 3430 | |
| 3431 | TypeSystemClangSP scratch_ts_sp = |
| 3432 | ScratchTypeSystemClang::GetForTarget(target&: process_sp->GetTarget()); |
| 3433 | if (!scratch_ts_sp) |
| 3434 | return; |
| 3435 | CompilerType voidstar = |
| 3436 | scratch_ts_sp->GetBasicType(type: lldb::eBasicTypeVoid).GetPointerType(); |
| 3437 | |
| 3438 | ValueList args; |
| 3439 | Value input_value; |
| 3440 | input_value.SetCompilerType(voidstar); |
| 3441 | args.PushValue(value: input_value); |
| 3442 | |
| 3443 | if (!abi->GetArgumentValues(thread&: *thread_sp, values&: args)) |
| 3444 | return; |
| 3445 | |
| 3446 | addr_t exception_addr = args.GetValueAtIndex(idx: 0)->GetScalar().ULongLong(); |
| 3447 | |
| 3448 | Value value(exception_addr); |
| 3449 | value.SetCompilerType(voidstar); |
| 3450 | exception = ValueObjectConstResult::Create(exe_scope: frame_sp.get(), value, |
| 3451 | name: ConstString("exception" )); |
| 3452 | exception = ValueObjectRecognizerSynthesizedValue::Create( |
| 3453 | parent&: *exception, type: eValueTypeVariableArgument); |
| 3454 | exception = exception->GetDynamicValue(valueType: eDynamicDontRunTarget); |
| 3455 | |
| 3456 | m_arguments = ValueObjectListSP(new ValueObjectList()); |
| 3457 | m_arguments->Append(val_obj_sp: exception); |
| 3458 | |
| 3459 | m_stop_desc = "hit Objective-C exception" ; |
| 3460 | } |
| 3461 | |
| 3462 | ValueObjectSP exception; |
| 3463 | |
| 3464 | lldb::ValueObjectSP GetExceptionObject() override { return exception; } |
| 3465 | }; |
| 3466 | |
| 3467 | class ObjCExceptionThrowFrameRecognizer : public StackFrameRecognizer { |
| 3468 | lldb::RecognizedStackFrameSP |
| 3469 | RecognizeFrame(lldb::StackFrameSP frame) override { |
| 3470 | return lldb::RecognizedStackFrameSP( |
| 3471 | new ObjCExceptionRecognizedStackFrame(frame)); |
| 3472 | }; |
| 3473 | std::string GetName() override { |
| 3474 | return "ObjC Exception Throw StackFrame Recognizer" ; |
| 3475 | } |
| 3476 | }; |
| 3477 | |
| 3478 | static void RegisterObjCExceptionRecognizer(Process *process) { |
| 3479 | FileSpec module; |
| 3480 | ConstString function; |
| 3481 | std::tie(args&: module, args&: function) = AppleObjCRuntime::GetExceptionThrowLocation(); |
| 3482 | std::vector<ConstString> symbols = {function}; |
| 3483 | |
| 3484 | process->GetTarget().GetFrameRecognizerManager().AddRecognizer( |
| 3485 | recognizer: StackFrameRecognizerSP(new ObjCExceptionThrowFrameRecognizer()), |
| 3486 | module: module.GetFilename(), symbols, symbol_mangling: Mangled::NamePreference::ePreferDemangled, |
| 3487 | /*first_instruction_only*/ true); |
| 3488 | } |
| 3489 | |