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 DITypeIdentifierMap 42 llvm::generateDITypeIdentifierMap(const Module &M) { 43 DITypeIdentifierMap Map; 44 for (DICompileUnit *CU : M.debug_compile_units()) { 45 DINodeArray Retain = CU->getRetainedTypes(); 46 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) { 47 if (!isa<DICompositeType>(Retain[Ti])) 48 continue; 49 auto *Ty = cast<DICompositeType>(Retain[Ti]); 50 if (MDString *TypeId = Ty->getRawIdentifier()) { 51 // Definition has priority over declaration. 52 // Try to insert (TypeId, Ty) to Map. 53 std::pair<DITypeIdentifierMap::iterator, bool> P = 54 Map.insert(std::make_pair(TypeId, Ty)); 55 // If TypeId already exists in Map and this is a definition, replace 56 // whatever we had (declaration or definition) with the definition. 57 if (!P.second && !Ty->isForwardDecl()) 58 P.first->second = Ty; 59 } 60 } 61 } 62 return Map; 63 } 64 65 //===----------------------------------------------------------------------===// 66 // DebugInfoFinder implementations. 67 //===----------------------------------------------------------------------===// 68 69 void DebugInfoFinder::reset() { 70 CUs.clear(); 71 SPs.clear(); 72 GVs.clear(); 73 TYs.clear(); 74 Scopes.clear(); 75 NodesSeen.clear(); 76 TypeIdentifierMap.clear(); 77 TypeMapInitialized = false; 78 } 79 80 void DebugInfoFinder::InitializeTypeMap(const Module &M) { 81 if (TypeMapInitialized) 82 return; 83 TypeIdentifierMap = generateDITypeIdentifierMap(M); 84 TypeMapInitialized = true; 85 } 86 87 void DebugInfoFinder::processModule(const Module &M) { 88 InitializeTypeMap(M); 89 for (auto *CU : M.debug_compile_units()) { 90 addCompileUnit(CU); 91 for (auto *DIG : CU->getGlobalVariables()) { 92 if (addGlobalVariable(DIG)) { 93 processScope(DIG->getScope()); 94 processType(DIG->getType().resolve(TypeIdentifierMap)); 95 } 96 } 97 for (auto *SP : CU->getSubprograms()) 98 processSubprogram(SP); 99 for (auto *ET : CU->getEnumTypes()) 100 processType(ET); 101 for (auto *RT : CU->getRetainedTypes()) 102 processType(RT); 103 for (auto *Import : CU->getImportedEntities()) { 104 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap); 105 if (auto *T = dyn_cast<DIType>(Entity)) 106 processType(T); 107 else if (auto *SP = dyn_cast<DISubprogram>(Entity)) 108 processSubprogram(SP); 109 else if (auto *NS = dyn_cast<DINamespace>(Entity)) 110 processScope(NS->getScope()); 111 else if (auto *M = dyn_cast<DIModule>(Entity)) 112 processScope(M->getScope()); 113 } 114 } 115 } 116 117 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) { 118 if (!Loc) 119 return; 120 InitializeTypeMap(M); 121 processScope(Loc->getScope()); 122 processLocation(M, Loc->getInlinedAt()); 123 } 124 125 void DebugInfoFinder::processType(DIType *DT) { 126 if (!addType(DT)) 127 return; 128 processScope(DT->getScope().resolve(TypeIdentifierMap)); 129 if (auto *ST = dyn_cast<DISubroutineType>(DT)) { 130 for (DITypeRef Ref : ST->getTypeArray()) 131 processType(Ref.resolve(TypeIdentifierMap)); 132 return; 133 } 134 if (auto *DCT = dyn_cast<DICompositeType>(DT)) { 135 processType(DCT->getBaseType().resolve(TypeIdentifierMap)); 136 for (Metadata *D : DCT->getElements()) { 137 if (auto *T = dyn_cast<DIType>(D)) 138 processType(T); 139 else if (auto *SP = dyn_cast<DISubprogram>(D)) 140 processSubprogram(SP); 141 } 142 return; 143 } 144 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) { 145 processType(DDT->getBaseType().resolve(TypeIdentifierMap)); 146 } 147 } 148 149 void DebugInfoFinder::processScope(DIScope *Scope) { 150 if (!Scope) 151 return; 152 if (auto *Ty = dyn_cast<DIType>(Scope)) { 153 processType(Ty); 154 return; 155 } 156 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) { 157 addCompileUnit(CU); 158 return; 159 } 160 if (auto *SP = dyn_cast<DISubprogram>(Scope)) { 161 processSubprogram(SP); 162 return; 163 } 164 if (!addScope(Scope)) 165 return; 166 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) { 167 processScope(LB->getScope()); 168 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) { 169 processScope(NS->getScope()); 170 } else if (auto *M = dyn_cast<DIModule>(Scope)) { 171 processScope(M->getScope()); 172 } 173 } 174 175 void DebugInfoFinder::processSubprogram(DISubprogram *SP) { 176 if (!addSubprogram(SP)) 177 return; 178 processScope(SP->getScope().resolve(TypeIdentifierMap)); 179 processType(SP->getType()); 180 for (auto *Element : SP->getTemplateParams()) { 181 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) { 182 processType(TType->getType().resolve(TypeIdentifierMap)); 183 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) { 184 processType(TVal->getType().resolve(TypeIdentifierMap)); 185 } 186 } 187 } 188 189 void DebugInfoFinder::processDeclare(const Module &M, 190 const DbgDeclareInst *DDI) { 191 auto *N = dyn_cast<MDNode>(DDI->getVariable()); 192 if (!N) 193 return; 194 InitializeTypeMap(M); 195 196 auto *DV = dyn_cast<DILocalVariable>(N); 197 if (!DV) 198 return; 199 200 if (!NodesSeen.insert(DV).second) 201 return; 202 processScope(DV->getScope()); 203 processType(DV->getType().resolve(TypeIdentifierMap)); 204 } 205 206 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { 207 auto *N = dyn_cast<MDNode>(DVI->getVariable()); 208 if (!N) 209 return; 210 InitializeTypeMap(M); 211 212 auto *DV = dyn_cast<DILocalVariable>(N); 213 if (!DV) 214 return; 215 216 if (!NodesSeen.insert(DV).second) 217 return; 218 processScope(DV->getScope()); 219 processType(DV->getType().resolve(TypeIdentifierMap)); 220 } 221 222 bool DebugInfoFinder::addType(DIType *DT) { 223 if (!DT) 224 return false; 225 226 if (!NodesSeen.insert(DT).second) 227 return false; 228 229 TYs.push_back(const_cast<DIType *>(DT)); 230 return true; 231 } 232 233 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) { 234 if (!CU) 235 return false; 236 if (!NodesSeen.insert(CU).second) 237 return false; 238 239 CUs.push_back(CU); 240 return true; 241 } 242 243 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) { 244 if (!DIG) 245 return false; 246 247 if (!NodesSeen.insert(DIG).second) 248 return false; 249 250 GVs.push_back(DIG); 251 return true; 252 } 253 254 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) { 255 if (!SP) 256 return false; 257 258 if (!NodesSeen.insert(SP).second) 259 return false; 260 261 SPs.push_back(SP); 262 return true; 263 } 264 265 bool DebugInfoFinder::addScope(DIScope *Scope) { 266 if (!Scope) 267 return false; 268 // FIXME: Ocaml binding generates a scope with no content, we treat it 269 // as null for now. 270 if (Scope->getNumOperands() == 0) 271 return false; 272 if (!NodesSeen.insert(Scope).second) 273 return false; 274 Scopes.push_back(Scope); 275 return true; 276 } 277 278 bool llvm::stripDebugInfo(Function &F) { 279 bool Changed = false; 280 if (F.getSubprogram()) { 281 Changed = true; 282 F.setSubprogram(nullptr); 283 } 284 for (BasicBlock &BB : F) { 285 for (Instruction &I : BB) { 286 if (I.getDebugLoc()) { 287 Changed = true; 288 I.setDebugLoc(DebugLoc()); 289 } 290 } 291 } 292 return Changed; 293 } 294 295 bool llvm::StripDebugInfo(Module &M) { 296 bool Changed = false; 297 298 // Remove all of the calls to the debugger intrinsics, and remove them from 299 // the module. 300 if (Function *Declare = M.getFunction("llvm.dbg.declare")) { 301 while (!Declare->use_empty()) { 302 CallInst *CI = cast<CallInst>(Declare->user_back()); 303 CI->eraseFromParent(); 304 } 305 Declare->eraseFromParent(); 306 Changed = true; 307 } 308 309 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { 310 while (!DbgVal->use_empty()) { 311 CallInst *CI = cast<CallInst>(DbgVal->user_back()); 312 CI->eraseFromParent(); 313 } 314 DbgVal->eraseFromParent(); 315 Changed = true; 316 } 317 318 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), 319 NME = M.named_metadata_end(); NMI != NME;) { 320 NamedMDNode *NMD = &*NMI; 321 ++NMI; 322 if (NMD->getName().startswith("llvm.dbg.")) { 323 NMD->eraseFromParent(); 324 Changed = true; 325 } 326 } 327 328 for (Function &F : M) 329 Changed |= stripDebugInfo(F); 330 331 if (GVMaterializer *Materializer = M.getMaterializer()) 332 Materializer->setStripDebugInfo(); 333 334 return Changed; 335 } 336 337 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { 338 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( 339 M.getModuleFlag("Debug Info Version"))) 340 return Val->getZExtValue(); 341 return 0; 342 } 343