ProfiledBinary.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. //===-- ProfiledBinary.cpp - Binary decoder ---------------------*- C++ -*-===//
  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. #include "ProfiledBinary.h"
  9. #include "ErrorHandling.h"
  10. #include "ProfileGenerator.h"
  11. #include "llvm/ADT/Triple.h"
  12. #include "llvm/Demangle/Demangle.h"
  13. #include "llvm/IR/DebugInfoMetadata.h"
  14. #include "llvm/Support/CommandLine.h"
  15. #include "llvm/Support/Format.h"
  16. #include "llvm/Support/TargetRegistry.h"
  17. #include "llvm/Support/TargetSelect.h"
  18. #define DEBUG_TYPE "load-binary"
  19. using namespace llvm;
  20. using namespace sampleprof;
  21. cl::opt<bool> ShowDisassemblyOnly("show-disassembly-only", cl::ReallyHidden,
  22. cl::init(false), cl::ZeroOrMore,
  23. cl::desc("Print disassembled code."));
  24. cl::opt<bool> ShowSourceLocations("show-source-locations", cl::ReallyHidden,
  25. cl::init(false), cl::ZeroOrMore,
  26. cl::desc("Print source locations."));
  27. cl::opt<bool> ShowPseudoProbe(
  28. "show-pseudo-probe", cl::ReallyHidden, cl::init(false), cl::ZeroOrMore,
  29. cl::desc("Print pseudo probe section and disassembled info."));
  30. namespace llvm {
  31. namespace sampleprof {
  32. static const Target *getTarget(const ObjectFile *Obj) {
  33. Triple TheTriple = Obj->makeTriple();
  34. std::string Error;
  35. std::string ArchName;
  36. const Target *TheTarget =
  37. TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
  38. if (!TheTarget)
  39. exitWithError(Error, Obj->getFileName());
  40. return TheTarget;
  41. }
  42. template <class ELFT>
  43. static uint64_t getELFImageLMAForSec(const ELFFile<ELFT> &Obj,
  44. const object::ELFSectionRef &Sec,
  45. StringRef FileName) {
  46. // Search for a PT_LOAD segment containing the requested section. Return this
  47. // segment's p_addr as the image load address for the section.
  48. const auto &PhdrRange = unwrapOrError(Obj.program_headers(), FileName);
  49. for (const typename ELFT::Phdr &Phdr : PhdrRange)
  50. if ((Phdr.p_type == ELF::PT_LOAD) && (Phdr.p_vaddr <= Sec.getAddress()) &&
  51. (Phdr.p_vaddr + Phdr.p_memsz > Sec.getAddress()))
  52. // Segments will always be loaded at a page boundary.
  53. return Phdr.p_paddr & ~(Phdr.p_align - 1U);
  54. return 0;
  55. }
  56. // Get the image load address for a specific section. Note that an image is
  57. // loaded by segments (a group of sections) and segments may not be consecutive
  58. // in memory.
  59. static uint64_t getELFImageLMAForSec(const object::ELFSectionRef &Sec) {
  60. if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Sec.getObject()))
  61. return getELFImageLMAForSec(ELFObj->getELFFile(), Sec,
  62. ELFObj->getFileName());
  63. else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Sec.getObject()))
  64. return getELFImageLMAForSec(ELFObj->getELFFile(), Sec,
  65. ELFObj->getFileName());
  66. else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Sec.getObject()))
  67. return getELFImageLMAForSec(ELFObj->getELFFile(), Sec,
  68. ELFObj->getFileName());
  69. const auto *ELFObj = cast<ELF64BEObjectFile>(Sec.getObject());
  70. return getELFImageLMAForSec(ELFObj->getELFFile(), Sec, ELFObj->getFileName());
  71. }
  72. void ProfiledBinary::load() {
  73. // Attempt to open the binary.
  74. OwningBinary<Binary> OBinary = unwrapOrError(createBinary(Path), Path);
  75. Binary &Binary = *OBinary.getBinary();
  76. auto *Obj = dyn_cast<ELFObjectFileBase>(&Binary);
  77. if (!Obj)
  78. exitWithError("not a valid Elf image", Path);
  79. TheTriple = Obj->makeTriple();
  80. // Current only support X86
  81. if (!TheTriple.isX86())
  82. exitWithError("unsupported target", TheTriple.getTriple());
  83. LLVM_DEBUG(dbgs() << "Loading " << Path << "\n");
  84. // Find the preferred base address for text sections.
  85. setPreferredBaseAddress(Obj);
  86. // Decode pseudo probe related section
  87. decodePseudoProbe(Obj);
  88. // Disassemble the text sections.
  89. disassemble(Obj);
  90. // Use function start and return address to infer prolog and epilog
  91. ProEpilogTracker.inferPrologOffsets(FuncStartAddrMap);
  92. ProEpilogTracker.inferEpilogOffsets(RetAddrs);
  93. // TODO: decode other sections.
  94. }
  95. bool ProfiledBinary::inlineContextEqual(uint64_t Address1,
  96. uint64_t Address2) const {
  97. uint64_t Offset1 = virtualAddrToOffset(Address1);
  98. uint64_t Offset2 = virtualAddrToOffset(Address2);
  99. const FrameLocationStack &Context1 = getFrameLocationStack(Offset1);
  100. const FrameLocationStack &Context2 = getFrameLocationStack(Offset2);
  101. if (Context1.size() != Context2.size())
  102. return false;
  103. if (Context1.empty())
  104. return false;
  105. // The leaf frame contains location within the leaf, and it
  106. // needs to be remove that as it's not part of the calling context
  107. return std::equal(Context1.begin(), Context1.begin() + Context1.size() - 1,
  108. Context2.begin(), Context2.begin() + Context2.size() - 1);
  109. }
  110. std::string ProfiledBinary::getExpandedContextStr(
  111. const SmallVectorImpl<uint64_t> &Stack) const {
  112. std::string ContextStr;
  113. SmallVector<std::string, 16> ContextVec;
  114. // Process from frame root to leaf
  115. for (auto Address : Stack) {
  116. uint64_t Offset = virtualAddrToOffset(Address);
  117. const FrameLocationStack &ExpandedContext = getFrameLocationStack(Offset);
  118. // An instruction without a valid debug line will be ignored by sample
  119. // processing
  120. if (ExpandedContext.empty())
  121. return std::string();
  122. for (const auto &Loc : ExpandedContext) {
  123. ContextVec.push_back(getCallSite(Loc));
  124. }
  125. }
  126. assert(ContextVec.size() && "Context length should be at least 1");
  127. // Compress the context string except for the leaf frame
  128. std::string LeafFrame = ContextVec.back();
  129. ContextVec.pop_back();
  130. CSProfileGenerator::compressRecursionContext<std::string>(ContextVec);
  131. std::ostringstream OContextStr;
  132. for (uint32_t I = 0; I < (uint32_t)ContextVec.size(); I++) {
  133. if (OContextStr.str().size()) {
  134. OContextStr << " @ ";
  135. }
  136. OContextStr << ContextVec[I];
  137. }
  138. // Only keep the function name for the leaf frame
  139. if (OContextStr.str().size())
  140. OContextStr << " @ ";
  141. OContextStr << StringRef(LeafFrame).split(":").first.str();
  142. return OContextStr.str();
  143. }
  144. void ProfiledBinary::setPreferredBaseAddress(const ELFObjectFileBase *Obj) {
  145. for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end();
  146. SI != SE; ++SI) {
  147. const SectionRef &Section = *SI;
  148. if (Section.isText()) {
  149. PreferredBaseAddress = getELFImageLMAForSec(Section);
  150. return;
  151. }
  152. }
  153. exitWithError("no text section found", Obj->getFileName());
  154. }
  155. void ProfiledBinary::decodePseudoProbe(const ELFObjectFileBase *Obj) {
  156. StringRef FileName = Obj->getFileName();
  157. for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end();
  158. SI != SE; ++SI) {
  159. const SectionRef &Section = *SI;
  160. StringRef SectionName = unwrapOrError(Section.getName(), FileName);
  161. if (SectionName == ".pseudo_probe_desc") {
  162. StringRef Contents = unwrapOrError(Section.getContents(), FileName);
  163. ProbeDecoder.buildGUID2FuncDescMap(
  164. reinterpret_cast<const uint8_t *>(Contents.data()), Contents.size());
  165. } else if (SectionName == ".pseudo_probe") {
  166. StringRef Contents = unwrapOrError(Section.getContents(), FileName);
  167. ProbeDecoder.buildAddress2ProbeMap(
  168. reinterpret_cast<const uint8_t *>(Contents.data()), Contents.size());
  169. // set UsePseudoProbes flag, used for PerfReader
  170. UsePseudoProbes = true;
  171. }
  172. }
  173. if (ShowPseudoProbe)
  174. ProbeDecoder.printGUID2FuncDescMap(outs());
  175. }
  176. bool ProfiledBinary::dissassembleSymbol(std::size_t SI, ArrayRef<uint8_t> Bytes,
  177. SectionSymbolsTy &Symbols,
  178. const SectionRef &Section) {
  179. std::size_t SE = Symbols.size();
  180. uint64_t SectionOffset = Section.getAddress() - PreferredBaseAddress;
  181. uint64_t SectSize = Section.getSize();
  182. uint64_t StartOffset = Symbols[SI].Addr - PreferredBaseAddress;
  183. uint64_t EndOffset = (SI + 1 < SE)
  184. ? Symbols[SI + 1].Addr - PreferredBaseAddress
  185. : SectionOffset + SectSize;
  186. if (StartOffset >= EndOffset)
  187. return true;
  188. std::string &&SymbolName = Symbols[SI].Name.str();
  189. if (ShowDisassemblyOnly)
  190. outs() << '<' << SymbolName << ">:\n";
  191. uint64_t Offset = StartOffset;
  192. while (Offset < EndOffset) {
  193. MCInst Inst;
  194. uint64_t Size;
  195. // Disassemble an instruction.
  196. if (!DisAsm->getInstruction(Inst, Size, Bytes.slice(Offset - SectionOffset),
  197. Offset + PreferredBaseAddress, nulls()))
  198. return false;
  199. if (ShowDisassemblyOnly) {
  200. if (ShowPseudoProbe) {
  201. ProbeDecoder.printProbeForAddress(outs(),
  202. Offset + PreferredBaseAddress);
  203. }
  204. outs() << format("%8" PRIx64 ":", Offset);
  205. size_t Start = outs().tell();
  206. IPrinter->printInst(&Inst, Offset + Size, "", *STI.get(), outs());
  207. if (ShowSourceLocations) {
  208. unsigned Cur = outs().tell() - Start;
  209. if (Cur < 40)
  210. outs().indent(40 - Cur);
  211. InstructionPointer Inst(this, Offset);
  212. outs() << getReversedLocWithContext(symbolize(Inst));
  213. }
  214. outs() << "\n";
  215. }
  216. const MCInstrDesc &MCDesc = MII->get(Inst.getOpcode());
  217. // Populate a vector of the symbolized callsite at this location
  218. // We don't need symbolized info for probe-based profile, just use an empty
  219. // stack as an entry to indicate a valid binary offset
  220. FrameLocationStack SymbolizedCallStack;
  221. if (!UsePseudoProbes) {
  222. InstructionPointer IP(this, Offset);
  223. SymbolizedCallStack = symbolize(IP, true);
  224. }
  225. Offset2LocStackMap[Offset] = SymbolizedCallStack;
  226. // Populate address maps.
  227. CodeAddrs.push_back(Offset);
  228. if (MCDesc.isCall())
  229. CallAddrs.insert(Offset);
  230. else if (MCDesc.isReturn())
  231. RetAddrs.insert(Offset);
  232. Offset += Size;
  233. }
  234. if (ShowDisassemblyOnly)
  235. outs() << "\n";
  236. FuncStartAddrMap[StartOffset] = Symbols[SI].Name.str();
  237. return true;
  238. }
  239. void ProfiledBinary::setUpDisassembler(const ELFObjectFileBase *Obj) {
  240. const Target *TheTarget = getTarget(Obj);
  241. std::string TripleName = TheTriple.getTriple();
  242. StringRef FileName = Obj->getFileName();
  243. MRI.reset(TheTarget->createMCRegInfo(TripleName));
  244. if (!MRI)
  245. exitWithError("no register info for target " + TripleName, FileName);
  246. MCTargetOptions MCOptions;
  247. AsmInfo.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
  248. if (!AsmInfo)
  249. exitWithError("no assembly info for target " + TripleName, FileName);
  250. SubtargetFeatures Features = Obj->getFeatures();
  251. STI.reset(
  252. TheTarget->createMCSubtargetInfo(TripleName, "", Features.getString()));
  253. if (!STI)
  254. exitWithError("no subtarget info for target " + TripleName, FileName);
  255. MII.reset(TheTarget->createMCInstrInfo());
  256. if (!MII)
  257. exitWithError("no instruction info for target " + TripleName, FileName);
  258. MCObjectFileInfo MOFI;
  259. MCContext Ctx(AsmInfo.get(), MRI.get(), &MOFI);
  260. MOFI.InitMCObjectFileInfo(Triple(TripleName), false, Ctx);
  261. DisAsm.reset(TheTarget->createMCDisassembler(*STI, Ctx));
  262. if (!DisAsm)
  263. exitWithError("no disassembler for target " + TripleName, FileName);
  264. MIA.reset(TheTarget->createMCInstrAnalysis(MII.get()));
  265. int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
  266. IPrinter.reset(TheTarget->createMCInstPrinter(
  267. Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
  268. IPrinter->setPrintBranchImmAsAddress(true);
  269. }
  270. void ProfiledBinary::disassemble(const ELFObjectFileBase *Obj) {
  271. // Set up disassembler and related components.
  272. setUpDisassembler(Obj);
  273. // Create a mapping from virtual address to symbol name. The symbols in text
  274. // sections are the candidates to dissassemble.
  275. std::map<SectionRef, SectionSymbolsTy> AllSymbols;
  276. StringRef FileName = Obj->getFileName();
  277. for (const SymbolRef &Symbol : Obj->symbols()) {
  278. const uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
  279. const StringRef Name = unwrapOrError(Symbol.getName(), FileName);
  280. section_iterator SecI = unwrapOrError(Symbol.getSection(), FileName);
  281. if (SecI != Obj->section_end())
  282. AllSymbols[*SecI].push_back(SymbolInfoTy(Addr, Name, ELF::STT_NOTYPE));
  283. }
  284. // Sort all the symbols. Use a stable sort to stabilize the output.
  285. for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
  286. stable_sort(SecSyms.second);
  287. if (ShowDisassemblyOnly)
  288. outs() << "\nDisassembly of " << FileName << ":\n";
  289. // Dissassemble a text section.
  290. for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end();
  291. SI != SE; ++SI) {
  292. const SectionRef &Section = *SI;
  293. if (!Section.isText())
  294. continue;
  295. uint64_t ImageLoadAddr = PreferredBaseAddress;
  296. uint64_t SectionOffset = Section.getAddress() - ImageLoadAddr;
  297. uint64_t SectSize = Section.getSize();
  298. if (!SectSize)
  299. continue;
  300. // Register the text section.
  301. TextSections.insert({SectionOffset, SectSize});
  302. if (ShowDisassemblyOnly) {
  303. StringRef SectionName = unwrapOrError(Section.getName(), FileName);
  304. outs() << "\nDisassembly of section " << SectionName;
  305. outs() << " [" << format("0x%" PRIx64, SectionOffset) << ", "
  306. << format("0x%" PRIx64, SectionOffset + SectSize) << "]:\n\n";
  307. }
  308. // Get the section data.
  309. ArrayRef<uint8_t> Bytes =
  310. arrayRefFromStringRef(unwrapOrError(Section.getContents(), FileName));
  311. // Get the list of all the symbols in this section.
  312. SectionSymbolsTy &Symbols = AllSymbols[Section];
  313. // Disassemble symbol by symbol.
  314. for (std::size_t SI = 0, SE = Symbols.size(); SI != SE; ++SI) {
  315. if (!dissassembleSymbol(SI, Bytes, Symbols, Section))
  316. exitWithError("disassembling error", FileName);
  317. }
  318. }
  319. }
  320. void ProfiledBinary::setupSymbolizer() {
  321. symbolize::LLVMSymbolizer::Options SymbolizerOpts;
  322. SymbolizerOpts.PrintFunctions =
  323. DILineInfoSpecifier::FunctionNameKind::LinkageName;
  324. SymbolizerOpts.Demangle = false;
  325. SymbolizerOpts.DefaultArch = TheTriple.getArchName().str();
  326. SymbolizerOpts.UseSymbolTable = false;
  327. SymbolizerOpts.RelativeAddresses = false;
  328. Symbolizer = std::make_unique<symbolize::LLVMSymbolizer>(SymbolizerOpts);
  329. }
  330. FrameLocationStack ProfiledBinary::symbolize(const InstructionPointer &IP,
  331. bool UseCanonicalFnName) {
  332. assert(this == IP.Binary &&
  333. "Binary should only symbolize its own instruction");
  334. auto Addr = object::SectionedAddress{IP.Offset + PreferredBaseAddress,
  335. object::SectionedAddress::UndefSection};
  336. DIInliningInfo InlineStack =
  337. unwrapOrError(Symbolizer->symbolizeInlinedCode(Path, Addr), getName());
  338. FrameLocationStack CallStack;
  339. for (int32_t I = InlineStack.getNumberOfFrames() - 1; I >= 0; I--) {
  340. const auto &CallerFrame = InlineStack.getFrame(I);
  341. if (CallerFrame.FunctionName == "<invalid>")
  342. break;
  343. StringRef FunctionName(CallerFrame.FunctionName);
  344. if (UseCanonicalFnName)
  345. FunctionName = FunctionSamples::getCanonicalFnName(FunctionName);
  346. LineLocation Line(CallerFrame.Line - CallerFrame.StartLine,
  347. DILocation::getBaseDiscriminatorFromDiscriminator(
  348. CallerFrame.Discriminator));
  349. FrameLocation Callsite(FunctionName.str(), Line);
  350. CallStack.push_back(Callsite);
  351. }
  352. return CallStack;
  353. }
  354. InstructionPointer::InstructionPointer(ProfiledBinary *Binary, uint64_t Address,
  355. bool RoundToNext)
  356. : Binary(Binary), Address(Address) {
  357. Index = Binary->getIndexForAddr(Address);
  358. if (RoundToNext) {
  359. // we might get address which is not the code
  360. // it should round to the next valid address
  361. this->Address = Binary->getAddressforIndex(Index);
  362. }
  363. }
  364. void InstructionPointer::advance() {
  365. Index++;
  366. Address = Binary->getAddressforIndex(Index);
  367. }
  368. void InstructionPointer::backward() {
  369. Index--;
  370. Address = Binary->getAddressforIndex(Index);
  371. }
  372. void InstructionPointer::update(uint64_t Addr) {
  373. Address = Addr;
  374. Index = Binary->getIndexForAddr(Address);
  375. }
  376. } // end namespace sampleprof
  377. } // end namespace llvm