1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===// 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 defines a bunch of datatypes that are useful for creating and 11 // walking debug info in LLVM IR form. They essentially provide wrappers around 12 // the information in the global variables that's needed when constructing the 13 // DWARF information. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #ifndef LLVM_IR_DEBUGINFO_H 18 #define LLVM_IR_DEBUGINFO_H 19 20 #include "llvm/ADT/DenseMap.h" 21 #include "llvm/ADT/SmallPtrSet.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/ADT/StringRef.h" 24 #include "llvm/ADT/iterator_range.h" 25 #include "llvm/IR/DebugInfoMetadata.h" 26 #include "llvm/Support/Casting.h" 27 #include "llvm/Support/Dwarf.h" 28 #include "llvm/Support/ErrorHandling.h" 29 #include <iterator> 30 31 namespace llvm { 32 class BasicBlock; 33 class Constant; 34 class Function; 35 class GlobalVariable; 36 class Module; 37 class Type; 38 class Value; 39 class DbgDeclareInst; 40 class DbgValueInst; 41 class Instruction; 42 class Metadata; 43 class MDNode; 44 class MDString; 45 class NamedMDNode; 46 class LLVMContext; 47 class raw_ostream; 48 49 class DIFile; 50 class DISubprogram; 51 class DILexicalBlock; 52 class DILexicalBlockFile; 53 class DIVariable; 54 class DIType; 55 class DIScope; 56 class DIObjCProperty; 57 58 /// \brief Maps from type identifier to the actual MDNode. 59 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap; 60 61 class DIDescriptor { 62 MDNode *N; 63 64 public: N(const_cast<MDNode * > (N))65 DIDescriptor(const MDNode *N = nullptr) : N(const_cast<MDNode *>(N)) {} 66 67 operator MDNode *() const { return N; } 68 MDNode *operator->() const { return N; } 69 MDNode &operator*() const { return *N; } 70 }; 71 72 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC) \ 73 class DESC; \ 74 template <> struct simplify_type<const DESC>; \ 75 template <> struct simplify_type<DESC>; 76 DECLARE_SIMPLIFY_DESCRIPTOR(DIDescriptor) 77 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange) 78 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator) 79 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope) 80 DECLARE_SIMPLIFY_DESCRIPTOR(DIType) 81 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType) 82 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType) 83 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType) 84 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType) 85 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile) 86 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit) 87 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram) 88 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock) 89 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile) 90 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace) 91 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter) 92 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter) 93 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable) 94 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable) 95 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression) 96 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation) 97 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty) 98 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity) 99 #undef DECLARE_SIMPLIFY_DESCRIPTOR 100 101 typedef DebugNodeArray DIArray; 102 typedef MDTypeRefArray DITypeArray; 103 typedef DebugNodeRef DIDescriptorRef; 104 typedef MDScopeRef DIScopeRef; 105 typedef MDTypeRef DITypeRef; 106 107 class DISubrange { 108 MDSubrange *N; 109 110 public: N(const_cast<MDSubrange * > (N))111 DISubrange(const MDSubrange *N = nullptr) : N(const_cast<MDSubrange *>(N)) {} 112 113 operator MDSubrange *() const { return N; } 114 MDSubrange *operator->() const { return N; } 115 MDSubrange &operator*() const { return *N; } 116 }; 117 118 class DIEnumerator { 119 MDEnumerator *N; 120 121 public: 122 DIEnumerator(const MDEnumerator *N = nullptr) N(const_cast<MDEnumerator * > (N))123 : N(const_cast<MDEnumerator *>(N)) {} 124 125 operator MDEnumerator *() const { return N; } 126 MDEnumerator *operator->() const { return N; } 127 MDEnumerator &operator*() const { return *N; } 128 }; 129 130 class DIScope { 131 MDScope *N; 132 133 public: N(const_cast<MDScope * > (N))134 DIScope(const MDScope *N = nullptr) : N(const_cast<MDScope *>(N)) {} 135 DIDescriptor()136 operator DIDescriptor() const { return N; } 137 operator MDScope *() const { return N; } 138 MDScope *operator->() const { return N; } 139 MDScope &operator*() const { return *N; } 140 }; 141 142 class DIType { 143 MDType *N; 144 145 public: N(const_cast<MDType * > (N))146 DIType(const MDType *N = nullptr) : N(const_cast<MDType *>(N)) {} 147 DIDescriptor()148 operator DIDescriptor() const { return N; } DIScope()149 operator DIScope() const { return N; } 150 operator MDType *() const { return N; } 151 MDType *operator->() const { return N; } 152 MDType &operator*() const { return *N; } 153 }; 154 155 class DIBasicType { 156 MDBasicType *N; 157 158 public: 159 DIBasicType(const MDBasicType *N = nullptr) N(const_cast<MDBasicType * > (N))160 : N(const_cast<MDBasicType *>(N)) {} 161 DIDescriptor()162 operator DIDescriptor() const { return N; } DIType()163 operator DIType() const { return N; } 164 operator MDBasicType *() const { return N; } 165 MDBasicType *operator->() const { return N; } 166 MDBasicType &operator*() const { return *N; } 167 }; 168 169 class DIDerivedType { 170 MDDerivedTypeBase *N; 171 172 public: 173 DIDerivedType(const MDDerivedTypeBase *N = nullptr) N(const_cast<MDDerivedTypeBase * > (N))174 : N(const_cast<MDDerivedTypeBase *>(N)) {} 175 DIDescriptor()176 operator DIDescriptor() const { return N; } DIType()177 operator DIType() const { return N; } 178 operator MDDerivedTypeBase *() const { return N; } 179 MDDerivedTypeBase *operator->() const { return N; } 180 MDDerivedTypeBase &operator*() const { return *N; } 181 }; 182 183 class DICompositeType { 184 MDCompositeTypeBase *N; 185 186 public: 187 DICompositeType(const MDCompositeTypeBase *N = nullptr) N(const_cast<MDCompositeTypeBase * > (N))188 : N(const_cast<MDCompositeTypeBase *>(N)) {} 189 DIDescriptor()190 operator DIDescriptor() const { return N; } DIType()191 operator DIType() const { return N; } 192 operator MDCompositeTypeBase *() const { return N; } 193 MDCompositeTypeBase *operator->() const { return N; } 194 MDCompositeTypeBase &operator*() const { return *N; } 195 }; 196 197 class DISubroutineType { 198 MDSubroutineType *N; 199 200 public: 201 DISubroutineType(const MDSubroutineType *N = nullptr) N(const_cast<MDSubroutineType * > (N))202 : N(const_cast<MDSubroutineType *>(N)) {} 203 DIDescriptor()204 operator DIDescriptor() const { return N; } DIType()205 operator DIType() const { return N; } DICompositeType()206 operator DICompositeType() const { return N; } 207 operator MDSubroutineType *() const { return N; } 208 MDSubroutineType *operator->() const { return N; } 209 MDSubroutineType &operator*() const { return *N; } 210 }; 211 212 class DIFile { 213 MDFile *N; 214 215 public: N(const_cast<MDFile * > (N))216 DIFile(const MDFile *N = nullptr) : N(const_cast<MDFile *>(N)) {} 217 DIDescriptor()218 operator DIDescriptor() const { return N; } DIScope()219 operator DIScope() const { return N; } 220 operator MDFile *() const { return N; } 221 MDFile *operator->() const { return N; } 222 MDFile &operator*() const { return *N; } 223 }; 224 225 class DICompileUnit { 226 MDCompileUnit *N; 227 228 public: 229 DICompileUnit(const MDCompileUnit *N = nullptr) N(const_cast<MDCompileUnit * > (N))230 : N(const_cast<MDCompileUnit *>(N)) {} 231 DIDescriptor()232 operator DIDescriptor() const { return N; } DIScope()233 operator DIScope() const { return N; } 234 operator MDCompileUnit *() const { return N; } 235 MDCompileUnit *operator->() const { return N; } 236 MDCompileUnit &operator*() const { return *N; } 237 }; 238 239 class DISubprogram { 240 MDSubprogram *N; 241 242 public: 243 DISubprogram(const MDSubprogram *N = nullptr) N(const_cast<MDSubprogram * > (N))244 : N(const_cast<MDSubprogram *>(N)) {} 245 DIDescriptor()246 operator DIDescriptor() const { return N; } DIScope()247 operator DIScope() const { return N; } 248 operator MDSubprogram *() const { return N; } 249 MDSubprogram *operator->() const { return N; } 250 MDSubprogram &operator*() const { return *N; } 251 }; 252 253 class DILexicalBlock { 254 MDLexicalBlockBase *N; 255 256 public: 257 DILexicalBlock(const MDLexicalBlockBase *N = nullptr) N(const_cast<MDLexicalBlockBase * > (N))258 : N(const_cast<MDLexicalBlockBase *>(N)) {} 259 DIDescriptor()260 operator DIDescriptor() const { return N; } 261 operator MDLexicalBlockBase *() const { return N; } 262 MDLexicalBlockBase *operator->() const { return N; } 263 MDLexicalBlockBase &operator*() const { return *N; } 264 }; 265 266 class DILexicalBlockFile { 267 MDLexicalBlockFile *N; 268 269 public: 270 DILexicalBlockFile(const MDLexicalBlockFile *N = nullptr) N(const_cast<MDLexicalBlockFile * > (N))271 : N(const_cast<MDLexicalBlockFile *>(N)) {} 272 DIDescriptor()273 operator DIDescriptor() const { return N; } 274 operator MDLexicalBlockFile *() const { return N; } 275 MDLexicalBlockFile *operator->() const { return N; } 276 MDLexicalBlockFile &operator*() const { return *N; } 277 }; 278 279 class DINameSpace { 280 MDNamespace *N; 281 282 public: 283 DINameSpace(const MDNamespace *N = nullptr) N(const_cast<MDNamespace * > (N))284 : N(const_cast<MDNamespace *>(N)) {} 285 DIDescriptor()286 operator DIDescriptor() const { return N; } DIScope()287 operator DIScope() const { return N; } 288 operator MDNamespace *() const { return N; } 289 MDNamespace *operator->() const { return N; } 290 MDNamespace &operator*() const { return *N; } 291 }; 292 293 class DITemplateTypeParameter { 294 MDTemplateTypeParameter *N; 295 296 public: 297 DITemplateTypeParameter(const MDTemplateTypeParameter *N = nullptr) N(const_cast<MDTemplateTypeParameter * > (N))298 : N(const_cast<MDTemplateTypeParameter *>(N)) {} 299 300 operator MDTemplateTypeParameter *() const { return N; } 301 MDTemplateTypeParameter *operator->() const { return N; } 302 MDTemplateTypeParameter &operator*() const { return *N; } 303 }; 304 305 class DITemplateValueParameter { 306 MDTemplateValueParameter *N; 307 308 public: 309 DITemplateValueParameter(const MDTemplateValueParameter *N = nullptr) N(const_cast<MDTemplateValueParameter * > (N))310 : N(const_cast<MDTemplateValueParameter *>(N)) {} 311 312 operator MDTemplateValueParameter *() const { return N; } 313 MDTemplateValueParameter *operator->() const { return N; } 314 MDTemplateValueParameter &operator*() const { return *N; } 315 }; 316 317 class DIGlobalVariable { 318 MDGlobalVariable *N; 319 320 public: 321 DIGlobalVariable(const MDGlobalVariable *N = nullptr) N(const_cast<MDGlobalVariable * > (N))322 : N(const_cast<MDGlobalVariable *>(N)) {} 323 DIDescriptor()324 operator DIDescriptor() const { return N; } 325 operator MDGlobalVariable *() const { return N; } 326 MDGlobalVariable *operator->() const { return N; } 327 MDGlobalVariable &operator*() const { return *N; } 328 }; 329 330 class DIVariable { 331 MDLocalVariable *N; 332 333 public: 334 DIVariable(const MDLocalVariable *N = nullptr) N(const_cast<MDLocalVariable * > (N))335 : N(const_cast<MDLocalVariable *>(N)) {} 336 337 operator MDLocalVariable *() const { return N; } 338 MDLocalVariable *operator->() const { return N; } 339 MDLocalVariable &operator*() const { return *N; } 340 }; 341 342 class DIExpression { 343 MDExpression *N; 344 345 public: 346 DIExpression(const MDExpression *N = nullptr) N(const_cast<MDExpression * > (N))347 : N(const_cast<MDExpression *>(N)) {} 348 349 operator MDExpression *() const { return N; } 350 MDExpression *operator->() const { return N; } 351 MDExpression &operator*() const { return *N; } 352 }; 353 354 class DILocation { 355 MDLocation *N; 356 357 public: N(const_cast<MDLocation * > (N))358 DILocation(const MDLocation *N = nullptr) : N(const_cast<MDLocation *>(N)) {} 359 360 operator MDLocation *() const { return N; } 361 MDLocation *operator->() const { return N; } 362 MDLocation &operator*() const { return *N; } 363 }; 364 365 class DIObjCProperty { 366 MDObjCProperty *N; 367 368 public: 369 DIObjCProperty(const MDObjCProperty *N = nullptr) N(const_cast<MDObjCProperty * > (N))370 : N(const_cast<MDObjCProperty *>(N)) {} 371 372 operator MDObjCProperty *() const { return N; } 373 MDObjCProperty *operator->() const { return N; } 374 MDObjCProperty &operator*() const { return *N; } 375 }; 376 377 class DIImportedEntity { 378 MDImportedEntity *N; 379 380 public: 381 DIImportedEntity(const MDImportedEntity *N = nullptr) N(const_cast<MDImportedEntity * > (N))382 : N(const_cast<MDImportedEntity *>(N)) {} 383 DIDescriptor()384 operator DIDescriptor() const { return N; } 385 operator MDImportedEntity *() const { return N; } 386 MDImportedEntity *operator->() const { return N; } 387 MDImportedEntity &operator*() const { return *N; } 388 }; 389 390 #define SIMPLIFY_DESCRIPTOR(DESC) \ 391 template <> struct simplify_type<const DESC> { \ 392 typedef Metadata *SimpleType; \ 393 static SimpleType getSimplifiedValue(const DESC &DI) { return DI; } \ 394 }; \ 395 template <> struct simplify_type<DESC> : simplify_type<const DESC> {}; 396 SIMPLIFY_DESCRIPTOR(DIDescriptor) 397 SIMPLIFY_DESCRIPTOR(DISubrange) 398 SIMPLIFY_DESCRIPTOR(DIEnumerator) 399 SIMPLIFY_DESCRIPTOR(DIScope) 400 SIMPLIFY_DESCRIPTOR(DIType) 401 SIMPLIFY_DESCRIPTOR(DIBasicType) 402 SIMPLIFY_DESCRIPTOR(DIDerivedType) 403 SIMPLIFY_DESCRIPTOR(DICompositeType) 404 SIMPLIFY_DESCRIPTOR(DISubroutineType) 405 SIMPLIFY_DESCRIPTOR(DIFile) 406 SIMPLIFY_DESCRIPTOR(DICompileUnit) 407 SIMPLIFY_DESCRIPTOR(DISubprogram) 408 SIMPLIFY_DESCRIPTOR(DILexicalBlock) 409 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile) 410 SIMPLIFY_DESCRIPTOR(DINameSpace) 411 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter) 412 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter) 413 SIMPLIFY_DESCRIPTOR(DIGlobalVariable) 414 SIMPLIFY_DESCRIPTOR(DIVariable) 415 SIMPLIFY_DESCRIPTOR(DIExpression) 416 SIMPLIFY_DESCRIPTOR(DILocation) 417 SIMPLIFY_DESCRIPTOR(DIObjCProperty) 418 SIMPLIFY_DESCRIPTOR(DIImportedEntity) 419 #undef SIMPLIFY_DESCRIPTOR 420 421 /// \brief Find subprogram that is enclosing this scope. 422 DISubprogram getDISubprogram(const MDNode *Scope); 423 424 /// \brief Find debug info for a given function. 425 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty 426 /// DISubprogram. 427 DISubprogram getDISubprogram(const Function *F); 428 429 /// \brief Find underlying composite type. 430 DICompositeType getDICompositeType(DIType T); 431 432 /// \brief Generate map by visiting all retained types. 433 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes); 434 435 /// \brief Strip debug info in the module if it exists. 436 /// 437 /// To do this, we remove all calls to the debugger intrinsics and any named 438 /// metadata for debugging. We also remove debug locations for instructions. 439 /// Return true if module is modified. 440 bool StripDebugInfo(Module &M); 441 bool stripDebugInfo(Function &F); 442 443 /// \brief Return Debug Info Metadata Version by checking module flags. 444 unsigned getDebugMetadataVersionFromModule(const Module &M); 445 446 /// \brief Utility to find all debug info in a module. 447 /// 448 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To 449 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses 450 /// processDeclare, processValue and processLocation to handle DbgDeclareInst, 451 /// DbgValueInst and DbgLoc attached to instructions. processModule will go 452 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes 453 /// used by the CUs. 454 class DebugInfoFinder { 455 public: DebugInfoFinder()456 DebugInfoFinder() : TypeMapInitialized(false) {} 457 458 /// \brief Process entire module and collect debug info anchors. 459 void processModule(const Module &M); 460 461 /// \brief Process DbgDeclareInst. 462 void processDeclare(const Module &M, const DbgDeclareInst *DDI); 463 /// \brief Process DbgValueInst. 464 void processValue(const Module &M, const DbgValueInst *DVI); 465 /// \brief Process DILocation. 466 void processLocation(const Module &M, DILocation Loc); 467 468 /// \brief Clear all lists. 469 void reset(); 470 471 private: 472 void InitializeTypeMap(const Module &M); 473 474 void processType(DIType DT); 475 void processSubprogram(DISubprogram SP); 476 void processScope(DIScope Scope); 477 bool addCompileUnit(DICompileUnit CU); 478 bool addGlobalVariable(DIGlobalVariable DIG); 479 bool addSubprogram(DISubprogram SP); 480 bool addType(DIType DT); 481 bool addScope(DIScope Scope); 482 483 public: 484 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator; 485 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator; 486 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator 487 global_variable_iterator; 488 typedef SmallVectorImpl<DIType>::const_iterator type_iterator; 489 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator; 490 compile_units()491 iterator_range<compile_unit_iterator> compile_units() const { 492 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end()); 493 } 494 subprograms()495 iterator_range<subprogram_iterator> subprograms() const { 496 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end()); 497 } 498 global_variables()499 iterator_range<global_variable_iterator> global_variables() const { 500 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end()); 501 } 502 types()503 iterator_range<type_iterator> types() const { 504 return iterator_range<type_iterator>(TYs.begin(), TYs.end()); 505 } 506 scopes()507 iterator_range<scope_iterator> scopes() const { 508 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end()); 509 } 510 compile_unit_count()511 unsigned compile_unit_count() const { return CUs.size(); } global_variable_count()512 unsigned global_variable_count() const { return GVs.size(); } subprogram_count()513 unsigned subprogram_count() const { return SPs.size(); } type_count()514 unsigned type_count() const { return TYs.size(); } scope_count()515 unsigned scope_count() const { return Scopes.size(); } 516 517 private: 518 SmallVector<DICompileUnit, 8> CUs; 519 SmallVector<DISubprogram, 8> SPs; 520 SmallVector<DIGlobalVariable, 8> GVs; 521 SmallVector<DIType, 8> TYs; 522 SmallVector<DIScope, 8> Scopes; 523 SmallPtrSet<MDNode *, 64> NodesSeen; 524 DITypeIdentifierMap TypeIdentifierMap; 525 526 /// \brief Specify if TypeIdentifierMap is initialized. 527 bool TypeMapInitialized; 528 }; 529 530 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M); 531 532 } // end namespace llvm 533 534 #endif 535