1 //===-- IRForTarget.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 "IRForTarget.h" 10 11 #include "ClangExpressionDeclMap.h" 12 #include "ClangUtil.h" 13 14 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" 15 #include "llvm/IR/Constants.h" 16 #include "llvm/IR/DataLayout.h" 17 #include "llvm/IR/InstrTypes.h" 18 #include "llvm/IR/Instructions.h" 19 #include "llvm/IR/Intrinsics.h" 20 #include "llvm/IR/LegacyPassManager.h" 21 #include "llvm/IR/Metadata.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/IR/ValueSymbolTable.h" 24 #include "llvm/Support/raw_ostream.h" 25 #include "llvm/Transforms/IPO.h" 26 27 #include "clang/AST/ASTContext.h" 28 29 #include "lldb/Core/dwarf.h" 30 #include "lldb/Expression/IRExecutionUnit.h" 31 #include "lldb/Expression/IRInterpreter.h" 32 #include "lldb/Symbol/CompilerType.h" 33 #include "lldb/Utility/ConstString.h" 34 #include "lldb/Utility/DataBufferHeap.h" 35 #include "lldb/Utility/Endian.h" 36 #include "lldb/Utility/Log.h" 37 #include "lldb/Utility/Scalar.h" 38 #include "lldb/Utility/StreamString.h" 39 40 #include <map> 41 42 using namespace llvm; 43 44 typedef SmallVector<Instruction *, 2> InstrList; 45 46 IRForTarget::FunctionValueCache::FunctionValueCache(Maker const &maker) 47 : m_maker(maker), m_values() {} 48 49 IRForTarget::FunctionValueCache::~FunctionValueCache() {} 50 51 llvm::Value * 52 IRForTarget::FunctionValueCache::GetValue(llvm::Function *function) { 53 if (!m_values.count(function)) { 54 llvm::Value *ret = m_maker(function); 55 m_values[function] = ret; 56 return ret; 57 } 58 return m_values[function]; 59 } 60 61 static llvm::Value *FindEntryInstruction(llvm::Function *function) { 62 if (function->empty()) 63 return nullptr; 64 65 return function->getEntryBlock().getFirstNonPHIOrDbg(); 66 } 67 68 IRForTarget::IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map, 69 bool resolve_vars, 70 lldb_private::IRExecutionUnit &execution_unit, 71 lldb_private::Stream &error_stream, 72 const char *func_name) 73 : m_resolve_vars(resolve_vars), m_func_name(func_name), 74 m_decl_map(decl_map), m_error_stream(error_stream), 75 m_execution_unit(execution_unit), 76 m_entry_instruction_finder(FindEntryInstruction) {} 77 78 /* Handy utility functions used at several places in the code */ 79 80 static std::string PrintValue(const Value *value, bool truncate = false) { 81 std::string s; 82 if (value) { 83 raw_string_ostream rso(s); 84 value->print(rso); 85 rso.flush(); 86 if (truncate) 87 s.resize(s.length() - 1); 88 } 89 return s; 90 } 91 92 static std::string PrintType(const llvm::Type *type, bool truncate = false) { 93 std::string s; 94 raw_string_ostream rso(s); 95 type->print(rso); 96 rso.flush(); 97 if (truncate) 98 s.resize(s.length() - 1); 99 return s; 100 } 101 102 bool IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function) { 103 llvm_function.setLinkage(GlobalValue::ExternalLinkage); 104 105 return true; 106 } 107 108 clang::NamedDecl *IRForTarget::DeclForGlobal(const GlobalValue *global_val, 109 Module *module) { 110 NamedMDNode *named_metadata = 111 module->getNamedMetadata("clang.global.decl.ptrs"); 112 113 if (!named_metadata) 114 return nullptr; 115 116 unsigned num_nodes = named_metadata->getNumOperands(); 117 unsigned node_index; 118 119 for (node_index = 0; node_index < num_nodes; ++node_index) { 120 llvm::MDNode *metadata_node = 121 dyn_cast<llvm::MDNode>(named_metadata->getOperand(node_index)); 122 if (!metadata_node) 123 return nullptr; 124 125 if (metadata_node->getNumOperands() != 2) 126 continue; 127 128 if (mdconst::dyn_extract_or_null<GlobalValue>( 129 metadata_node->getOperand(0)) != global_val) 130 continue; 131 132 ConstantInt *constant_int = 133 mdconst::dyn_extract<ConstantInt>(metadata_node->getOperand(1)); 134 135 if (!constant_int) 136 return nullptr; 137 138 uintptr_t ptr = constant_int->getZExtValue(); 139 140 return reinterpret_cast<clang::NamedDecl *>(ptr); 141 } 142 143 return nullptr; 144 } 145 146 clang::NamedDecl *IRForTarget::DeclForGlobal(GlobalValue *global_val) { 147 return DeclForGlobal(global_val, m_module); 148 } 149 150 /// Returns true iff the mangled symbol is for a static guard variable. 151 static bool isGuardVariableSymbol(llvm::StringRef mangled_symbol, 152 bool check_ms_abi = true) { 153 bool result = mangled_symbol.startswith("_ZGV"); // Itanium ABI guard variable 154 if (check_ms_abi) 155 result |= mangled_symbol.endswith("@4IA"); // Microsoft ABI 156 return result; 157 } 158 159 bool IRForTarget::CreateResultVariable(llvm::Function &llvm_function) { 160 lldb_private::Log *log( 161 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 162 163 if (!m_resolve_vars) 164 return true; 165 166 // Find the result variable. If it doesn't exist, we can give up right here. 167 168 ValueSymbolTable &value_symbol_table = m_module->getValueSymbolTable(); 169 170 llvm::StringRef result_name; 171 bool found_result = false; 172 173 for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) { 174 result_name = value_symbol.first(); 175 176 // Check if this is a guard variable. It seems this causes some hiccups 177 // on Windows, so let's only check for Itanium guard variables. 178 bool is_guard_var = isGuardVariableSymbol(result_name, /*MS ABI*/ false); 179 180 if (result_name.contains("$__lldb_expr_result_ptr") && !is_guard_var) { 181 found_result = true; 182 m_result_is_pointer = true; 183 break; 184 } 185 186 if (result_name.contains("$__lldb_expr_result") && !is_guard_var) { 187 found_result = true; 188 m_result_is_pointer = false; 189 break; 190 } 191 } 192 193 if (!found_result) { 194 LLDB_LOG(log, "Couldn't find result variable"); 195 196 return true; 197 } 198 199 LLDB_LOG(log, "Result name: \"{0}\"", result_name); 200 201 Value *result_value = m_module->getNamedValue(result_name); 202 203 if (!result_value) { 204 LLDB_LOG(log, "Result variable had no data"); 205 206 m_error_stream.Format("Internal error [IRForTarget]: Result variable's " 207 "name ({0}) exists, but not its definition\n", 208 result_name); 209 210 return false; 211 } 212 213 LLDB_LOG(log, "Found result in the IR: \"{0}\"", 214 PrintValue(result_value, false)); 215 216 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value); 217 218 if (!result_global) { 219 LLDB_LOG(log, "Result variable isn't a GlobalVariable"); 220 221 m_error_stream.Format("Internal error [IRForTarget]: Result variable ({0}) " 222 "is defined, but is not a global variable\n", 223 result_name); 224 225 return false; 226 } 227 228 clang::NamedDecl *result_decl = DeclForGlobal(result_global); 229 if (!result_decl) { 230 LLDB_LOG(log, "Result variable doesn't have a corresponding Decl"); 231 232 m_error_stream.Format("Internal error [IRForTarget]: Result variable ({0}) " 233 "does not have a corresponding Clang entity\n", 234 result_name); 235 236 return false; 237 } 238 239 if (log) { 240 std::string decl_desc_str; 241 raw_string_ostream decl_desc_stream(decl_desc_str); 242 result_decl->print(decl_desc_stream); 243 decl_desc_stream.flush(); 244 245 LLDB_LOG(log, "Found result decl: \"{0}\"", decl_desc_str); 246 } 247 248 clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl); 249 if (!result_var) { 250 LLDB_LOG(log, "Result variable Decl isn't a VarDecl"); 251 252 m_error_stream.Format("Internal error [IRForTarget]: Result variable " 253 "({0})'s corresponding Clang entity isn't a " 254 "variable\n", 255 result_name); 256 257 return false; 258 } 259 260 // Get the next available result name from m_decl_map and create the 261 // persistent variable for it 262 263 // If the result is an Lvalue, it is emitted as a pointer; see 264 // ASTResultSynthesizer::SynthesizeBodyResult. 265 if (m_result_is_pointer) { 266 clang::QualType pointer_qual_type = result_var->getType(); 267 const clang::Type *pointer_type = pointer_qual_type.getTypePtr(); 268 269 const clang::PointerType *pointer_pointertype = 270 pointer_type->getAs<clang::PointerType>(); 271 const clang::ObjCObjectPointerType *pointer_objcobjpointertype = 272 pointer_type->getAs<clang::ObjCObjectPointerType>(); 273 274 if (pointer_pointertype) { 275 clang::QualType element_qual_type = pointer_pointertype->getPointeeType(); 276 277 m_result_type = lldb_private::TypeFromParser( 278 m_decl_map->GetTypeSystem()->GetType(element_qual_type)); 279 } else if (pointer_objcobjpointertype) { 280 clang::QualType element_qual_type = 281 clang::QualType(pointer_objcobjpointertype->getObjectType(), 0); 282 283 m_result_type = lldb_private::TypeFromParser( 284 m_decl_map->GetTypeSystem()->GetType(element_qual_type)); 285 } else { 286 LLDB_LOG(log, "Expected result to have pointer type, but it did not"); 287 288 m_error_stream.Format("Internal error [IRForTarget]: Lvalue result ({0}) " 289 "is not a pointer variable\n", 290 result_name); 291 292 return false; 293 } 294 } else { 295 m_result_type = lldb_private::TypeFromParser( 296 m_decl_map->GetTypeSystem()->GetType(result_var->getType())); 297 } 298 299 lldb::TargetSP target_sp(m_execution_unit.GetTarget()); 300 llvm::Optional<uint64_t> bit_size = m_result_type.GetBitSize(target_sp.get()); 301 if (!bit_size) { 302 lldb_private::StreamString type_desc_stream; 303 m_result_type.DumpTypeDescription(&type_desc_stream); 304 305 LLDB_LOG(log, "Result type has unknown size"); 306 307 m_error_stream.Printf("Error [IRForTarget]: Size of result type '%s' " 308 "couldn't be determined\n", 309 type_desc_stream.GetData()); 310 return false; 311 } 312 313 if (log) { 314 lldb_private::StreamString type_desc_stream; 315 m_result_type.DumpTypeDescription(&type_desc_stream); 316 317 LLDB_LOG(log, "Result decl type: \"{0}\"", type_desc_stream.GetData()); 318 } 319 320 m_result_name = lldb_private::ConstString("$RESULT_NAME"); 321 322 LLDB_LOG(log, "Creating a new result global: \"{0}\" with size {1}", 323 m_result_name, 324 m_result_type.GetByteSize(target_sp.get()).getValueOr(0)); 325 326 // Construct a new result global and set up its metadata 327 328 GlobalVariable *new_result_global = new GlobalVariable( 329 (*m_module), result_global->getType()->getElementType(), 330 false, /* not constant */ 331 GlobalValue::ExternalLinkage, nullptr, /* no initializer */ 332 m_result_name.GetCString()); 333 334 // It's too late in compilation to create a new VarDecl for this, but we 335 // don't need to. We point the metadata at the old VarDecl. This creates an 336 // odd anomaly: a variable with a Value whose name is something like $0 and a 337 // Decl whose name is $__lldb_expr_result. This condition is handled in 338 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is 339 // fixed up. 340 341 ConstantInt *new_constant_int = 342 ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()), 343 reinterpret_cast<uintptr_t>(result_decl), false); 344 345 llvm::Metadata *values[2]; 346 values[0] = ConstantAsMetadata::get(new_result_global); 347 values[1] = ConstantAsMetadata::get(new_constant_int); 348 349 ArrayRef<Metadata *> value_ref(values, 2); 350 351 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref); 352 NamedMDNode *named_metadata = 353 m_module->getNamedMetadata("clang.global.decl.ptrs"); 354 named_metadata->addOperand(persistent_global_md); 355 356 LLDB_LOG(log, "Replacing \"{0}\" with \"{1}\"", PrintValue(result_global), 357 PrintValue(new_result_global)); 358 359 if (result_global->use_empty()) { 360 // We need to synthesize a store for this variable, because otherwise 361 // there's nothing to put into its equivalent persistent variable. 362 363 BasicBlock &entry_block(llvm_function.getEntryBlock()); 364 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg()); 365 366 if (!first_entry_instruction) 367 return false; 368 369 if (!result_global->hasInitializer()) { 370 LLDB_LOG(log, "Couldn't find initializer for unused variable"); 371 372 m_error_stream.Format("Internal error [IRForTarget]: Result variable " 373 "({0}) has no writes and no initializer\n", 374 result_name); 375 376 return false; 377 } 378 379 Constant *initializer = result_global->getInitializer(); 380 381 StoreInst *synthesized_store = 382 new StoreInst(initializer, new_result_global, first_entry_instruction); 383 384 LLDB_LOG(log, "Synthesized result store \"{0}\"\n", 385 PrintValue(synthesized_store)); 386 } else { 387 result_global->replaceAllUsesWith(new_result_global); 388 } 389 390 if (!m_decl_map->AddPersistentVariable( 391 result_decl, m_result_name, m_result_type, true, m_result_is_pointer)) 392 return false; 393 394 result_global->eraseFromParent(); 395 396 return true; 397 } 398 399 bool IRForTarget::RewriteObjCConstString(llvm::GlobalVariable *ns_str, 400 llvm::GlobalVariable *cstr) { 401 lldb_private::Log *log( 402 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 403 404 Type *ns_str_ty = ns_str->getType(); 405 406 Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext()); 407 Type *i32_ty = Type::getInt32Ty(m_module->getContext()); 408 Type *i8_ty = Type::getInt8Ty(m_module->getContext()); 409 410 if (!m_CFStringCreateWithBytes) { 411 lldb::addr_t CFStringCreateWithBytes_addr; 412 413 static lldb_private::ConstString g_CFStringCreateWithBytes_str( 414 "CFStringCreateWithBytes"); 415 416 bool missing_weak = false; 417 CFStringCreateWithBytes_addr = 418 m_execution_unit.FindSymbol(g_CFStringCreateWithBytes_str, 419 missing_weak); 420 if (CFStringCreateWithBytes_addr == LLDB_INVALID_ADDRESS || missing_weak) { 421 LLDB_LOG(log, "Couldn't find CFStringCreateWithBytes in the target"); 422 423 m_error_stream.Printf("Error [IRForTarget]: Rewriting an Objective-C " 424 "constant string requires " 425 "CFStringCreateWithBytes\n"); 426 427 return false; 428 } 429 430 LLDB_LOG(log, "Found CFStringCreateWithBytes at {0}", 431 CFStringCreateWithBytes_addr); 432 433 // Build the function type: 434 // 435 // CFStringRef CFStringCreateWithBytes ( 436 // CFAllocatorRef alloc, 437 // const UInt8 *bytes, 438 // CFIndex numBytes, 439 // CFStringEncoding encoding, 440 // Boolean isExternalRepresentation 441 // ); 442 // 443 // We make the following substitutions: 444 // 445 // CFStringRef -> i8* 446 // CFAllocatorRef -> i8* 447 // UInt8 * -> i8* 448 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its 449 // pointer size for now) CFStringEncoding -> i32 Boolean -> i8 450 451 Type *arg_type_array[5]; 452 453 arg_type_array[0] = i8_ptr_ty; 454 arg_type_array[1] = i8_ptr_ty; 455 arg_type_array[2] = m_intptr_ty; 456 arg_type_array[3] = i32_ty; 457 arg_type_array[4] = i8_ty; 458 459 ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5); 460 461 llvm::FunctionType *CFSCWB_ty = 462 FunctionType::get(ns_str_ty, CFSCWB_arg_types, false); 463 464 // Build the constant containing the pointer to the function 465 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty); 466 Constant *CFSCWB_addr_int = 467 ConstantInt::get(m_intptr_ty, CFStringCreateWithBytes_addr, false); 468 m_CFStringCreateWithBytes = { 469 CFSCWB_ty, ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty)}; 470 } 471 472 ConstantDataSequential *string_array = nullptr; 473 474 if (cstr) 475 string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer()); 476 477 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty); 478 Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) 479 : Constant::getNullValue(i8_ptr_ty); 480 Constant *numBytes_arg = ConstantInt::get( 481 m_intptr_ty, cstr ? (string_array->getNumElements() - 1) * string_array->getElementByteSize() : 0, false); 482 int encoding_flags = 0; 483 switch (cstr ? string_array->getElementByteSize() : 1) { 484 case 1: 485 encoding_flags = 0x08000100; /* 0x08000100 is kCFStringEncodingUTF8 */ 486 break; 487 case 2: 488 encoding_flags = 0x0100; /* 0x0100 is kCFStringEncodingUTF16 */ 489 break; 490 case 4: 491 encoding_flags = 0x0c000100; /* 0x0c000100 is kCFStringEncodingUTF32 */ 492 break; 493 default: 494 encoding_flags = 0x0600; /* fall back to 0x0600, kCFStringEncodingASCII */ 495 LLDB_LOG(log, "Encountered an Objective-C constant string with unusual " 496 "element size {0}", 497 string_array->getElementByteSize()); 498 } 499 Constant *encoding_arg = ConstantInt::get(i32_ty, encoding_flags, false); 500 Constant *isExternal_arg = 501 ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */ 502 503 Value *argument_array[5]; 504 505 argument_array[0] = alloc_arg; 506 argument_array[1] = bytes_arg; 507 argument_array[2] = numBytes_arg; 508 argument_array[3] = encoding_arg; 509 argument_array[4] = isExternal_arg; 510 511 ArrayRef<Value *> CFSCWB_arguments(argument_array, 5); 512 513 FunctionValueCache CFSCWB_Caller( 514 [this, &CFSCWB_arguments](llvm::Function *function) -> llvm::Value * { 515 return CallInst::Create( 516 m_CFStringCreateWithBytes, CFSCWB_arguments, 517 "CFStringCreateWithBytes", 518 llvm::cast<Instruction>( 519 m_entry_instruction_finder.GetValue(function))); 520 }); 521 522 if (!UnfoldConstant(ns_str, nullptr, CFSCWB_Caller, m_entry_instruction_finder, 523 m_error_stream)) { 524 LLDB_LOG(log, "Couldn't replace the NSString with the result of the call"); 525 526 m_error_stream.Printf("error [IRForTarget internal]: Couldn't replace an " 527 "Objective-C constant string with a dynamic " 528 "string\n"); 529 530 return false; 531 } 532 533 ns_str->eraseFromParent(); 534 535 return true; 536 } 537 538 bool IRForTarget::RewriteObjCConstStrings() { 539 lldb_private::Log *log( 540 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 541 542 ValueSymbolTable &value_symbol_table = m_module->getValueSymbolTable(); 543 544 for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) { 545 llvm::StringRef value_name = value_symbol.first(); 546 547 if (value_name.contains("_unnamed_cfstring_")) { 548 Value *nsstring_value = value_symbol.second; 549 550 GlobalVariable *nsstring_global = 551 dyn_cast<GlobalVariable>(nsstring_value); 552 553 if (!nsstring_global) { 554 LLDB_LOG(log, "NSString variable is not a GlobalVariable"); 555 556 m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C " 557 "constant string is not a global variable\n"); 558 559 return false; 560 } 561 562 if (!nsstring_global->hasInitializer()) { 563 LLDB_LOG(log, "NSString variable does not have an initializer"); 564 565 m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C " 566 "constant string does not have an initializer\n"); 567 568 return false; 569 } 570 571 ConstantStruct *nsstring_struct = 572 dyn_cast<ConstantStruct>(nsstring_global->getInitializer()); 573 574 if (!nsstring_struct) { 575 LLDB_LOG(log, 576 "NSString variable's initializer is not a ConstantStruct"); 577 578 m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C " 579 "constant string is not a structure constant\n"); 580 581 return false; 582 } 583 584 // We expect the following structure: 585 // 586 // struct { 587 // int *isa; 588 // int flags; 589 // char *str; 590 // long length; 591 // }; 592 593 if (nsstring_struct->getNumOperands() != 4) { 594 595 LLDB_LOG(log, 596 "NSString variable's initializer structure has an " 597 "unexpected number of members. Should be 4, is {0}", 598 nsstring_struct->getNumOperands()); 599 600 m_error_stream.Printf("Internal error [IRForTarget]: The struct for an " 601 "Objective-C constant string is not as " 602 "expected\n"); 603 604 return false; 605 } 606 607 Constant *nsstring_member = nsstring_struct->getOperand(2); 608 609 if (!nsstring_member) { 610 LLDB_LOG(log, "NSString initializer's str element was empty"); 611 612 m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C " 613 "constant string does not have a string " 614 "initializer\n"); 615 616 return false; 617 } 618 619 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member); 620 621 if (!nsstring_expr) { 622 LLDB_LOG(log, 623 "NSString initializer's str element is not a ConstantExpr"); 624 625 m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C " 626 "constant string's string initializer is not " 627 "constant\n"); 628 629 return false; 630 } 631 632 GlobalVariable *cstr_global = nullptr; 633 634 if (nsstring_expr->getOpcode() == Instruction::GetElementPtr) { 635 Constant *nsstring_cstr = nsstring_expr->getOperand(0); 636 cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr); 637 } else if (nsstring_expr->getOpcode() == Instruction::BitCast) { 638 Constant *nsstring_cstr = nsstring_expr->getOperand(0); 639 cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr); 640 } 641 642 if (!cstr_global) { 643 LLDB_LOG(log, 644 "NSString initializer's str element is not a GlobalVariable"); 645 646 m_error_stream.Printf("Internal error [IRForTarget]: Unhandled" 647 "constant string initializer\n"); 648 649 return false; 650 } 651 652 if (!cstr_global->hasInitializer()) { 653 LLDB_LOG(log, "NSString initializer's str element does not have an " 654 "initializer"); 655 656 m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C " 657 "constant string's string initializer doesn't " 658 "point to initialized data\n"); 659 660 return false; 661 } 662 663 /* 664 if (!cstr_array) 665 { 666 if (log) 667 log->PutCString("NSString initializer's str element is not a 668 ConstantArray"); 669 670 if (m_error_stream) 671 m_error_stream.Printf("Internal error [IRForTarget]: An 672 Objective-C constant string's string initializer doesn't point to an 673 array\n"); 674 675 return false; 676 } 677 678 if (!cstr_array->isCString()) 679 { 680 if (log) 681 log->PutCString("NSString initializer's str element is not a C 682 string array"); 683 684 if (m_error_stream) 685 m_error_stream.Printf("Internal error [IRForTarget]: An 686 Objective-C constant string's string initializer doesn't point to a C 687 string\n"); 688 689 return false; 690 } 691 */ 692 693 ConstantDataArray *cstr_array = 694 dyn_cast<ConstantDataArray>(cstr_global->getInitializer()); 695 696 if (cstr_array) 697 LLDB_LOG(log, "Found NSString constant {0}, which contains \"{1}\"", 698 value_name, cstr_array->getAsString()); 699 else 700 LLDB_LOG(log, "Found NSString constant {0}, which contains \"\"", 701 value_name); 702 703 if (!cstr_array) 704 cstr_global = nullptr; 705 706 if (!RewriteObjCConstString(nsstring_global, cstr_global)) { 707 LLDB_LOG(log, "Error rewriting the constant string"); 708 709 // We don't print an error message here because RewriteObjCConstString 710 // has done so for us. 711 712 return false; 713 } 714 } 715 } 716 717 for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) { 718 llvm::StringRef value_name = value_symbol.first(); 719 720 if (value_name == "__CFConstantStringClassReference") { 721 GlobalVariable *gv = dyn_cast<GlobalVariable>(value_symbol.second); 722 723 if (!gv) { 724 LLDB_LOG(log, 725 "__CFConstantStringClassReference is not a global variable"); 726 727 m_error_stream.Printf("Internal error [IRForTarget]: Found a " 728 "CFConstantStringClassReference, but it is not a " 729 "global object\n"); 730 731 return false; 732 } 733 734 gv->eraseFromParent(); 735 736 break; 737 } 738 } 739 740 return true; 741 } 742 743 static bool IsObjCSelectorRef(Value *value) { 744 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value); 745 746 return !(!global_variable || !global_variable->hasName() || 747 !global_variable->getName().startswith("OBJC_SELECTOR_REFERENCES_")); 748 } 749 750 // This function does not report errors; its callers are responsible. 751 bool IRForTarget::RewriteObjCSelector(Instruction *selector_load) { 752 lldb_private::Log *log( 753 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 754 755 LoadInst *load = dyn_cast<LoadInst>(selector_load); 756 757 if (!load) 758 return false; 759 760 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend 761 // gets represented as 762 // 763 // %tmp = load i8** @"OBJC_SELECTOR_REFERENCES_" ; <i8*> %call = call 764 // i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*> 765 // 766 // where %obj is the object pointer and %tmp is the selector. 767 // 768 // @"OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called 769 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_". 770 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string. 771 772 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) 773 // and get the string from its target 774 775 GlobalVariable *_objc_selector_references_ = 776 dyn_cast<GlobalVariable>(load->getPointerOperand()); 777 778 if (!_objc_selector_references_ || 779 !_objc_selector_references_->hasInitializer()) 780 return false; 781 782 Constant *osr_initializer = _objc_selector_references_->getInitializer(); 783 784 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer); 785 786 if (!osr_initializer_expr || 787 osr_initializer_expr->getOpcode() != Instruction::GetElementPtr) 788 return false; 789 790 Value *osr_initializer_base = osr_initializer_expr->getOperand(0); 791 792 if (!osr_initializer_base) 793 return false; 794 795 // Find the string's initializer (a ConstantArray) and get the string from it 796 797 GlobalVariable *_objc_meth_var_name_ = 798 dyn_cast<GlobalVariable>(osr_initializer_base); 799 800 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer()) 801 return false; 802 803 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer(); 804 805 ConstantDataArray *omvn_initializer_array = 806 dyn_cast<ConstantDataArray>(omvn_initializer); 807 808 if (!omvn_initializer_array->isString()) 809 return false; 810 811 std::string omvn_initializer_string = 812 std::string(omvn_initializer_array->getAsString()); 813 814 LLDB_LOG(log, "Found Objective-C selector reference \"{0}\"", 815 omvn_initializer_string); 816 817 // Construct a call to sel_registerName 818 819 if (!m_sel_registerName) { 820 lldb::addr_t sel_registerName_addr; 821 822 bool missing_weak = false; 823 static lldb_private::ConstString g_sel_registerName_str("sel_registerName"); 824 sel_registerName_addr = m_execution_unit.FindSymbol(g_sel_registerName_str, 825 missing_weak); 826 if (sel_registerName_addr == LLDB_INVALID_ADDRESS || missing_weak) 827 return false; 828 829 LLDB_LOG(log, "Found sel_registerName at {0}", sel_registerName_addr); 830 831 // Build the function type: struct objc_selector 832 // *sel_registerName(uint8_t*) 833 834 // The below code would be "more correct," but in actuality what's required 835 // is uint8_t* 836 // Type *sel_type = StructType::get(m_module->getContext()); 837 // Type *sel_ptr_type = PointerType::getUnqual(sel_type); 838 Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext()); 839 840 Type *type_array[1]; 841 842 type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext()); 843 844 ArrayRef<Type *> srN_arg_types(type_array, 1); 845 846 llvm::FunctionType *srN_type = 847 FunctionType::get(sel_ptr_type, srN_arg_types, false); 848 849 // Build the constant containing the pointer to the function 850 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type); 851 Constant *srN_addr_int = 852 ConstantInt::get(m_intptr_ty, sel_registerName_addr, false); 853 m_sel_registerName = {srN_type, 854 ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty)}; 855 } 856 857 Value *argument_array[1]; 858 859 Constant *omvn_pointer = ConstantExpr::getBitCast( 860 _objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext())); 861 862 argument_array[0] = omvn_pointer; 863 864 ArrayRef<Value *> srN_arguments(argument_array, 1); 865 866 CallInst *srN_call = CallInst::Create(m_sel_registerName, srN_arguments, 867 "sel_registerName", selector_load); 868 869 // Replace the load with the call in all users 870 871 selector_load->replaceAllUsesWith(srN_call); 872 873 selector_load->eraseFromParent(); 874 875 return true; 876 } 877 878 bool IRForTarget::RewriteObjCSelectors(BasicBlock &basic_block) { 879 lldb_private::Log *log( 880 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 881 882 InstrList selector_loads; 883 884 for (Instruction &inst : basic_block) { 885 if (LoadInst *load = dyn_cast<LoadInst>(&inst)) 886 if (IsObjCSelectorRef(load->getPointerOperand())) 887 selector_loads.push_back(&inst); 888 } 889 890 for (Instruction *inst : selector_loads) { 891 if (!RewriteObjCSelector(inst)) { 892 m_error_stream.Printf("Internal error [IRForTarget]: Couldn't change a " 893 "static reference to an Objective-C selector to a " 894 "dynamic reference\n"); 895 896 LLDB_LOG(log, "Couldn't rewrite a reference to an Objective-C selector"); 897 898 return false; 899 } 900 } 901 902 return true; 903 } 904 905 static bool IsObjCClassReference(Value *value) { 906 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value); 907 908 return !(!global_variable || !global_variable->hasName() || 909 !global_variable->getName().startswith("OBJC_CLASS_REFERENCES_")); 910 } 911 912 // This function does not report errors; its callers are responsible. 913 bool IRForTarget::RewriteObjCClassReference(Instruction *class_load) { 914 lldb_private::Log *log( 915 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 916 917 LoadInst *load = dyn_cast<LoadInst>(class_load); 918 919 if (!load) 920 return false; 921 922 // Unpack the class name from the reference. In LLVM IR, a reference to an 923 // Objective-C class gets represented as 924 // 925 // %tmp = load %struct._objc_class*, 926 // %struct._objc_class** @OBJC_CLASS_REFERENCES_, align 4 927 // 928 // @"OBJC_CLASS_REFERENCES_ is a bitcast of a character array called 929 // @OBJC_CLASS_NAME_. @OBJC_CLASS_NAME contains the string. 930 931 // Find the pointer's initializer (a ConstantExpr with opcode BitCast) and 932 // get the string from its target 933 934 GlobalVariable *_objc_class_references_ = 935 dyn_cast<GlobalVariable>(load->getPointerOperand()); 936 937 if (!_objc_class_references_ || 938 !_objc_class_references_->hasInitializer()) 939 return false; 940 941 Constant *ocr_initializer = _objc_class_references_->getInitializer(); 942 943 ConstantExpr *ocr_initializer_expr = dyn_cast<ConstantExpr>(ocr_initializer); 944 945 if (!ocr_initializer_expr || 946 ocr_initializer_expr->getOpcode() != Instruction::BitCast) 947 return false; 948 949 Value *ocr_initializer_base = ocr_initializer_expr->getOperand(0); 950 951 if (!ocr_initializer_base) 952 return false; 953 954 // Find the string's initializer (a ConstantArray) and get the string from it 955 956 GlobalVariable *_objc_class_name_ = 957 dyn_cast<GlobalVariable>(ocr_initializer_base); 958 959 if (!_objc_class_name_ || !_objc_class_name_->hasInitializer()) 960 return false; 961 962 Constant *ocn_initializer = _objc_class_name_->getInitializer(); 963 964 ConstantDataArray *ocn_initializer_array = 965 dyn_cast<ConstantDataArray>(ocn_initializer); 966 967 if (!ocn_initializer_array->isString()) 968 return false; 969 970 std::string ocn_initializer_string = 971 std::string(ocn_initializer_array->getAsString()); 972 973 LLDB_LOG(log, "Found Objective-C class reference \"{0}\"", 974 ocn_initializer_string); 975 976 // Construct a call to objc_getClass 977 978 if (!m_objc_getClass) { 979 lldb::addr_t objc_getClass_addr; 980 981 bool missing_weak = false; 982 static lldb_private::ConstString g_objc_getClass_str("objc_getClass"); 983 objc_getClass_addr = m_execution_unit.FindSymbol(g_objc_getClass_str, 984 missing_weak); 985 if (objc_getClass_addr == LLDB_INVALID_ADDRESS || missing_weak) 986 return false; 987 988 LLDB_LOG(log, "Found objc_getClass at {0}", objc_getClass_addr); 989 990 // Build the function type: %struct._objc_class *objc_getClass(i8*) 991 992 Type *class_type = load->getType(); 993 Type *type_array[1]; 994 type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext()); 995 996 ArrayRef<Type *> ogC_arg_types(type_array, 1); 997 998 llvm::FunctionType *ogC_type = 999 FunctionType::get(class_type, ogC_arg_types, false); 1000 1001 // Build the constant containing the pointer to the function 1002 PointerType *ogC_ptr_ty = PointerType::getUnqual(ogC_type); 1003 Constant *ogC_addr_int = 1004 ConstantInt::get(m_intptr_ty, objc_getClass_addr, false); 1005 m_objc_getClass = {ogC_type, 1006 ConstantExpr::getIntToPtr(ogC_addr_int, ogC_ptr_ty)}; 1007 } 1008 1009 Value *argument_array[1]; 1010 1011 Constant *ocn_pointer = ConstantExpr::getBitCast( 1012 _objc_class_name_, Type::getInt8PtrTy(m_module->getContext())); 1013 1014 argument_array[0] = ocn_pointer; 1015 1016 ArrayRef<Value *> ogC_arguments(argument_array, 1); 1017 1018 CallInst *ogC_call = CallInst::Create(m_objc_getClass, ogC_arguments, 1019 "objc_getClass", class_load); 1020 1021 // Replace the load with the call in all users 1022 1023 class_load->replaceAllUsesWith(ogC_call); 1024 1025 class_load->eraseFromParent(); 1026 1027 return true; 1028 } 1029 1030 bool IRForTarget::RewriteObjCClassReferences(BasicBlock &basic_block) { 1031 lldb_private::Log *log( 1032 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1033 1034 InstrList class_loads; 1035 1036 for (Instruction &inst : basic_block) { 1037 if (LoadInst *load = dyn_cast<LoadInst>(&inst)) 1038 if (IsObjCClassReference(load->getPointerOperand())) 1039 class_loads.push_back(&inst); 1040 } 1041 1042 for (Instruction *inst : class_loads) { 1043 if (!RewriteObjCClassReference(inst)) { 1044 m_error_stream.Printf("Internal error [IRForTarget]: Couldn't change a " 1045 "static reference to an Objective-C class to a " 1046 "dynamic reference\n"); 1047 1048 LLDB_LOG(log, "Couldn't rewrite a reference to an Objective-C class"); 1049 1050 return false; 1051 } 1052 } 1053 1054 return true; 1055 } 1056 1057 // This function does not report errors; its callers are responsible. 1058 bool IRForTarget::RewritePersistentAlloc(llvm::Instruction *persistent_alloc) { 1059 lldb_private::Log *log( 1060 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1061 1062 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc); 1063 1064 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr"); 1065 1066 if (!alloc_md || !alloc_md->getNumOperands()) 1067 return false; 1068 1069 ConstantInt *constant_int = 1070 mdconst::dyn_extract<ConstantInt>(alloc_md->getOperand(0)); 1071 1072 if (!constant_int) 1073 return false; 1074 1075 // We attempt to register this as a new persistent variable with the DeclMap. 1076 1077 uintptr_t ptr = constant_int->getZExtValue(); 1078 1079 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr); 1080 1081 lldb_private::TypeFromParser result_decl_type( 1082 m_decl_map->GetTypeSystem()->GetType(decl->getType())); 1083 1084 StringRef decl_name(decl->getName()); 1085 lldb_private::ConstString persistent_variable_name(decl_name.data(), 1086 decl_name.size()); 1087 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, 1088 result_decl_type, false, false)) 1089 return false; 1090 1091 GlobalVariable *persistent_global = new GlobalVariable( 1092 (*m_module), alloc->getType(), false, /* not constant */ 1093 GlobalValue::ExternalLinkage, nullptr, /* no initializer */ 1094 alloc->getName().str()); 1095 1096 // What we're going to do here is make believe this was a regular old 1097 // external variable. That means we need to make the metadata valid. 1098 1099 NamedMDNode *named_metadata = 1100 m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs"); 1101 1102 llvm::Metadata *values[2]; 1103 values[0] = ConstantAsMetadata::get(persistent_global); 1104 values[1] = ConstantAsMetadata::get(constant_int); 1105 1106 ArrayRef<llvm::Metadata *> value_ref(values, 2); 1107 1108 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref); 1109 named_metadata->addOperand(persistent_global_md); 1110 1111 // Now, since the variable is a pointer variable, we will drop in a load of 1112 // that pointer variable. 1113 1114 LoadInst *persistent_load = 1115 new LoadInst(persistent_global->getType()->getPointerElementType(), 1116 persistent_global, "", alloc); 1117 1118 LLDB_LOG(log, "Replacing \"{0}\" with \"{1}\"", PrintValue(alloc), 1119 PrintValue(persistent_load)); 1120 1121 alloc->replaceAllUsesWith(persistent_load); 1122 alloc->eraseFromParent(); 1123 1124 return true; 1125 } 1126 1127 bool IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block) { 1128 if (!m_resolve_vars) 1129 return true; 1130 1131 lldb_private::Log *log( 1132 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1133 1134 InstrList pvar_allocs; 1135 1136 for (Instruction &inst : basic_block) { 1137 1138 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst)) { 1139 llvm::StringRef alloc_name = alloc->getName(); 1140 1141 if (alloc_name.startswith("$") && !alloc_name.startswith("$__lldb")) { 1142 if (alloc_name.find_first_of("0123456789") == 1) { 1143 LLDB_LOG(log, "Rejecting a numeric persistent variable."); 1144 1145 m_error_stream.Printf("Error [IRForTarget]: Names starting with $0, " 1146 "$1, ... are reserved for use as result " 1147 "names\n"); 1148 1149 return false; 1150 } 1151 1152 pvar_allocs.push_back(alloc); 1153 } 1154 } 1155 } 1156 1157 for (Instruction *inst : pvar_allocs) { 1158 if (!RewritePersistentAlloc(inst)) { 1159 m_error_stream.Printf("Internal error [IRForTarget]: Couldn't rewrite " 1160 "the creation of a persistent variable\n"); 1161 1162 LLDB_LOG(log, "Couldn't rewrite the creation of a persistent variable"); 1163 1164 return false; 1165 } 1166 } 1167 1168 return true; 1169 } 1170 1171 // This function does not report errors; its callers are responsible. 1172 bool IRForTarget::MaybeHandleVariable(Value *llvm_value_ptr) { 1173 lldb_private::Log *log( 1174 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1175 1176 LLDB_LOG(log, "MaybeHandleVariable ({0})", PrintValue(llvm_value_ptr)); 1177 1178 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr)) { 1179 switch (constant_expr->getOpcode()) { 1180 default: 1181 break; 1182 case Instruction::GetElementPtr: 1183 case Instruction::BitCast: 1184 Value *s = constant_expr->getOperand(0); 1185 if (!MaybeHandleVariable(s)) 1186 return false; 1187 } 1188 } else if (GlobalVariable *global_variable = 1189 dyn_cast<GlobalVariable>(llvm_value_ptr)) { 1190 if (!GlobalValue::isExternalLinkage(global_variable->getLinkage())) 1191 return true; 1192 1193 clang::NamedDecl *named_decl = DeclForGlobal(global_variable); 1194 1195 if (!named_decl) { 1196 if (IsObjCSelectorRef(llvm_value_ptr)) 1197 return true; 1198 1199 if (!global_variable->hasExternalLinkage()) 1200 return true; 1201 1202 LLDB_LOG(log, "Found global variable \"{0}\" without metadata", 1203 global_variable->getName()); 1204 1205 return false; 1206 } 1207 1208 llvm::StringRef name(named_decl->getName()); 1209 1210 clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl); 1211 if (value_decl == nullptr) 1212 return false; 1213 1214 lldb_private::CompilerType compiler_type = 1215 m_decl_map->GetTypeSystem()->GetType(value_decl->getType()); 1216 1217 const Type *value_type = nullptr; 1218 1219 if (name.startswith("$")) { 1220 // The $__lldb_expr_result name indicates the return value has allocated 1221 // as a static variable. Per the comment at 1222 // ASTResultSynthesizer::SynthesizeBodyResult, accesses to this static 1223 // variable need to be redirected to the result of dereferencing a 1224 // pointer that is passed in as one of the arguments. 1225 // 1226 // Consequently, when reporting the size of the type, we report a pointer 1227 // type pointing to the type of $__lldb_expr_result, not the type itself. 1228 // 1229 // We also do this for any user-declared persistent variables. 1230 compiler_type = compiler_type.GetPointerType(); 1231 value_type = PointerType::get(global_variable->getType(), 0); 1232 } else { 1233 value_type = global_variable->getType(); 1234 } 1235 1236 auto *target = m_execution_unit.GetTarget().get(); 1237 llvm::Optional<uint64_t> value_size = compiler_type.GetByteSize(target); 1238 if (!value_size) 1239 return false; 1240 llvm::Optional<size_t> opt_alignment = 1241 compiler_type.GetTypeBitAlign(target); 1242 if (!opt_alignment) 1243 return false; 1244 lldb::offset_t value_alignment = (*opt_alignment + 7ull) / 8ull; 1245 1246 LLDB_LOG(log, 1247 "Type of \"{0}\" is [clang \"{1}\", llvm \"{2}\"] [size {3}, " 1248 "align {4}]", 1249 name, 1250 lldb_private::ClangUtil::GetQualType(compiler_type).getAsString(), 1251 PrintType(value_type), *value_size, value_alignment); 1252 1253 if (named_decl) 1254 m_decl_map->AddValueToStruct(named_decl, lldb_private::ConstString(name), 1255 llvm_value_ptr, *value_size, 1256 value_alignment); 1257 } else if (dyn_cast<llvm::Function>(llvm_value_ptr)) { 1258 LLDB_LOG(log, "Function pointers aren't handled right now"); 1259 1260 return false; 1261 } 1262 1263 return true; 1264 } 1265 1266 // This function does not report errors; its callers are responsible. 1267 bool IRForTarget::HandleSymbol(Value *symbol) { 1268 lldb_private::Log *log( 1269 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1270 1271 lldb_private::ConstString name(symbol->getName().str().c_str()); 1272 1273 lldb::addr_t symbol_addr = 1274 m_decl_map->GetSymbolAddress(name, lldb::eSymbolTypeAny); 1275 1276 if (symbol_addr == LLDB_INVALID_ADDRESS) { 1277 LLDB_LOG(log, "Symbol \"{0}\" had no address", name); 1278 1279 return false; 1280 } 1281 1282 LLDB_LOG(log, "Found \"{0}\" at {1}", name, symbol_addr); 1283 1284 Type *symbol_type = symbol->getType(); 1285 1286 Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false); 1287 1288 Value *symbol_addr_ptr = 1289 ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type); 1290 1291 LLDB_LOG(log, "Replacing {0} with {1}", PrintValue(symbol), 1292 PrintValue(symbol_addr_ptr)); 1293 1294 symbol->replaceAllUsesWith(symbol_addr_ptr); 1295 1296 return true; 1297 } 1298 1299 bool IRForTarget::MaybeHandleCallArguments(CallInst *Old) { 1300 lldb_private::Log *log( 1301 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1302 1303 LLDB_LOG(log, "MaybeHandleCallArguments({0})", PrintValue(Old)); 1304 1305 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands(); 1306 op_index < num_ops; ++op_index) 1307 // conservatively believe that this is a store 1308 if (!MaybeHandleVariable(Old->getArgOperand(op_index))) { 1309 m_error_stream.Printf("Internal error [IRForTarget]: Couldn't rewrite " 1310 "one of the arguments of a function call.\n"); 1311 1312 return false; 1313 } 1314 1315 return true; 1316 } 1317 1318 bool IRForTarget::HandleObjCClass(Value *classlist_reference) { 1319 lldb_private::Log *log( 1320 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1321 1322 GlobalVariable *global_variable = 1323 dyn_cast<GlobalVariable>(classlist_reference); 1324 1325 if (!global_variable) 1326 return false; 1327 1328 Constant *initializer = global_variable->getInitializer(); 1329 1330 if (!initializer) 1331 return false; 1332 1333 if (!initializer->hasName()) 1334 return false; 1335 1336 StringRef name(initializer->getName()); 1337 lldb_private::ConstString name_cstr(name.str().c_str()); 1338 lldb::addr_t class_ptr = 1339 m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass); 1340 1341 LLDB_LOG(log, "Found reference to Objective-C class {0} ({1})", name, 1342 (unsigned long long)class_ptr); 1343 1344 if (class_ptr == LLDB_INVALID_ADDRESS) 1345 return false; 1346 1347 if (global_variable->use_empty()) 1348 return false; 1349 1350 SmallVector<LoadInst *, 2> load_instructions; 1351 1352 for (llvm::User *u : global_variable->users()) { 1353 if (LoadInst *load_instruction = dyn_cast<LoadInst>(u)) 1354 load_instructions.push_back(load_instruction); 1355 } 1356 1357 if (load_instructions.empty()) 1358 return false; 1359 1360 Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr); 1361 1362 for (LoadInst *load_instruction : load_instructions) { 1363 Constant *class_bitcast = 1364 ConstantExpr::getIntToPtr(class_addr, load_instruction->getType()); 1365 1366 load_instruction->replaceAllUsesWith(class_bitcast); 1367 1368 load_instruction->eraseFromParent(); 1369 } 1370 1371 return true; 1372 } 1373 1374 bool IRForTarget::RemoveCXAAtExit(BasicBlock &basic_block) { 1375 std::vector<CallInst *> calls_to_remove; 1376 1377 for (Instruction &inst : basic_block) { 1378 CallInst *call = dyn_cast<CallInst>(&inst); 1379 1380 // MaybeHandleCallArguments handles error reporting; we are silent here 1381 if (!call) 1382 continue; 1383 1384 bool remove = false; 1385 1386 llvm::Function *func = call->getCalledFunction(); 1387 1388 if (func && func->getName() == "__cxa_atexit") 1389 remove = true; 1390 1391 llvm::Value *val = call->getCalledOperand(); 1392 1393 if (val && val->getName() == "__cxa_atexit") 1394 remove = true; 1395 1396 if (remove) 1397 calls_to_remove.push_back(call); 1398 } 1399 1400 for (CallInst *ci : calls_to_remove) 1401 ci->eraseFromParent(); 1402 1403 return true; 1404 } 1405 1406 bool IRForTarget::ResolveCalls(BasicBlock &basic_block) { 1407 // Prepare the current basic block for execution in the remote process 1408 1409 for (Instruction &inst : basic_block) { 1410 CallInst *call = dyn_cast<CallInst>(&inst); 1411 1412 // MaybeHandleCallArguments handles error reporting; we are silent here 1413 if (call && !MaybeHandleCallArguments(call)) 1414 return false; 1415 } 1416 1417 return true; 1418 } 1419 1420 bool IRForTarget::ResolveExternals(Function &llvm_function) { 1421 lldb_private::Log *log( 1422 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1423 1424 for (GlobalVariable &global_var : m_module->globals()) { 1425 llvm::StringRef global_name = global_var.getName(); 1426 1427 LLDB_LOG(log, "Examining {0}, DeclForGlobalValue returns {1}", global_name, 1428 static_cast<void *>(DeclForGlobal(&global_var))); 1429 1430 if (global_name.startswith("OBJC_IVAR")) { 1431 if (!HandleSymbol(&global_var)) { 1432 m_error_stream.Format("Error [IRForTarget]: Couldn't find Objective-C " 1433 "indirect ivar symbol {0}\n", 1434 global_name); 1435 1436 return false; 1437 } 1438 } else if (global_name.contains("OBJC_CLASSLIST_REFERENCES_$")) { 1439 if (!HandleObjCClass(&global_var)) { 1440 m_error_stream.Printf("Error [IRForTarget]: Couldn't resolve the class " 1441 "for an Objective-C static method call\n"); 1442 1443 return false; 1444 } 1445 } else if (global_name.contains("OBJC_CLASSLIST_SUP_REFS_$")) { 1446 if (!HandleObjCClass(&global_var)) { 1447 m_error_stream.Printf("Error [IRForTarget]: Couldn't resolve the class " 1448 "for an Objective-C static method call\n"); 1449 1450 return false; 1451 } 1452 } else if (DeclForGlobal(&global_var)) { 1453 if (!MaybeHandleVariable(&global_var)) { 1454 m_error_stream.Format("Internal error [IRForTarget]: Couldn't rewrite " 1455 "external variable {0}\n", 1456 global_name); 1457 1458 return false; 1459 } 1460 } 1461 } 1462 1463 return true; 1464 } 1465 1466 static bool isGuardVariableRef(Value *V) { 1467 Constant *Old = dyn_cast<Constant>(V); 1468 1469 if (!Old) 1470 return false; 1471 1472 if (auto CE = dyn_cast<ConstantExpr>(V)) { 1473 if (CE->getOpcode() != Instruction::BitCast) 1474 return false; 1475 1476 Old = CE->getOperand(0); 1477 } 1478 1479 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old); 1480 1481 if (!GV || !GV->hasName() || !isGuardVariableSymbol(GV->getName())) 1482 return false; 1483 1484 return true; 1485 } 1486 1487 void IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction *guard_load) { 1488 Constant *zero(Constant::getNullValue(guard_load->getType())); 1489 guard_load->replaceAllUsesWith(zero); 1490 guard_load->eraseFromParent(); 1491 } 1492 1493 static void ExciseGuardStore(Instruction *guard_store) { 1494 guard_store->eraseFromParent(); 1495 } 1496 1497 bool IRForTarget::RemoveGuards(BasicBlock &basic_block) { 1498 // Eliminate any reference to guard variables found. 1499 1500 InstrList guard_loads; 1501 InstrList guard_stores; 1502 1503 for (Instruction &inst : basic_block) { 1504 1505 if (LoadInst *load = dyn_cast<LoadInst>(&inst)) 1506 if (isGuardVariableRef(load->getPointerOperand())) 1507 guard_loads.push_back(&inst); 1508 1509 if (StoreInst *store = dyn_cast<StoreInst>(&inst)) 1510 if (isGuardVariableRef(store->getPointerOperand())) 1511 guard_stores.push_back(&inst); 1512 } 1513 1514 for (Instruction *inst : guard_loads) 1515 TurnGuardLoadIntoZero(inst); 1516 1517 for (Instruction *inst : guard_stores) 1518 ExciseGuardStore(inst); 1519 1520 return true; 1521 } 1522 1523 // This function does not report errors; its callers are responsible. 1524 bool IRForTarget::UnfoldConstant(Constant *old_constant, 1525 llvm::Function *llvm_function, 1526 FunctionValueCache &value_maker, 1527 FunctionValueCache &entry_instruction_finder, 1528 lldb_private::Stream &error_stream) { 1529 SmallVector<User *, 16> users; 1530 1531 // We do this because the use list might change, invalidating our iterator. 1532 // Much better to keep a work list ourselves. 1533 for (llvm::User *u : old_constant->users()) 1534 users.push_back(u); 1535 1536 for (size_t i = 0; i < users.size(); ++i) { 1537 User *user = users[i]; 1538 1539 if (Constant *constant = dyn_cast<Constant>(user)) { 1540 // synthesize a new non-constant equivalent of the constant 1541 1542 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) { 1543 switch (constant_expr->getOpcode()) { 1544 default: 1545 error_stream.Printf("error [IRForTarget internal]: Unhandled " 1546 "constant expression type: \"%s\"", 1547 PrintValue(constant_expr).c_str()); 1548 return false; 1549 case Instruction::BitCast: { 1550 FunctionValueCache bit_cast_maker( 1551 [&value_maker, &entry_instruction_finder, old_constant, 1552 constant_expr](llvm::Function *function) -> llvm::Value * { 1553 // UnaryExpr 1554 // OperandList[0] is value 1555 1556 if (constant_expr->getOperand(0) != old_constant) 1557 return constant_expr; 1558 1559 return new BitCastInst( 1560 value_maker.GetValue(function), constant_expr->getType(), 1561 "", llvm::cast<Instruction>( 1562 entry_instruction_finder.GetValue(function))); 1563 }); 1564 1565 if (!UnfoldConstant(constant_expr, llvm_function, bit_cast_maker, 1566 entry_instruction_finder, error_stream)) 1567 return false; 1568 } break; 1569 case Instruction::GetElementPtr: { 1570 // GetElementPtrConstantExpr 1571 // OperandList[0] is base 1572 // OperandList[1]... are indices 1573 1574 FunctionValueCache get_element_pointer_maker( 1575 [&value_maker, &entry_instruction_finder, old_constant, 1576 constant_expr](llvm::Function *function) -> llvm::Value * { 1577 Value *ptr = constant_expr->getOperand(0); 1578 1579 if (ptr == old_constant) 1580 ptr = value_maker.GetValue(function); 1581 1582 std::vector<Value *> index_vector; 1583 1584 unsigned operand_index; 1585 unsigned num_operands = constant_expr->getNumOperands(); 1586 1587 for (operand_index = 1; operand_index < num_operands; 1588 ++operand_index) { 1589 Value *operand = constant_expr->getOperand(operand_index); 1590 1591 if (operand == old_constant) 1592 operand = value_maker.GetValue(function); 1593 1594 index_vector.push_back(operand); 1595 } 1596 1597 ArrayRef<Value *> indices(index_vector); 1598 1599 return GetElementPtrInst::Create( 1600 nullptr, ptr, indices, "", 1601 llvm::cast<Instruction>( 1602 entry_instruction_finder.GetValue(function))); 1603 }); 1604 1605 if (!UnfoldConstant(constant_expr, llvm_function, 1606 get_element_pointer_maker, 1607 entry_instruction_finder, error_stream)) 1608 return false; 1609 } break; 1610 } 1611 } else { 1612 error_stream.Printf( 1613 "error [IRForTarget internal]: Unhandled constant type: \"%s\"", 1614 PrintValue(constant).c_str()); 1615 return false; 1616 } 1617 } else { 1618 if (Instruction *inst = llvm::dyn_cast<Instruction>(user)) { 1619 if (llvm_function && inst->getParent()->getParent() != llvm_function) { 1620 error_stream.PutCString("error: Capturing non-local variables in " 1621 "expressions is unsupported.\n"); 1622 return false; 1623 } 1624 inst->replaceUsesOfWith( 1625 old_constant, value_maker.GetValue(inst->getParent()->getParent())); 1626 } else { 1627 error_stream.Printf( 1628 "error [IRForTarget internal]: Unhandled non-constant type: \"%s\"", 1629 PrintValue(user).c_str()); 1630 return false; 1631 } 1632 } 1633 } 1634 1635 if (!isa<GlobalValue>(old_constant)) { 1636 old_constant->destroyConstant(); 1637 } 1638 1639 return true; 1640 } 1641 1642 bool IRForTarget::ReplaceVariables(Function &llvm_function) { 1643 if (!m_resolve_vars) 1644 return true; 1645 1646 lldb_private::Log *log( 1647 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1648 1649 m_decl_map->DoStructLayout(); 1650 1651 LLDB_LOG(log, "Element arrangement:"); 1652 1653 uint32_t num_elements; 1654 uint32_t element_index; 1655 1656 size_t size; 1657 lldb::offset_t alignment; 1658 1659 if (!m_decl_map->GetStructInfo(num_elements, size, alignment)) 1660 return false; 1661 1662 Function::arg_iterator iter(llvm_function.arg_begin()); 1663 1664 if (iter == llvm_function.arg_end()) { 1665 m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes no " 1666 "arguments (should take at least a struct pointer)"); 1667 1668 return false; 1669 } 1670 1671 Argument *argument = &*iter; 1672 1673 if (argument->getName().equals("this")) { 1674 ++iter; 1675 1676 if (iter == llvm_function.arg_end()) { 1677 m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only " 1678 "'this' argument (should take a struct pointer " 1679 "too)"); 1680 1681 return false; 1682 } 1683 1684 argument = &*iter; 1685 } else if (argument->getName().equals("self")) { 1686 ++iter; 1687 1688 if (iter == llvm_function.arg_end()) { 1689 m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only " 1690 "'self' argument (should take '_cmd' and a struct " 1691 "pointer too)"); 1692 1693 return false; 1694 } 1695 1696 if (!iter->getName().equals("_cmd")) { 1697 m_error_stream.Format("Internal error [IRForTarget]: Wrapper takes '{0}' " 1698 "after 'self' argument (should take '_cmd')", 1699 iter->getName()); 1700 1701 return false; 1702 } 1703 1704 ++iter; 1705 1706 if (iter == llvm_function.arg_end()) { 1707 m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only " 1708 "'self' and '_cmd' arguments (should take a struct " 1709 "pointer too)"); 1710 1711 return false; 1712 } 1713 1714 argument = &*iter; 1715 } 1716 1717 if (!argument->getName().equals("$__lldb_arg")) { 1718 m_error_stream.Format("Internal error [IRForTarget]: Wrapper takes an " 1719 "argument named '{0}' instead of the struct pointer", 1720 argument->getName()); 1721 1722 return false; 1723 } 1724 1725 LLDB_LOG(log, "Arg: \"{0}\"", PrintValue(argument)); 1726 1727 BasicBlock &entry_block(llvm_function.getEntryBlock()); 1728 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg()); 1729 1730 if (!FirstEntryInstruction) { 1731 m_error_stream.Printf("Internal error [IRForTarget]: Couldn't find the " 1732 "first instruction in the wrapper for use in " 1733 "rewriting"); 1734 1735 return false; 1736 } 1737 1738 LLVMContext &context(m_module->getContext()); 1739 IntegerType *offset_type(Type::getInt32Ty(context)); 1740 1741 if (!offset_type) { 1742 m_error_stream.Printf( 1743 "Internal error [IRForTarget]: Couldn't produce an offset type"); 1744 1745 return false; 1746 } 1747 1748 for (element_index = 0; element_index < num_elements; ++element_index) { 1749 const clang::NamedDecl *decl = nullptr; 1750 Value *value = nullptr; 1751 lldb::offset_t offset; 1752 lldb_private::ConstString name; 1753 1754 if (!m_decl_map->GetStructElement(decl, value, offset, name, 1755 element_index)) { 1756 m_error_stream.Printf( 1757 "Internal error [IRForTarget]: Structure information is incomplete"); 1758 1759 return false; 1760 } 1761 1762 LLDB_LOG(log, " \"{0}\" (\"{1}\") placed at {2}", name, 1763 decl->getNameAsString(), offset); 1764 1765 if (value) { 1766 LLDB_LOG(log, " Replacing [{0}]", PrintValue(value)); 1767 1768 FunctionValueCache body_result_maker( 1769 [this, name, offset_type, offset, argument, 1770 value](llvm::Function *function) -> llvm::Value * { 1771 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, 1772 // in cases where the result variable is an rvalue, we have to 1773 // synthesize a dereference of the appropriate structure entry in 1774 // order to produce the static variable that the AST thinks it is 1775 // accessing. 1776 1777 llvm::Instruction *entry_instruction = llvm::cast<Instruction>( 1778 m_entry_instruction_finder.GetValue(function)); 1779 1780 ConstantInt *offset_int( 1781 ConstantInt::get(offset_type, offset, true)); 1782 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create( 1783 nullptr, argument, offset_int, "", entry_instruction); 1784 1785 if (name == m_result_name && !m_result_is_pointer) { 1786 BitCastInst *bit_cast = new BitCastInst( 1787 get_element_ptr, value->getType()->getPointerTo(), "", 1788 entry_instruction); 1789 1790 LoadInst *load = 1791 new LoadInst(bit_cast->getType()->getPointerElementType(), 1792 bit_cast, "", entry_instruction); 1793 1794 return load; 1795 } else { 1796 BitCastInst *bit_cast = new BitCastInst( 1797 get_element_ptr, value->getType(), "", entry_instruction); 1798 1799 return bit_cast; 1800 } 1801 }); 1802 1803 if (Constant *constant = dyn_cast<Constant>(value)) { 1804 if (!UnfoldConstant(constant, &llvm_function, body_result_maker, 1805 m_entry_instruction_finder, m_error_stream)) { 1806 return false; 1807 } 1808 } else if (Instruction *instruction = dyn_cast<Instruction>(value)) { 1809 if (instruction->getParent()->getParent() != &llvm_function) { 1810 m_error_stream.PutCString("error: Capturing non-local variables in " 1811 "expressions is unsupported.\n"); 1812 return false; 1813 } 1814 value->replaceAllUsesWith( 1815 body_result_maker.GetValue(instruction->getParent()->getParent())); 1816 } else { 1817 LLDB_LOG(log, "Unhandled non-constant type: \"{0}\"", 1818 PrintValue(value)); 1819 return false; 1820 } 1821 1822 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value)) 1823 var->eraseFromParent(); 1824 } 1825 } 1826 1827 LLDB_LOG(log, "Total structure [align {0}, size {1}]", (int64_t)alignment, 1828 (uint64_t)size); 1829 1830 return true; 1831 } 1832 1833 bool IRForTarget::runOnModule(Module &llvm_module) { 1834 lldb_private::Log *log( 1835 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1836 1837 m_module = &llvm_module; 1838 m_target_data = std::make_unique<DataLayout>(m_module); 1839 m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(), 1840 m_target_data->getPointerSizeInBits()); 1841 1842 if (log) { 1843 std::string s; 1844 raw_string_ostream oss(s); 1845 1846 m_module->print(oss, nullptr); 1847 1848 oss.flush(); 1849 1850 LLDB_LOG(log, "Module as passed in to IRForTarget: \n\"{0}\"", s); 1851 } 1852 1853 Function *const main_function = 1854 m_func_name.IsEmpty() ? nullptr 1855 : m_module->getFunction(m_func_name.GetStringRef()); 1856 1857 if (!m_func_name.IsEmpty() && !main_function) { 1858 LLDB_LOG(log, "Couldn't find \"{0}()\" in the module", m_func_name); 1859 1860 m_error_stream.Format("Internal error [IRForTarget]: Couldn't find wrapper " 1861 "'{0}' in the module", 1862 m_func_name); 1863 1864 return false; 1865 } 1866 1867 if (main_function) { 1868 if (!FixFunctionLinkage(*main_function)) { 1869 LLDB_LOG(log, "Couldn't fix the linkage for the function"); 1870 1871 return false; 1872 } 1873 } 1874 1875 llvm::Type *int8_ty = Type::getInt8Ty(m_module->getContext()); 1876 1877 m_reloc_placeholder = new llvm::GlobalVariable( 1878 (*m_module), int8_ty, false /* IsConstant */, 1879 GlobalVariable::InternalLinkage, Constant::getNullValue(int8_ty), 1880 "reloc_placeholder", nullptr /* InsertBefore */, 1881 GlobalVariable::NotThreadLocal /* ThreadLocal */, 0 /* AddressSpace */); 1882 1883 //////////////////////////////////////////////////////////// 1884 // Replace $__lldb_expr_result with a persistent variable 1885 // 1886 1887 if (main_function) { 1888 if (!CreateResultVariable(*main_function)) { 1889 LLDB_LOG(log, "CreateResultVariable() failed"); 1890 1891 // CreateResultVariable() reports its own errors, so we don't do so here 1892 1893 return false; 1894 } 1895 } 1896 1897 if (log && log->GetVerbose()) { 1898 std::string s; 1899 raw_string_ostream oss(s); 1900 1901 m_module->print(oss, nullptr); 1902 1903 oss.flush(); 1904 1905 LLDB_LOG(log, "Module after creating the result variable: \n\"{0}\"", s); 1906 } 1907 1908 for (llvm::Function &function : *m_module) { 1909 for (BasicBlock &bb : function) { 1910 if (!RemoveGuards(bb)) { 1911 LLDB_LOG(log, "RemoveGuards() failed"); 1912 1913 // RemoveGuards() reports its own errors, so we don't do so here 1914 1915 return false; 1916 } 1917 1918 if (!RewritePersistentAllocs(bb)) { 1919 LLDB_LOG(log, "RewritePersistentAllocs() failed"); 1920 1921 // RewritePersistentAllocs() reports its own errors, so we don't do so 1922 // here 1923 1924 return false; 1925 } 1926 1927 if (!RemoveCXAAtExit(bb)) { 1928 LLDB_LOG(log, "RemoveCXAAtExit() failed"); 1929 1930 // RemoveCXAAtExit() reports its own errors, so we don't do so here 1931 1932 return false; 1933 } 1934 } 1935 } 1936 1937 /////////////////////////////////////////////////////////////////////////////// 1938 // Fix all Objective-C constant strings to use NSStringWithCString:encoding: 1939 // 1940 1941 if (!RewriteObjCConstStrings()) { 1942 LLDB_LOG(log, "RewriteObjCConstStrings() failed"); 1943 1944 // RewriteObjCConstStrings() reports its own errors, so we don't do so here 1945 1946 return false; 1947 } 1948 1949 for (llvm::Function &function : *m_module) { 1950 for (llvm::BasicBlock &bb : function) { 1951 if (!RewriteObjCSelectors(bb)) { 1952 LLDB_LOG(log, "RewriteObjCSelectors() failed"); 1953 1954 // RewriteObjCSelectors() reports its own errors, so we don't do so 1955 // here 1956 1957 return false; 1958 } 1959 1960 if (!RewriteObjCClassReferences(bb)) { 1961 LLDB_LOG(log, "RewriteObjCClassReferences() failed"); 1962 1963 // RewriteObjCClasses() reports its own errors, so we don't do so here 1964 1965 return false; 1966 } 1967 } 1968 } 1969 1970 for (llvm::Function &function : *m_module) { 1971 for (BasicBlock &bb : function) { 1972 if (!ResolveCalls(bb)) { 1973 LLDB_LOG(log, "ResolveCalls() failed"); 1974 1975 // ResolveCalls() reports its own errors, so we don't do so here 1976 1977 return false; 1978 } 1979 } 1980 } 1981 1982 //////////////////////////////////////////////////////////////////////// 1983 // Run function-level passes that only make sense on the main function 1984 // 1985 1986 if (main_function) { 1987 if (!ResolveExternals(*main_function)) { 1988 LLDB_LOG(log, "ResolveExternals() failed"); 1989 1990 // ResolveExternals() reports its own errors, so we don't do so here 1991 1992 return false; 1993 } 1994 1995 if (!ReplaceVariables(*main_function)) { 1996 LLDB_LOG(log, "ReplaceVariables() failed"); 1997 1998 // ReplaceVariables() reports its own errors, so we don't do so here 1999 2000 return false; 2001 } 2002 } 2003 2004 if (log && log->GetVerbose()) { 2005 std::string s; 2006 raw_string_ostream oss(s); 2007 2008 m_module->print(oss, nullptr); 2009 2010 oss.flush(); 2011 2012 LLDB_LOG(log, "Module after preparing for execution: \n\"{0}\"", s); 2013 } 2014 2015 return true; 2016 } 2017