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; 243 for (MDNode *Flag : NMD->operands()) 244 Flags.push_back(Flag); 245 NMD->clearOperands(); 246 for (MDNode *Flag : Flags) { 247 MDString *Key = dyn_cast_or_null<MDString>(Flag->getOperand(1)); 248 if (Key->getString() == "Debug Info Version") { 249 Changed = true; 250 continue; 251 } 252 NMD->addOperand(Flag); 253 } 254 // If we left it empty we might as well remove it. 255 if (NMD->getNumOperands() == 0) 256 NMD->eraseFromParent(); 257 258 return Changed; 259 } 260 261 namespace { 262 /// Return true if a mis-sized diagnostic is issued for \p DVI. 263 bool diagnoseMisSizedDbgValue(Module &M, DbgValueInst *DVI) { 264 // The size of a dbg.value's value operand should match the size of the 265 // variable it corresponds to. 266 // 267 // TODO: This, along with a check for non-null value operands, should be 268 // promoted to verifier failures. 269 Value *V = DVI->getValue(); 270 if (!V) 271 return false; 272 273 // For now, don't try to interpret anything more complicated than an empty 274 // DIExpression. Eventually we should try to handle OP_deref and fragments. 275 if (DVI->getExpression()->getNumElements()) 276 return false; 277 278 Type *Ty = V->getType(); 279 uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty); 280 Optional<uint64_t> DbgVarSize = DVI->getFragmentSizeInBits(); 281 if (!ValueOperandSize || !DbgVarSize) 282 return false; 283 284 bool HasBadSize = false; 285 if (Ty->isIntegerTy()) { 286 auto Signedness = DVI->getVariable()->getSignedness(); 287 if (Signedness && *Signedness == DIBasicType::Signedness::Signed) 288 HasBadSize = ValueOperandSize < *DbgVarSize; 289 } else { 290 HasBadSize = ValueOperandSize != *DbgVarSize; 291 } 292 293 if (HasBadSize) { 294 dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize 295 << ", but its variable has size " << *DbgVarSize << ": "; 296 DVI->print(dbg()); 297 dbg() << "\n"; 298 } 299 return HasBadSize; 300 } 301 302 bool checkDebugifyMetadata(Module &M, 303 iterator_range<Module::iterator> Functions, 304 StringRef NameOfWrappedPass, StringRef Banner, 305 bool Strip, DebugifyStatsMap *StatsMap) { 306 // Skip modules without debugify metadata. 307 NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify"); 308 if (!NMD) { 309 dbg() << Banner << ": Skipping module without debugify metadata\n"; 310 return false; 311 } 312 313 auto getDebugifyOperand = [&](unsigned Idx) -> unsigned { 314 return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0)) 315 ->getZExtValue(); 316 }; 317 assert(NMD->getNumOperands() == 2 && 318 "llvm.debugify should have exactly 2 operands!"); 319 unsigned OriginalNumLines = getDebugifyOperand(0); 320 unsigned OriginalNumVars = getDebugifyOperand(1); 321 bool HasErrors = false; 322 323 // Track debug info loss statistics if able. 324 DebugifyStatistics *Stats = nullptr; 325 if (StatsMap && !NameOfWrappedPass.empty()) 326 Stats = &StatsMap->operator[](NameOfWrappedPass); 327 328 BitVector MissingLines{OriginalNumLines, true}; 329 BitVector MissingVars{OriginalNumVars, true}; 330 for (Function &F : Functions) { 331 if (isFunctionSkipped(F)) 332 continue; 333 334 // Find missing lines. 335 for (Instruction &I : instructions(F)) { 336 if (isa<DbgValueInst>(&I) || isa<PHINode>(&I)) 337 continue; 338 339 auto DL = I.getDebugLoc(); 340 if (DL && DL.getLine() != 0) { 341 MissingLines.reset(DL.getLine() - 1); 342 continue; 343 } 344 345 if (!DL) { 346 dbg() << "WARNING: Instruction with empty DebugLoc in function "; 347 dbg() << F.getName() << " --"; 348 I.print(dbg()); 349 dbg() << "\n"; 350 } 351 } 352 353 // Find missing variables and mis-sized debug values. 354 for (Instruction &I : instructions(F)) { 355 auto *DVI = dyn_cast<DbgValueInst>(&I); 356 if (!DVI) 357 continue; 358 359 unsigned Var = ~0U; 360 (void)to_integer(DVI->getVariable()->getName(), Var, 10); 361 assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable"); 362 bool HasBadSize = diagnoseMisSizedDbgValue(M, DVI); 363 if (!HasBadSize) 364 MissingVars.reset(Var - 1); 365 HasErrors |= HasBadSize; 366 } 367 } 368 369 // Print the results. 370 for (unsigned Idx : MissingLines.set_bits()) 371 dbg() << "WARNING: Missing line " << Idx + 1 << "\n"; 372 373 for (unsigned Idx : MissingVars.set_bits()) 374 dbg() << "WARNING: Missing variable " << Idx + 1 << "\n"; 375 376 // Update DI loss statistics. 377 if (Stats) { 378 Stats->NumDbgLocsExpected += OriginalNumLines; 379 Stats->NumDbgLocsMissing += MissingLines.count(); 380 Stats->NumDbgValuesExpected += OriginalNumVars; 381 Stats->NumDbgValuesMissing += MissingVars.count(); 382 } 383 384 dbg() << Banner; 385 if (!NameOfWrappedPass.empty()) 386 dbg() << " [" << NameOfWrappedPass << "]"; 387 dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n'; 388 389 // Strip debugify metadata if required. 390 if (Strip) 391 return stripDebugifyMetadata(M); 392 393 return false; 394 } 395 396 /// ModulePass for attaching synthetic debug info to everything, used with the 397 /// legacy module pass manager. 398 struct DebugifyModulePass : public ModulePass { 399 bool runOnModule(Module &M) override { return applyDebugify(M); } 400 401 DebugifyModulePass() : ModulePass(ID) {} 402 403 void getAnalysisUsage(AnalysisUsage &AU) const override { 404 AU.setPreservesAll(); 405 } 406 407 static char ID; // Pass identification. 408 }; 409 410 /// FunctionPass for attaching synthetic debug info to instructions within a 411 /// single function, used with the legacy module pass manager. 412 struct DebugifyFunctionPass : public FunctionPass { 413 bool runOnFunction(Function &F) override { return applyDebugify(F); } 414 415 DebugifyFunctionPass() : FunctionPass(ID) {} 416 417 void getAnalysisUsage(AnalysisUsage &AU) const override { 418 AU.setPreservesAll(); 419 } 420 421 static char ID; // Pass identification. 422 }; 423 424 /// ModulePass for checking debug info inserted by -debugify, used with the 425 /// legacy module pass manager. 426 struct CheckDebugifyModulePass : public ModulePass { 427 bool runOnModule(Module &M) override { 428 return checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass, 429 "CheckModuleDebugify", Strip, StatsMap); 430 } 431 432 CheckDebugifyModulePass(bool Strip = false, StringRef NameOfWrappedPass = "", 433 DebugifyStatsMap *StatsMap = nullptr) 434 : ModulePass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass), 435 StatsMap(StatsMap) {} 436 437 void getAnalysisUsage(AnalysisUsage &AU) const override { 438 AU.setPreservesAll(); 439 } 440 441 static char ID; // Pass identification. 442 443 private: 444 bool Strip; 445 StringRef NameOfWrappedPass; 446 DebugifyStatsMap *StatsMap; 447 }; 448 449 /// FunctionPass for checking debug info inserted by -debugify-function, used 450 /// with the legacy module pass manager. 451 struct CheckDebugifyFunctionPass : public FunctionPass { 452 bool runOnFunction(Function &F) override { 453 Module &M = *F.getParent(); 454 auto FuncIt = F.getIterator(); 455 return checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)), 456 NameOfWrappedPass, "CheckFunctionDebugify", 457 Strip, StatsMap); 458 } 459 460 CheckDebugifyFunctionPass(bool Strip = false, 461 StringRef NameOfWrappedPass = "", 462 DebugifyStatsMap *StatsMap = nullptr) 463 : FunctionPass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass), 464 StatsMap(StatsMap) {} 465 466 void getAnalysisUsage(AnalysisUsage &AU) const override { 467 AU.setPreservesAll(); 468 } 469 470 static char ID; // Pass identification. 471 472 private: 473 bool Strip; 474 StringRef NameOfWrappedPass; 475 DebugifyStatsMap *StatsMap; 476 }; 477 478 } // end anonymous namespace 479 480 void llvm::exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map) { 481 std::error_code EC; 482 raw_fd_ostream OS{Path, EC}; 483 if (EC) { 484 errs() << "Could not open file: " << EC.message() << ", " << Path << '\n'; 485 return; 486 } 487 488 OS << "Pass Name" << ',' << "# of missing debug values" << ',' 489 << "# of missing locations" << ',' << "Missing/Expected value ratio" << ',' 490 << "Missing/Expected location ratio" << '\n'; 491 for (const auto &Entry : Map) { 492 StringRef Pass = Entry.first; 493 DebugifyStatistics Stats = Entry.second; 494 495 OS << Pass << ',' << Stats.NumDbgValuesMissing << ',' 496 << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ',' 497 << Stats.getEmptyLocationRatio() << '\n'; 498 } 499 } 500 501 ModulePass *llvm::createDebugifyModulePass() { 502 return new DebugifyModulePass(); 503 } 504 505 FunctionPass *llvm::createDebugifyFunctionPass() { 506 return new DebugifyFunctionPass(); 507 } 508 509 PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) { 510 applyDebugifyMetadata(M, M.functions(), 511 "ModuleDebugify: ", /*ApplyToMF*/ nullptr); 512 return PreservedAnalyses::all(); 513 } 514 515 ModulePass *llvm::createCheckDebugifyModulePass(bool Strip, 516 StringRef NameOfWrappedPass, 517 DebugifyStatsMap *StatsMap) { 518 return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap); 519 } 520 521 FunctionPass * 522 llvm::createCheckDebugifyFunctionPass(bool Strip, StringRef NameOfWrappedPass, 523 DebugifyStatsMap *StatsMap) { 524 return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap); 525 } 526 527 PreservedAnalyses NewPMCheckDebugifyPass::run(Module &M, 528 ModuleAnalysisManager &) { 529 checkDebugifyMetadata(M, M.functions(), "", "CheckModuleDebugify", false, 530 nullptr); 531 return PreservedAnalyses::all(); 532 } 533 534 static bool isIgnoredPass(StringRef PassID) { 535 return isSpecialPass(PassID, {"PassManager", "PassAdaptor", 536 "AnalysisManagerProxy", "PrintFunctionPass", 537 "PrintModulePass", "BitcodeWriterPass", 538 "ThinLTOBitcodeWriterPass", "VerifierPass"}); 539 } 540 541 void DebugifyEachInstrumentation::registerCallbacks( 542 PassInstrumentationCallbacks &PIC) { 543 PIC.registerBeforeNonSkippedPassCallback([](StringRef P, Any IR) { 544 if (isIgnoredPass(P)) 545 return; 546 if (any_isa<const Function *>(IR)) 547 applyDebugify(*const_cast<Function *>(any_cast<const Function *>(IR))); 548 else if (any_isa<const Module *>(IR)) 549 applyDebugify(*const_cast<Module *>(any_cast<const Module *>(IR))); 550 }); 551 PIC.registerAfterPassCallback([this](StringRef P, Any IR, 552 const PreservedAnalyses &PassPA) { 553 if (isIgnoredPass(P)) 554 return; 555 if (any_isa<const Function *>(IR)) { 556 auto &F = *const_cast<Function *>(any_cast<const Function *>(IR)); 557 Module &M = *F.getParent(); 558 auto It = F.getIterator(); 559 checkDebugifyMetadata(M, make_range(It, std::next(It)), P, 560 "CheckFunctionDebugify", /*Strip=*/true, &StatsMap); 561 } else if (any_isa<const Module *>(IR)) { 562 auto &M = *const_cast<Module *>(any_cast<const Module *>(IR)); 563 checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify", 564 /*Strip=*/true, &StatsMap); 565 } 566 }); 567 } 568 569 char DebugifyModulePass::ID = 0; 570 static RegisterPass<DebugifyModulePass> DM("debugify", 571 "Attach debug info to everything"); 572 573 char CheckDebugifyModulePass::ID = 0; 574 static RegisterPass<CheckDebugifyModulePass> 575 CDM("check-debugify", "Check debug info from -debugify"); 576 577 char DebugifyFunctionPass::ID = 0; 578 static RegisterPass<DebugifyFunctionPass> DF("debugify-function", 579 "Attach debug info to a function"); 580 581 char CheckDebugifyFunctionPass::ID = 0; 582 static RegisterPass<CheckDebugifyFunctionPass> 583 CDF("check-debugify-function", "Check debug info from -debugify-function"); 584