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