1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the helper classes used to build and interpret debug 11 // information in LLVM IR form. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/IR/DebugInfo.h" 16 #include "LLVMContextImpl.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SmallPtrSet.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/IR/Constants.h" 21 #include "llvm/IR/DIBuilder.h" 22 #include "llvm/IR/DerivedTypes.h" 23 #include "llvm/IR/Instructions.h" 24 #include "llvm/IR/IntrinsicInst.h" 25 #include "llvm/IR/Intrinsics.h" 26 #include "llvm/IR/GVMaterializer.h" 27 #include "llvm/IR/Module.h" 28 #include "llvm/IR/ValueHandle.h" 29 #include "llvm/Support/Debug.h" 30 #include "llvm/Support/Dwarf.h" 31 #include "llvm/Support/raw_ostream.h" 32 using namespace llvm; 33 using namespace llvm::dwarf; 34 35 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) { 36 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope)) 37 return LocalScope->getSubprogram(); 38 return nullptr; 39 } 40 41 DISubprogram *llvm::getDISubprogram(const Function *F) { 42 // We look for the first instr that has a debug annotation leading back to F. 43 for (auto &BB : *F) { 44 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) { 45 return Inst.getDebugLoc(); 46 }); 47 if (Inst == BB.end()) 48 continue; 49 DebugLoc DLoc = Inst->getDebugLoc(); 50 const MDNode *Scope = DLoc.getInlinedAtScope(); 51 auto *Subprogram = getDISubprogram(Scope); 52 return Subprogram->describes(F) ? Subprogram : nullptr; 53 } 54 55 return nullptr; 56 } 57 58 DITypeIdentifierMap 59 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { 60 DITypeIdentifierMap Map; 61 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) { 62 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(CUi)); 63 DINodeArray Retain = CU->getRetainedTypes(); 64 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) { 65 if (!isa<DICompositeType>(Retain[Ti])) 66 continue; 67 auto *Ty = cast<DICompositeType>(Retain[Ti]); 68 if (MDString *TypeId = Ty->getRawIdentifier()) { 69 // Definition has priority over declaration. 70 // Try to insert (TypeId, Ty) to Map. 71 std::pair<DITypeIdentifierMap::iterator, bool> P = 72 Map.insert(std::make_pair(TypeId, Ty)); 73 // If TypeId already exists in Map and this is a definition, replace 74 // whatever we had (declaration or definition) with the definition. 75 if (!P.second && !Ty->isForwardDecl()) 76 P.first->second = Ty; 77 } 78 } 79 } 80 return Map; 81 } 82 83 //===----------------------------------------------------------------------===// 84 // DebugInfoFinder implementations. 85 //===----------------------------------------------------------------------===// 86 87 void DebugInfoFinder::reset() { 88 CUs.clear(); 89 SPs.clear(); 90 GVs.clear(); 91 TYs.clear(); 92 Scopes.clear(); 93 NodesSeen.clear(); 94 TypeIdentifierMap.clear(); 95 TypeMapInitialized = false; 96 } 97 98 void DebugInfoFinder::InitializeTypeMap(const Module &M) { 99 if (!TypeMapInitialized) 100 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { 101 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); 102 TypeMapInitialized = true; 103 } 104 } 105 106 void DebugInfoFinder::processModule(const Module &M) { 107 InitializeTypeMap(M); 108 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { 109 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { 110 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i)); 111 addCompileUnit(CU); 112 for (auto *DIG : CU->getGlobalVariables()) { 113 if (addGlobalVariable(DIG)) { 114 processScope(DIG->getScope()); 115 processType(DIG->getType().resolve(TypeIdentifierMap)); 116 } 117 } 118 for (auto *SP : CU->getSubprograms()) 119 processSubprogram(SP); 120 for (auto *ET : CU->getEnumTypes()) 121 processType(ET); 122 for (auto *RT : CU->getRetainedTypes()) 123 processType(RT); 124 for (auto *Import : CU->getImportedEntities()) { 125 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap); 126 if (auto *T = dyn_cast<DIType>(Entity)) 127 processType(T); 128 else if (auto *SP = dyn_cast<DISubprogram>(Entity)) 129 processSubprogram(SP); 130 else if (auto *NS = dyn_cast<DINamespace>(Entity)) 131 processScope(NS->getScope()); 132 else if (auto *M = dyn_cast<DIModule>(Entity)) 133 processScope(M->getScope()); 134 } 135 } 136 } 137 } 138 139 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) { 140 if (!Loc) 141 return; 142 InitializeTypeMap(M); 143 processScope(Loc->getScope()); 144 processLocation(M, Loc->getInlinedAt()); 145 } 146 147 void DebugInfoFinder::processType(DIType *DT) { 148 if (!addType(DT)) 149 return; 150 processScope(DT->getScope().resolve(TypeIdentifierMap)); 151 if (auto *ST = dyn_cast<DISubroutineType>(DT)) { 152 for (DITypeRef Ref : ST->getTypeArray()) 153 processType(Ref.resolve(TypeIdentifierMap)); 154 return; 155 } 156 if (auto *DCT = dyn_cast<DICompositeType>(DT)) { 157 processType(DCT->getBaseType().resolve(TypeIdentifierMap)); 158 for (Metadata *D : DCT->getElements()) { 159 if (auto *T = dyn_cast<DIType>(D)) 160 processType(T); 161 else if (auto *SP = dyn_cast<DISubprogram>(D)) 162 processSubprogram(SP); 163 } 164 return; 165 } 166 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) { 167 processType(DDT->getBaseType().resolve(TypeIdentifierMap)); 168 } 169 } 170 171 void DebugInfoFinder::processScope(DIScope *Scope) { 172 if (!Scope) 173 return; 174 if (auto *Ty = dyn_cast<DIType>(Scope)) { 175 processType(Ty); 176 return; 177 } 178 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) { 179 addCompileUnit(CU); 180 return; 181 } 182 if (auto *SP = dyn_cast<DISubprogram>(Scope)) { 183 processSubprogram(SP); 184 return; 185 } 186 if (!addScope(Scope)) 187 return; 188 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) { 189 processScope(LB->getScope()); 190 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) { 191 processScope(NS->getScope()); 192 } else if (auto *M = dyn_cast<DIModule>(Scope)) { 193 processScope(M->getScope()); 194 } 195 } 196 197 void DebugInfoFinder::processSubprogram(DISubprogram *SP) { 198 if (!addSubprogram(SP)) 199 return; 200 processScope(SP->getScope().resolve(TypeIdentifierMap)); 201 processType(SP->getType()); 202 for (auto *Element : SP->getTemplateParams()) { 203 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) { 204 processType(TType->getType().resolve(TypeIdentifierMap)); 205 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) { 206 processType(TVal->getType().resolve(TypeIdentifierMap)); 207 } 208 } 209 } 210 211 void DebugInfoFinder::processDeclare(const Module &M, 212 const DbgDeclareInst *DDI) { 213 auto *N = dyn_cast<MDNode>(DDI->getVariable()); 214 if (!N) 215 return; 216 InitializeTypeMap(M); 217 218 auto *DV = dyn_cast<DILocalVariable>(N); 219 if (!DV) 220 return; 221 222 if (!NodesSeen.insert(DV).second) 223 return; 224 processScope(DV->getScope()); 225 processType(DV->getType().resolve(TypeIdentifierMap)); 226 } 227 228 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { 229 auto *N = dyn_cast<MDNode>(DVI->getVariable()); 230 if (!N) 231 return; 232 InitializeTypeMap(M); 233 234 auto *DV = dyn_cast<DILocalVariable>(N); 235 if (!DV) 236 return; 237 238 if (!NodesSeen.insert(DV).second) 239 return; 240 processScope(DV->getScope()); 241 processType(DV->getType().resolve(TypeIdentifierMap)); 242 } 243 244 bool DebugInfoFinder::addType(DIType *DT) { 245 if (!DT) 246 return false; 247 248 if (!NodesSeen.insert(DT).second) 249 return false; 250 251 TYs.push_back(const_cast<DIType *>(DT)); 252 return true; 253 } 254 255 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) { 256 if (!CU) 257 return false; 258 if (!NodesSeen.insert(CU).second) 259 return false; 260 261 CUs.push_back(CU); 262 return true; 263 } 264 265 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) { 266 if (!DIG) 267 return false; 268 269 if (!NodesSeen.insert(DIG).second) 270 return false; 271 272 GVs.push_back(DIG); 273 return true; 274 } 275 276 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) { 277 if (!SP) 278 return false; 279 280 if (!NodesSeen.insert(SP).second) 281 return false; 282 283 SPs.push_back(SP); 284 return true; 285 } 286 287 bool DebugInfoFinder::addScope(DIScope *Scope) { 288 if (!Scope) 289 return false; 290 // FIXME: Ocaml binding generates a scope with no content, we treat it 291 // as null for now. 292 if (Scope->getNumOperands() == 0) 293 return false; 294 if (!NodesSeen.insert(Scope).second) 295 return false; 296 Scopes.push_back(Scope); 297 return true; 298 } 299 300 bool llvm::stripDebugInfo(Function &F) { 301 bool Changed = false; 302 if (F.getSubprogram()) { 303 Changed = true; 304 F.setSubprogram(nullptr); 305 } 306 for (BasicBlock &BB : F) { 307 for (Instruction &I : BB) { 308 if (I.getDebugLoc()) { 309 Changed = true; 310 I.setDebugLoc(DebugLoc()); 311 } 312 } 313 } 314 return Changed; 315 } 316 317 bool llvm::StripDebugInfo(Module &M) { 318 bool Changed = false; 319 320 // Remove all of the calls to the debugger intrinsics, and remove them from 321 // the module. 322 if (Function *Declare = M.getFunction("llvm.dbg.declare")) { 323 while (!Declare->use_empty()) { 324 CallInst *CI = cast<CallInst>(Declare->user_back()); 325 CI->eraseFromParent(); 326 } 327 Declare->eraseFromParent(); 328 Changed = true; 329 } 330 331 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { 332 while (!DbgVal->use_empty()) { 333 CallInst *CI = cast<CallInst>(DbgVal->user_back()); 334 CI->eraseFromParent(); 335 } 336 DbgVal->eraseFromParent(); 337 Changed = true; 338 } 339 340 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), 341 NME = M.named_metadata_end(); NMI != NME;) { 342 NamedMDNode *NMD = &*NMI; 343 ++NMI; 344 if (NMD->getName().startswith("llvm.dbg.")) { 345 NMD->eraseFromParent(); 346 Changed = true; 347 } 348 } 349 350 for (Function &F : M) 351 Changed |= stripDebugInfo(F); 352 353 if (GVMaterializer *Materializer = M.getMaterializer()) 354 Materializer->setStripDebugInfo(); 355 356 return Changed; 357 } 358 359 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { 360 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( 361 M.getModuleFlag("Debug Info Version"))) 362 return Val->getZExtValue(); 363 return 0; 364 } 365