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