DebugInfo.cpp revision fe07db323b2b403236375af0a54b0ed2294cd0e6
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/DebugInfo.h" 16#include "llvm/ADT/STLExtras.h" 17#include "llvm/ADT/SmallPtrSet.h" 18#include "llvm/ADT/SmallString.h" 19#include "llvm/Analysis/ValueTracking.h" 20#include "llvm/IR/Constants.h" 21#include "llvm/IR/DerivedTypes.h" 22#include "llvm/IR/Instructions.h" 23#include "llvm/IR/IntrinsicInst.h" 24#include "llvm/IR/Intrinsics.h" 25#include "llvm/IR/Module.h" 26#include "llvm/Support/Debug.h" 27#include "llvm/Support/Dwarf.h" 28#include "llvm/Support/raw_ostream.h" 29using namespace llvm; 30using namespace llvm::dwarf; 31 32//===----------------------------------------------------------------------===// 33// DIDescriptor 34//===----------------------------------------------------------------------===// 35 36DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) { 37} 38 39DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) { 40} 41 42DIDescriptor::DIDescriptor(const DILexicalBlockFile F) : DbgNode(F.DbgNode) { 43} 44 45DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) { 46} 47 48DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) { 49} 50 51DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) { 52} 53 54StringRef 55DIDescriptor::getStringField(unsigned Elt) const { 56 if (DbgNode == 0) 57 return StringRef(); 58 59 if (Elt < DbgNode->getNumOperands()) 60 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt))) 61 return MDS->getString(); 62 63 return StringRef(); 64} 65 66uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { 67 if (DbgNode == 0) 68 return 0; 69 70 if (Elt < DbgNode->getNumOperands()) 71 if (ConstantInt *CI 72 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt))) 73 return CI->getZExtValue(); 74 75 return 0; 76} 77 78int64_t DIDescriptor::getInt64Field(unsigned Elt) const { 79 if (DbgNode == 0) 80 return 0; 81 82 if (Elt < DbgNode->getNumOperands()) 83 if (ConstantInt *CI 84 = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt))) 85 return CI->getSExtValue(); 86 87 return 0; 88} 89 90DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { 91 if (DbgNode == 0) 92 return DIDescriptor(); 93 94 if (Elt < DbgNode->getNumOperands()) 95 return 96 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt))); 97 return DIDescriptor(); 98} 99 100GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { 101 if (DbgNode == 0) 102 return 0; 103 104 if (Elt < DbgNode->getNumOperands()) 105 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt)); 106 return 0; 107} 108 109Constant *DIDescriptor::getConstantField(unsigned Elt) const { 110 if (DbgNode == 0) 111 return 0; 112 113 if (Elt < DbgNode->getNumOperands()) 114 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt)); 115 return 0; 116} 117 118Function *DIDescriptor::getFunctionField(unsigned Elt) const { 119 if (DbgNode == 0) 120 return 0; 121 122 if (Elt < DbgNode->getNumOperands()) 123 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt)); 124 return 0; 125} 126 127void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) { 128 if (DbgNode == 0) 129 return; 130 131 if (Elt < DbgNode->getNumOperands()) { 132 MDNode *Node = const_cast<MDNode*>(DbgNode); 133 Node->replaceOperandWith(Elt, F); 134 } 135} 136 137unsigned DIVariable::getNumAddrElements() const { 138 if (getVersion() <= LLVMDebugVersion8) 139 return DbgNode->getNumOperands()-6; 140 if (getVersion() == LLVMDebugVersion9) 141 return DbgNode->getNumOperands()-7; 142 return DbgNode->getNumOperands()-8; 143} 144 145/// getInlinedAt - If this variable is inlined then return inline location. 146MDNode *DIVariable::getInlinedAt() const { 147 if (getVersion() <= LLVMDebugVersion9) 148 return NULL; 149 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7)); 150} 151 152//===----------------------------------------------------------------------===// 153// Predicates 154//===----------------------------------------------------------------------===// 155 156/// isBasicType - Return true if the specified tag is legal for 157/// DIBasicType. 158bool DIDescriptor::isBasicType() const { 159 if (!DbgNode) return false; 160 switch (getTag()) { 161 case dwarf::DW_TAG_base_type: 162 case dwarf::DW_TAG_unspecified_type: 163 return true; 164 default: 165 return false; 166 } 167} 168 169/// isDerivedType - Return true if the specified tag is legal for DIDerivedType. 170bool DIDescriptor::isDerivedType() const { 171 if (!DbgNode) return false; 172 switch (getTag()) { 173 case dwarf::DW_TAG_typedef: 174 case dwarf::DW_TAG_pointer_type: 175 case dwarf::DW_TAG_ptr_to_member_type: 176 case dwarf::DW_TAG_reference_type: 177 case dwarf::DW_TAG_rvalue_reference_type: 178 case dwarf::DW_TAG_const_type: 179 case dwarf::DW_TAG_volatile_type: 180 case dwarf::DW_TAG_restrict_type: 181 case dwarf::DW_TAG_member: 182 case dwarf::DW_TAG_inheritance: 183 case dwarf::DW_TAG_friend: 184 return true; 185 default: 186 // CompositeTypes are currently modelled as DerivedTypes. 187 return isCompositeType(); 188 } 189} 190 191/// isCompositeType - Return true if the specified tag is legal for 192/// DICompositeType. 193bool DIDescriptor::isCompositeType() const { 194 if (!DbgNode) return false; 195 switch (getTag()) { 196 case dwarf::DW_TAG_array_type: 197 case dwarf::DW_TAG_structure_type: 198 case dwarf::DW_TAG_union_type: 199 case dwarf::DW_TAG_enumeration_type: 200 case dwarf::DW_TAG_subroutine_type: 201 case dwarf::DW_TAG_class_type: 202 return true; 203 default: 204 return false; 205 } 206} 207 208/// isVariable - Return true if the specified tag is legal for DIVariable. 209bool DIDescriptor::isVariable() const { 210 if (!DbgNode) return false; 211 switch (getTag()) { 212 case dwarf::DW_TAG_auto_variable: 213 case dwarf::DW_TAG_arg_variable: 214 return true; 215 default: 216 return false; 217 } 218} 219 220/// isType - Return true if the specified tag is legal for DIType. 221bool DIDescriptor::isType() const { 222 return isBasicType() || isCompositeType() || isDerivedType(); 223} 224 225/// isSubprogram - Return true if the specified tag is legal for 226/// DISubprogram. 227bool DIDescriptor::isSubprogram() const { 228 return DbgNode && getTag() == dwarf::DW_TAG_subprogram; 229} 230 231/// isGlobalVariable - Return true if the specified tag is legal for 232/// DIGlobalVariable. 233bool DIDescriptor::isGlobalVariable() const { 234 return DbgNode && (getTag() == dwarf::DW_TAG_variable || 235 getTag() == dwarf::DW_TAG_constant); 236} 237 238/// isGlobal - Return true if the specified tag is legal for DIGlobal. 239bool DIDescriptor::isGlobal() const { 240 return isGlobalVariable(); 241} 242 243/// isUnspecifiedParmeter - Return true if the specified tag is 244/// DW_TAG_unspecified_parameters. 245bool DIDescriptor::isUnspecifiedParameter() const { 246 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters; 247} 248 249/// isScope - Return true if the specified tag is one of the scope 250/// related tag. 251bool DIDescriptor::isScope() const { 252 if (!DbgNode) return false; 253 switch (getTag()) { 254 case dwarf::DW_TAG_compile_unit: 255 case dwarf::DW_TAG_lexical_block: 256 case dwarf::DW_TAG_subprogram: 257 case dwarf::DW_TAG_namespace: 258 return true; 259 default: 260 break; 261 } 262 return false; 263} 264 265/// isTemplateTypeParameter - Return true if the specified tag is 266/// DW_TAG_template_type_parameter. 267bool DIDescriptor::isTemplateTypeParameter() const { 268 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter; 269} 270 271/// isTemplateValueParameter - Return true if the specified tag is 272/// DW_TAG_template_value_parameter. 273bool DIDescriptor::isTemplateValueParameter() const { 274 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter; 275} 276 277/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit. 278bool DIDescriptor::isCompileUnit() const { 279 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit; 280} 281 282/// isFile - Return true if the specified tag is DW_TAG_file_type. 283bool DIDescriptor::isFile() const { 284 return DbgNode && getTag() == dwarf::DW_TAG_file_type; 285} 286 287/// isNameSpace - Return true if the specified tag is DW_TAG_namespace. 288bool DIDescriptor::isNameSpace() const { 289 return DbgNode && getTag() == dwarf::DW_TAG_namespace; 290} 291 292/// isLexicalBlockFile - Return true if the specified descriptor is a 293/// lexical block with an extra file. 294bool DIDescriptor::isLexicalBlockFile() const { 295 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && 296 (DbgNode->getNumOperands() == 3); 297} 298 299/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block. 300bool DIDescriptor::isLexicalBlock() const { 301 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && 302 (DbgNode->getNumOperands() > 3); 303} 304 305/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type. 306bool DIDescriptor::isSubrange() const { 307 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type; 308} 309 310/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator. 311bool DIDescriptor::isEnumerator() const { 312 return DbgNode && getTag() == dwarf::DW_TAG_enumerator; 313} 314 315/// isObjCProperty - Return true if the specified tag is DW_TAG 316bool DIDescriptor::isObjCProperty() const { 317 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property; 318} 319//===----------------------------------------------------------------------===// 320// Simple Descriptor Constructors and other Methods 321//===----------------------------------------------------------------------===// 322 323DIType::DIType(const MDNode *N) : DIScope(N) { 324 if (!N) return; 325 if (!isBasicType() && !isDerivedType() && !isCompositeType()) { 326 DbgNode = 0; 327 } 328} 329 330unsigned DIArray::getNumElements() const { 331 if (!DbgNode) 332 return 0; 333 return DbgNode->getNumOperands(); 334} 335 336/// replaceAllUsesWith - Replace all uses of debug info referenced by 337/// this descriptor. 338void DIType::replaceAllUsesWith(DIDescriptor &D) { 339 if (!DbgNode) 340 return; 341 342 // Since we use a TrackingVH for the node, its easy for clients to manufacture 343 // legitimate situations where they want to replaceAllUsesWith() on something 344 // which, due to uniquing, has merged with the source. We shield clients from 345 // this detail by allowing a value to be replaced with replaceAllUsesWith() 346 // itself. 347 if (DbgNode != D) { 348 MDNode *Node = const_cast<MDNode*>(DbgNode); 349 const MDNode *DN = D; 350 const Value *V = cast_or_null<Value>(DN); 351 Node->replaceAllUsesWith(const_cast<Value*>(V)); 352 MDNode::deleteTemporary(Node); 353 } 354} 355 356/// replaceAllUsesWith - Replace all uses of debug info referenced by 357/// this descriptor. 358void DIType::replaceAllUsesWith(MDNode *D) { 359 if (!DbgNode) 360 return; 361 362 // Since we use a TrackingVH for the node, its easy for clients to manufacture 363 // legitimate situations where they want to replaceAllUsesWith() on something 364 // which, due to uniquing, has merged with the source. We shield clients from 365 // this detail by allowing a value to be replaced with replaceAllUsesWith() 366 // itself. 367 if (DbgNode != D) { 368 MDNode *Node = const_cast<MDNode*>(DbgNode); 369 const MDNode *DN = D; 370 const Value *V = cast_or_null<Value>(DN); 371 Node->replaceAllUsesWith(const_cast<Value*>(V)); 372 MDNode::deleteTemporary(Node); 373 } 374} 375 376/// isUnsignedDIType - Return true if type encoding is unsigned. 377bool DIType::isUnsignedDIType() { 378 DIDerivedType DTy(DbgNode); 379 if (DTy.Verify()) 380 return DTy.getTypeDerivedFrom().isUnsignedDIType(); 381 382 DIBasicType BTy(DbgNode); 383 if (BTy.Verify()) { 384 unsigned Encoding = BTy.getEncoding(); 385 if (Encoding == dwarf::DW_ATE_unsigned || 386 Encoding == dwarf::DW_ATE_unsigned_char || 387 Encoding == dwarf::DW_ATE_boolean) 388 return true; 389 } 390 return false; 391} 392 393/// Verify - Verify that a compile unit is well formed. 394bool DICompileUnit::Verify() const { 395 if (!DbgNode) 396 return false; 397 StringRef N = getFilename(); 398 if (N.empty()) 399 return false; 400 // It is possible that directory and produce string is empty. 401 return true; 402} 403 404/// Verify - Verify that an ObjC property is well formed. 405bool DIObjCProperty::Verify() const { 406 if (!DbgNode) 407 return false; 408 unsigned Tag = getTag(); 409 if (Tag != dwarf::DW_TAG_APPLE_property) return false; 410 DIType Ty = getType(); 411 if (!Ty.Verify()) return false; 412 413 // Don't worry about the rest of the strings for now. 414 return true; 415} 416 417/// Verify - Verify that a type descriptor is well formed. 418bool DIType::Verify() const { 419 if (!DbgNode) 420 return false; 421 if (getContext() && !getContext().Verify()) 422 return false; 423 unsigned Tag = getTag(); 424 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type && 425 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type && 426 Tag != dwarf::DW_TAG_ptr_to_member_type && 427 Tag != dwarf::DW_TAG_reference_type && 428 Tag != dwarf::DW_TAG_rvalue_reference_type && 429 Tag != dwarf::DW_TAG_restrict_type && 430 Tag != dwarf::DW_TAG_array_type && 431 Tag != dwarf::DW_TAG_enumeration_type && 432 Tag != dwarf::DW_TAG_subroutine_type && 433 getFilename().empty()) 434 return false; 435 return true; 436} 437 438/// Verify - Verify that a basic type descriptor is well formed. 439bool DIBasicType::Verify() const { 440 return isBasicType(); 441} 442 443/// Verify - Verify that a derived type descriptor is well formed. 444bool DIDerivedType::Verify() const { 445 return isDerivedType(); 446} 447 448/// Verify - Verify that a composite type descriptor is well formed. 449bool DICompositeType::Verify() const { 450 if (!DbgNode) 451 return false; 452 if (getContext() && !getContext().Verify()) 453 return false; 454 455 return true; 456} 457 458/// Verify - Verify that a subprogram descriptor is well formed. 459bool DISubprogram::Verify() const { 460 if (!DbgNode) 461 return false; 462 463 if (getContext() && !getContext().Verify()) 464 return false; 465 466 DICompositeType Ty = getType(); 467 if (!Ty.Verify()) 468 return false; 469 return true; 470} 471 472/// Verify - Verify that a global variable descriptor is well formed. 473bool DIGlobalVariable::Verify() const { 474 if (!DbgNode) 475 return false; 476 477 if (getDisplayName().empty()) 478 return false; 479 480 if (getContext() && !getContext().Verify()) 481 return false; 482 483 DIType Ty = getType(); 484 if (!Ty.Verify()) 485 return false; 486 487 if (!getGlobal() && !getConstant()) 488 return false; 489 490 return true; 491} 492 493/// Verify - Verify that a variable descriptor is well formed. 494bool DIVariable::Verify() const { 495 if (!DbgNode) 496 return false; 497 498 if (getContext() && !getContext().Verify()) 499 return false; 500 501 DIType Ty = getType(); 502 if (!Ty.Verify()) 503 return false; 504 505 return true; 506} 507 508/// Verify - Verify that a location descriptor is well formed. 509bool DILocation::Verify() const { 510 if (!DbgNode) 511 return false; 512 513 return DbgNode->getNumOperands() == 4; 514} 515 516/// Verify - Verify that a namespace descriptor is well formed. 517bool DINameSpace::Verify() const { 518 if (!DbgNode) 519 return false; 520 if (getName().empty()) 521 return false; 522 return true; 523} 524 525/// getOriginalTypeSize - If this type is derived from a base type then 526/// return base type size. 527uint64_t DIDerivedType::getOriginalTypeSize() const { 528 unsigned Tag = getTag(); 529 530 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && 531 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && 532 Tag != dwarf::DW_TAG_restrict_type) 533 return getSizeInBits(); 534 535 DIType BaseType = getTypeDerivedFrom(); 536 537 // If this type is not derived from any type then take conservative approach. 538 if (!BaseType.isValid()) 539 return getSizeInBits(); 540 541 // If this is a derived type, go ahead and get the base type, unless it's a 542 // reference then it's just the size of the field. Pointer types have no need 543 // of this since they're a different type of qualification on the type. 544 if (BaseType.getTag() == dwarf::DW_TAG_reference_type || 545 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type) 546 return getSizeInBits(); 547 548 if (BaseType.isDerivedType()) 549 return DIDerivedType(BaseType).getOriginalTypeSize(); 550 551 return BaseType.getSizeInBits(); 552} 553 554/// getObjCProperty - Return property node, if this ivar is associated with one. 555MDNode *DIDerivedType::getObjCProperty() const { 556 if (getVersion() <= LLVMDebugVersion11 || DbgNode->getNumOperands() <= 10) 557 return NULL; 558 return dyn_cast_or_null<MDNode>(DbgNode->getOperand(10)); 559} 560 561/// isInlinedFnArgument - Return true if this variable provides debugging 562/// information for an inlined function arguments. 563bool DIVariable::isInlinedFnArgument(const Function *CurFn) { 564 assert(CurFn && "Invalid function"); 565 if (!getContext().isSubprogram()) 566 return false; 567 // This variable is not inlined function argument if its scope 568 // does not describe current function. 569 return !DISubprogram(getContext()).describes(CurFn); 570} 571 572/// describes - Return true if this subprogram provides debugging 573/// information for the function F. 574bool DISubprogram::describes(const Function *F) { 575 assert(F && "Invalid function"); 576 if (F == getFunction()) 577 return true; 578 StringRef Name = getLinkageName(); 579 if (Name.empty()) 580 Name = getName(); 581 if (F->getName() == Name) 582 return true; 583 return false; 584} 585 586unsigned DISubprogram::isOptimized() const { 587 assert (DbgNode && "Invalid subprogram descriptor!"); 588 if (DbgNode->getNumOperands() == 16) 589 return getUnsignedField(15); 590 return 0; 591} 592 593MDNode *DISubprogram::getVariablesNodes() const { 594 if (!DbgNode || DbgNode->getNumOperands() <= 19) 595 return NULL; 596 if (MDNode *Temp = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19))) 597 return dyn_cast_or_null<MDNode>(Temp->getOperand(0)); 598 return NULL; 599} 600 601DIArray DISubprogram::getVariables() const { 602 if (!DbgNode || DbgNode->getNumOperands() <= 19) 603 return DIArray(); 604 if (MDNode *T = dyn_cast_or_null<MDNode>(DbgNode->getOperand(19))) 605 if (MDNode *A = dyn_cast_or_null<MDNode>(T->getOperand(0))) 606 return DIArray(A); 607 return DIArray(); 608} 609 610StringRef DIScope::getFilename() const { 611 if (!DbgNode) 612 return StringRef(); 613 if (isLexicalBlockFile()) 614 return DILexicalBlockFile(DbgNode).getFilename(); 615 if (isLexicalBlock()) 616 return DILexicalBlock(DbgNode).getFilename(); 617 if (isSubprogram()) 618 return DISubprogram(DbgNode).getFilename(); 619 if (isCompileUnit()) 620 return DICompileUnit(DbgNode).getFilename(); 621 if (isNameSpace()) 622 return DINameSpace(DbgNode).getFilename(); 623 if (isType()) 624 return DIType(DbgNode).getFilename(); 625 if (isFile()) 626 return DIFile(DbgNode).getFilename(); 627 llvm_unreachable("Invalid DIScope!"); 628} 629 630StringRef DIScope::getDirectory() const { 631 if (!DbgNode) 632 return StringRef(); 633 if (isLexicalBlockFile()) 634 return DILexicalBlockFile(DbgNode).getDirectory(); 635 if (isLexicalBlock()) 636 return DILexicalBlock(DbgNode).getDirectory(); 637 if (isSubprogram()) 638 return DISubprogram(DbgNode).getDirectory(); 639 if (isCompileUnit()) 640 return DICompileUnit(DbgNode).getDirectory(); 641 if (isNameSpace()) 642 return DINameSpace(DbgNode).getDirectory(); 643 if (isType()) 644 return DIType(DbgNode).getDirectory(); 645 if (isFile()) 646 return DIFile(DbgNode).getDirectory(); 647 llvm_unreachable("Invalid DIScope!"); 648} 649 650DIArray DICompileUnit::getEnumTypes() const { 651 if (!DbgNode || DbgNode->getNumOperands() < 14) 652 return DIArray(); 653 654 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(10))) 655 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0))) 656 return DIArray(A); 657 return DIArray(); 658} 659 660DIArray DICompileUnit::getRetainedTypes() const { 661 if (!DbgNode || DbgNode->getNumOperands() < 14) 662 return DIArray(); 663 664 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(11))) 665 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0))) 666 return DIArray(A); 667 return DIArray(); 668} 669 670DIArray DICompileUnit::getSubprograms() const { 671 if (!DbgNode || DbgNode->getNumOperands() < 14) 672 return DIArray(); 673 674 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(12))) 675 if (N->getNumOperands() > 0) 676 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0))) 677 return DIArray(A); 678 return DIArray(); 679} 680 681 682DIArray DICompileUnit::getGlobalVariables() const { 683 if (!DbgNode || DbgNode->getNumOperands() < 14) 684 return DIArray(); 685 686 if (MDNode *N = dyn_cast_or_null<MDNode>(DbgNode->getOperand(13))) 687 if (MDNode *A = dyn_cast_or_null<MDNode>(N->getOperand(0))) 688 return DIArray(A); 689 return DIArray(); 690} 691 692/// fixupObjcLikeName - Replace contains special characters used 693/// in a typical Objective-C names with '.' in a given string. 694static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) { 695 bool isObjCLike = false; 696 for (size_t i = 0, e = Str.size(); i < e; ++i) { 697 char C = Str[i]; 698 if (C == '[') 699 isObjCLike = true; 700 701 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' || 702 C == '+' || C == '(' || C == ')')) 703 Out.push_back('.'); 704 else 705 Out.push_back(C); 706 } 707} 708 709/// getFnSpecificMDNode - Return a NameMDNode, if available, that is 710/// suitable to hold function specific information. 711NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) { 712 SmallString<32> Name = StringRef("llvm.dbg.lv."); 713 StringRef FName = "fn"; 714 if (Fn.getFunction()) 715 FName = Fn.getFunction()->getName(); 716 else 717 FName = Fn.getName(); 718 char One = '\1'; 719 if (FName.startswith(StringRef(&One, 1))) 720 FName = FName.substr(1); 721 fixupObjcLikeName(FName, Name); 722 return M.getNamedMetadata(Name.str()); 723} 724 725/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable 726/// to hold function specific information. 727NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) { 728 SmallString<32> Name = StringRef("llvm.dbg.lv."); 729 StringRef FName = "fn"; 730 if (Fn.getFunction()) 731 FName = Fn.getFunction()->getName(); 732 else 733 FName = Fn.getName(); 734 char One = '\1'; 735 if (FName.startswith(StringRef(&One, 1))) 736 FName = FName.substr(1); 737 fixupObjcLikeName(FName, Name); 738 739 return M.getOrInsertNamedMetadata(Name.str()); 740} 741 742/// createInlinedVariable - Create a new inlined variable based on current 743/// variable. 744/// @param DV Current Variable. 745/// @param InlinedScope Location at current variable is inlined. 746DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, 747 LLVMContext &VMContext) { 748 SmallVector<Value *, 16> Elts; 749 // Insert inlined scope as 7th element. 750 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i) 751 i == 7 ? Elts.push_back(InlinedScope) : 752 Elts.push_back(DV->getOperand(i)); 753 return DIVariable(MDNode::get(VMContext, Elts)); 754} 755 756/// cleanseInlinedVariable - Remove inlined scope from the variable. 757DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { 758 SmallVector<Value *, 16> Elts; 759 // Insert inlined scope as 7th element. 760 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i) 761 i == 7 ? 762 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))): 763 Elts.push_back(DV->getOperand(i)); 764 return DIVariable(MDNode::get(VMContext, Elts)); 765} 766 767/// getDISubprogram - Find subprogram that is enclosing this scope. 768DISubprogram llvm::getDISubprogram(const MDNode *Scope) { 769 DIDescriptor D(Scope); 770 if (D.isSubprogram()) 771 return DISubprogram(Scope); 772 773 if (D.isLexicalBlockFile()) 774 return getDISubprogram(DILexicalBlockFile(Scope).getContext()); 775 776 if (D.isLexicalBlock()) 777 return getDISubprogram(DILexicalBlock(Scope).getContext()); 778 779 return DISubprogram(); 780} 781 782/// getDICompositeType - Find underlying composite type. 783DICompositeType llvm::getDICompositeType(DIType T) { 784 if (T.isCompositeType()) 785 return DICompositeType(T); 786 787 if (T.isDerivedType()) 788 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom()); 789 790 return DICompositeType(); 791} 792 793/// isSubprogramContext - Return true if Context is either a subprogram 794/// or another context nested inside a subprogram. 795bool llvm::isSubprogramContext(const MDNode *Context) { 796 if (!Context) 797 return false; 798 DIDescriptor D(Context); 799 if (D.isSubprogram()) 800 return true; 801 if (D.isType()) 802 return isSubprogramContext(DIType(Context).getContext()); 803 return false; 804} 805 806//===----------------------------------------------------------------------===// 807// DebugInfoFinder implementations. 808//===----------------------------------------------------------------------===// 809 810/// processModule - Process entire module and collect debug info. 811void DebugInfoFinder::processModule(const Module &M) { 812 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { 813 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { 814 DICompileUnit CU(CU_Nodes->getOperand(i)); 815 addCompileUnit(CU); 816 if (CU.getVersion() > LLVMDebugVersion10) { 817 DIArray GVs = CU.getGlobalVariables(); 818 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) { 819 DIGlobalVariable DIG(GVs.getElement(i)); 820 if (addGlobalVariable(DIG)) 821 processType(DIG.getType()); 822 } 823 DIArray SPs = CU.getSubprograms(); 824 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) 825 processSubprogram(DISubprogram(SPs.getElement(i))); 826 DIArray EnumTypes = CU.getEnumTypes(); 827 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i) 828 processType(DIType(EnumTypes.getElement(i))); 829 DIArray RetainedTypes = CU.getRetainedTypes(); 830 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i) 831 processType(DIType(RetainedTypes.getElement(i))); 832 return; 833 } 834 } 835 } 836 837 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) 838 for (Function::const_iterator FI = (*I).begin(), FE = (*I).end(); 839 FI != FE; ++FI) 840 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end(); 841 BI != BE; ++BI) { 842 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) 843 processDeclare(DDI); 844 845 DebugLoc Loc = BI->getDebugLoc(); 846 if (Loc.isUnknown()) 847 continue; 848 849 LLVMContext &Ctx = BI->getContext(); 850 DIDescriptor Scope(Loc.getScope(Ctx)); 851 852 if (Scope.isCompileUnit()) 853 addCompileUnit(DICompileUnit(Scope)); 854 else if (Scope.isSubprogram()) 855 processSubprogram(DISubprogram(Scope)); 856 else if (Scope.isLexicalBlockFile()) { 857 DILexicalBlockFile DBF = DILexicalBlockFile(Scope); 858 processLexicalBlock(DILexicalBlock(DBF.getScope())); 859 } 860 else if (Scope.isLexicalBlock()) 861 processLexicalBlock(DILexicalBlock(Scope)); 862 863 if (MDNode *IA = Loc.getInlinedAt(Ctx)) 864 processLocation(DILocation(IA)); 865 } 866 867 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) { 868 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { 869 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i))); 870 if (addGlobalVariable(DIG)) { 871 if (DIG.getVersion() <= LLVMDebugVersion10) 872 addCompileUnit(DIG.getCompileUnit()); 873 processType(DIG.getType()); 874 } 875 } 876 } 877 878 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) 879 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 880 processSubprogram(DISubprogram(NMD->getOperand(i))); 881} 882 883/// processLocation - Process DILocation. 884void DebugInfoFinder::processLocation(DILocation Loc) { 885 if (!Loc.Verify()) return; 886 DIDescriptor S(Loc.getScope()); 887 if (S.isCompileUnit()) 888 addCompileUnit(DICompileUnit(S)); 889 else if (S.isSubprogram()) 890 processSubprogram(DISubprogram(S)); 891 else if (S.isLexicalBlock()) 892 processLexicalBlock(DILexicalBlock(S)); 893 else if (S.isLexicalBlockFile()) { 894 DILexicalBlockFile DBF = DILexicalBlockFile(S); 895 processLexicalBlock(DILexicalBlock(DBF.getScope())); 896 } 897 processLocation(Loc.getOrigLocation()); 898} 899 900/// processType - Process DIType. 901void DebugInfoFinder::processType(DIType DT) { 902 if (!addType(DT)) 903 return; 904 if (DT.getVersion() <= LLVMDebugVersion10) 905 addCompileUnit(DT.getCompileUnit()); 906 if (DT.isCompositeType()) { 907 DICompositeType DCT(DT); 908 processType(DCT.getTypeDerivedFrom()); 909 DIArray DA = DCT.getTypeArray(); 910 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) { 911 DIDescriptor D = DA.getElement(i); 912 if (D.isType()) 913 processType(DIType(D)); 914 else if (D.isSubprogram()) 915 processSubprogram(DISubprogram(D)); 916 } 917 } else if (DT.isDerivedType()) { 918 DIDerivedType DDT(DT); 919 processType(DDT.getTypeDerivedFrom()); 920 } 921} 922 923/// processLexicalBlock 924void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) { 925 DIScope Context = LB.getContext(); 926 if (Context.isLexicalBlock()) 927 return processLexicalBlock(DILexicalBlock(Context)); 928 else if (Context.isLexicalBlockFile()) { 929 DILexicalBlockFile DBF = DILexicalBlockFile(Context); 930 return processLexicalBlock(DILexicalBlock(DBF.getScope())); 931 } 932 else 933 return processSubprogram(DISubprogram(Context)); 934} 935 936/// processSubprogram - Process DISubprogram. 937void DebugInfoFinder::processSubprogram(DISubprogram SP) { 938 if (!addSubprogram(SP)) 939 return; 940 if (SP.getVersion() <= LLVMDebugVersion10) 941 addCompileUnit(SP.getCompileUnit()); 942 processType(SP.getType()); 943} 944 945/// processDeclare - Process DbgDeclareInst. 946void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) { 947 MDNode *N = dyn_cast<MDNode>(DDI->getVariable()); 948 if (!N) return; 949 950 DIDescriptor DV(N); 951 if (!DV.isVariable()) 952 return; 953 954 if (!NodesSeen.insert(DV)) 955 return; 956 if (DIVariable(N).getVersion() <= LLVMDebugVersion10) 957 addCompileUnit(DIVariable(N).getCompileUnit()); 958 processType(DIVariable(N).getType()); 959} 960 961/// addType - Add type into Tys. 962bool DebugInfoFinder::addType(DIType DT) { 963 if (!DT.isValid()) 964 return false; 965 966 if (!NodesSeen.insert(DT)) 967 return false; 968 969 TYs.push_back(DT); 970 return true; 971} 972 973/// addCompileUnit - Add compile unit into CUs. 974bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) { 975 if (!CU.Verify()) 976 return false; 977 978 if (!NodesSeen.insert(CU)) 979 return false; 980 981 CUs.push_back(CU); 982 return true; 983} 984 985/// addGlobalVariable - Add global variable into GVs. 986bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) { 987 if (!DIDescriptor(DIG).isGlobalVariable()) 988 return false; 989 990 if (!NodesSeen.insert(DIG)) 991 return false; 992 993 GVs.push_back(DIG); 994 return true; 995} 996 997// addSubprogram - Add subprgoram into SPs. 998bool DebugInfoFinder::addSubprogram(DISubprogram SP) { 999 if (!DIDescriptor(SP).isSubprogram()) 1000 return false; 1001 1002 if (!NodesSeen.insert(SP)) 1003 return false; 1004 1005 SPs.push_back(SP); 1006 return true; 1007} 1008 1009//===----------------------------------------------------------------------===// 1010// DIDescriptor: dump routines for all descriptors. 1011//===----------------------------------------------------------------------===// 1012 1013/// dump - Print descriptor to dbgs() with a newline. 1014void DIDescriptor::dump() const { 1015 print(dbgs()); dbgs() << '\n'; 1016} 1017 1018/// print - Print descriptor. 1019void DIDescriptor::print(raw_ostream &OS) const { 1020 if (!DbgNode) return; 1021 1022 if (const char *Tag = dwarf::TagString(getTag())) 1023 OS << "[ " << Tag << " ]"; 1024 1025 if (this->isSubrange()) { 1026 DISubrange(DbgNode).printInternal(OS); 1027 } else if (this->isCompileUnit()) { 1028 DICompileUnit(DbgNode).printInternal(OS); 1029 } else if (this->isFile()) { 1030 DIFile(DbgNode).printInternal(OS); 1031 } else if (this->isEnumerator()) { 1032 DIEnumerator(DbgNode).printInternal(OS); 1033 } else if (this->isBasicType()) { 1034 DIType(DbgNode).printInternal(OS); 1035 } else if (this->isDerivedType()) { 1036 DIDerivedType(DbgNode).printInternal(OS); 1037 } else if (this->isCompositeType()) { 1038 DICompositeType(DbgNode).printInternal(OS); 1039 } else if (this->isSubprogram()) { 1040 DISubprogram(DbgNode).printInternal(OS); 1041 } else if (this->isGlobalVariable()) { 1042 DIGlobalVariable(DbgNode).printInternal(OS); 1043 } else if (this->isVariable()) { 1044 DIVariable(DbgNode).printInternal(OS); 1045 } else if (this->isObjCProperty()) { 1046 DIObjCProperty(DbgNode).printInternal(OS); 1047 } else if (this->isScope()) { 1048 DIScope(DbgNode).printInternal(OS); 1049 } 1050} 1051 1052void DISubrange::printInternal(raw_ostream &OS) const { 1053 int64_t Count = getCount(); 1054 if (Count != -1) 1055 OS << " [" << getLo() << ", " << Count - 1 << ']'; 1056 else 1057 OS << " [unbounded]"; 1058} 1059 1060void DIScope::printInternal(raw_ostream &OS) const { 1061 OS << " [" << getDirectory() << "/" << getFilename() << ']'; 1062} 1063 1064void DICompileUnit::printInternal(raw_ostream &OS) const { 1065 DIScope::printInternal(OS); 1066 if (unsigned Lang = getLanguage()) 1067 OS << " [" << dwarf::LanguageString(Lang) << ']'; 1068} 1069 1070void DIEnumerator::printInternal(raw_ostream &OS) const { 1071 OS << " [" << getName() << " :: " << getEnumValue() << ']'; 1072} 1073 1074void DIType::printInternal(raw_ostream &OS) const { 1075 if (!DbgNode) return; 1076 1077 StringRef Res = getName(); 1078 if (!Res.empty()) 1079 OS << " [" << Res << "]"; 1080 1081 // TODO: Print context? 1082 1083 OS << " [line " << getLineNumber() 1084 << ", size " << getSizeInBits() 1085 << ", align " << getAlignInBits() 1086 << ", offset " << getOffsetInBits(); 1087 if (isBasicType()) 1088 if (const char *Enc = 1089 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding())) 1090 OS << ", enc " << Enc; 1091 OS << "]"; 1092 1093 if (isPrivate()) 1094 OS << " [private]"; 1095 else if (isProtected()) 1096 OS << " [protected]"; 1097 1098 if (isArtificial()) 1099 OS << " [artificial]"; 1100 1101 if (isForwardDecl()) 1102 OS << " [fwd]"; 1103 if (isVector()) 1104 OS << " [vector]"; 1105 if (isStaticMember()) 1106 OS << " [static]"; 1107} 1108 1109void DIDerivedType::printInternal(raw_ostream &OS) const { 1110 DIType::printInternal(OS); 1111 OS << " [from " << getTypeDerivedFrom().getName() << ']'; 1112} 1113 1114void DICompositeType::printInternal(raw_ostream &OS) const { 1115 DIType::printInternal(OS); 1116 DIArray A = getTypeArray(); 1117 OS << " [" << A.getNumElements() << " elements]"; 1118} 1119 1120void DISubprogram::printInternal(raw_ostream &OS) const { 1121 // TODO : Print context 1122 OS << " [line " << getLineNumber() << ']'; 1123 1124 if (isLocalToUnit()) 1125 OS << " [local]"; 1126 1127 if (isDefinition()) 1128 OS << " [def]"; 1129 1130 if (getScopeLineNumber() != getLineNumber()) 1131 OS << " [scope " << getScopeLineNumber() << "]"; 1132 1133 if (isPrivate()) 1134 OS << " [private]"; 1135 else if (isProtected()) 1136 OS << " [protected]"; 1137 1138 StringRef Res = getName(); 1139 if (!Res.empty()) 1140 OS << " [" << Res << ']'; 1141} 1142 1143void DIGlobalVariable::printInternal(raw_ostream &OS) const { 1144 StringRef Res = getName(); 1145 if (!Res.empty()) 1146 OS << " [" << Res << ']'; 1147 1148 OS << " [line " << getLineNumber() << ']'; 1149 1150 // TODO : Print context 1151 1152 if (isLocalToUnit()) 1153 OS << " [local]"; 1154 1155 if (isDefinition()) 1156 OS << " [def]"; 1157} 1158 1159void DIVariable::printInternal(raw_ostream &OS) const { 1160 StringRef Res = getName(); 1161 if (!Res.empty()) 1162 OS << " [" << Res << ']'; 1163 1164 OS << " [line " << getLineNumber() << ']'; 1165} 1166 1167void DIObjCProperty::printInternal(raw_ostream &OS) const { 1168 StringRef Name = getObjCPropertyName(); 1169 if (!Name.empty()) 1170 OS << " [" << Name << ']'; 1171 1172 OS << " [line " << getLineNumber() 1173 << ", properties " << getUnsignedField(6) << ']'; 1174} 1175 1176static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS, 1177 const LLVMContext &Ctx) { 1178 if (!DL.isUnknown()) { // Print source line info. 1179 DIScope Scope(DL.getScope(Ctx)); 1180 // Omit the directory, because it's likely to be long and uninteresting. 1181 if (Scope.Verify()) 1182 CommentOS << Scope.getFilename(); 1183 else 1184 CommentOS << "<unknown>"; 1185 CommentOS << ':' << DL.getLine(); 1186 if (DL.getCol() != 0) 1187 CommentOS << ':' << DL.getCol(); 1188 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx)); 1189 if (!InlinedAtDL.isUnknown()) { 1190 CommentOS << " @[ "; 1191 printDebugLoc(InlinedAtDL, CommentOS, Ctx); 1192 CommentOS << " ]"; 1193 } 1194 } 1195} 1196 1197void DIVariable::printExtendedName(raw_ostream &OS) const { 1198 const LLVMContext &Ctx = DbgNode->getContext(); 1199 StringRef Res = getName(); 1200 if (!Res.empty()) 1201 OS << Res << "," << getLineNumber(); 1202 if (MDNode *InlinedAt = getInlinedAt()) { 1203 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt); 1204 if (!InlinedAtDL.isUnknown()) { 1205 OS << " @["; 1206 printDebugLoc(InlinedAtDL, OS, Ctx); 1207 OS << "]"; 1208 } 1209 } 1210} 1211