1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the helper classes used to build and interpret debug 11 // information in LLVM IR form. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/IR/DebugInfo.h" 16 #include "LLVMContextImpl.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SmallPtrSet.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/Analysis/ValueTracking.h" 21 #include "llvm/IR/Constants.h" 22 #include "llvm/IR/DIBuilder.h" 23 #include "llvm/IR/DerivedTypes.h" 24 #include "llvm/IR/Instructions.h" 25 #include "llvm/IR/IntrinsicInst.h" 26 #include "llvm/IR/Intrinsics.h" 27 #include "llvm/IR/Module.h" 28 #include "llvm/IR/ValueHandle.h" 29 #include "llvm/Support/Debug.h" 30 #include "llvm/Support/Dwarf.h" 31 #include "llvm/Support/raw_ostream.h" 32 using namespace llvm; 33 using namespace llvm::dwarf; 34 35 //===----------------------------------------------------------------------===// 36 // DIDescriptor 37 //===----------------------------------------------------------------------===// 38 39 bool DIDescriptor::Verify() const { 40 return DbgNode && 41 (DIDerivedType(DbgNode).Verify() || 42 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() || 43 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() || 44 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() || 45 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() || 46 DILexicalBlock(DbgNode).Verify() || 47 DILexicalBlockFile(DbgNode).Verify() || 48 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() || 49 DIObjCProperty(DbgNode).Verify() || 50 DITemplateTypeParameter(DbgNode).Verify() || 51 DITemplateValueParameter(DbgNode).Verify() || 52 DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify()); 53 } 54 55 static Value *getField(const MDNode *DbgNode, unsigned Elt) { 56 if (!DbgNode || Elt >= DbgNode->getNumOperands()) 57 return nullptr; 58 return DbgNode->getOperand(Elt); 59 } 60 61 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) { 62 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt)); 63 } 64 65 static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) { 66 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt))) 67 return MDS->getString(); 68 return StringRef(); 69 } 70 71 StringRef DIDescriptor::getStringField(unsigned Elt) const { 72 return ::getStringField(DbgNode, Elt); 73 } 74 75 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { 76 if (!DbgNode) 77 return 0; 78 79 if (Elt < DbgNode->getNumOperands()) 80 if (ConstantInt *CI = 81 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt))) 82 return CI->getZExtValue(); 83 84 return 0; 85 } 86 87 int64_t DIDescriptor::getInt64Field(unsigned Elt) const { 88 if (!DbgNode) 89 return 0; 90 91 if (Elt < DbgNode->getNumOperands()) 92 if (ConstantInt *CI = 93 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt))) 94 return CI->getSExtValue(); 95 96 return 0; 97 } 98 99 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { 100 MDNode *Field = getNodeField(DbgNode, Elt); 101 return DIDescriptor(Field); 102 } 103 104 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { 105 if (!DbgNode) 106 return nullptr; 107 108 if (Elt < DbgNode->getNumOperands()) 109 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt)); 110 return nullptr; 111 } 112 113 Constant *DIDescriptor::getConstantField(unsigned Elt) const { 114 if (!DbgNode) 115 return nullptr; 116 117 if (Elt < DbgNode->getNumOperands()) 118 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt)); 119 return nullptr; 120 } 121 122 Function *DIDescriptor::getFunctionField(unsigned Elt) const { 123 if (!DbgNode) 124 return nullptr; 125 126 if (Elt < DbgNode->getNumOperands()) 127 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt)); 128 return nullptr; 129 } 130 131 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) { 132 if (!DbgNode) 133 return; 134 135 if (Elt < DbgNode->getNumOperands()) { 136 MDNode *Node = const_cast<MDNode *>(DbgNode); 137 Node->replaceOperandWith(Elt, F); 138 } 139 } 140 141 static unsigned DIVariableInlinedAtIndex = 4; 142 MDNode *DIVariable::getInlinedAt() const { 143 return getNodeField(DbgNode, DIVariableInlinedAtIndex); 144 } 145 146 /// Return the size reported by the variable's type. 147 unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) { 148 DIType Ty = getType().resolve(Map); 149 // Follow derived types until we reach a type that 150 // reports back a size. 151 while (Ty.isDerivedType() && !Ty.getSizeInBits()) { 152 DIDerivedType DT(&*Ty); 153 Ty = DT.getTypeDerivedFrom().resolve(Map); 154 } 155 assert(Ty.getSizeInBits() && "type with size 0"); 156 return Ty.getSizeInBits(); 157 } 158 159 uint64_t DIExpression::getElement(unsigned Idx) const { 160 unsigned I = Idx + 1; 161 assert(I < getNumHeaderFields() && 162 "non-existing complex address element requested"); 163 return getHeaderFieldAs<int64_t>(I); 164 } 165 166 bool DIExpression::isVariablePiece() const { 167 return getNumElements() && getElement(0) == dwarf::DW_OP_piece; 168 } 169 170 uint64_t DIExpression::getPieceOffset() const { 171 assert(isVariablePiece()); 172 return getElement(1); 173 } 174 175 uint64_t DIExpression::getPieceSize() const { 176 assert(isVariablePiece()); 177 return getElement(2); 178 } 179 180 //===----------------------------------------------------------------------===// 181 // Predicates 182 //===----------------------------------------------------------------------===// 183 184 bool DIDescriptor::isSubroutineType() const { 185 return isCompositeType() && getTag() == dwarf::DW_TAG_subroutine_type; 186 } 187 188 /// isBasicType - Return true if the specified tag is legal for 189 /// DIBasicType. 190 bool DIDescriptor::isBasicType() const { 191 if (!DbgNode) 192 return false; 193 switch (getTag()) { 194 case dwarf::DW_TAG_base_type: 195 case dwarf::DW_TAG_unspecified_type: 196 return true; 197 default: 198 return false; 199 } 200 } 201 202 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType. 203 bool DIDescriptor::isDerivedType() const { 204 if (!DbgNode) 205 return false; 206 switch (getTag()) { 207 case dwarf::DW_TAG_typedef: 208 case dwarf::DW_TAG_pointer_type: 209 case dwarf::DW_TAG_ptr_to_member_type: 210 case dwarf::DW_TAG_reference_type: 211 case dwarf::DW_TAG_rvalue_reference_type: 212 case dwarf::DW_TAG_const_type: 213 case dwarf::DW_TAG_volatile_type: 214 case dwarf::DW_TAG_restrict_type: 215 case dwarf::DW_TAG_member: 216 case dwarf::DW_TAG_inheritance: 217 case dwarf::DW_TAG_friend: 218 return true; 219 default: 220 // CompositeTypes are currently modelled as DerivedTypes. 221 return isCompositeType(); 222 } 223 } 224 225 /// isCompositeType - Return true if the specified tag is legal for 226 /// DICompositeType. 227 bool DIDescriptor::isCompositeType() const { 228 if (!DbgNode) 229 return false; 230 switch (getTag()) { 231 case dwarf::DW_TAG_array_type: 232 case dwarf::DW_TAG_structure_type: 233 case dwarf::DW_TAG_union_type: 234 case dwarf::DW_TAG_enumeration_type: 235 case dwarf::DW_TAG_subroutine_type: 236 case dwarf::DW_TAG_class_type: 237 return true; 238 default: 239 return false; 240 } 241 } 242 243 /// isVariable - Return true if the specified tag is legal for DIVariable. 244 bool DIDescriptor::isVariable() const { 245 if (!DbgNode) 246 return false; 247 switch (getTag()) { 248 case dwarf::DW_TAG_auto_variable: 249 case dwarf::DW_TAG_arg_variable: 250 return true; 251 default: 252 return false; 253 } 254 } 255 256 /// isType - Return true if the specified tag is legal for DIType. 257 bool DIDescriptor::isType() const { 258 return isBasicType() || isCompositeType() || isDerivedType(); 259 } 260 261 /// isSubprogram - Return true if the specified tag is legal for 262 /// DISubprogram. 263 bool DIDescriptor::isSubprogram() const { 264 return DbgNode && getTag() == dwarf::DW_TAG_subprogram; 265 } 266 267 /// isGlobalVariable - Return true if the specified tag is legal for 268 /// DIGlobalVariable. 269 bool DIDescriptor::isGlobalVariable() const { 270 return DbgNode && (getTag() == dwarf::DW_TAG_variable || 271 getTag() == dwarf::DW_TAG_constant); 272 } 273 274 /// isScope - Return true if the specified tag is one of the scope 275 /// related tag. 276 bool DIDescriptor::isScope() const { 277 if (!DbgNode) 278 return false; 279 switch (getTag()) { 280 case dwarf::DW_TAG_compile_unit: 281 case dwarf::DW_TAG_lexical_block: 282 case dwarf::DW_TAG_subprogram: 283 case dwarf::DW_TAG_namespace: 284 case dwarf::DW_TAG_file_type: 285 return true; 286 default: 287 break; 288 } 289 return isType(); 290 } 291 292 /// isTemplateTypeParameter - Return true if the specified tag is 293 /// DW_TAG_template_type_parameter. 294 bool DIDescriptor::isTemplateTypeParameter() const { 295 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter; 296 } 297 298 /// isTemplateValueParameter - Return true if the specified tag is 299 /// DW_TAG_template_value_parameter. 300 bool DIDescriptor::isTemplateValueParameter() const { 301 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter || 302 getTag() == dwarf::DW_TAG_GNU_template_template_param || 303 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack); 304 } 305 306 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit. 307 bool DIDescriptor::isCompileUnit() const { 308 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit; 309 } 310 311 /// isFile - Return true if the specified tag is DW_TAG_file_type. 312 bool DIDescriptor::isFile() const { 313 return DbgNode && getTag() == dwarf::DW_TAG_file_type; 314 } 315 316 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace. 317 bool DIDescriptor::isNameSpace() const { 318 return DbgNode && getTag() == dwarf::DW_TAG_namespace; 319 } 320 321 /// isLexicalBlockFile - Return true if the specified descriptor is a 322 /// lexical block with an extra file. 323 bool DIDescriptor::isLexicalBlockFile() const { 324 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && 325 DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 2; 326 } 327 328 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block. 329 bool DIDescriptor::isLexicalBlock() const { 330 // FIXME: There are always exactly 4 header fields in DILexicalBlock, but 331 // something relies on this returning true for DILexicalBlockFile. 332 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && 333 DbgNode->getNumOperands() == 3 && 334 (getNumHeaderFields() == 2 || getNumHeaderFields() == 4); 335 } 336 337 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type. 338 bool DIDescriptor::isSubrange() const { 339 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type; 340 } 341 342 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator. 343 bool DIDescriptor::isEnumerator() const { 344 return DbgNode && getTag() == dwarf::DW_TAG_enumerator; 345 } 346 347 /// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property. 348 bool DIDescriptor::isObjCProperty() const { 349 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property; 350 } 351 352 /// \brief Return true if the specified tag is DW_TAG_imported_module or 353 /// DW_TAG_imported_declaration. 354 bool DIDescriptor::isImportedEntity() const { 355 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module || 356 getTag() == dwarf::DW_TAG_imported_declaration); 357 } 358 359 /// \brief Return true if the specified tag is DW_TAG_imported_module or 360 /// DW_TAG_imported_declaration. 361 bool DIDescriptor::isExpression() const { 362 return DbgNode && (getTag() == dwarf::DW_TAG_expression); 363 } 364 365 //===----------------------------------------------------------------------===// 366 // Simple Descriptor Constructors and other Methods 367 //===----------------------------------------------------------------------===// 368 369 /// replaceAllUsesWith - Replace all uses of the MDNode used by this 370 /// type with the one in the passed descriptor. 371 void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) { 372 373 assert(DbgNode && "Trying to replace an unverified type!"); 374 375 // Since we use a TrackingVH for the node, its easy for clients to manufacture 376 // legitimate situations where they want to replaceAllUsesWith() on something 377 // which, due to uniquing, has merged with the source. We shield clients from 378 // this detail by allowing a value to be replaced with replaceAllUsesWith() 379 // itself. 380 const MDNode *DN = D; 381 if (DbgNode == DN) { 382 SmallVector<Value*, 10> Ops(DbgNode->getNumOperands()); 383 for (size_t i = 0; i != Ops.size(); ++i) 384 Ops[i] = DbgNode->getOperand(i); 385 DN = MDNode::get(VMContext, Ops); 386 } 387 388 MDNode *Node = const_cast<MDNode *>(DbgNode); 389 const Value *V = cast_or_null<Value>(DN); 390 Node->replaceAllUsesWith(const_cast<Value *>(V)); 391 MDNode::deleteTemporary(Node); 392 DbgNode = DN; 393 } 394 395 /// replaceAllUsesWith - Replace all uses of the MDNode used by this 396 /// type with the one in D. 397 void DIDescriptor::replaceAllUsesWith(MDNode *D) { 398 399 assert(DbgNode && "Trying to replace an unverified type!"); 400 assert(DbgNode != D && "This replacement should always happen"); 401 MDNode *Node = const_cast<MDNode *>(DbgNode); 402 const MDNode *DN = D; 403 const Value *V = cast_or_null<Value>(DN); 404 Node->replaceAllUsesWith(const_cast<Value *>(V)); 405 MDNode::deleteTemporary(Node); 406 } 407 408 /// Verify - Verify that a compile unit is well formed. 409 bool DICompileUnit::Verify() const { 410 if (!isCompileUnit()) 411 return false; 412 413 // Don't bother verifying the compilation directory or producer string 414 // as those could be empty. 415 if (getFilename().empty()) 416 return false; 417 418 return DbgNode->getNumOperands() == 7 && getNumHeaderFields() == 8; 419 } 420 421 /// Verify - Verify that an ObjC property is well formed. 422 bool DIObjCProperty::Verify() const { 423 if (!isObjCProperty()) 424 return false; 425 426 // Don't worry about the rest of the strings for now. 427 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 6; 428 } 429 430 /// Check if a field at position Elt of a MDNode is a MDNode. 431 /// We currently allow an empty string and an integer. 432 /// But we don't allow a non-empty string in a MDNode field. 433 static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) { 434 // FIXME: This function should return true, if the field is null or the field 435 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld). 436 Value *Fld = getField(DbgNode, Elt); 437 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty()) 438 return false; 439 return true; 440 } 441 442 /// Check if a field at position Elt of a MDNode is a MDString. 443 static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) { 444 Value *Fld = getField(DbgNode, Elt); 445 return !Fld || isa<MDString>(Fld); 446 } 447 448 /// Check if a value can be a reference to a type. 449 static bool isTypeRef(const Value *Val) { 450 return !Val || 451 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) || 452 (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType()); 453 } 454 455 /// Check if a field at position Elt of a MDNode can be a reference to a type. 456 static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) { 457 Value *Fld = getField(DbgNode, Elt); 458 return isTypeRef(Fld); 459 } 460 461 /// Check if a value can be a ScopeRef. 462 static bool isScopeRef(const Value *Val) { 463 return !Val || 464 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) || 465 // Not checking for Val->isScope() here, because it would work 466 // only for lexical scopes and not all subclasses of DIScope. 467 isa<MDNode>(Val); 468 } 469 470 /// Check if a field at position Elt of a MDNode can be a ScopeRef. 471 static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) { 472 Value *Fld = getField(DbgNode, Elt); 473 return isScopeRef(Fld); 474 } 475 476 /// Verify - Verify that a type descriptor is well formed. 477 bool DIType::Verify() const { 478 if (!isType()) 479 return false; 480 // Make sure Context @ field 2 is MDNode. 481 if (!fieldIsScopeRef(DbgNode, 2)) 482 return false; 483 484 // FIXME: Sink this into the various subclass verifies. 485 uint16_t Tag = getTag(); 486 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type && 487 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type && 488 Tag != dwarf::DW_TAG_ptr_to_member_type && 489 Tag != dwarf::DW_TAG_reference_type && 490 Tag != dwarf::DW_TAG_rvalue_reference_type && 491 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type && 492 Tag != dwarf::DW_TAG_enumeration_type && 493 Tag != dwarf::DW_TAG_subroutine_type && 494 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend && 495 getFilename().empty()) 496 return false; 497 498 // DIType is abstract, it should be a BasicType, a DerivedType or 499 // a CompositeType. 500 if (isBasicType()) 501 return DIBasicType(DbgNode).Verify(); 502 else if (isCompositeType()) 503 return DICompositeType(DbgNode).Verify(); 504 else if (isDerivedType()) 505 return DIDerivedType(DbgNode).Verify(); 506 else 507 return false; 508 } 509 510 /// Verify - Verify that a basic type descriptor is well formed. 511 bool DIBasicType::Verify() const { 512 return isBasicType() && DbgNode->getNumOperands() == 3 && 513 getNumHeaderFields() == 8; 514 } 515 516 /// Verify - Verify that a derived type descriptor is well formed. 517 bool DIDerivedType::Verify() const { 518 // Make sure DerivedFrom @ field 3 is TypeRef. 519 if (!fieldIsTypeRef(DbgNode, 3)) 520 return false; 521 if (getTag() == dwarf::DW_TAG_ptr_to_member_type) 522 // Make sure ClassType @ field 4 is a TypeRef. 523 if (!fieldIsTypeRef(DbgNode, 4)) 524 return false; 525 526 return isDerivedType() && DbgNode->getNumOperands() >= 4 && 527 DbgNode->getNumOperands() <= 8 && getNumHeaderFields() >= 7 && 528 getNumHeaderFields() <= 8; 529 } 530 531 /// Verify - Verify that a composite type descriptor is well formed. 532 bool DICompositeType::Verify() const { 533 if (!isCompositeType()) 534 return false; 535 536 // Make sure DerivedFrom @ field 3 and ContainingType @ field 5 are TypeRef. 537 if (!fieldIsTypeRef(DbgNode, 3)) 538 return false; 539 if (!fieldIsTypeRef(DbgNode, 5)) 540 return false; 541 542 // Make sure the type identifier at field 7 is MDString, it can be null. 543 if (!fieldIsMDString(DbgNode, 7)) 544 return false; 545 546 // A subroutine type can't be both & and &&. 547 if (isLValueReference() && isRValueReference()) 548 return false; 549 550 return DbgNode->getNumOperands() == 8 && getNumHeaderFields() == 8; 551 } 552 553 /// Verify - Verify that a subprogram descriptor is well formed. 554 bool DISubprogram::Verify() const { 555 if (!isSubprogram()) 556 return false; 557 558 // Make sure context @ field 2 is a ScopeRef and type @ field 3 is a MDNode. 559 if (!fieldIsScopeRef(DbgNode, 2)) 560 return false; 561 if (!fieldIsMDNode(DbgNode, 3)) 562 return false; 563 // Containing type @ field 4. 564 if (!fieldIsTypeRef(DbgNode, 4)) 565 return false; 566 567 // A subprogram can't be both & and &&. 568 if (isLValueReference() && isRValueReference()) 569 return false; 570 571 // If a DISubprogram has an llvm::Function*, then scope chains from all 572 // instructions within the function should lead to this DISubprogram. 573 if (auto *F = getFunction()) { 574 LLVMContext &Ctxt = F->getContext(); 575 for (auto &BB : *F) { 576 for (auto &I : BB) { 577 DebugLoc DL = I.getDebugLoc(); 578 if (DL.isUnknown()) 579 continue; 580 581 MDNode *Scope = nullptr; 582 MDNode *IA = nullptr; 583 // walk the inlined-at scopes 584 while (DL.getScopeAndInlinedAt(Scope, IA, F->getContext()), IA) 585 DL = DebugLoc::getFromDILocation(IA); 586 DL.getScopeAndInlinedAt(Scope, IA, Ctxt); 587 assert(!IA); 588 while (!DIDescriptor(Scope).isSubprogram()) { 589 DILexicalBlockFile D(Scope); 590 Scope = D.isLexicalBlockFile() 591 ? D.getScope() 592 : DebugLoc::getFromDILexicalBlock(Scope).getScope(Ctxt); 593 } 594 if (!DISubprogram(Scope).describes(F)) 595 return false; 596 } 597 } 598 } 599 return DbgNode->getNumOperands() == 9 && getNumHeaderFields() == 12; 600 } 601 602 /// Verify - Verify that a global variable descriptor is well formed. 603 bool DIGlobalVariable::Verify() const { 604 if (!isGlobalVariable()) 605 return false; 606 607 if (getDisplayName().empty()) 608 return false; 609 // Make sure context @ field 1 is an MDNode. 610 if (!fieldIsMDNode(DbgNode, 1)) 611 return false; 612 // Make sure that type @ field 3 is a DITypeRef. 613 if (!fieldIsTypeRef(DbgNode, 3)) 614 return false; 615 // Make sure StaticDataMemberDeclaration @ field 5 is MDNode. 616 if (!fieldIsMDNode(DbgNode, 5)) 617 return false; 618 619 return DbgNode->getNumOperands() == 6 && getNumHeaderFields() == 7; 620 } 621 622 /// Verify - Verify that a variable descriptor is well formed. 623 bool DIVariable::Verify() const { 624 if (!isVariable()) 625 return false; 626 627 // Make sure context @ field 1 is an MDNode. 628 if (!fieldIsMDNode(DbgNode, 1)) 629 return false; 630 // Make sure that type @ field 3 is a DITypeRef. 631 if (!fieldIsTypeRef(DbgNode, 3)) 632 return false; 633 634 // Check the number of header fields, which is common between complex and 635 // simple variables. 636 if (getNumHeaderFields() != 4) 637 return false; 638 639 // Variable without an inline location. 640 if (DbgNode->getNumOperands() == 4) 641 return true; 642 643 // Variable with an inline location. 644 return getInlinedAt() != nullptr && DbgNode->getNumOperands() == 5; 645 } 646 647 /// Verify - Verify that a variable descriptor is well formed. 648 bool DIExpression::Verify() const { 649 // Empty DIExpressions may be represented as a nullptr. 650 if (!DbgNode) 651 return true; 652 653 return isExpression() && DbgNode->getNumOperands() == 1; 654 } 655 656 /// Verify - Verify that a location descriptor is well formed. 657 bool DILocation::Verify() const { 658 if (!DbgNode) 659 return false; 660 661 return DbgNode->getNumOperands() == 4; 662 } 663 664 /// Verify - Verify that a namespace descriptor is well formed. 665 bool DINameSpace::Verify() const { 666 if (!isNameSpace()) 667 return false; 668 return DbgNode->getNumOperands() == 3 && getNumHeaderFields() == 3; 669 } 670 671 /// \brief Retrieve the MDNode for the directory/file pair. 672 MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); } 673 674 /// \brief Verify that the file descriptor is well formed. 675 bool DIFile::Verify() const { 676 return isFile() && DbgNode->getNumOperands() == 2; 677 } 678 679 /// \brief Verify that the enumerator descriptor is well formed. 680 bool DIEnumerator::Verify() const { 681 return isEnumerator() && DbgNode->getNumOperands() == 1 && 682 getNumHeaderFields() == 3; 683 } 684 685 /// \brief Verify that the subrange descriptor is well formed. 686 bool DISubrange::Verify() const { 687 return isSubrange() && DbgNode->getNumOperands() == 1 && 688 getNumHeaderFields() == 3; 689 } 690 691 /// \brief Verify that the lexical block descriptor is well formed. 692 bool DILexicalBlock::Verify() const { 693 return isLexicalBlock() && DbgNode->getNumOperands() == 3 && 694 getNumHeaderFields() == 4; 695 } 696 697 /// \brief Verify that the file-scoped lexical block descriptor is well formed. 698 bool DILexicalBlockFile::Verify() const { 699 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3 && 700 getNumHeaderFields() == 2; 701 } 702 703 /// \brief Verify that the template type parameter descriptor is well formed. 704 bool DITemplateTypeParameter::Verify() const { 705 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 4 && 706 getNumHeaderFields() == 4; 707 } 708 709 /// \brief Verify that the template value parameter descriptor is well formed. 710 bool DITemplateValueParameter::Verify() const { 711 return isTemplateValueParameter() && DbgNode->getNumOperands() == 5 && 712 getNumHeaderFields() == 4; 713 } 714 715 /// \brief Verify that the imported module descriptor is well formed. 716 bool DIImportedEntity::Verify() const { 717 return isImportedEntity() && DbgNode->getNumOperands() == 3 && 718 getNumHeaderFields() == 3; 719 } 720 721 /// getObjCProperty - Return property node, if this ivar is associated with one. 722 MDNode *DIDerivedType::getObjCProperty() const { 723 return getNodeField(DbgNode, 4); 724 } 725 726 MDString *DICompositeType::getIdentifier() const { 727 return cast_or_null<MDString>(getField(DbgNode, 7)); 728 } 729 730 #ifndef NDEBUG 731 static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) { 732 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) { 733 // Skip the 'empty' list (that's a single i32 0, rather than truly empty). 734 if (i == 0 && isa<ConstantInt>(LHS->getOperand(i))) 735 continue; 736 const MDNode *E = cast<MDNode>(LHS->getOperand(i)); 737 bool found = false; 738 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j) 739 found = E == RHS->getOperand(j); 740 assert(found && "Losing a member during member list replacement"); 741 } 742 } 743 #endif 744 745 /// \brief Set the array of member DITypes. 746 void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) { 747 TrackingVH<MDNode> N(*this); 748 if (Elements) { 749 #ifndef NDEBUG 750 // Check that the new list of members contains all the old members as well. 751 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(4))) 752 VerifySubsetOf(El, Elements); 753 #endif 754 N->replaceOperandWith(4, Elements); 755 } 756 if (TParams) 757 N->replaceOperandWith(6, TParams); 758 DbgNode = N; 759 } 760 761 /// Generate a reference to this DIType. Uses the type identifier instead 762 /// of the actual MDNode if possible, to help type uniquing. 763 DIScopeRef DIScope::getRef() const { 764 if (!isCompositeType()) 765 return DIScopeRef(*this); 766 DICompositeType DTy(DbgNode); 767 if (!DTy.getIdentifier()) 768 return DIScopeRef(*this); 769 return DIScopeRef(DTy.getIdentifier()); 770 } 771 772 /// \brief Set the containing type. 773 void DICompositeType::setContainingType(DICompositeType ContainingType) { 774 TrackingVH<MDNode> N(*this); 775 N->replaceOperandWith(5, ContainingType.getRef()); 776 DbgNode = N; 777 } 778 779 /// isInlinedFnArgument - Return true if this variable provides debugging 780 /// information for an inlined function arguments. 781 bool DIVariable::isInlinedFnArgument(const Function *CurFn) { 782 assert(CurFn && "Invalid function"); 783 if (!getContext().isSubprogram()) 784 return false; 785 // This variable is not inlined function argument if its scope 786 // does not describe current function. 787 return !DISubprogram(getContext()).describes(CurFn); 788 } 789 790 /// describes - Return true if this subprogram provides debugging 791 /// information for the function F. 792 bool DISubprogram::describes(const Function *F) { 793 assert(F && "Invalid function"); 794 if (F == getFunction()) 795 return true; 796 StringRef Name = getLinkageName(); 797 if (Name.empty()) 798 Name = getName(); 799 if (F->getName() == Name) 800 return true; 801 return false; 802 } 803 804 MDNode *DISubprogram::getVariablesNodes() const { 805 return getNodeField(DbgNode, 8); 806 } 807 808 DIArray DISubprogram::getVariables() const { 809 return DIArray(getNodeField(DbgNode, 8)); 810 } 811 812 Value *DITemplateValueParameter::getValue() const { 813 return getField(DbgNode, 3); 814 } 815 816 // If the current node has a parent scope then return that, 817 // else return an empty scope. 818 DIScopeRef DIScope::getContext() const { 819 820 if (isType()) 821 return DIType(DbgNode).getContext(); 822 823 if (isSubprogram()) 824 return DIScopeRef(DISubprogram(DbgNode).getContext()); 825 826 if (isLexicalBlock()) 827 return DIScopeRef(DILexicalBlock(DbgNode).getContext()); 828 829 if (isLexicalBlockFile()) 830 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext()); 831 832 if (isNameSpace()) 833 return DIScopeRef(DINameSpace(DbgNode).getContext()); 834 835 assert((isFile() || isCompileUnit()) && "Unhandled type of scope."); 836 return DIScopeRef(nullptr); 837 } 838 839 // If the scope node has a name, return that, else return an empty string. 840 StringRef DIScope::getName() const { 841 if (isType()) 842 return DIType(DbgNode).getName(); 843 if (isSubprogram()) 844 return DISubprogram(DbgNode).getName(); 845 if (isNameSpace()) 846 return DINameSpace(DbgNode).getName(); 847 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() || 848 isCompileUnit()) && 849 "Unhandled type of scope."); 850 return StringRef(); 851 } 852 853 StringRef DIScope::getFilename() const { 854 if (!DbgNode) 855 return StringRef(); 856 return ::getStringField(getNodeField(DbgNode, 1), 0); 857 } 858 859 StringRef DIScope::getDirectory() const { 860 if (!DbgNode) 861 return StringRef(); 862 return ::getStringField(getNodeField(DbgNode, 1), 1); 863 } 864 865 DIArray DICompileUnit::getEnumTypes() const { 866 if (!DbgNode || DbgNode->getNumOperands() < 7) 867 return DIArray(); 868 869 return DIArray(getNodeField(DbgNode, 2)); 870 } 871 872 DIArray DICompileUnit::getRetainedTypes() const { 873 if (!DbgNode || DbgNode->getNumOperands() < 7) 874 return DIArray(); 875 876 return DIArray(getNodeField(DbgNode, 3)); 877 } 878 879 DIArray DICompileUnit::getSubprograms() const { 880 if (!DbgNode || DbgNode->getNumOperands() < 7) 881 return DIArray(); 882 883 return DIArray(getNodeField(DbgNode, 4)); 884 } 885 886 DIArray DICompileUnit::getGlobalVariables() const { 887 if (!DbgNode || DbgNode->getNumOperands() < 7) 888 return DIArray(); 889 890 return DIArray(getNodeField(DbgNode, 5)); 891 } 892 893 DIArray DICompileUnit::getImportedEntities() const { 894 if (!DbgNode || DbgNode->getNumOperands() < 7) 895 return DIArray(); 896 897 return DIArray(getNodeField(DbgNode, 6)); 898 } 899 900 void DICompileUnit::replaceSubprograms(DIArray Subprograms) { 901 assert(Verify() && "Expected compile unit"); 902 if (Subprograms == getSubprograms()) 903 return; 904 905 const_cast<MDNode *>(DbgNode)->replaceOperandWith(4, Subprograms); 906 } 907 908 void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) { 909 assert(Verify() && "Expected compile unit"); 910 if (GlobalVariables == getGlobalVariables()) 911 return; 912 913 const_cast<MDNode *>(DbgNode)->replaceOperandWith(5, GlobalVariables); 914 } 915 916 /// copyWithNewScope - Return a copy of this location, replacing the 917 /// current scope with the given one. 918 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, 919 DILexicalBlockFile NewScope) { 920 SmallVector<Value *, 10> Elts; 921 assert(Verify()); 922 for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) { 923 if (I != 2) 924 Elts.push_back(DbgNode->getOperand(I)); 925 else 926 Elts.push_back(NewScope); 927 } 928 MDNode *NewDIL = MDNode::get(Ctx, Elts); 929 return DILocation(NewDIL); 930 } 931 932 /// computeNewDiscriminator - Generate a new discriminator value for this 933 /// file and line location. 934 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) { 935 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber()); 936 return ++Ctx.pImpl->DiscriminatorTable[Key]; 937 } 938 939 /// fixupSubprogramName - Replace contains special characters used 940 /// in a typical Objective-C names with '.' in a given string. 941 static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) { 942 StringRef FName = 943 Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName(); 944 FName = Function::getRealLinkageName(FName); 945 946 StringRef Prefix("llvm.dbg.lv."); 947 Out.reserve(FName.size() + Prefix.size()); 948 Out.append(Prefix.begin(), Prefix.end()); 949 950 bool isObjCLike = false; 951 for (size_t i = 0, e = FName.size(); i < e; ++i) { 952 char C = FName[i]; 953 if (C == '[') 954 isObjCLike = true; 955 956 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' || 957 C == '+' || C == '(' || C == ')')) 958 Out.push_back('.'); 959 else 960 Out.push_back(C); 961 } 962 } 963 964 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is 965 /// suitable to hold function specific information. 966 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) { 967 SmallString<32> Name; 968 fixupSubprogramName(Fn, Name); 969 return M.getNamedMetadata(Name.str()); 970 } 971 972 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable 973 /// to hold function specific information. 974 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) { 975 SmallString<32> Name; 976 fixupSubprogramName(Fn, Name); 977 return M.getOrInsertNamedMetadata(Name.str()); 978 } 979 980 /// createInlinedVariable - Create a new inlined variable based on current 981 /// variable. 982 /// @param DV Current Variable. 983 /// @param InlinedScope Location at current variable is inlined. 984 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, 985 LLVMContext &VMContext) { 986 assert(DIVariable(DV).Verify() && "Expected a DIVariable"); 987 if (!InlinedScope) 988 return cleanseInlinedVariable(DV, VMContext); 989 990 // Insert inlined scope. 991 SmallVector<Value *, 8> Elts; 992 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I) 993 Elts.push_back(DV->getOperand(I)); 994 Elts.push_back(InlinedScope); 995 996 DIVariable Inlined(MDNode::get(VMContext, Elts)); 997 assert(Inlined.Verify() && "Expected to create a DIVariable"); 998 return Inlined; 999 } 1000 1001 /// cleanseInlinedVariable - Remove inlined scope from the variable. 1002 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { 1003 assert(DIVariable(DV).Verify() && "Expected a DIVariable"); 1004 if (!DIVariable(DV).getInlinedAt()) 1005 return DIVariable(DV); 1006 1007 // Remove inlined scope. 1008 SmallVector<Value *, 8> Elts; 1009 for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I) 1010 Elts.push_back(DV->getOperand(I)); 1011 1012 DIVariable Cleansed(MDNode::get(VMContext, Elts)); 1013 assert(Cleansed.Verify() && "Expected to create a DIVariable"); 1014 return Cleansed; 1015 } 1016 1017 /// getDISubprogram - Find subprogram that is enclosing this scope. 1018 DISubprogram llvm::getDISubprogram(const MDNode *Scope) { 1019 DIDescriptor D(Scope); 1020 if (D.isSubprogram()) 1021 return DISubprogram(Scope); 1022 1023 if (D.isLexicalBlockFile()) 1024 return getDISubprogram(DILexicalBlockFile(Scope).getContext()); 1025 1026 if (D.isLexicalBlock()) 1027 return getDISubprogram(DILexicalBlock(Scope).getContext()); 1028 1029 return DISubprogram(); 1030 } 1031 1032 /// getDICompositeType - Find underlying composite type. 1033 DICompositeType llvm::getDICompositeType(DIType T) { 1034 if (T.isCompositeType()) 1035 return DICompositeType(T); 1036 1037 if (T.isDerivedType()) { 1038 // This function is currently used by dragonegg and dragonegg does 1039 // not generate identifier for types, so using an empty map to resolve 1040 // DerivedFrom should be fine. 1041 DITypeIdentifierMap EmptyMap; 1042 return getDICompositeType( 1043 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap)); 1044 } 1045 1046 return DICompositeType(); 1047 } 1048 1049 /// Update DITypeIdentifierMap by going through retained types of each CU. 1050 DITypeIdentifierMap 1051 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { 1052 DITypeIdentifierMap Map; 1053 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) { 1054 DICompileUnit CU(CU_Nodes->getOperand(CUi)); 1055 DIArray Retain = CU.getRetainedTypes(); 1056 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) { 1057 if (!Retain.getElement(Ti).isCompositeType()) 1058 continue; 1059 DICompositeType Ty(Retain.getElement(Ti)); 1060 if (MDString *TypeId = Ty.getIdentifier()) { 1061 // Definition has priority over declaration. 1062 // Try to insert (TypeId, Ty) to Map. 1063 std::pair<DITypeIdentifierMap::iterator, bool> P = 1064 Map.insert(std::make_pair(TypeId, Ty)); 1065 // If TypeId already exists in Map and this is a definition, replace 1066 // whatever we had (declaration or definition) with the definition. 1067 if (!P.second && !Ty.isForwardDecl()) 1068 P.first->second = Ty; 1069 } 1070 } 1071 } 1072 return Map; 1073 } 1074 1075 //===----------------------------------------------------------------------===// 1076 // DebugInfoFinder implementations. 1077 //===----------------------------------------------------------------------===// 1078 1079 void DebugInfoFinder::reset() { 1080 CUs.clear(); 1081 SPs.clear(); 1082 GVs.clear(); 1083 TYs.clear(); 1084 Scopes.clear(); 1085 NodesSeen.clear(); 1086 TypeIdentifierMap.clear(); 1087 TypeMapInitialized = false; 1088 } 1089 1090 void DebugInfoFinder::InitializeTypeMap(const Module &M) { 1091 if (!TypeMapInitialized) 1092 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { 1093 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); 1094 TypeMapInitialized = true; 1095 } 1096 } 1097 1098 /// processModule - Process entire module and collect debug info. 1099 void DebugInfoFinder::processModule(const Module &M) { 1100 InitializeTypeMap(M); 1101 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { 1102 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { 1103 DICompileUnit CU(CU_Nodes->getOperand(i)); 1104 addCompileUnit(CU); 1105 DIArray GVs = CU.getGlobalVariables(); 1106 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) { 1107 DIGlobalVariable DIG(GVs.getElement(i)); 1108 if (addGlobalVariable(DIG)) { 1109 processScope(DIG.getContext()); 1110 processType(DIG.getType().resolve(TypeIdentifierMap)); 1111 } 1112 } 1113 DIArray SPs = CU.getSubprograms(); 1114 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) 1115 processSubprogram(DISubprogram(SPs.getElement(i))); 1116 DIArray EnumTypes = CU.getEnumTypes(); 1117 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i) 1118 processType(DIType(EnumTypes.getElement(i))); 1119 DIArray RetainedTypes = CU.getRetainedTypes(); 1120 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i) 1121 processType(DIType(RetainedTypes.getElement(i))); 1122 DIArray Imports = CU.getImportedEntities(); 1123 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) { 1124 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i)); 1125 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap); 1126 if (Entity.isType()) 1127 processType(DIType(Entity)); 1128 else if (Entity.isSubprogram()) 1129 processSubprogram(DISubprogram(Entity)); 1130 else if (Entity.isNameSpace()) 1131 processScope(DINameSpace(Entity).getContext()); 1132 } 1133 } 1134 } 1135 } 1136 1137 /// processLocation - Process DILocation. 1138 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) { 1139 if (!Loc) 1140 return; 1141 InitializeTypeMap(M); 1142 processScope(Loc.getScope()); 1143 processLocation(M, Loc.getOrigLocation()); 1144 } 1145 1146 /// processType - Process DIType. 1147 void DebugInfoFinder::processType(DIType DT) { 1148 if (!addType(DT)) 1149 return; 1150 processScope(DT.getContext().resolve(TypeIdentifierMap)); 1151 if (DT.isCompositeType()) { 1152 DICompositeType DCT(DT); 1153 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); 1154 if (DT.isSubroutineType()) { 1155 DITypeArray DTA = DISubroutineType(DT).getTypeArray(); 1156 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i) 1157 processType(DTA.getElement(i).resolve(TypeIdentifierMap)); 1158 return; 1159 } 1160 DIArray DA = DCT.getElements(); 1161 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) { 1162 DIDescriptor D = DA.getElement(i); 1163 if (D.isType()) 1164 processType(DIType(D)); 1165 else if (D.isSubprogram()) 1166 processSubprogram(DISubprogram(D)); 1167 } 1168 } else if (DT.isDerivedType()) { 1169 DIDerivedType DDT(DT); 1170 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); 1171 } 1172 } 1173 1174 void DebugInfoFinder::processScope(DIScope Scope) { 1175 if (Scope.isType()) { 1176 DIType Ty(Scope); 1177 processType(Ty); 1178 return; 1179 } 1180 if (Scope.isCompileUnit()) { 1181 addCompileUnit(DICompileUnit(Scope)); 1182 return; 1183 } 1184 if (Scope.isSubprogram()) { 1185 processSubprogram(DISubprogram(Scope)); 1186 return; 1187 } 1188 if (!addScope(Scope)) 1189 return; 1190 if (Scope.isLexicalBlock()) { 1191 DILexicalBlock LB(Scope); 1192 processScope(LB.getContext()); 1193 } else if (Scope.isLexicalBlockFile()) { 1194 DILexicalBlockFile LBF = DILexicalBlockFile(Scope); 1195 processScope(LBF.getScope()); 1196 } else if (Scope.isNameSpace()) { 1197 DINameSpace NS(Scope); 1198 processScope(NS.getContext()); 1199 } 1200 } 1201 1202 /// processSubprogram - Process DISubprogram. 1203 void DebugInfoFinder::processSubprogram(DISubprogram SP) { 1204 if (!addSubprogram(SP)) 1205 return; 1206 processScope(SP.getContext().resolve(TypeIdentifierMap)); 1207 processType(SP.getType()); 1208 DIArray TParams = SP.getTemplateParams(); 1209 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) { 1210 DIDescriptor Element = TParams.getElement(I); 1211 if (Element.isTemplateTypeParameter()) { 1212 DITemplateTypeParameter TType(Element); 1213 processScope(TType.getContext().resolve(TypeIdentifierMap)); 1214 processType(TType.getType().resolve(TypeIdentifierMap)); 1215 } else if (Element.isTemplateValueParameter()) { 1216 DITemplateValueParameter TVal(Element); 1217 processScope(TVal.getContext().resolve(TypeIdentifierMap)); 1218 processType(TVal.getType().resolve(TypeIdentifierMap)); 1219 } 1220 } 1221 } 1222 1223 /// processDeclare - Process DbgDeclareInst. 1224 void DebugInfoFinder::processDeclare(const Module &M, 1225 const DbgDeclareInst *DDI) { 1226 MDNode *N = dyn_cast<MDNode>(DDI->getVariable()); 1227 if (!N) 1228 return; 1229 InitializeTypeMap(M); 1230 1231 DIDescriptor DV(N); 1232 if (!DV.isVariable()) 1233 return; 1234 1235 if (!NodesSeen.insert(DV)) 1236 return; 1237 processScope(DIVariable(N).getContext()); 1238 processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); 1239 } 1240 1241 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { 1242 MDNode *N = dyn_cast<MDNode>(DVI->getVariable()); 1243 if (!N) 1244 return; 1245 InitializeTypeMap(M); 1246 1247 DIDescriptor DV(N); 1248 if (!DV.isVariable()) 1249 return; 1250 1251 if (!NodesSeen.insert(DV)) 1252 return; 1253 processScope(DIVariable(N).getContext()); 1254 processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); 1255 } 1256 1257 /// addType - Add type into Tys. 1258 bool DebugInfoFinder::addType(DIType DT) { 1259 if (!DT) 1260 return false; 1261 1262 if (!NodesSeen.insert(DT)) 1263 return false; 1264 1265 TYs.push_back(DT); 1266 return true; 1267 } 1268 1269 /// addCompileUnit - Add compile unit into CUs. 1270 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) { 1271 if (!CU) 1272 return false; 1273 if (!NodesSeen.insert(CU)) 1274 return false; 1275 1276 CUs.push_back(CU); 1277 return true; 1278 } 1279 1280 /// addGlobalVariable - Add global variable into GVs. 1281 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) { 1282 if (!DIG) 1283 return false; 1284 1285 if (!NodesSeen.insert(DIG)) 1286 return false; 1287 1288 GVs.push_back(DIG); 1289 return true; 1290 } 1291 1292 // addSubprogram - Add subprgoram into SPs. 1293 bool DebugInfoFinder::addSubprogram(DISubprogram SP) { 1294 if (!SP) 1295 return false; 1296 1297 if (!NodesSeen.insert(SP)) 1298 return false; 1299 1300 SPs.push_back(SP); 1301 return true; 1302 } 1303 1304 bool DebugInfoFinder::addScope(DIScope Scope) { 1305 if (!Scope) 1306 return false; 1307 // FIXME: Ocaml binding generates a scope with no content, we treat it 1308 // as null for now. 1309 if (Scope->getNumOperands() == 0) 1310 return false; 1311 if (!NodesSeen.insert(Scope)) 1312 return false; 1313 Scopes.push_back(Scope); 1314 return true; 1315 } 1316 1317 //===----------------------------------------------------------------------===// 1318 // DIDescriptor: dump routines for all descriptors. 1319 //===----------------------------------------------------------------------===// 1320 1321 /// dump - Print descriptor to dbgs() with a newline. 1322 void DIDescriptor::dump() const { 1323 print(dbgs()); 1324 dbgs() << '\n'; 1325 } 1326 1327 /// print - Print descriptor. 1328 void DIDescriptor::print(raw_ostream &OS) const { 1329 if (!DbgNode) 1330 return; 1331 1332 if (const char *Tag = dwarf::TagString(getTag())) 1333 OS << "[ " << Tag << " ]"; 1334 1335 if (this->isSubrange()) { 1336 DISubrange(DbgNode).printInternal(OS); 1337 } else if (this->isCompileUnit()) { 1338 DICompileUnit(DbgNode).printInternal(OS); 1339 } else if (this->isFile()) { 1340 DIFile(DbgNode).printInternal(OS); 1341 } else if (this->isEnumerator()) { 1342 DIEnumerator(DbgNode).printInternal(OS); 1343 } else if (this->isBasicType()) { 1344 DIType(DbgNode).printInternal(OS); 1345 } else if (this->isDerivedType()) { 1346 DIDerivedType(DbgNode).printInternal(OS); 1347 } else if (this->isCompositeType()) { 1348 DICompositeType(DbgNode).printInternal(OS); 1349 } else if (this->isSubprogram()) { 1350 DISubprogram(DbgNode).printInternal(OS); 1351 } else if (this->isGlobalVariable()) { 1352 DIGlobalVariable(DbgNode).printInternal(OS); 1353 } else if (this->isVariable()) { 1354 DIVariable(DbgNode).printInternal(OS); 1355 } else if (this->isObjCProperty()) { 1356 DIObjCProperty(DbgNode).printInternal(OS); 1357 } else if (this->isNameSpace()) { 1358 DINameSpace(DbgNode).printInternal(OS); 1359 } else if (this->isScope()) { 1360 DIScope(DbgNode).printInternal(OS); 1361 } else if (this->isExpression()) { 1362 DIExpression(DbgNode).printInternal(OS); 1363 } 1364 } 1365 1366 void DISubrange::printInternal(raw_ostream &OS) const { 1367 int64_t Count = getCount(); 1368 if (Count != -1) 1369 OS << " [" << getLo() << ", " << Count - 1 << ']'; 1370 else 1371 OS << " [unbounded]"; 1372 } 1373 1374 void DIScope::printInternal(raw_ostream &OS) const { 1375 OS << " [" << getDirectory() << "/" << getFilename() << ']'; 1376 } 1377 1378 void DICompileUnit::printInternal(raw_ostream &OS) const { 1379 DIScope::printInternal(OS); 1380 OS << " ["; 1381 unsigned Lang = getLanguage(); 1382 if (const char *LangStr = dwarf::LanguageString(Lang)) 1383 OS << LangStr; 1384 else 1385 (OS << "lang 0x").write_hex(Lang); 1386 OS << ']'; 1387 } 1388 1389 void DIEnumerator::printInternal(raw_ostream &OS) const { 1390 OS << " [" << getName() << " :: " << getEnumValue() << ']'; 1391 } 1392 1393 void DIType::printInternal(raw_ostream &OS) const { 1394 if (!DbgNode) 1395 return; 1396 1397 StringRef Res = getName(); 1398 if (!Res.empty()) 1399 OS << " [" << Res << "]"; 1400 1401 // TODO: Print context? 1402 1403 OS << " [line " << getLineNumber() << ", size " << getSizeInBits() 1404 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits(); 1405 if (isBasicType()) 1406 if (const char *Enc = 1407 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding())) 1408 OS << ", enc " << Enc; 1409 OS << "]"; 1410 1411 if (isPrivate()) 1412 OS << " [private]"; 1413 else if (isProtected()) 1414 OS << " [protected]"; 1415 else if (isPublic()) 1416 OS << " [public]"; 1417 1418 if (isArtificial()) 1419 OS << " [artificial]"; 1420 1421 if (isForwardDecl()) 1422 OS << " [decl]"; 1423 else if (getTag() == dwarf::DW_TAG_structure_type || 1424 getTag() == dwarf::DW_TAG_union_type || 1425 getTag() == dwarf::DW_TAG_enumeration_type || 1426 getTag() == dwarf::DW_TAG_class_type) 1427 OS << " [def]"; 1428 if (isVector()) 1429 OS << " [vector]"; 1430 if (isStaticMember()) 1431 OS << " [static]"; 1432 1433 if (isLValueReference()) 1434 OS << " [reference]"; 1435 1436 if (isRValueReference()) 1437 OS << " [rvalue reference]"; 1438 } 1439 1440 void DIDerivedType::printInternal(raw_ostream &OS) const { 1441 DIType::printInternal(OS); 1442 OS << " [from " << getTypeDerivedFrom().getName() << ']'; 1443 } 1444 1445 void DICompositeType::printInternal(raw_ostream &OS) const { 1446 DIType::printInternal(OS); 1447 DIArray A = getElements(); 1448 OS << " [" << A.getNumElements() << " elements]"; 1449 } 1450 1451 void DINameSpace::printInternal(raw_ostream &OS) const { 1452 StringRef Name = getName(); 1453 if (!Name.empty()) 1454 OS << " [" << Name << ']'; 1455 1456 OS << " [line " << getLineNumber() << ']'; 1457 } 1458 1459 void DISubprogram::printInternal(raw_ostream &OS) const { 1460 // TODO : Print context 1461 OS << " [line " << getLineNumber() << ']'; 1462 1463 if (isLocalToUnit()) 1464 OS << " [local]"; 1465 1466 if (isDefinition()) 1467 OS << " [def]"; 1468 1469 if (getScopeLineNumber() != getLineNumber()) 1470 OS << " [scope " << getScopeLineNumber() << "]"; 1471 1472 if (isPrivate()) 1473 OS << " [private]"; 1474 else if (isProtected()) 1475 OS << " [protected]"; 1476 else if (isPublic()) 1477 OS << " [public]"; 1478 1479 if (isLValueReference()) 1480 OS << " [reference]"; 1481 1482 if (isRValueReference()) 1483 OS << " [rvalue reference]"; 1484 1485 StringRef Res = getName(); 1486 if (!Res.empty()) 1487 OS << " [" << Res << ']'; 1488 } 1489 1490 void DIGlobalVariable::printInternal(raw_ostream &OS) const { 1491 StringRef Res = getName(); 1492 if (!Res.empty()) 1493 OS << " [" << Res << ']'; 1494 1495 OS << " [line " << getLineNumber() << ']'; 1496 1497 // TODO : Print context 1498 1499 if (isLocalToUnit()) 1500 OS << " [local]"; 1501 1502 if (isDefinition()) 1503 OS << " [def]"; 1504 } 1505 1506 void DIVariable::printInternal(raw_ostream &OS) const { 1507 StringRef Res = getName(); 1508 if (!Res.empty()) 1509 OS << " [" << Res << ']'; 1510 1511 OS << " [line " << getLineNumber() << ']'; 1512 } 1513 1514 void DIExpression::printInternal(raw_ostream &OS) const { 1515 for (unsigned I = 0; I < getNumElements(); ++I) { 1516 uint64_t OpCode = getElement(I); 1517 OS << " [" << OperationEncodingString(OpCode); 1518 switch (OpCode) { 1519 case DW_OP_plus: { 1520 OS << " " << getElement(++I); 1521 break; 1522 } 1523 case DW_OP_piece: { 1524 unsigned Offset = getElement(++I); 1525 unsigned Size = getElement(++I); 1526 OS << " offset=" << Offset << ", size=" << Size; 1527 break; 1528 } 1529 default: 1530 // Else bail out early. This may be a line table entry. 1531 OS << "Unknown]"; 1532 return; 1533 } 1534 OS << "]"; 1535 } 1536 } 1537 1538 void DIObjCProperty::printInternal(raw_ostream &OS) const { 1539 StringRef Name = getObjCPropertyName(); 1540 if (!Name.empty()) 1541 OS << " [" << Name << ']'; 1542 1543 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6) 1544 << ']'; 1545 } 1546 1547 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS, 1548 const LLVMContext &Ctx) { 1549 if (!DL.isUnknown()) { // Print source line info. 1550 DIScope Scope(DL.getScope(Ctx)); 1551 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope."); 1552 // Omit the directory, because it's likely to be long and uninteresting. 1553 CommentOS << Scope.getFilename(); 1554 CommentOS << ':' << DL.getLine(); 1555 if (DL.getCol() != 0) 1556 CommentOS << ':' << DL.getCol(); 1557 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx)); 1558 if (!InlinedAtDL.isUnknown()) { 1559 CommentOS << " @[ "; 1560 printDebugLoc(InlinedAtDL, CommentOS, Ctx); 1561 CommentOS << " ]"; 1562 } 1563 } 1564 } 1565 1566 void DIVariable::printExtendedName(raw_ostream &OS) const { 1567 const LLVMContext &Ctx = DbgNode->getContext(); 1568 StringRef Res = getName(); 1569 if (!Res.empty()) 1570 OS << Res << "," << getLineNumber(); 1571 if (MDNode *InlinedAt = getInlinedAt()) { 1572 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt); 1573 if (!InlinedAtDL.isUnknown()) { 1574 OS << " @["; 1575 printDebugLoc(InlinedAtDL, OS, Ctx); 1576 OS << "]"; 1577 } 1578 } 1579 } 1580 1581 /// Specialize constructor to make sure it has the correct type. 1582 template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) { 1583 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); 1584 } 1585 template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) { 1586 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); 1587 } 1588 1589 /// Specialize getFieldAs to handle fields that are references to DIScopes. 1590 template <> 1591 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const { 1592 return DIScopeRef(getField(DbgNode, Elt)); 1593 } 1594 /// Specialize getFieldAs to handle fields that are references to DITypes. 1595 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const { 1596 return DITypeRef(getField(DbgNode, Elt)); 1597 } 1598 1599 /// Strip debug info in the module if it exists. 1600 /// To do this, we remove all calls to the debugger intrinsics and any named 1601 /// metadata for debugging. We also remove debug locations for instructions. 1602 /// Return true if module is modified. 1603 bool llvm::StripDebugInfo(Module &M) { 1604 1605 bool Changed = false; 1606 1607 // Remove all of the calls to the debugger intrinsics, and remove them from 1608 // the module. 1609 if (Function *Declare = M.getFunction("llvm.dbg.declare")) { 1610 while (!Declare->use_empty()) { 1611 CallInst *CI = cast<CallInst>(Declare->user_back()); 1612 CI->eraseFromParent(); 1613 } 1614 Declare->eraseFromParent(); 1615 Changed = true; 1616 } 1617 1618 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { 1619 while (!DbgVal->use_empty()) { 1620 CallInst *CI = cast<CallInst>(DbgVal->user_back()); 1621 CI->eraseFromParent(); 1622 } 1623 DbgVal->eraseFromParent(); 1624 Changed = true; 1625 } 1626 1627 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), 1628 NME = M.named_metadata_end(); NMI != NME;) { 1629 NamedMDNode *NMD = NMI; 1630 ++NMI; 1631 if (NMD->getName().startswith("llvm.dbg.")) { 1632 NMD->eraseFromParent(); 1633 Changed = true; 1634 } 1635 } 1636 1637 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) 1638 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; 1639 ++FI) 1640 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; 1641 ++BI) { 1642 if (!BI->getDebugLoc().isUnknown()) { 1643 Changed = true; 1644 BI->setDebugLoc(DebugLoc()); 1645 } 1646 } 1647 1648 return Changed; 1649 } 1650 1651 /// Return Debug Info Metadata Version by checking module flags. 1652 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { 1653 Value *Val = M.getModuleFlag("Debug Info Version"); 1654 if (!Val) 1655 return 0; 1656 return cast<ConstantInt>(Val)->getZExtValue(); 1657 } 1658 1659 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram> 1660 llvm::makeSubprogramMap(const Module &M) { 1661 DenseMap<const Function *, DISubprogram> R; 1662 1663 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); 1664 if (!CU_Nodes) 1665 return R; 1666 1667 for (MDNode *N : CU_Nodes->operands()) { 1668 DICompileUnit CUNode(N); 1669 DIArray SPs = CUNode.getSubprograms(); 1670 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { 1671 DISubprogram SP(SPs.getElement(i)); 1672 if (Function *F = SP.getFunction()) 1673 R.insert(std::make_pair(F, SP)); 1674 } 1675 } 1676 return R; 1677 } 1678