1 //===- Debugify.cpp - Attach synthetic debug info to everything -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file This pass attaches synthetic debug info to everything. It can be used
10 /// to create targeted tests for debug info preservation.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/Utils/Debugify.h"
15 #include "llvm/ADT/BitVector.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/IR/DIBuilder.h"
18 #include "llvm/IR/DebugInfo.h"
19 #include "llvm/IR/InstIterator.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/CommandLine.h"
25 
26 using namespace llvm;
27 
28 namespace {
29 
30 cl::opt<bool> Quiet("debugify-quiet",
31                     cl::desc("Suppress verbose debugify output"));
32 
33 enum class Level {
34   Locations,
35   LocationsAndVariables
36 };
37 cl::opt<Level> DebugifyLevel(
38     "debugify-level", cl::desc("Kind of debug info to add"),
39     cl::values(clEnumValN(Level::Locations, "locations", "Locations only"),
40                clEnumValN(Level::LocationsAndVariables, "location+variables",
41                           "Locations and Variables")),
42     cl::init(Level::LocationsAndVariables));
43 
44 raw_ostream &dbg() { return Quiet ? nulls() : errs(); }
45 
46 uint64_t getAllocSizeInBits(Module &M, Type *Ty) {
47   return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0;
48 }
49 
50 bool isFunctionSkipped(Function &F) {
51   return F.isDeclaration() || !F.hasExactDefinition();
52 }
53 
54 /// Find the basic block's terminating instruction.
55 ///
56 /// Special care is needed to handle musttail and deopt calls, as these behave
57 /// like (but are in fact not) terminators.
58 Instruction *findTerminatingInstruction(BasicBlock &BB) {
59   if (auto *I = BB.getTerminatingMustTailCall())
60     return I;
61   if (auto *I = BB.getTerminatingDeoptimizeCall())
62     return I;
63   return BB.getTerminator();
64 }
65 
66 bool applyDebugifyMetadata(Module &M,
67                            iterator_range<Module::iterator> Functions,
68                            StringRef Banner) {
69   // Skip modules with debug info.
70   if (M.getNamedMetadata("llvm.dbg.cu")) {
71     dbg() << Banner << "Skipping module with debug info\n";
72     return false;
73   }
74 
75   DIBuilder DIB(M);
76   LLVMContext &Ctx = M.getContext();
77 
78   // Get a DIType which corresponds to Ty.
79   DenseMap<uint64_t, DIType *> TypeCache;
80   auto getCachedDIType = [&](Type *Ty) -> DIType * {
81     uint64_t Size = getAllocSizeInBits(M, Ty);
82     DIType *&DTy = TypeCache[Size];
83     if (!DTy) {
84       std::string Name = "ty" + utostr(Size);
85       DTy = DIB.createBasicType(Name, Size, dwarf::DW_ATE_unsigned);
86     }
87     return DTy;
88   };
89 
90   unsigned NextLine = 1;
91   unsigned NextVar = 1;
92   auto File = DIB.createFile(M.getName(), "/");
93   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "debugify",
94                                   /*isOptimized=*/true, "", 0);
95 
96   // Visit each instruction.
97   for (Function &F : Functions) {
98     if (isFunctionSkipped(F))
99       continue;
100 
101     auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
102     DISubprogram::DISPFlags SPFlags =
103         DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized;
104     if (F.hasPrivateLinkage() || F.hasInternalLinkage())
105       SPFlags |= DISubprogram::SPFlagLocalToUnit;
106     auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine,
107                                  SPType, NextLine, DINode::FlagZero, SPFlags);
108     F.setSubprogram(SP);
109     for (BasicBlock &BB : F) {
110       // Attach debug locations.
111       for (Instruction &I : BB)
112         I.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
113 
114       if (DebugifyLevel < Level::LocationsAndVariables)
115         continue;
116 
117       // Inserting debug values into EH pads can break IR invariants.
118       if (BB.isEHPad())
119         continue;
120 
121       // Find the terminating instruction, after which no debug values are
122       // attached.
123       Instruction *LastInst = findTerminatingInstruction(BB);
124       assert(LastInst && "Expected basic block with a terminator");
125 
126       // Maintain an insertion point which can't be invalidated when updates
127       // are made.
128       BasicBlock::iterator InsertPt = BB.getFirstInsertionPt();
129       assert(InsertPt != BB.end() && "Expected to find an insertion point");
130       Instruction *InsertBefore = &*InsertPt;
131 
132       // Attach debug values.
133       for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) {
134         // Skip void-valued instructions.
135         if (I->getType()->isVoidTy())
136           continue;
137 
138         // Phis and EH pads must be grouped at the beginning of the block.
139         // Only advance the insertion point when we finish visiting these.
140         if (!isa<PHINode>(I) && !I->isEHPad())
141           InsertBefore = I->getNextNode();
142 
143         std::string Name = utostr(NextVar++);
144         const DILocation *Loc = I->getDebugLoc().get();
145         auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(),
146                                                getCachedDIType(I->getType()),
147                                                /*AlwaysPreserve=*/true);
148         DIB.insertDbgValueIntrinsic(I, LocalVar, DIB.createExpression(), Loc,
149                                     InsertBefore);
150       }
151     }
152     DIB.finalizeSubprogram(SP);
153   }
154   DIB.finalize();
155 
156   // Track the number of distinct lines and variables.
157   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify");
158   auto *IntTy = Type::getInt32Ty(Ctx);
159   auto addDebugifyOperand = [&](unsigned N) {
160     NMD->addOperand(MDNode::get(
161         Ctx, ValueAsMetadata::getConstant(ConstantInt::get(IntTy, N))));
162   };
163   addDebugifyOperand(NextLine - 1); // Original number of lines.
164   addDebugifyOperand(NextVar - 1);  // Original number of variables.
165   assert(NMD->getNumOperands() == 2 &&
166          "llvm.debugify should have exactly 2 operands!");
167 
168   // Claim that this synthetic debug info is valid.
169   StringRef DIVersionKey = "Debug Info Version";
170   if (!M.getModuleFlag(DIVersionKey))
171     M.addModuleFlag(Module::Warning, DIVersionKey, DEBUG_METADATA_VERSION);
172 
173   return true;
174 }
175 
176 /// Return true if a mis-sized diagnostic is issued for \p DVI.
177 bool diagnoseMisSizedDbgValue(Module &M, DbgValueInst *DVI) {
178   // The size of a dbg.value's value operand should match the size of the
179   // variable it corresponds to.
180   //
181   // TODO: This, along with a check for non-null value operands, should be
182   // promoted to verifier failures.
183   Value *V = DVI->getValue();
184   if (!V)
185     return false;
186 
187   // For now, don't try to interpret anything more complicated than an empty
188   // DIExpression. Eventually we should try to handle OP_deref and fragments.
189   if (DVI->getExpression()->getNumElements())
190     return false;
191 
192   Type *Ty = V->getType();
193   uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty);
194   Optional<uint64_t> DbgVarSize = DVI->getFragmentSizeInBits();
195   if (!ValueOperandSize || !DbgVarSize)
196     return false;
197 
198   bool HasBadSize = false;
199   if (Ty->isIntegerTy()) {
200     auto Signedness = DVI->getVariable()->getSignedness();
201     if (Signedness && *Signedness == DIBasicType::Signedness::Signed)
202       HasBadSize = ValueOperandSize < *DbgVarSize;
203   } else {
204     HasBadSize = ValueOperandSize != *DbgVarSize;
205   }
206 
207   if (HasBadSize) {
208     dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize
209           << ", but its variable has size " << *DbgVarSize << ": ";
210     DVI->print(dbg());
211     dbg() << "\n";
212   }
213   return HasBadSize;
214 }
215 
216 bool checkDebugifyMetadata(Module &M,
217                            iterator_range<Module::iterator> Functions,
218                            StringRef NameOfWrappedPass, StringRef Banner,
219                            bool Strip, DebugifyStatsMap *StatsMap) {
220   // Skip modules without debugify metadata.
221   NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");
222   if (!NMD) {
223     dbg() << Banner << "Skipping module without debugify metadata\n";
224     return false;
225   }
226 
227   auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
228     return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
229         ->getZExtValue();
230   };
231   assert(NMD->getNumOperands() == 2 &&
232          "llvm.debugify should have exactly 2 operands!");
233   unsigned OriginalNumLines = getDebugifyOperand(0);
234   unsigned OriginalNumVars = getDebugifyOperand(1);
235   bool HasErrors = false;
236 
237   // Track debug info loss statistics if able.
238   DebugifyStatistics *Stats = nullptr;
239   if (StatsMap && !NameOfWrappedPass.empty())
240     Stats = &StatsMap->operator[](NameOfWrappedPass);
241 
242   BitVector MissingLines{OriginalNumLines, true};
243   BitVector MissingVars{OriginalNumVars, true};
244   for (Function &F : Functions) {
245     if (isFunctionSkipped(F))
246       continue;
247 
248     // Find missing lines.
249     for (Instruction &I : instructions(F)) {
250       if (isa<DbgValueInst>(&I) || isa<PHINode>(&I))
251         continue;
252 
253       auto DL = I.getDebugLoc();
254       if (DL && DL.getLine() != 0) {
255         MissingLines.reset(DL.getLine() - 1);
256         continue;
257       }
258 
259       if (!DL) {
260         dbg() << "ERROR: Instruction with empty DebugLoc in function ";
261         dbg() << F.getName() << " --";
262         I.print(dbg());
263         dbg() << "\n";
264         HasErrors = true;
265       }
266     }
267 
268     // Find missing variables and mis-sized debug values.
269     for (Instruction &I : instructions(F)) {
270       auto *DVI = dyn_cast<DbgValueInst>(&I);
271       if (!DVI)
272         continue;
273 
274       unsigned Var = ~0U;
275       (void)to_integer(DVI->getVariable()->getName(), Var, 10);
276       assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable");
277       bool HasBadSize = diagnoseMisSizedDbgValue(M, DVI);
278       if (!HasBadSize)
279         MissingVars.reset(Var - 1);
280       HasErrors |= HasBadSize;
281     }
282   }
283 
284   // Print the results.
285   for (unsigned Idx : MissingLines.set_bits())
286     dbg() << "WARNING: Missing line " << Idx + 1 << "\n";
287 
288   for (unsigned Idx : MissingVars.set_bits())
289     dbg() << "WARNING: Missing variable " << Idx + 1 << "\n";
290 
291   // Update DI loss statistics.
292   if (Stats) {
293     Stats->NumDbgLocsExpected += OriginalNumLines;
294     Stats->NumDbgLocsMissing += MissingLines.count();
295     Stats->NumDbgValuesExpected += OriginalNumVars;
296     Stats->NumDbgValuesMissing += MissingVars.count();
297   }
298 
299   dbg() << Banner;
300   if (!NameOfWrappedPass.empty())
301     dbg() << " [" << NameOfWrappedPass << "]";
302   dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n';
303 
304   // Strip the Debugify Metadata if required.
305   if (Strip) {
306     StripDebugInfo(M);
307     M.eraseNamedMetadata(NMD);
308     return true;
309   }
310 
311   return false;
312 }
313 
314 /// ModulePass for attaching synthetic debug info to everything, used with the
315 /// legacy module pass manager.
316 struct DebugifyModulePass : public ModulePass {
317   bool runOnModule(Module &M) override {
318     return applyDebugifyMetadata(M, M.functions(), "ModuleDebugify: ");
319   }
320 
321   DebugifyModulePass() : ModulePass(ID) {}
322 
323   void getAnalysisUsage(AnalysisUsage &AU) const override {
324     AU.setPreservesAll();
325   }
326 
327   static char ID; // Pass identification.
328 };
329 
330 /// FunctionPass for attaching synthetic debug info to instructions within a
331 /// single function, used with the legacy module pass manager.
332 struct DebugifyFunctionPass : public FunctionPass {
333   bool runOnFunction(Function &F) override {
334     Module &M = *F.getParent();
335     auto FuncIt = F.getIterator();
336     return applyDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
337                                  "FunctionDebugify: ");
338   }
339 
340   DebugifyFunctionPass() : FunctionPass(ID) {}
341 
342   void getAnalysisUsage(AnalysisUsage &AU) const override {
343     AU.setPreservesAll();
344   }
345 
346   static char ID; // Pass identification.
347 };
348 
349 /// ModulePass for checking debug info inserted by -debugify, used with the
350 /// legacy module pass manager.
351 struct CheckDebugifyModulePass : public ModulePass {
352   bool runOnModule(Module &M) override {
353     return checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
354                                  "CheckModuleDebugify", Strip, StatsMap);
355   }
356 
357   CheckDebugifyModulePass(bool Strip = false, StringRef NameOfWrappedPass = "",
358                           DebugifyStatsMap *StatsMap = nullptr)
359       : ModulePass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass),
360         StatsMap(StatsMap) {}
361 
362   void getAnalysisUsage(AnalysisUsage &AU) const override {
363     AU.setPreservesAll();
364   }
365 
366   static char ID; // Pass identification.
367 
368 private:
369   bool Strip;
370   StringRef NameOfWrappedPass;
371   DebugifyStatsMap *StatsMap;
372 };
373 
374 /// FunctionPass for checking debug info inserted by -debugify-function, used
375 /// with the legacy module pass manager.
376 struct CheckDebugifyFunctionPass : public FunctionPass {
377   bool runOnFunction(Function &F) override {
378     Module &M = *F.getParent();
379     auto FuncIt = F.getIterator();
380     return checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
381                                  NameOfWrappedPass, "CheckFunctionDebugify",
382                                  Strip, StatsMap);
383   }
384 
385   CheckDebugifyFunctionPass(bool Strip = false,
386                             StringRef NameOfWrappedPass = "",
387                             DebugifyStatsMap *StatsMap = nullptr)
388       : FunctionPass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass),
389         StatsMap(StatsMap) {}
390 
391   void getAnalysisUsage(AnalysisUsage &AU) const override {
392     AU.setPreservesAll();
393   }
394 
395   static char ID; // Pass identification.
396 
397 private:
398   bool Strip;
399   StringRef NameOfWrappedPass;
400   DebugifyStatsMap *StatsMap;
401 };
402 
403 } // end anonymous namespace
404 
405 ModulePass *createDebugifyModulePass() { return new DebugifyModulePass(); }
406 
407 FunctionPass *createDebugifyFunctionPass() {
408   return new DebugifyFunctionPass();
409 }
410 
411 PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) {
412   applyDebugifyMetadata(M, M.functions(), "ModuleDebugify: ");
413   return PreservedAnalyses::all();
414 }
415 
416 ModulePass *createCheckDebugifyModulePass(bool Strip,
417                                           StringRef NameOfWrappedPass,
418                                           DebugifyStatsMap *StatsMap) {
419   return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap);
420 }
421 
422 FunctionPass *createCheckDebugifyFunctionPass(bool Strip,
423                                               StringRef NameOfWrappedPass,
424                                               DebugifyStatsMap *StatsMap) {
425   return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap);
426 }
427 
428 PreservedAnalyses NewPMCheckDebugifyPass::run(Module &M,
429                                               ModuleAnalysisManager &) {
430   checkDebugifyMetadata(M, M.functions(), "", "CheckModuleDebugify", false,
431                         nullptr);
432   return PreservedAnalyses::all();
433 }
434 
435 char DebugifyModulePass::ID = 0;
436 static RegisterPass<DebugifyModulePass> DM("debugify",
437                                            "Attach debug info to everything");
438 
439 char CheckDebugifyModulePass::ID = 0;
440 static RegisterPass<CheckDebugifyModulePass>
441     CDM("check-debugify", "Check debug info from -debugify");
442 
443 char DebugifyFunctionPass::ID = 0;
444 static RegisterPass<DebugifyFunctionPass> DF("debugify-function",
445                                              "Attach debug info to a function");
446 
447 char CheckDebugifyFunctionPass::ID = 0;
448 static RegisterPass<CheckDebugifyFunctionPass>
449     CDF("check-debugify-function", "Check debug info from -debugify-function");
450