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