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/IR/PassInstrumentation.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/CommandLine.h"
26 
27 using namespace llvm;
28 
29 namespace {
30 
31 cl::opt<bool> Quiet("debugify-quiet",
32                     cl::desc("Suppress verbose debugify output"));
33 
34 enum class Level {
35   Locations,
36   LocationsAndVariables
37 };
38 cl::opt<Level> DebugifyLevel(
39     "debugify-level", cl::desc("Kind of debug info to add"),
40     cl::values(clEnumValN(Level::Locations, "locations", "Locations only"),
41                clEnumValN(Level::LocationsAndVariables, "location+variables",
42                           "Locations and Variables")),
43     cl::init(Level::LocationsAndVariables));
44 
45 raw_ostream &dbg() { return Quiet ? nulls() : errs(); }
46 
47 uint64_t getAllocSizeInBits(Module &M, Type *Ty) {
48   return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0;
49 }
50 
51 bool isFunctionSkipped(Function &F) {
52   return F.isDeclaration() || !F.hasExactDefinition();
53 }
54 
55 /// Find the basic block's terminating instruction.
56 ///
57 /// Special care is needed to handle musttail and deopt calls, as these behave
58 /// like (but are in fact not) terminators.
59 Instruction *findTerminatingInstruction(BasicBlock &BB) {
60   if (auto *I = BB.getTerminatingMustTailCall())
61     return I;
62   if (auto *I = BB.getTerminatingDeoptimizeCall())
63     return I;
64   return BB.getTerminator();
65 }
66 } // end anonymous namespace
67 
68 bool llvm::applyDebugifyMetadata(
69     Module &M, iterator_range<Module::iterator> Functions, StringRef Banner,
70     std::function<bool(DIBuilder &DIB, Function &F)> ApplyToMF) {
71   // Skip modules with debug info.
72   if (M.getNamedMetadata("llvm.dbg.cu")) {
73     dbg() << Banner << "Skipping module with debug info\n";
74     return false;
75   }
76 
77   DIBuilder DIB(M);
78   LLVMContext &Ctx = M.getContext();
79   auto *Int32Ty = Type::getInt32Ty(Ctx);
80 
81   // Get a DIType which corresponds to Ty.
82   DenseMap<uint64_t, DIType *> TypeCache;
83   auto getCachedDIType = [&](Type *Ty) -> DIType * {
84     uint64_t Size = getAllocSizeInBits(M, Ty);
85     DIType *&DTy = TypeCache[Size];
86     if (!DTy) {
87       std::string Name = "ty" + utostr(Size);
88       DTy = DIB.createBasicType(Name, Size, dwarf::DW_ATE_unsigned);
89     }
90     return DTy;
91   };
92 
93   unsigned NextLine = 1;
94   unsigned NextVar = 1;
95   auto File = DIB.createFile(M.getName(), "/");
96   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "debugify",
97                                   /*isOptimized=*/true, "", 0);
98 
99   // Visit each instruction.
100   for (Function &F : Functions) {
101     if (isFunctionSkipped(F))
102       continue;
103 
104     bool InsertedDbgVal = false;
105     auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
106     DISubprogram::DISPFlags SPFlags =
107         DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized;
108     if (F.hasPrivateLinkage() || F.hasInternalLinkage())
109       SPFlags |= DISubprogram::SPFlagLocalToUnit;
110     auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine,
111                                  SPType, NextLine, DINode::FlagZero, SPFlags);
112     F.setSubprogram(SP);
113 
114     // Helper that inserts a dbg.value before \p InsertBefore, copying the
115     // location (and possibly the type, if it's non-void) from \p TemplateInst.
116     auto insertDbgVal = [&](Instruction &TemplateInst,
117                             Instruction *InsertBefore) {
118       std::string Name = utostr(NextVar++);
119       Value *V = &TemplateInst;
120       if (TemplateInst.getType()->isVoidTy())
121         V = ConstantInt::get(Int32Ty, 0);
122       const DILocation *Loc = TemplateInst.getDebugLoc().get();
123       auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(),
124                                              getCachedDIType(V->getType()),
125                                              /*AlwaysPreserve=*/true);
126       DIB.insertDbgValueIntrinsic(V, LocalVar, DIB.createExpression(), Loc,
127                                   InsertBefore);
128     };
129 
130     for (BasicBlock &BB : F) {
131       // Attach debug locations.
132       for (Instruction &I : BB)
133         I.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
134 
135       if (DebugifyLevel < Level::LocationsAndVariables)
136         continue;
137 
138       // Inserting debug values into EH pads can break IR invariants.
139       if (BB.isEHPad())
140         continue;
141 
142       // Find the terminating instruction, after which no debug values are
143       // attached.
144       Instruction *LastInst = findTerminatingInstruction(BB);
145       assert(LastInst && "Expected basic block with a terminator");
146 
147       // Maintain an insertion point which can't be invalidated when updates
148       // are made.
149       BasicBlock::iterator InsertPt = BB.getFirstInsertionPt();
150       assert(InsertPt != BB.end() && "Expected to find an insertion point");
151       Instruction *InsertBefore = &*InsertPt;
152 
153       // Attach debug values.
154       for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) {
155         // Skip void-valued instructions.
156         if (I->getType()->isVoidTy())
157           continue;
158 
159         // Phis and EH pads must be grouped at the beginning of the block.
160         // Only advance the insertion point when we finish visiting these.
161         if (!isa<PHINode>(I) && !I->isEHPad())
162           InsertBefore = I->getNextNode();
163 
164         insertDbgVal(*I, InsertBefore);
165         InsertedDbgVal = true;
166       }
167     }
168     // Make sure we emit at least one dbg.value, otherwise MachineDebugify may
169     // not have anything to work with as it goes about inserting DBG_VALUEs.
170     // (It's common for MIR tests to be written containing skeletal IR with
171     // empty functions -- we're still interested in debugifying the MIR within
172     // those tests, and this helps with that.)
173     if (DebugifyLevel == Level::LocationsAndVariables && !InsertedDbgVal) {
174       auto *Term = findTerminatingInstruction(F.getEntryBlock());
175       insertDbgVal(*Term, Term);
176     }
177     if (ApplyToMF)
178       ApplyToMF(DIB, F);
179     DIB.finalizeSubprogram(SP);
180   }
181   DIB.finalize();
182 
183   // Track the number of distinct lines and variables.
184   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify");
185   auto addDebugifyOperand = [&](unsigned N) {
186     NMD->addOperand(MDNode::get(
187         Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N))));
188   };
189   addDebugifyOperand(NextLine - 1); // Original number of lines.
190   addDebugifyOperand(NextVar - 1);  // Original number of variables.
191   assert(NMD->getNumOperands() == 2 &&
192          "llvm.debugify should have exactly 2 operands!");
193 
194   // Claim that this synthetic debug info is valid.
195   StringRef DIVersionKey = "Debug Info Version";
196   if (!M.getModuleFlag(DIVersionKey))
197     M.addModuleFlag(Module::Warning, DIVersionKey, DEBUG_METADATA_VERSION);
198 
199   return true;
200 }
201 
202 static bool applyDebugify(Function &F) {
203   Module &M = *F.getParent();
204   auto FuncIt = F.getIterator();
205   return applyDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
206                                "FunctionDebugify: ", /*ApplyToMF=*/nullptr);
207 }
208 
209 static bool applyDebugify(Module &M) {
210   return applyDebugifyMetadata(M, M.functions(),
211                                "ModuleDebugify: ", /*ApplyToMF=*/nullptr);
212 }
213 
214 bool llvm::stripDebugifyMetadata(Module &M) {
215   bool Changed = false;
216 
217   // Remove the llvm.debugify module-level named metadata.
218   NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
219   if (DebugifyMD) {
220     M.eraseNamedMetadata(DebugifyMD);
221     Changed = true;
222   }
223 
224   // Strip out all debug intrinsics and supporting metadata (subprograms, types,
225   // variables, etc).
226   Changed |= StripDebugInfo(M);
227 
228   // Strip out the dead dbg.value prototype.
229   Function *DbgValF = M.getFunction("llvm.dbg.value");
230   if (DbgValF) {
231     assert(DbgValF->isDeclaration() && DbgValF->use_empty() &&
232            "Not all debug info stripped?");
233     DbgValF->eraseFromParent();
234     Changed = true;
235   }
236 
237   // Strip out the module-level Debug Info Version metadata.
238   // FIXME: There must be an easier way to remove an operand from a NamedMDNode.
239   NamedMDNode *NMD = M.getModuleFlagsMetadata();
240   if (!NMD)
241     return Changed;
242   SmallVector<MDNode *, 4> Flags(NMD->operands());
243   NMD->clearOperands();
244   for (MDNode *Flag : Flags) {
245     MDString *Key = dyn_cast_or_null<MDString>(Flag->getOperand(1));
246     if (Key->getString() == "Debug Info Version") {
247       Changed = true;
248       continue;
249     }
250     NMD->addOperand(Flag);
251   }
252   // If we left it empty we might as well remove it.
253   if (NMD->getNumOperands() == 0)
254     NMD->eraseFromParent();
255 
256   return Changed;
257 }
258 
259 namespace {
260 /// Return true if a mis-sized diagnostic is issued for \p DVI.
261 bool diagnoseMisSizedDbgValue(Module &M, DbgValueInst *DVI) {
262   // The size of a dbg.value's value operand should match the size of the
263   // variable it corresponds to.
264   //
265   // TODO: This, along with a check for non-null value operands, should be
266   // promoted to verifier failures.
267   Value *V = DVI->getValue();
268   if (!V)
269     return false;
270 
271   // For now, don't try to interpret anything more complicated than an empty
272   // DIExpression. Eventually we should try to handle OP_deref and fragments.
273   if (DVI->getExpression()->getNumElements())
274     return false;
275 
276   Type *Ty = V->getType();
277   uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty);
278   Optional<uint64_t> DbgVarSize = DVI->getFragmentSizeInBits();
279   if (!ValueOperandSize || !DbgVarSize)
280     return false;
281 
282   bool HasBadSize = false;
283   if (Ty->isIntegerTy()) {
284     auto Signedness = DVI->getVariable()->getSignedness();
285     if (Signedness && *Signedness == DIBasicType::Signedness::Signed)
286       HasBadSize = ValueOperandSize < *DbgVarSize;
287   } else {
288     HasBadSize = ValueOperandSize != *DbgVarSize;
289   }
290 
291   if (HasBadSize) {
292     dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize
293           << ", but its variable has size " << *DbgVarSize << ": ";
294     DVI->print(dbg());
295     dbg() << "\n";
296   }
297   return HasBadSize;
298 }
299 
300 bool checkDebugifyMetadata(Module &M,
301                            iterator_range<Module::iterator> Functions,
302                            StringRef NameOfWrappedPass, StringRef Banner,
303                            bool Strip, DebugifyStatsMap *StatsMap) {
304   // Skip modules without debugify metadata.
305   NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");
306   if (!NMD) {
307     dbg() << Banner << ": Skipping module without debugify metadata\n";
308     return false;
309   }
310 
311   auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
312     return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
313         ->getZExtValue();
314   };
315   assert(NMD->getNumOperands() == 2 &&
316          "llvm.debugify should have exactly 2 operands!");
317   unsigned OriginalNumLines = getDebugifyOperand(0);
318   unsigned OriginalNumVars = getDebugifyOperand(1);
319   bool HasErrors = false;
320 
321   // Track debug info loss statistics if able.
322   DebugifyStatistics *Stats = nullptr;
323   if (StatsMap && !NameOfWrappedPass.empty())
324     Stats = &StatsMap->operator[](NameOfWrappedPass);
325 
326   BitVector MissingLines{OriginalNumLines, true};
327   BitVector MissingVars{OriginalNumVars, true};
328   for (Function &F : Functions) {
329     if (isFunctionSkipped(F))
330       continue;
331 
332     // Find missing lines.
333     for (Instruction &I : instructions(F)) {
334       if (isa<DbgValueInst>(&I) || isa<PHINode>(&I))
335         continue;
336 
337       auto DL = I.getDebugLoc();
338       if (DL && DL.getLine() != 0) {
339         MissingLines.reset(DL.getLine() - 1);
340         continue;
341       }
342 
343       if (!DL) {
344         dbg() << "WARNING: Instruction with empty DebugLoc in function ";
345         dbg() << F.getName() << " --";
346         I.print(dbg());
347         dbg() << "\n";
348       }
349     }
350 
351     // Find missing variables and mis-sized debug values.
352     for (Instruction &I : instructions(F)) {
353       auto *DVI = dyn_cast<DbgValueInst>(&I);
354       if (!DVI)
355         continue;
356 
357       unsigned Var = ~0U;
358       (void)to_integer(DVI->getVariable()->getName(), Var, 10);
359       assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable");
360       bool HasBadSize = diagnoseMisSizedDbgValue(M, DVI);
361       if (!HasBadSize)
362         MissingVars.reset(Var - 1);
363       HasErrors |= HasBadSize;
364     }
365   }
366 
367   // Print the results.
368   for (unsigned Idx : MissingLines.set_bits())
369     dbg() << "WARNING: Missing line " << Idx + 1 << "\n";
370 
371   for (unsigned Idx : MissingVars.set_bits())
372     dbg() << "WARNING: Missing variable " << Idx + 1 << "\n";
373 
374   // Update DI loss statistics.
375   if (Stats) {
376     Stats->NumDbgLocsExpected += OriginalNumLines;
377     Stats->NumDbgLocsMissing += MissingLines.count();
378     Stats->NumDbgValuesExpected += OriginalNumVars;
379     Stats->NumDbgValuesMissing += MissingVars.count();
380   }
381 
382   dbg() << Banner;
383   if (!NameOfWrappedPass.empty())
384     dbg() << " [" << NameOfWrappedPass << "]";
385   dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n';
386 
387   // Strip debugify metadata if required.
388   if (Strip)
389     return stripDebugifyMetadata(M);
390 
391   return false;
392 }
393 
394 /// ModulePass for attaching synthetic debug info to everything, used with the
395 /// legacy module pass manager.
396 struct DebugifyModulePass : public ModulePass {
397   bool runOnModule(Module &M) override { return applyDebugify(M); }
398 
399   DebugifyModulePass() : ModulePass(ID) {}
400 
401   void getAnalysisUsage(AnalysisUsage &AU) const override {
402     AU.setPreservesAll();
403   }
404 
405   static char ID; // Pass identification.
406 };
407 
408 /// FunctionPass for attaching synthetic debug info to instructions within a
409 /// single function, used with the legacy module pass manager.
410 struct DebugifyFunctionPass : public FunctionPass {
411   bool runOnFunction(Function &F) override { return applyDebugify(F); }
412 
413   DebugifyFunctionPass() : FunctionPass(ID) {}
414 
415   void getAnalysisUsage(AnalysisUsage &AU) const override {
416     AU.setPreservesAll();
417   }
418 
419   static char ID; // Pass identification.
420 };
421 
422 /// ModulePass for checking debug info inserted by -debugify, used with the
423 /// legacy module pass manager.
424 struct CheckDebugifyModulePass : public ModulePass {
425   bool runOnModule(Module &M) override {
426     return checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
427                                  "CheckModuleDebugify", Strip, StatsMap);
428   }
429 
430   CheckDebugifyModulePass(bool Strip = false, StringRef NameOfWrappedPass = "",
431                           DebugifyStatsMap *StatsMap = nullptr)
432       : ModulePass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass),
433         StatsMap(StatsMap) {}
434 
435   void getAnalysisUsage(AnalysisUsage &AU) const override {
436     AU.setPreservesAll();
437   }
438 
439   static char ID; // Pass identification.
440 
441 private:
442   bool Strip;
443   StringRef NameOfWrappedPass;
444   DebugifyStatsMap *StatsMap;
445 };
446 
447 /// FunctionPass for checking debug info inserted by -debugify-function, used
448 /// with the legacy module pass manager.
449 struct CheckDebugifyFunctionPass : public FunctionPass {
450   bool runOnFunction(Function &F) override {
451     Module &M = *F.getParent();
452     auto FuncIt = F.getIterator();
453     return checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
454                                  NameOfWrappedPass, "CheckFunctionDebugify",
455                                  Strip, StatsMap);
456   }
457 
458   CheckDebugifyFunctionPass(bool Strip = false,
459                             StringRef NameOfWrappedPass = "",
460                             DebugifyStatsMap *StatsMap = nullptr)
461       : FunctionPass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass),
462         StatsMap(StatsMap) {}
463 
464   void getAnalysisUsage(AnalysisUsage &AU) const override {
465     AU.setPreservesAll();
466   }
467 
468   static char ID; // Pass identification.
469 
470 private:
471   bool Strip;
472   StringRef NameOfWrappedPass;
473   DebugifyStatsMap *StatsMap;
474 };
475 
476 } // end anonymous namespace
477 
478 void llvm::exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map) {
479   std::error_code EC;
480   raw_fd_ostream OS{Path, EC};
481   if (EC) {
482     errs() << "Could not open file: " << EC.message() << ", " << Path << '\n';
483     return;
484   }
485 
486   OS << "Pass Name" << ',' << "# of missing debug values" << ','
487      << "# of missing locations" << ',' << "Missing/Expected value ratio" << ','
488      << "Missing/Expected location ratio" << '\n';
489   for (const auto &Entry : Map) {
490     StringRef Pass = Entry.first;
491     DebugifyStatistics Stats = Entry.second;
492 
493     OS << Pass << ',' << Stats.NumDbgValuesMissing << ','
494        << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ','
495        << Stats.getEmptyLocationRatio() << '\n';
496   }
497 }
498 
499 ModulePass *llvm::createDebugifyModulePass() {
500   return new DebugifyModulePass();
501 }
502 
503 FunctionPass *llvm::createDebugifyFunctionPass() {
504   return new DebugifyFunctionPass();
505 }
506 
507 PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) {
508   applyDebugifyMetadata(M, M.functions(),
509                         "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
510   return PreservedAnalyses::all();
511 }
512 
513 ModulePass *llvm::createCheckDebugifyModulePass(bool Strip,
514                                                 StringRef NameOfWrappedPass,
515                                                 DebugifyStatsMap *StatsMap) {
516   return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap);
517 }
518 
519 FunctionPass *
520 llvm::createCheckDebugifyFunctionPass(bool Strip, StringRef NameOfWrappedPass,
521                                       DebugifyStatsMap *StatsMap) {
522   return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap);
523 }
524 
525 PreservedAnalyses NewPMCheckDebugifyPass::run(Module &M,
526                                               ModuleAnalysisManager &) {
527   checkDebugifyMetadata(M, M.functions(), "", "CheckModuleDebugify", false,
528                         nullptr);
529   return PreservedAnalyses::all();
530 }
531 
532 static bool isIgnoredPass(StringRef PassID) {
533   return isSpecialPass(PassID, {"PassManager", "PassAdaptor",
534                                 "AnalysisManagerProxy", "PrintFunctionPass",
535                                 "PrintModulePass", "BitcodeWriterPass",
536                                 "ThinLTOBitcodeWriterPass", "VerifierPass"});
537 }
538 
539 void DebugifyEachInstrumentation::registerCallbacks(
540     PassInstrumentationCallbacks &PIC) {
541   PIC.registerBeforeNonSkippedPassCallback([](StringRef P, Any IR) {
542     if (isIgnoredPass(P))
543       return;
544     if (any_isa<const Function *>(IR))
545       applyDebugify(*const_cast<Function *>(any_cast<const Function *>(IR)));
546     else if (any_isa<const Module *>(IR))
547       applyDebugify(*const_cast<Module *>(any_cast<const Module *>(IR)));
548   });
549   PIC.registerAfterPassCallback([this](StringRef P, Any IR,
550                                        const PreservedAnalyses &PassPA) {
551     if (isIgnoredPass(P))
552       return;
553     if (any_isa<const Function *>(IR)) {
554       auto &F = *const_cast<Function *>(any_cast<const Function *>(IR));
555       Module &M = *F.getParent();
556       auto It = F.getIterator();
557       checkDebugifyMetadata(M, make_range(It, std::next(It)), P,
558                             "CheckFunctionDebugify", /*Strip=*/true, &StatsMap);
559     } else if (any_isa<const Module *>(IR)) {
560       auto &M = *const_cast<Module *>(any_cast<const Module *>(IR));
561       checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify",
562                             /*Strip=*/true, &StatsMap);
563     }
564   });
565 }
566 
567 char DebugifyModulePass::ID = 0;
568 static RegisterPass<DebugifyModulePass> DM("debugify",
569                                            "Attach debug info to everything");
570 
571 char CheckDebugifyModulePass::ID = 0;
572 static RegisterPass<CheckDebugifyModulePass>
573     CDM("check-debugify", "Check debug info from -debugify");
574 
575 char DebugifyFunctionPass::ID = 0;
576 static RegisterPass<DebugifyFunctionPass> DF("debugify-function",
577                                              "Attach debug info to a function");
578 
579 char CheckDebugifyFunctionPass::ID = 0;
580 static RegisterPass<CheckDebugifyFunctionPass>
581     CDF("check-debugify-function", "Check debug info from -debugify-function");
582