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