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