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